python监听麦克风并调用阿里云的实时语音转文字

bash 复制代码
import time
import threading
import queue
import sounddevice as sd
import numpy as np
import nls
import sys

# 阿里云配置信息
URL = "wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1"
TOKEN = "016ca1620aff421da8fac81b9fb52dc5"  # 参考https://help.aliyun.com/document_detail/450255.html获取token
APPKEY = "ahS8ZDaimkpWALHi"  # 获取Appkey请前往控制台:https://nls-portal.console.aliyun.com/applist


# Queue to hold the recorded audio data
audio_queue = queue.Queue()

# Callback function to capture audio data
def audio_callback(indata, frames, time, status):
    if status:
        print(status, file=sys.stderr)
    audio_queue.put(indata.copy())

class RealTimeSpeechRecognizer:
    def __init__(self, url, token, appkey):
        self.url = url
        self.token = token
        self.appkey = appkey
        self.transcriber = None
        self.__initialize_transcriber()

    def __initialize_transcriber(self):
        self.transcriber = nls.NlsSpeechTranscriber(
            url=self.url,
            token=self.token,
            appkey=self.appkey,
            on_sentence_begin=self.on_sentence_begin,
            on_sentence_end=self.on_sentence_end,
            on_start=self.on_start,
            on_result_changed=self.on_result_changed,
            on_completed=self.on_completed,
            on_error=self.on_error,
            on_close=self.on_close,
            callback_args=[self]
        )
        self.transcriber.start(aformat="pcm", enable_intermediate_result=True,
                               enable_punctuation_prediction=True, enable_inverse_text_normalization=True)

    def send_audio(self, audio_data):
        if self.transcriber:
            self.transcriber.send_audio(audio_data)

    def stop_transcription(self):
        if self.transcriber:
            self.transcriber.stop()

    def on_sentence_begin(self, message, *args):
        print("Sentence begin: {}".format(message))

    def on_sentence_end(self, message, *args):
        print("Sentence end: {}".format(message))

    def on_start(self, message, *args):
        print("Start: {}".format(message))

    def on_result_changed(self, message, *args):
        print("Result changed: {}".format(message))

    def on_completed(self, message, *args):
        print("Completed: {}".format(message))

    def on_error(self, message, *args):
        print("Error: {}".format(message))

    def on_close(self, *args):
        print("Closed: {}".format(args))

# 调用阿里云的语音转文字的接口
def recognize_speech(audio_data, recognizer):
    audio_data = np.concatenate(audio_data)
    recognizer.send_audio(audio_data.tobytes())

# Start the audio stream and process audio data
def start_audio_stream(recognizer):
    with sd.InputStream(callback=audio_callback, channels=1, samplerate=16000, dtype='int16'):
        print("Recording audio... Press Ctrl+C to stop.")
        audio_buffer = []
        try:
            while True:
                while not audio_queue.empty():
                    audio_buffer.append(audio_queue.get())
                if len(audio_buffer) >= 10:  # 调整音频数据块大小
                    audio_data = np.concatenate(audio_buffer)
                    recognize_speech(audio_data, recognizer)
                    audio_buffer = []  # Clear buffer after sending
                time.sleep(0.1)
        except KeyboardInterrupt:
            print("Stopping audio recording.")
            recognizer.stop_transcription()

if __name__ == "__main__":
    recognizer = RealTimeSpeechRecognizer(URL, TOKEN, APPKEY)
    start_audio_stream(recognizer)

这段代码实现了一个实时语音转文字系统,使用阿里云的语音转文字服务 (NlsSpeechTranscriber) 来处理从麦克风捕获的音频数据。以下是代码的详细解释:

主要模块和库

  • timethreading:用于处理时间和多线程。
  • queue:用于实现线程间通信的队列。
  • sounddevice (sd):用于从麦克风捕获音频数据。
  • numpy (np):用于处理音频数据数组。
  • nls:阿里云的语音服务库。
  • sys:用于处理系统相关的操作,如错误输出。

阿里云配置信息

python 复制代码
URL = "wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1"
TOKEN = "016ca1620aff421da8fac81b9fb52dc5"
APPKEY = "ahS8ZDaimkpWALHi"

这些变量存储了阿里云语音服务的配置信息,包括服务的 URL、令牌(TOKEN)和应用密钥(APPKEY)。

音频数据队列

python 复制代码
audio_queue = queue.Queue()

用于存储从麦克风捕获的音频数据。

音频数据回调函数

python 复制代码
def audio_callback(indata, frames, time, status):
    if status:
        print(status, file=sys.stderr)
    audio_queue.put(indata.copy())

这个回调函数会在音频数据可用时被调用,将捕获到的音频数据复制到队列 audio_queue 中。

RealTimeSpeechRecognizer 类

python 复制代码
class RealTimeSpeechRecognizer:
    def __init__(self, url, token, appkey):
        self.url = url
        self.token = token
        self.appkey = appkey
        self.transcriber = None
        self.__initialize_transcriber()

