spatialaudio / python-sounddevice

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

sd.Stream sounds not smooth #472

Open yufengyao-lingoace opened 1 year ago

yufengyao-lingoace commented 1 year ago

When I use this code, the audio sounds not smooth. I am trying recording and playing at the same time, but I found it worded bad on mac, but normal on windows. I got a noisy sound on mac, when the recording is open. But it sounds normal when I stopped the recording process.

def stream_generator(blocksize, *, channels=1, dtype='float32',pre_fill_blocks=10):

    data, fs = sf.read("tmp.wav", always_2d=True)

    def callback(indata, outdata, frame_count, time_info, status):
        global current_frame
        chunksize = min(len(data) - current_frame, frame_count)
        outdata[:chunksize] = data[current_frame:current_frame + chunksize]
        if chunksize < frame_count:
            outdata[chunksize:] = 0
            raise sd.CallbackStop()
        current_frame += chunksize

    stream = sd.Stream(samplerate=22050,blocksize=4096, device=[0,1], callback=callback, dtype=dtype,
                       channels=1)
    with stream:
        event.wait()

stream_generator(1024)
mgeier commented 5 months ago

The event variable is not defined, or am I missing something?

Do the example programs work on mac/windows?