|
|
import speech_recognition as sr
|
|
|
import pyttsx3
|
|
|
|
|
|
|
|
|
def transcribe_audio(audio_file):
|
|
|
recognizer = sr.Recognizer()
|
|
|
with sr.AudioFile(audio_file) as source:
|
|
|
audio_data = recognizer.record(source)
|
|
|
try:
|
|
|
text = recognizer.recognize_sphinx(audio_data)
|
|
|
return text
|
|
|
except sr.UnknownValueError:
|
|
|
return "Speech recognition could not understand audio"
|
|
|
except sr.RequestError as e:
|
|
|
return "Error with speech recognition service: {0}".format(e)
|
|
|
|
|
|
|
|
|
def generate_audio(text, output_file, rate=150):
|
|
|
engine = pyttsx3.init()
|
|
|
engine.setProperty('rate', rate)
|
|
|
engine.save_to_file(text, output_file)
|
|
|
engine.runAndWait()
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
audio_file = "input_audio.wav"
|
|
|
|
|
|
|
|
|
transcribed_text = transcribe_audio(audio_file)
|
|
|
|
|
|
if transcribed_text:
|
|
|
print("Transcribed text:", transcribed_text)
|
|
|
|
|
|
|
|
|
output_file = "output_audio.wav"
|
|
|
generate_audio(transcribed_text, output_file)
|
|
|
|
|
|
print("Audio generated successfully:", output_file)
|
|
|
else:
|
|
|
print("Failed to transcribe audio.")
|
|
|
|