spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
1.02k stars 149 forks source link

Record and play at the same time #449

Open LXP-Never opened 1 year ago

LXP-Never commented 1 year ago

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

mgeier commented 1 year ago

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.

LXP-Never commented 1 year ago

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