是否可以将 numpy 数组和采样率发送到 Microsoft speech-to-text 而不是将其保存到 wav 文件?

Is it possible to send numpy array and sample rate to microsoft speech-to-text instead of saving this to wav file?

我正在使用 Microsoft Cognitive Services 语音转文本 python API 进行转录。

现在,我通过网络 API 获取声音(使用此处的麦克风部分:https://ricardodeazambuja.com/deep_learning/2019/03/09/audio_and_video_google_colab/),然后我将声音写入 'sound.wav' 然后发送'sound.wav' 到 MCS STT 引擎以获取转录。 Web API 给了我一个 numpy 数组和声音的采样率。

我的问题是:是否可以将 numpy 数组和采样率直接发送到 MCS STT 而不是写入 wav 文件?

这是我的代码:

import azure.cognitiveservices.speech as speechsdk
import scipy.io.wavfile

audio, sr = get_audio()

p  = 'sound.wav'
scipy.io.wavfile.write(p,sr,audio)

speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
audio_input = speechsdk.AudioConfig(filename=p)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_input)

根据我的研究和查看代码:

您将无法在 Google 协作中直接使用麦克风 - 因为 python 被执行的实例 - 您不太可能 access/operate 相同.因此,您使用了有助于在网络浏览器级别录制音频的文章。

现在 - 录制的音频在 WEBM format.As 每个代码中,他们进一步使用 FFMPEG 以转换为 WAV 格式。

但是,请注意,除了音频数据外,还有 headers

现在这不是在 below snippet code 中 returned - 而不是在 get_audio() 中 returning audio,sr 你将不得不return riff - 这是以字节为单位的 WAV 音频(但这包括 header 除了音频数据)

看到 post 解释了字节级别的 WAV 文件的组成(这可能与输出有关)

http://soundfile.sapp.org/doc/WaveFormat/

在此你必须去掉音频数据字节、每秒采样和所有必要的数据&使用PushAudioInputStream方法

样本

channels = 1
bitsPerSample = 16
samplesPerSecond = 16000
audioFormat = AudioStreamFormat(samplesPerSecond, bitsPerSample, channels)
custom_push_stream = speechsdk.audio.PushAudioInputStream(stream_format=audioFormat)

在此custom_push_stream - 您可以写入音频数据来执行 STT

custom_push_stream.write(audiodata)