spatialaudio / python-sounddevice

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

callback fails #341

Open bactone opened 3 years ago

bactone commented 3 years ago

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 commented 3 years ago

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

HaHeho commented 3 years ago

@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).

bactone commented 3 years ago

@HaHeho thanks for your answer