python-sounddevice icon indicating copy to clipboard operation
python-sounddevice copied to clipboard

Record and play at the same time

Open LXP-Never opened this issue 2 years ago • 2 comments

Mixed in reverb, and the sound suddenly big and small, sometimes there will be swallow word

import sounddevice as sd
import time
import numpy as np
import soundfile
Stream = sd.Stream(samplerate=16000, blocksize=256, channels=1)

Stream.start()
enhance_audio = np.zeros((0,))
print("开始")
for _ in range(2000):
    start_time = time.time()
    data, overflowed = Stream.read(frames=256, )
    enhance_audio = np.append(enhance_audio, data)
    Stream.write(data)

    print(time.time() - start_time)
soundfile.write("no_alg.wav", data=enhance_audio, samplerate=16000)

image

record_and_play_wav.zip

LXP-Never avatar Jan 11 '23 15:01 LXP-Never

You should check the overflowed variable. I guess it is True some of the times?

If that's the case, you could try to increase your block size.

Other than that, I can't really say anything about "blocking" mode, because I rarely use it and I don't know how to use it for recording and playback at the same time.

I would use the "callback" mode instead.

mgeier avatar Jan 17 '23 19:01 mgeier

You should check the overflowed variable. I guess it is True some of the times?

If that's the case, you could try to increase your block size.

Other than that, I can't really say anything about "blocking" mode, because I rarely use it and I don't know how to use it for recording and playback at the same time.

I would use the "callback" mode instead.

When playback and recording are in progress at the same time, the signal recorded by the microphone will be processed by the window's mic (voice enhancement, spatial audio), we need to turn them off image

image

LXP-Never avatar Jan 18 '23 01:01 LXP-Never