Microsoft ADPCM WAVs load at a fraction of their length (audio comes out sped up) through torchaudio.load
Ran into this with a couple of WAV files that played fine in a normal player but came out way too fast once loaded. Took a while to pin down: the files are fine, the decode isn't.
What happens
Some WAVs are Microsoft ADPCM (format tag 0x0002) rather than plain PCM. Plenty of older tools and some converters still write them. When one goes through torchaudio.load (which decodes via torchcodec now), only a fraction of the samples come back, still labelled with the original sample rate. Nothing errors or warns. The audio just plays back sped up and choppy, and its duration is half or a quarter of what it should be.
From poking at it: the WAV demuxer hands out 4096-byte packets, each holding several ADPCM blocks, and only the first block of each packet survives. A file with 2048-byte blocks comes back at 1/2 length, one with 1024-byte blocks at 1/4. The samples that do come back line up exactly with the real ones, so whole blocks are being dropped, not resampled.
soundfile, PyAV and the ffmpeg CLI all decode the same files correctly.
Repro
ffmpeg -f lavfi -i "sine=frequency=440:duration=10:sample_rate=48000" -ac 2 -c:a adpcm_ms tone_adpcm.wavimport torchaudio, soundfile as sf
w, sr = torchaudio.load("tone_adpcm.wav")
print(w.shape[-1] / sr) # 2.509 s
d, sr = sf.read("tone_adpcm.wav")
print(len(d) / sr) # 10.015 sVersions: torch 2.13.0, torchaudio 2.11.0, torchcodec 0.16.0, av 16.0.1, Linux. I haven't tried the torchcodec 0.15.0 that the installer pins, so the snippet above is a quick way to check that one.
Where it hits in ai-toolkit
The one that matters most is the audio dataloader, since it quietly trains on the wrong audio:
toolkit/dataloader_mixins.py#L495(audio files)toolkit/dataloader_mixins.py#L780(video soundtracks; ADPCM is rarer there, but it's the same call)
The rest decode the same way:
extensions_built_in/audio_models/ace_step/src/model.py#L1367(get_latent)extensions_built_in/captioner/AceStepCaptioner.py#L378and#L409extensions_built_in/captioner/Qwen25OmniCaptioner.py#L188extensions_built_in/llm_models/qwen25_omni.py#L422extensions_built_in/diffusion_models/minimax_h3/minimax_h3.py#L1372extensions_built_in/diffusion_models/minimax_h3/src/ref_video_cache.py#L260scripts/caption_audio_dataset.py#L55toolkit/audio/melbandroformer/__main__.py#L39(uses torchcodec'sAudioDecoderdirectly)
Workarounds
- Users can convert affected files to PCM or FLAC first, e.g.
ffmpeg -i in.wav -c:a pcm_s16le out.wav.soundfile.info(path).subtypereportsMS_ADPCMfor the affected ones. - Decoding through PyAV (already a dependency) or soundfile instead of
torchaudio.loadreturns the full length. The root cause looks like it sits in torchcodec, so it's probably worth a report over there too.
Source: ostris/ai-toolkit