46 – Real-World Python Projects – Text-to-Speech and Speech-to-Text App

This project allows users to:

✔ Convert Text → Speech (TTS)

✔ Convert Speech → Text (STT)

✔ Save audio files

✔ Use microphone input

✔ Build a small GUI app (optional)


🛠 Skills You Will Learn

  • Python audio processing
  • Using gTTS (Google Text-to-Speech)
  • Using SpeechRecognition library
  • Handling audio files (MP3/WAV)
  • Optional: GUI using Tkinter

📦 Install Required Libraries

Run:

pip install gtts speechrecognition pyaudio playsound

If pyaudio gives error:

pip install pipwin
pipwin install pyaudio

🧩 PART 1 — Text-to-Speech (TTS)

from gtts import gTTS
from playsound import playsound

text = input("Enter text to speak: ")

tts = gTTS(text)
tts.save("speech.mp3")

print("Speaking...")
playsound("speech.mp3")

✅ Converts text → audio
✅ Saves MP3 file
✅ Plays it automatically


🧩 PART 2 — Speech-to-Text (STT)

import speech_recognition as sr

r = sr.Recognizer()

with sr.Microphone() as mic:
    print("Speak now...")
    audio = r.listen(mic)

try:
    text = r.recognize_google(audio)
    print("You said:", text)
except:
    print("Sorry, I could not understand.")

✔ Records live microphone audio
✔ Uses Google Speech Recognition
✔ Prints recognized text


🧩 PART 3 — Full App (TTS + STT Combined)

from gtts import gTTS
import speech_recognition as sr
from playsound import playsound

def text_to_speech():
    text = input("Enter text: ")
    tts = gTTS(text)
    tts.save("tts_output.mp3")
    playsound("tts_output.mp3")

def speech_to_text():
    r = sr.Recognizer()
    with sr.Microphone() as mic:
        print("Speak now...")
        audio = r.listen(mic)

    try:
        print("You said:", r.recognize_google(audio))
    except:
        print("Could not understand.")

print("1 - Text to Speech")
print("2 - Speech to Text")

choice = input("Choose option: ")

if choice == "1":
    text_to_speech()
else:
    speech_to_text()

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *