1

I'm not too familiar with python, apologies if this is too trivial question

I have a script that get an audio file from an Url, I need to convert the file from .ogg type to .wav

Then I want to pass the converted and loaded file, to a function that has as argument a filepath string.

Below is my code:

import os
import pydub
import glob
import time

from io import BytesIO

import pandas as pd

from urllib.request import Request, urlopen
import urllib.error

import azure.cognitiveservices.speech as speechsdk
import time

#%%

audio_file = "https = url.whatever.com.co/audio_file.ogg"

req = Request(audio_file)

try: response = urlopen(req).read()
except urllib.error.URLError as e:
    print(e.reason)   

sound =  pydub.AudioSegment.from_ogg(BytesIO(response))
sound_wav = sound.export(format = "wav")

speech_key, service_region = "XXXXXXXXXXXXXXXX", "eastus"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
speech_config.speech_recognition_language="es-ES"
audio_filename = r"C:\some_file_path\3AC3844337F7E5CEAE95.wav"

#audio_config = speechsdk.audio.AudioConfig(sound_wav)
audio_config = speechsdk.audio.AudioConfig(audio_filename = audio_filename)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

done = False

def stop_cb(evt):
    """callback that stops continuous recognition upon receiving an event `evt`"""
    print('CLOSING on {}'.format(evt))
    speech_recognizer.stop_continuous_recognition()
    global done
    done = True

all_results = []
def handle_final_result(evt):
    all_results.append(evt.result.text)

speech_recognizer.recognized.connect(handle_final_result)
# Connect callbacks to the events fired by the speech recognizer
speech_recognizer.recognized.connect(lambda evt: print('RECOGNIZED: {}'.format(evt)))
# stop continuous recognition on either session stopped or canceled events
speech_recognizer.session_stopped.connect(stop_cb)
speech_recognizer.canceled.connect(stop_cb)

# Start continuous speech recognition
speech_recognizer.start_continuous_recognition()
while not done:
    time.sleep(.5)

print("Printing all results:")
print(all_results)

When I run my code with this line:

audio_config = speechsdk.audio.AudioConfig(audio_filename = audio_filename)

It works correct...

However when I run it with this line:

audio_config = speechsdk.audio.AudioConfig(sound_wav)

I get this error:

ValueError: use_default_microphone must be a bool, is "tempfile._TemporaryFileWrapper object at 0x0000020EC4297668"

juanferrs
  • 13
  • 1
  • 2

1 Answers1

2

The error message you got suggests that sound_wav is a temporary filename. Then, as seen in the documentation, it looks like audio_config = speechsdk.audio.AudioConfig(filename = sound_wav) is what you need.

As you used audio_filename as a parameter, it could happen that a different version uses that different name. You can using that instead.

olepinto
  • 351
  • 3
  • 8