Sub topic 2
Audio basics you need before calling an API
This chapter assumes you can write basic Python, have called an SDK-based LLM chat API, and keep API keys in environment variables. You also need to know that an MP3 or WAV file holds audio. Here you learn the four properties that decide whether a speech API accepts your file and how well it transcribes it.
What a digital recording contains
Sound is a pressure wave. A microphone turns it into a voltage, and an analog-to-digital converter measures that voltage many times per second. Each measurement is a sample. Four terms describe the result.
- Sample rate is the number of samples per second, in hertz (Hz). Telephone audio uses 8,000 Hz. Speech models commonly work at 16,000 Hz. Music CDs use 44,100 Hz.
- Bit depth is the number of bits per sample. 16 bits is common.
- Channels are the separate streams in a file. Mono has one. Stereo has two.
- Codec is the method that encodes and compresses samples. Examples are PCM, MP3, AAC, and Opus.
A container is the file format that wraps encoded audio and its metadata. WAV, MP3, M4A, and OGG are common. Some names refer to both: a .wav file usually holds uncompressed PCM, while an .m4a container usually holds AAC.
Why these properties matter for speech
Human speech carries most of its information below about 4 kHz. By the Nyquist rule, a sample rate must be at least twice the highest frequency you want to keep. So 16,000 Hz captures speech well, and higher rates mostly add file size. Rates below 16,000 Hz, such as 8,000 Hz phone audio, lose consonant detail and usually raise transcription errors.
Channels matter because speech models usually want one stream. Many APIs mix stereo down to mono. If two speakers sit on separate channels, the mix-down merges them, so you lose the ability to tell them apart by channel.
Compression matters in two ways. Lossy codecs like MP3 discard detail to save space. Heavy compression, such as very low bitrates, can hurt accuracy. Lossless or uncompressed formats keep everything but make larger files, and file size is the limit you hit first (see the chapter on long recordings).
| Format | Compression | Typical use | Size for 1 minute of speech |
|---|---|---|---|
| WAV (PCM) | None | Recording, editing | About 1.9 MB at 16 kHz mono, 16-bit |
| MP3 | Lossy | Sharing, upload | About 0.5 to 1 MB at 64 to 128 kbps |
| FLAC | Lossless | Archiving | About 1 MB |
| Opus (OGG) | Lossy | Voice and streaming | About 0.2 to 0.5 MB |
You can check the WAV figure yourself: 16,000 samples × 2 bytes × 60 seconds = 1,920,000 bytes.
Inspecting a file before you send it
Check a file's properties first. The standard library reads WAV headers without extra packages.
import wave
with wave.open("sample.wav", "rb") as wav:
rate = wav.getframerate()
channels = wav.getnchannels()
width = wav.getsampwidth()
seconds = wav.getnframes() / rate
print(f"sample rate: {rate} Hz")
print(f"channels: {channels}")
print(f"bit depth: {width * 8}")
print(f"duration: {seconds:.1f} s")For MP3 and other formats, use ffprobe from FFmpeg or the pydub library, which relies on FFmpeg being installed.
Common mistakes
- Uploading a huge WAV. A one-hour 44.1 kHz stereo WAV is about 635 MB. Convert to mono MP3 or Opus first.
- Upsampling to "improve" quality. Converting 8 kHz audio to 44.1 kHz adds samples but no lost detail returns.
- Trusting the file extension. A renamed file keeps its original codec. Check the real format with
ffprobe. - Clipping. If the input is too loud, peaks are cut off and distorted. Record with headroom.
Exercise
A 10-minute recording is stored as 16-bit, 16,000 Hz, mono PCM in a WAV file. About how large is it in megabytes, ignoring the small header?
Show answer
Bytes = 16,000 samples/s × 2 bytes × 600 s = 19,200,000 bytes, or about 19.2 MB (roughly 18.3 MiB). Converting to MP3 at 64 kbps would shrink it to about 4.8 MB.