Speech Recognition
Speech recognition converts spoken audio into text, and Python has mature libraries for every layer of the pipeline: microphone capture, feature extraction, acoustic modeling, and text-to-speech for the reverse direction. Classic use cases are voice assistants, meeting transcription, voice dictation, IVR systems, and audio search over media archives.
Capturing Audio
Recognition models consume raw PCM samples, usually 16-bit mono at 16 kHz. The speech_recognition package wraps microphone access and several engines behind an AudioData object, with adjust_for_ambient_noise calibrating the threshold so it does not cut off speech or amplify background hum.
import speech_recognition as sr
rec = sr.Recognizer()
with sr.Microphone(sample_rate=16000) as mic:
rec.adjust_for_ambient_noise(mic, duration=1)
print("Speak now...")
audio = rec.listen(mic, phrase_time_limit=10)
# Offline engine, no cloud dependency
text = rec.recognize_sphinx(audio)
print("you said:", text)
Recognizing an Audio File
For files, the same library dispatches to an engine chosen by quality and cost. Google Web Speech is convenient for prototypes, Whisper (via openai-whisper or faster-whisper) is the offline quality leader, and recognize_sphinx is fully local.
import speech_recognition as sr
rec = sr.Recognizer()
with sr.AudioFile("meeting.wav") as src:
rec.adjust_for_ambient_noise(src, duration=0.5)
audio = rec.record(src)
try:
print(rec.recognize_google(audio, language="en-US"))
except sr.UnknownValueError:
print("Could not transcribe the audio")
except sr.RequestError as e:
print("Engine unavailable:", e)
Whisper Using Transformers
Hugging Face transformers offers OpenAI's Whisper models through the same pipeline interface used elsewhere in this course. Whisper is multilingual, robust to accents and noise, and punctuates output naturally — qualities that make it the default choice for production-grade transcription.
from transformers import pipeline
asr = pipeline(
task="automatic-speech-recognition",
model="openai/whisper-base",
)
result = asr(
"meeting.wav",
generate_kwargs={"task": "transcribe", "language": "english"},
)
print(result["text"])
For long recordings, chunk with chunk_length_s=30 so the model processes windows with overlap; batch multiple files by passing a list to the same call.
Text-to-Speech with pyttsx3
The other direction — speech synthesis — often accompanies recognition in voice interfaces. pyttsx3 performs offline TTS with the operating system's voices, exposes rate, volume, and voice selection, and can render straight to an MP3 for later playback.
import pyttsx3
engine = pyttsx3.init()
engine.setProperty("rate", 160)
engine.setProperty("volume", 0.9)
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[0].id) # try other indexes too
engine.say("Your report has been generated successfully.")
engine.save_to_file("Summary complete.", "message.mp3")
engine.runAndWait()
Designing "Awake Word" Loops
Always-on assistants combine these pieces: listen in the background, trigger on a wake word, send the following phrase to recognition, route the intent to application logic, and answer via TTS. Structure the loop as a straightforward state machine — BackgroundListener fills a queue, a worker thread picks audio up, and recognition stays off the GUI thread so the interface never blocks.
Summary
Python's speech stack covers capture with speech_recognition, cloud or offline transcription through Google, Sphinx, and Whisper via the transformers pipeline, and offline synthesis with pyttsx3. Record at 16 kHz mono, always calibrate against ambient noise, and keep long audio chunked. With those pieces assembled around a listen-recognize-respond loop, a working voice interface is a few dozen lines of code.