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
) 来处理从麦克风捕获的音频数据。以下是代码的详细解释:
主要模块和库
- time 和 threading:用于处理时间和多线程。
- 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
实例并开始录音和处理音频数据。
通过这些步骤,代码实现了从麦克风捕获音频数据并实时发送到阿里云进行语音转文字的功能。