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

callback fails

Open bactone opened this issue 4 years ago • 3 comments

hi, I have been running the callback example given at the web tutorial, https://python-sounddevice.readthedocs.io/en/0.4.1/usage.html#callback-streams, if use the default setting of devices, It works, however if i change the default device, i can't hear the voice played back, the revised code is given as:

import sounddevice as sd

BLOCKSIZE = 1024

# print(sd.query_devices())
sd.default.device = 'ASIO Fireface USB'
sd.default.samplerate = 44100
sd.default.channels = 1
sd.default.blocksize = BLOCKSIZE

duration = 5.5  # seconds


def callback(indata, outdata, frames, time, status):
    if status:
        print(status)
    outdata[:] = indata


with sd.Stream(channels=[1, 1], callback=callback):
    sd.sleep(int(duration * 1000))

print(sd.query_devices())

so my question is, how to set the device, channel, and other paramters for callback and stream? thanks

bactone avatar Jun 29 '21 02:06 bactone

while I found it's because the output level is too small, if i change the code outdata[:] = indata to outdata[:] = 5*indata, then i can hear the playback sound, thanks

bactone avatar Jun 29 '21 02:06 bactone

@bactone please post code in a ``` environment so it is formatted in a readable way.

The way you want to specify the audio device and further parameters is probably more like this. Also see the documentation for Stream for further parameters.

import sounddevice as sd

DEVICE = "ASIO Fireface USB"  # according to `sd.query_devices()`
SAMPLERATE = 44100  # Hz
CHANNELS = [1, 1]
BLOCKSIZE = 1024  # samples

DURATION = 5.5  # seconds


def callback(indata, outdata, frames, time, status):
    if status:
        print(status)
    outdata[:] = indata


print(sd.query_devices())
with sd.Stream(
    samplerate=SAMPLERATE,
    blocksize=BLOCKSIZE,
    device=DEVICE,
    channels=CHANNELS,
    callback=callback,
):
    sd.sleep(int(DURATION * 1000))

I don't know what your hardware or intended use case is. But the outdata[:] = 5*indata is of course a matter of signal amplification. That depends on what your respective levels of the audio hardware is (where there might be different ways of how they are set or influenced when using a ASIO vs. a non-ASIO driver on Windows for the same audio interface ... maybe).

HaHeho avatar Jun 29 '21 17:06 HaHeho

@HaHeho thanks for your answer

bactone avatar Jun 30 '21 06:06 bactone