Python SoundDeviceStream write/read underflow/overflow errors

1.3k Views Asked by At

Using Raspberry Pi Model B+ running Raspbian GNU/Linux 8 (jessie) with Blue "Snowball" usb mic and powered speakers connected to 3.5mm jack output. Trying to run pushtotalk.py, a Google Assistant sample from GassistPi. Sample runs but produces multiple underflow errors and choppy audio out. Git search revealed this similar issue and points to buffer parameter settings in audio_helpers.py, (full code here) and possible CPU issues. Code snippets below show parameter settings and also code that produce the error message. Looked through the more detailed explanation of python-sounddevice here but it is way beyond me (only just started to learn Python!). Looking for a succinct (and relatively simple) answer to the question, what determines the values of the parameter settings and how can the code/parameters be modified to prevent/reduce the underflow errors?

Code snippet #1 Parameter setting in audio_helpers.py sample:

import sounddevice as sd


DEFAULT_AUDIO_SAMPLE_RATE = 16000
DEFAULT_AUDIO_SAMPLE_WIDTH = 2
DEFAULT_AUDIO_ITER_SIZE = 3200
DEFAULT_AUDIO_DEVICE_BLOCK_SIZE = 6400
DEFAULT_AUDIO_DEVICE_FLUSH_SIZE = 25600


def normalize_audio_buffer(buf, volume_percentage, sample_width=2):
"""Adjusts the loudness of the audio data in the given buffer.

Code snippet #2 Error reporting code in audio_helpers.py sample:

class SoundDeviceStream(object):
     """Audio stream based on an underlying sound device.

It can be used as an audio source (read) and a audio sink (write).

Args:
  sample_rate: sample rate in hertz.
  sample_width: size of a single sample in bytes.
  block_size: size in bytes of each read and write operation.
  flush_size: size in bytes of silence data written during flush operation.
"""
def __init__(self, sample_rate, sample_width, block_size, flush_size):
    if sample_width == 2:
        audio_format = 'int16'
    else:
        raise Exception('unsupported sample width:', sample_width)
    self._audio_stream = sd.RawStream(
        samplerate=sample_rate, dtype=audio_format, channels=1,
        blocksize=int(block_size/2),  # blocksize is in number of frames.
    )
    self._block_size = block_size
    self._flush_size = flush_size
    self._sample_rate = sample_rate

def read(self, size):
    """Read bytes from the stream."""
    buf, overflow = self._audio_stream.read(size)
    if overflow:
        logging.warning('SoundDeviceStream read overflow (%d, %d)',
                        size, len(buf))
    return bytes(buf)

def write(self, buf):
    """Write bytes to the stream."""
    underflow = self._audio_stream.write(buf)
    if underflow:
        logging.warning('SoundDeviceStream write underflow (size: %d)',
                        len(buf))
    return len(buf)

and finally the warning output when running pushtotalk.py

WARNING:root:SoundDeviceStream write underflow (size: 4000)
WARNING:root:SoundDeviceStream write underflow (size: 4000)
WARNING:root:SoundDeviceStream write underflow (size: 4000)
WARNING:root:SoundDeviceStream write underflow (size: 4000)
WARNING:root:SoundDeviceStream write underflow (size: 4000)
WARNING:root:SoundDeviceStream write underflow (size: 4000)

UPDATE OK with a bit of tinkering I have managed to reduce the choppy audio out by commenting out code in audio_helpers.py that generates the warning message output for the underflow and overflow conditions. It seems that when an under/overflow condition is detected, logging.warning is outputting to the terminal (stdout?) and this is causing the audio playback to briefly be interrupted. You can still detect a slight choppiness in the audio out but it is considerably better than before. Ideally, instead of preventing the output of the warning messages, it would be much better to prevent the under/overflow conditions occurring in the first place!

Code snippet below shows the two logging.warning functions commented out in audio_helpers.py. This file is located at /home/pi/env/lib/python3.5/site-packages/googlesamples/assistant/grpc on my setup.

    def read(self, size):
    """Read bytes from the stream."""
    buf, overflow = self._audio_stream.read(size)
    #  if overflow:
    #   logging.warning('SoundDeviceStream read overflow (%d, %d)',
    #                   size, len(buf))"""
    return bytes(buf)

    def write(self, buf):
    """Write bytes to the stream."""
    underflow = self._audio_stream.write(buf)
    #   if underflow:
    #   logging.warning('SoundDeviceStream write underflow (size: %d)',
    #                   len(buf))"""
    return len(buf)

Use of Doc-String """ now changed to # for commented out sections

UPDATE 2 At @Matthias suggestion, details of how pushtotalk.pyis started and also results of using switch --audio-block-size

pi@raspberrypi:~ $ /home/pi/env/bin/googlesamples-assistant-pushtotalk --project-id 'gassistpi-xxxxx' --device-model-id 'gassistpi-xxxxx-gassistpi-xxxxx' --audio-block-size -77
Traceback (most recent call last):
  File "/home/pi/env/bin/googlesamples-assistant-pushtotalk", line 11, in <module>
    sys.exit(main())
  File "/home/pi/env/lib/python3.5/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/home/pi/env/lib/python3.5/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/pi/env/lib/python3.5/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/pi/env/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/pi/env/lib/python3.5/site-packages/googlesamples/assistant/grpc/pushtotalk.py", line 351, in main
    flush_size=audio_flush_size
  File "/home/pi/env/lib/python3.5/site-packages/googlesamples/assistant/grpc/audio_helpers.py", line 190, in __init__
    blocksize=int(block_size/2),  # blocksize is in number of frames.
  File "/home/pi/env/lib/python3.5/site-packages/sounddevice.py", line 1264, in __init__
    **_remove_self(locals()))
  File "/home/pi/env/lib/python3.5/site-packages/sounddevice.py", line 779, in __init__
    callback_ptr, userdata),
OverflowError: can't convert negative number to unsigned
pi@raspberrypi:~ $ 

I also tried (valid binary) values for --audio-block-size ranging from 1024 through to 65536 and also 0 for good measure. All produced the same original result or runtime errors.

0

There are 0 best solutions below