This is python code to change voice, first, it converts your voice to text, next it converts text to speech.
Requirements.txt
SpeechRecognition
pyttsx3
Speech to text
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
# read the audio data from the default microphone
print("Recognizing...")
while True:
# r.adjust_for_ambient_noise(source)
audio_data = r.listen(source)
# convert speech to text
try:
text = r.recognize_google(audio_data)
print(text)
except sr.UnknownValueError:
pass
Text To Speech
import pyttsx3
converter = pyttsx3.init()
voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0"
# voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0"
converter.setProperty('voice', voice_id)
# Sets speed percent Can be more than 100
converter.setProperty('rate', 150)
# Set volume 0-1
converter.setProperty('volume', 0.7)
while True:
text = input()
converter.say(text)
converter.runAndWait()
Voice Changer
import speech_recognition as sr
import pyttsx3
r = sr.Recognizer()
# combine both speech to text and text to speech
converter = pyttsx3.init()
voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0"
# voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0"
converter.setProperty('voice', voice_id)
# Sets speed percent Can be more than 100
converter.setProperty('rate', 150)
# Set volume 0-1
converter.setProperty('volume', 0.7)
with sr.Microphone() as source:
# read the audio data from the default microphone
print("Recognizing...")
while True:
r.adjust_for_ambient_noise(source, duration=0.2)
audio_data = r.listen(source)
# convert speech to text
try:
text = r.recognize_google(audio_data)
# text = r.recognize_sphinx(audio_data)
print(text)
converter.say(text)
converter.runAndWait()
except sr.UnknownValueError as e:
pass
No comments:
Post a Comment