"""Decode and validate PCM WAV samples. Lesson: projects/voice-note-transcriber-pipeline/stages/01-pcm/docs/en.md The implementation is original and uses explicit local data contracts. Run its tests through scripts/project_test.py. """ import io import struct import wave def decode_wav(data): with wave.open(io.BytesIO(data), "rb") as audio: if ( audio.getnchannels() != 1 or audio.getsampwidth() != 2 or audio.getcomptype() != "NONE" ): raise ValueError("16-bit mono PCM required") rate = audio.getframerate() frames = audio.getnframes() raw = audio.readframes(frames) if len(raw) != frames * 2: raise ValueError("truncated audio") return { "rate": rate, "samples": [x[0] / 32768 for x in struct.iter_unpack("