Python SoundDeviceStream写入/读取下溢/上溢错误

时间:2018-11-09 11:21:46

标签: python raspberry-pi google-assistant-sdk google-assist-api python-sounddevice

使用运行Raspbian GNU / Linux 8(jessie)的Raspberry Pi Model B +,其蓝色“ Snowball” USB麦克风和有源扬声器连接到3.5mm插孔输出。尝试运行GassistPi的Google Assistant示例pushtotalk.py。样本运行,但会产生多个下溢错误,并且音频断断续续。 Git搜索显示this similar issue,并指向audio_helpers.py中的缓冲区参数设置(完整代码here)和可能的CPU问题。下面的代码段显示了参数设置以及产生错误消息的代码。浏览了有关python-sounddevice here的更详细的解释,但这超出了我的理解(才刚刚开始学习Python!)。寻找一个简洁(且相对简单)的答案,是什么决定参数设置的值,以及如何修改代码/参数以防止/减少下溢错误?

audio_helpers.py示例中的代码段#1参数设置:

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.

代码段#2 audio_helpers.py示例中的错误报告代码:

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)

最后是运行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)

更新好吧,我做了一些修补,通过注释掉audio_helpers.py中的代码来减少断断续续的音频,该代码针对下溢和上溢情况生成警告消息输出。似乎当检测到下溢/上溢情况时,logging.warning将输出到终端(stdout?),这会导致音频播放短暂中断。您仍然可以在音频输出中检测到轻微的震颤,但它比以前好得多。理想情况下,与其阻止警告消息的输出,不如首先防止下溢/溢出情况发生,那就更好了!

下面的代码段显示了audio_helpers.py中注释的两个logging.warning函数。该文件位于我的设置中的/home/pi/env/lib/python3.5/site-packages/googlesamples/assistant/grpc上。

    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)

对于注释掉的部分,现在将文档字符串““”的使用更改为#

更新2 根据@Matthias的建议,详细介绍了pushtotalk.py的启动方式以及使用开关--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:~ $ 

我还尝试了--audio-block-size的(有效二进制)值,范围从1024到65536,并且也很好地设置了0。全部产生相同的原始结果或运行时错误。

0 个答案:

没有答案