@Gustavo Jakobi Thanks for getting back. You can leverage the PushAudioInputStream.Write Method.. This writes the audio data specified by making an internal copy of the data. Note: The dataBuffer must not contain an audio header.
. Sample code:
import os
import azure.cognitiveservices.speech as speechsdk
import wave
def recognize_from_wav_file(filename):
# This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
speech_config = speechsdk.SpeechConfig(subscription="4cd859XXXXXXXXX1b8", region="westeurope")
speech_config.speech_recognition_language="en-US"
# Open the .wav file
wf = wave.open(filename, 'rb')
# Set up the audio stream
push_stream = speechsdk.audio.PushAudioInputStream()
audio_config = speechsdk.audio.AudioConfig(stream=push_stream)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
# Read the .wav file in chunks and feed them to the speech recognizer
CHUNK = 1024
data = wf.readframes(CHUNK)
while len(data) > 0:
push_stream.write(data)
data = wf.readframes(CHUNK)
# Close the stream to signal that all audio data has been written
push_stream.close()
# Recognize the speech from the .wav file
speech_recognition_result = speech_recognizer.recognize_once_async().get()
if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized: {}".format(speech_recognition_result.text))
elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = speech_recognition_result.cancellation_details
print("Speech Recognition canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_details))
print("Did you set the speech resource key and region values?")
recognize_from_wav_file('MyAudioFile.wav')