通过websockets流式音频IBM不工作

2024-06-26 03:45:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过IBM Watson的websockets来传输麦克风音频。我得到以下错误:

TypeError: The system cannot find the file specified

我觉得子流程有问题,请帮我纠正一下/让它正常工作。在

from ws4py.client.threadedclient import WebSocketClient
import base64, json, ssl, subprocess, threading, time

class SpeechToTextClient(WebSocketClient):
    def __init__(self):
        ws_url = "wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize"

        username = "81bffb34-906b-4057-becf-72752e14e756"
        password= "jPwZAMMB5Fwp"
        auth_string = "%s:%s" % (username, password)
        base64string= base64.encodestring(auth_string).replace("\n", "")

        self.listening = False

        try:
            WebSocketClient.__init__(self, ws_url, headers=[("Authorization", "Basic %s" % base64string)])
            self.connect()
        except: print("Failed to open WebSocket.")

    def opened(self):
        self.send('{"action": "start", "content-type": "audio/l16;rate=16000"}')
        self.stream_audio_thread = threading.Thread(target=self.stream_audio)
        self.stream_audio_thread.start()

    def received_message(self, message):
        message = json.loads(str(message))
        if "state" in message:
            if message["state"] == "listening":
                self.listening = True
        print("Message received: " + str(message))

    def stream_audio(self):
        while not self.listening:
            time.sleep(0.1)

        reccmd = ["arecord", "-f", "S16_LE", "-r", "16000", "-t", "raw"]
        p = subprocess.Popen(reccmd, stdout=subprocess.PIPE)

        while self.listening:
            data = p.stdout.read(1024)

            try:
                self.send(bytearray(data), binary=True)
            except ssl.SSLError:
                pass

        p.kill()

    def close_connection(self):
        self.listening = False
        self.stream_audio_thread.join()
        WebSocketClient.close(self)





if __name__ == "__main__":

    SpeechToTextClient()

以下是完整的错误输出:

^{pr2}$

Tags: importselfjsonsslmessagestreamifdef
1条回答
网友
1楼 · 发布于 2024-06-26 03:45:31

Arecord是一个在Alsa框架中录制音频的linux工具,它在linux上不起作用。你需要用像pyaudio这样的东西来录制声音。在

   def stream_audio(self):
        while not self.listening:
            time.sleep(0.1)

       p = pyaudio.PyAudio()

       stream = p.open(format=pyaudio.paInt16,
                channels=1, 
                rate=16000, 
                input=True,
                frames_per_buffer=1024)

        while self.listening:
            data = stream.read(2048)

            try:
                self.send(bytearray(data), binary=True)
            except ssl.SSLError:
                pass

相关问题 更多 >