使用语音识别时,麦克风出现问题

时间:2020-07-10 07:35:14

标签: python speech-recognition microphone pyaudio nvidia-jetson

我有一个内部装有麦克风的Jetson Xavier,USB麦克风和网络摄像头。总共2个麦克风。我正在使用语音识别。当我尝试同时使用网络摄像头和USB麦克风时,默认情况下软件会将网络摄像头上的麦克风作为软件。插入两个设备时都想使用USB Mic。我找到了它的device_index,并且已经编写了软件。但是我有一个错误,因为:

"OSError: [Errno -9997] Invalid sample rate"

这是我的麦克风:

Microphone with name "UAC 1.0 Microphone & HID-Mediak: USB Audio (hw:2,0)" found for `Microphone(device_index=24)

您可能会在下面找到相关代码。我尝试将采样率更改为44100。错误消失了,但麦克风无法正常工作。

P.S .:当我单独插入USB麦克风时,它可以正常工作。代码使用它作为默认值。仅当我有2个连接的设备时,我才有问题。

from speech_recognition import (
    Microphone,
    AudioSource,
    AudioData
)

class xMicrophone(Microphone):
    def __init__(self, device_index=None, sample_rate=16000, chunk_size=1024,
                 mute=False):
        Microphone.__init__(self, device_index=device_index,
                            sample_rate=sample_rate, chunk_size=chunk_size)
        self.muted = False
        if mute:
            self.mute()
        self.StreamClass = MutableStream

    def __enter__(self):
        return self._start()

    def _start(self):
        """Open the selected device and setup the stream."""
        assert self.stream is None, \
            "This audio source is already inside a context manager"
        with contexts.ignore_stderr(), contexts.ignore_stdout():
            self.audio = pyaudio.PyAudio()
        self.stream = self.StreamClass(self.audio.open(
            input_device_index=self.device_index, channels=1,
            format=self.format, rate=self.SAMPLE_RATE,
            frames_per_buffer=self.CHUNK,
            input=True,  # stream is an input stream
        ), self.format, self.muted)
        return self

0 个答案:

没有答案