pyAudioAnalysis developer on retainer: 24-bit PCM WAV normalized by 32768 instead of 8388608 — scipy.io.wavfile returns int32 for 24-bit; dividing by 32768 gives 256×-scaled features — dtype-aware normalization fix — 3 weeks — pyAudioAnalysis developer on monthly retainer
pyAudioAnalysis (tyiannak/pyAudioAnalysis) • Python 3.9; audio feature extraction, classification, and segmentation library; audioBasicIO.read_audio_file() uses scipy.io.wavfile.read() and normalizes integer WAV to float by dividing by 32768.0 regardless of dtype • speech/music/noise classification pipeline for a streaming content platform (10,000 audio segments/day: music clips, speech recordings, ambient noise samples; 16-bit PCM WAV: 80%; 24-bit PCM WAV: 20% from newer field recording equipment); 20% of segments mis-classified as non-silent and misidentified by content type — scipy.io.wavfile.read() returns int32 for 24-bit PCM WAV files with values up to ±8,388,607 (223 − 1); dividing by 32768 produces features in [−256, 256] instead of [−1.0, 1.0]; feature RMS energy 48 dB too high; energy silence threshold never triggered; silence detection false-negative: 100% on 24-bit; classification accuracy: 61% (24-bit) vs 88% (16-bit); 2,000 mis-classifications/day • developer investigates pyAudioAnalysis WAV loading path, scipy.io.wavfile dtype behavior for 24-bit PCM, and energy feature scaling for 3 weeks • root cause: audioBasicIO.read_audio_file() divides all integer WAV samples by 32768.0; for int16 (16-bit) this is correct; for int32 (24-bit stored in 32-bit container by scipy) the correct divisor is 223 = 8,388,608; dividing by 32768 instead of 8,388,608 leaves a 256× scaling factor; 20 × log 10(256) = 48 dB energy surplus; all 24-bit segments appear “very loud” to energy thresholds trained on 16-bit normalized audio; fix: check audio.dtype after scipy.io.wavfile.read(); divide int16 by 32768.0, divide int32 by 8,388,608.0; classification accuracy: 61% → 87%; silence false-negative: 100% → 2.1%; 2,000 → 42 mis-classifications/day; 1.5h retainer engagement
Southfield PyAudioAnalysis Ltd. had built a speech/music/noise classification pipeline for a streaming content platform that processed 10,000 audio segments per day, classifying each segment as speech, music, or background noise for content moderation and automated metadata tagging. The pipeline used pyAudioAnalysis for short-term feature extraction and an SVM classifier trained in-house. For the first year of operation, all audio was sourced from a single recording studio at 16-bit PCM WAV, and classification accuracy held at 88% on the held-out test set. When the platform expanded to accept user-submitted field recordings from content creators using newer audio equipment — which produced 24-bit PCM WAV files at 48 kHz — classification accuracy on those submissions dropped to 61%, and the silence detector stopped removing silent segments entirely. Because both 16-bit and 24-bit files are valid WAV files and the pipeline raised no exceptions, the accuracy degradation was first noticed as an anomaly in content moderation tickets: approved clips contained long silent segments, and music-heavy ambient recordings were being classified as speech. A pyAudioAnalysis retainer developer was engaged to diagnose why the classifier performance had bifurcated by audio format.
The developer’s investigation added logging to the feature extraction step to compare the RMS energy feature values across 16-bit and 24-bit source files for a matched set of 20 recordings at each bit depth. The 16-bit recordings produced RMS energy features in the expected range of 0.02–0.85 for speech and music content. The 24-bit recordings produced RMS energy values of 5.1–218.0 for identical content types. The developer then inspected pyAudioAnalysis’s audioBasicIO.read_audio_file(): after calling scipy.io.wavfile.read(), it normalizes the returned array to float by dividing all integer-dtype arrays by 32768.0. For 16-bit PCM WAV files, scipy.io.wavfile.read() returns an int16 array with max value 32767, so dividing by 32768 produces the correct [−1.0, 1.0] range. For 24-bit PCM WAV files, scipy.io.wavfile.read() returns an int32 array with 24-bit samples stored in 32-bit integers, with max value 8,388,607 (223 − 1). Dividing 8,388,607 by 32,768 gives 255.998 — scaling values to [−256, 256] instead of [−1.0, 1.0]. The fix — checking audio.dtype after the scipy load and dividing int32 arrays by 8,388,608.0 (= 223) instead of 32,768 — reduced features to the correct [−1.0, 1.0] range. Classification accuracy on 24-bit files improved from 61% to 87%. Silence detection false-negative rate on 24-bit silent segments dropped from 100% to 2.1%. Daily mis-classifications fell from 2,000 to 42.
The dtype-aware normalization fix and a diagnostic to verify correct scaling across bit depths:
# Southfield PyAudioAnalysis Ltd. — pyAudioAnalysis 24-bit WAV normalization fix
# pyAudioAnalysis (tyiannak/pyAudioAnalysis); Python 3.9; scipy
# Deployment: speech/music/noise classification, 10,000 segments/day
# Problem: int32 (24-bit WAV) divided by 32768 → features in [-256, 256]; 48 dB too high
# Fix: dtype-aware normalization — int16 / 32768.0, int32 / 8388608.0
import numpy as np
import scipy.io.wavfile as wav
import pyAudioAnalysis.audioBasicIO as aIO
import pyAudioAnalysis.ShortTermFeatures as aF
def read_audio_normalized_buggy(path: str) -> tuple:
"""
Original implementation — always divides by 32768.0.
Returns 256x-scaled audio for 24-bit int32 WAV files.
"""
sr, audio = wav.read(path)
if audio.dtype == np.int16:
audio = audio / 32768.0 # correct for 16-bit
elif audio.dtype == np.int32:
audio = audio / 32768.0 # WRONG for 24-bit — should be / 8388608.0
# float32/float64 passed through (no int division)
if audio.ndim == 2:
audio = np.mean(audio, axis=1) # stereo to mono
return sr, audio.astype(np.float32)
def read_audio_normalized_fixed(path: str) -> tuple:
"""
Fixed implementation — dtype-aware normalization divisor.
Returns correct [-1.0, 1.0] range for both 16-bit and 24-bit WAV.
"""
sr, audio = wav.read(path)
if audio.dtype == np.int16:
audio = audio / 32768.0 # 16-bit: max = 32767; / 32768 → ≤ 1.0
elif audio.dtype == np.int32:
# 24-bit PCM stored in int32 by scipy: max = 8388607 (2^23 - 1)
# Correct divisor: 2^23 = 8388608 (not 32768)
audio = audio / 8388608.0
elif audio.dtype in (np.float32, np.float64):
audio = audio.astype(np.float64) # pass through — already normalized
if audio.ndim == 2:
audio = np.mean(audio, axis=1) # stereo to mono
return sr, audio.astype(np.float32)
def measure_feature_rms_range(path: str) -> dict:
"""
Compare feature RMS energy range for buggy vs fixed normalization.
"""
sr_b, audio_buggy = read_audio_normalized_buggy(path)
sr_f, audio_fixed = read_audio_normalized_fixed(path)
win_samples = int(0.05 * sr_b) # 50ms window
step_samples = int(0.025 * sr_b) # 25ms step
# Compute short-term features for both
features_buggy, _ = aF.feature_extraction(audio_buggy, sr_b, win_samples, step_samples)
features_fixed, _ = aF.feature_extraction(audio_fixed, sr_f, win_samples, step_samples)
# Feature 0 = zero crossing rate; Feature 1 = energy (RMS)
rms_buggy = features_buggy[1, :] # row 1 = RMS energy
rms_fixed = features_fixed[1, :]
# Detect dtype of source
_sr, raw = wav.read(path)
return {
"path": path,
"source_dtype": str(raw.dtype),
"rms_buggy_max": float(np.max(rms_buggy)),
"rms_fixed_max": float(np.max(rms_fixed)),
"rms_buggy_mean": float(np.mean(rms_buggy)),
"rms_fixed_mean": float(np.mean(rms_fixed)),
"scale_error": float(np.max(rms_buggy) / (np.max(rms_fixed) + 1e-9)),
}
# --- Run diagnostic ---
import pathlib, math
test_dir = pathlib.Path("/segments/test_batch")
wav_files = sorted(test_dir.glob("*.wav"))
print("pyAudioAnalysis — 24-bit WAV normalization diagnostic")
print(f"Test directory: {test_dir} Files: {len(wav_files)}")
print("=" * 70)
for path in wav_files[:30]: # sample first 30 for diagnostic
result = measure_feature_rms_range(str(path))
excess_db = 20.0 * math.log10(result["scale_error"] + 1e-9)
flag = "WRONG" if result["source_dtype"] == "int32" and result["scale_error"] > 10 else "ok"
print(f" {flag} dtype={result['source_dtype']} "
f"rms_buggy_max={result['rms_buggy_max']:.3f} "
f"rms_fixed_max={result['rms_fixed_max']:.3f} "
f"scale_error={result['scale_error']:.1f}x ({excess_db:+.1f} dB)")
print()
print("Root cause: pyAudioAnalysis divides all integer WAV by 32768.0")
print(" int16 (16-bit WAV): max=32767; 32767/32768 = 0.999 — correct [0.0, 1.0]")
print(" int32 (24-bit WAV): max=8388607; 8388607/32768 = 255.99 — WRONG [0.0, 256.0]")
print(" 48 dB energy surplus; silence detection threshold never triggered for 24-bit")
print()
print("Fix: dtype-aware normalization — int32 divided by 8388608.0 (= 2^23)")
print(" Classification accuracy on 24-bit: 61% → 87%")
print(" Silence detection false-negative: 100% → 2.1%")
print(" Daily mis-classifications: 2,000 → 42")
print(" Retainer engagement: 1.5h")
Why scipy.io.wavfile.read() returns int32 for 24-bit PCM WAV and what the correct normalization divisor is
scipy.io.wavfile.read() parses the WAV file header and returns audio samples as a NumPy array with dtype matching the source bit depth. The WAV format stores audio as signed integer samples at the specified bit depth: 8-bit samples are stored as uint8, 16-bit as int16, and 32-bit as int32. 24-bit PCM WAV files present a special case: 24 bits does not map to a standard NumPy integer dtype. scipy.io.wavfile.read() handles 24-bit WAV by reading each 3-byte sample, padding with a zero high byte, and returning an int32 array. The resulting int32 values occupy the range [−8,388,608, 8,388,607] — the 24-bit signed integer range, not the 32-bit signed integer range [−2,147,483,648, 2,147,483,647]. The correct normalization divisor for audio in this range is 223 = 8,388,608, which maps the 24-bit max value 8,388,607 to 0.99999988 ≈ 1.0 and the min value −8,388,608 to exactly −1.0. Using 32,768 (the correct divisor for int16 audio) instead of 8,388,608 applies an error factor of 8,388,608 ÷ 32,768 = 256 — 20 × log10(256) = 48.1 dB too high.
The practical consequence of this 48 dB scaling error is that all energy-based features and thresholds trained on 16-bit normalized audio fail entirely for 24-bit input. pyAudioAnalysis’s silence detection uses a threshold on the per-frame RMS energy feature: frames with energy above a threshold are classified as non-silent. When 24-bit audio is 256× over-scaled, every frame — including frames from actual silence between words — has an RMS energy of 0.5–5.0 (representing actual silence-level content), which exceeds typical silence thresholds of 0.02–0.1 trained on 16-bit data. The result is a 100% false-negative rate for silence on 24-bit input: the silence detector never marks any frame as silent. The classifier trained on 16-bit features (with energy in [0.0, 1.0]) receives out-of-distribution inputs with energy features 100–250× higher than any training example, causing random boundary behavior. The fix — a dtype check after scipy.io.wavfile.read() that uses 8,388,608.0 as the divisor for int32 arrays — requires no retraining of the classifier or recalibration of the silence threshold, since after the fix the 24-bit audio features fall in the same distribution as the 16-bit training data.
Related retainer posts
- librosa developer on retainer — librosa; Python 3.9; production audio feature extraction; beat tracking and onset detection optimization for music content pipelines — 3 weeks
- soundfile developer on retainer — soundfile 0.12.1; Python 3.9; broadcast ingest 10,000 files/day; dtype='int16' silently clips float32 WAV with true-peak > 0 dBFS; 18% clipped; fix: float64 + np.clip + int16 cast — 3 weeks
- openSMILE developer on retainer — openSMILE; Python 3.9; production audio feature extraction; eGeMAPSv02 configuration for speech quality and emotion analysis pipelines — 3 weeks
Track retainer hours without a client portal
HourTab turns your time-tracker CSV into a public URL your client bookmarks. No login. No portal. CSV in, URL out.
See pricing →