初始化函数,接收 URL、TOKEN 和 APPKEY,并调用内部函数 __initialize_transcriber 初始化语音转文字服务。

python 复制代码
def __initialize_transcriber(self):
    self.transcriber = nls.NlsSpeechTranscriber(
        url=self.url,
        token=self.token,
        appkey=self.appkey,
        on_sentence_begin=self.on_sentence_begin,
        on_sentence_end=self.on_sentence_end,
        on_start=self.on_start,
        on_result_changed=self.on_result_changed,
        on_completed=self.on_completed,
        on_error=self.on_error,
        on_close=self.on_close,
        callback_args=[self]
    )
    self.transcriber.start(aformat="pcm", enable_intermediate_result=True,
                           enable_punctuation_prediction=True, enable_inverse_text_normalization=True)

初始化语音转文字服务并配置相关回调函数。

python 复制代码
def send_audio(self, audio_data):
    if self.transcriber:
        self.transcriber.send_audio(audio_data)

def stop_transcription(self):
    if self.transcriber:
        self.transcriber.stop()

用于发送音频数据到阿里云并停止转录。

回调函数

python 复制代码
def on_sentence_begin(self, message, *args):
    print("Sentence begin: {}".format(message))

def on_sentence_end(self, message, *args):
    print("Sentence end: {}".format(message))

def on_start(self, message, *args):
    print("Start: {}".format(message))

def on_result_changed(self, message, *args):
    print("Result changed: {}".format(message))

def on_completed(self, message, *args):
    print("Completed: {}".format(message))

def on_error(self, message, *args):
    print("Error: {}".format(message))

def on_close(self, *args):
    print("Closed: {}".format(args))

这些函数在语音转文字服务的不同事件发生时被调用,打印相关信息。

处理音频数据

python 复制代码
def recognize_speech(audio_data, recognizer):
    audio_data = np.concatenate(audio_data)
    recognizer.send_audio(audio_data.tobytes())

将音频数据连接成一个数组并发送给阿里云语音转文字服务。

开始音频流并处理音频数据

python 复制代码
def start_audio_stream(recognizer):
    with sd.InputStream(callback=audio_callback, channels=1, samplerate=16000, dtype='int16'):
        print("Recording audio... Press Ctrl+C to stop.")
        audio_buffer = []
        try:
            while True:
                while not audio_queue.empty():
                    audio_buffer.append(audio_queue.get())
                if len(audio_buffer) >= 10:  # 调整音频数据块大小
                    audio_data = np.concatenate(audio_buffer)
                    recognize_speech(audio_data, recognizer)
                    audio_buffer = []  # Clear buffer after sending
                time.sleep(0.1)
        except KeyboardInterrupt:
            print("Stopping audio recording.")
            recognizer.stop_transcription()

这个函数打开音频输入流,开始录音并处理音频数据,将其发送到阿里云进行转录。当用户按下 Ctrl+C 时,停止录音并结束转录。

主程序入口

python 复制代码
if __name__ == "__main__":
    recognizer = RealTimeSpeechRecognizer(URL, TOKEN, APPKEY)
    start_audio_stream(recognizer)

创建一个 RealTimeSpeechRecognizer 实例并开始录音和处理音频数据。

通过这些步骤,代码实现了从麦克风捕获音频数据并实时发送到阿里云进行语音转文字的功能。

相关推荐
小二·7 小时前
Python Web 开发进阶实战:性能压测与调优 —— Locust + Prometheus + Grafana 构建高并发可观测系统
前端·python·prometheus
七牛云行业应用8 小时前
重构实录:我删了 5 家大模型 SDK,只留了 OpenAI 标准库
python·系统架构·大模型·aigc·deepseek
知乎的哥廷根数学学派8 小时前
基于多模态特征融合和可解释性深度学习的工业压缩机异常分类与预测性维护智能诊断(Python)
网络·人工智能·pytorch·python·深度学习·机器学习·分类
一人の梅雨9 小时前
亚马逊SP-API商品详情接口轻量化实战:合规与商业价值提取指南
python
梦梦代码精10 小时前
《全栈开源智能体:终结企业AI拼图时代》
人工智能·后端·深度学习·小程序·前端框架·开源·语音识别
袁气满满~_~10 小时前
Python数据分析学习
开发语言·笔记·python·学习
axinawang11 小时前
二、信息系统与安全--考点--浙江省高中信息技术学考(Python)
python·浙江省高中信息技术
寻星探路11 小时前
【算法专题】滑动窗口:从“无重复字符”到“字母异位词”的深度剖析
java·开发语言·c++·人工智能·python·算法·ai
Dxy123931021611 小时前
python连接minio报错:‘SSL routines‘, ‘ssl3_get_record‘, ‘wrong version number‘
开发语言·python·ssl
吨吨不打野11 小时前
CS336——2. PyTorch, resource accounting
人工智能·pytorch·python