0

I am using sounddevice to continuously record audio with a Raspberry Pi 3 connected to an external USB Audio Interface (Zoom-U22). But I see some sections of audio are repeating or struck like in a loop. The recording continues without any problem after this.

What causes such problems with recording? (buffer underflow, overflow?)

How do I fix it?

  • Change latency settings? I am already using a high latency setting.
  • Change blocksize? Does blocksize have to match the latency setting?

Here's a screenshot of the repeating section.

enter image description here

My python code goes like this. I open a InputStream with a callback that computes STFT using librosa and puts the audio data in a queue.Queue object. Another function uses soundfile to write from the audio queue to a wavfile.

def callback(in_data, frame_count, time_info, status):
    """A callback to be passed to sounddevice stream."""
    in_data_array = np.frombuffer(in_data, dtype=NP_DTYPE)
    Q_audio.put(in_data_array)
    # Some code for STFT and calculating RMS
    Q_RMS.put(rms)

def init_sounddevice(callback):
    """Initialize sounddevice and return the stream object."""
    sd.default.device = select_device_sd()
    sd.default.samplerate = 44100
    sd.default.channels = 1
    sd.default.dtype = 'int16'
    sd.default.latency = 0.1
    sd.default.blocksize = 2**12
    stream = sd.InputStream(callback=callback)
    print('Started input audio stream.')
    return stream

def main():
    """Starts the stream to record audio and call the callback for audio processing."""
    stream = init_sounddevice(callback=callback)
    stream.start()
    file = fo.create_wave_file()
    while True:
        fo.save_rms_to_csv(Q_RMS)
        _ = fo.write_wave_file(file, Q_audio)
        if file.closed:
            file = fo.create_wave_file()

def write_wave_file(wf, Q_audio):
    """Write audio to wavfile while audio is available in the queue.
    If file length exceeds certain duration, close it."""
    try:
        wf.write(Q_audio.get())
        if wf.tell() > 44100 * 20 * 60:
            wf.close()
            print('Wav file writing complete')
    except Exception as e:
        print(e)

Thanks in advance!

Sashi
  • 79
  • 11

0 Answers0