Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to playback realtime audio in python while also constantly recording?

I want to create a speech jammer. It is essentially something that repeats back to you what you just said, but it is continuous. I was trying to use the sounddevice library and record what I am saying while also playing it back. Then I changed it to originally record what I was saying, then play it back while also recording something new. However it is not functioning as I would like it. Any suggestions for other libraries? Or if someone sees a suggestion for the code I already have.

Instead of constantly playing back to me, it is starting and stopping. It does this at intervals of the duration specified. So it will record for 500 ms, then play that back for 500 ms and then start recording again. Wanted behavior would be - recording for 500ms while playing back the audio it is recording at some ms delay.

import sounddevice as sd
import numpy as np

fs = 44100
sd.default.samplerate = fs
sd.default.channels = 2
#the above is to avoid having to specify arguments in every function call
duration = .5

myarray = sd.rec(int(duration*fs))
while(True):
    sd.wait()
    myarray = sd.playrec(myarray)
    sd.wait()
like image 679
Mataan P Avatar asked Mar 18 '26 12:03

Mataan P


1 Answers

Paraphrasing my own answer from https://stackoverflow.com/a/54569667:

The functions sd.play(), sd.rec() and sd.playrec() are not meant to be used repeatedly in rapid succession. Internally, they each time create an sd.OutputStream, sd.InputStream or sd.Stream (respectively), play/record the audio data and close the stream again. Because of opening and closing the stream, gaps will occur. This is expected.

For continuous playback you can use the so-called "blocking mode" by creating a single stream and calling the read() and/or write() methods on it.

Or, what I normally prefer, you can use the so-called "non-blocking mode" by creating a custom "callback" function and passing it to the stream on creation. In this callback function, you can e.g. write the input data to a queue.Queue and read the output data from the same queue. By filling the queue by a certain amount of zeros beforehand, you can specify how long the delay between input and output shall be.

You can have a look at the examples to see how callback functions and queues are used.

Let me know if you need more help, then I can try to come up with a concrete code example.

like image 194
Matthias Avatar answered Mar 21 '26 04:03

Matthias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!