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

What is the actual block size when using read/write commands and blocksize=0?

Open Antovigo opened this issue 4 years ago • 1 comments

Hello, According to the documentation, setting blocksize=0 in a stream is preferable, and may result in a variable block size. But then, the docstring for Stream.read says: high performance applications will want to match this parameter to the blocksize parameter sed when opening the stream. If I set blocksize to 0 as recommended, what am I supposed to use as an argument for Stream.read()? Here is a minimal example that directs the mic input directly to the output:

import numpy as np
import sounddevice as sd
import time
duration = 10 # in seconds
t_end = time.time() + duration
blocksize = 512 # I guess this is arbitrary
samplerate = sd.query_devices(0, 'output')['default_samplerate']
s = sd.Stream(channels=(1,2), blocksize=0)
s.start()
while time.time() < t_end:
    indata,of = s.read(blocksize) # Input from mic
    volume = np.linalg.norm(indata) # Volume from mic
    left = indata
    right = indata
    outdata = np.hstack([left,right])
    uf = s.write(np.float32(outdata))    
s.stop()

I couldn't find clear examples of read() and write() being used for live audio processing, so I'd like to know if I'm doing something wrong. The code above definitely produces some sound artifacts.

Antovigo avatar Jul 28 '20 13:07 Antovigo

I couldn't find clear examples of read() and write() being used for live audio processing

Me neither. I think there are none.

I don't know how to use those methods either, I normally prefer to use a callback function.

I've just copied the help text from the PortAudio docs, see http://www.portaudio.com/docs/v19-doxydocs/portaudio_8h.html#a0b62d4b74b5d3d88368e9e4c0b8b2dc7.

Please ask at the PortAudio mailing list (http://portaudio.com/contacts.html) and let me know what they recommend!

mgeier avatar Jul 28 '20 14:07 mgeier