SubtitleEdit/subtitleedit · error · InvalidDataException
Cannot read bits per sample of {Header.BitsPerSample}
Error message
Cannot read bits per sample of {Header.BitsPerSample} What it means
Thrown by GetSampleDataReader when Header.BytesPerSample (derived from BitsPerSample, rounded up to whole bytes) is not 1, 2, 3, or 4. The reader only has implementations for 8/16/24/32-bit PCM samples; any other byte-per-sample value (e.g. 0 from a 0-bit header, or a non-PCM format) has no read path. The message echoes BitsPerSample rather than BytesPerSample, which can be slightly misleading.
Source
Thrown at src/ui/Logic/Media/WaveToVisualizer2.cs:914
private double GetSampleAndChannelScale()
{
return GetSampleScale() / Header.NumberOfChannels;
}
private ReadSampleDataValue GetSampleDataReader()
{
switch (Header.BytesPerSample)
{
case 1:
return ReadValue8Bit;
case 2:
return ReadValue16Bit;
case 3:
return ReadValue24Bit;
case 4:
return ReadValue32Bit;
default:
throw new InvalidDataException("Cannot read bits per sample of " + Header.BitsPerSample);
}
}
private WriteSampleDataValue GetSampleDataWriter()
{
switch (Header.BytesPerSample)
{
case 1:
return WriteValue8Bit;
case 2:
return WriteValue16Bit;
case 3:
return WriteValue24Bit;
case 4:
return WriteValue32Bit;
default:
throw new InvalidDataException("Cannot write bits per sample of " + Header.BitsPerSample);
}View on GitHub (pinned to 17a9f07487)
Solutions
- Check Header.BitsPerSample is 8, 16, 24, or 32 before invoking sample-reading APIs.
- For 32-bit float audio, convert to 16-bit PCM first or extend the reader.
- Validate Header.AudioFormat == PCM (1) before reading samples.
- Re-encode the source WAV to a supported PCM bit depth if it is an exotic format.
Example fix
// before
var reader = GetSampleDataReader();
// after
if (Header.BytesPerSample is not (1 or 2 or 3 or 4))
throw new NotSupportedException($"Unsupported bit depth: {Header.BitsPerSample}-bit");
var reader = GetSampleDataReader(); Defensive patterns
Strategy: validation
Validate before calling
if (wave.Header.BytesPerSample is not (1 or 2 or 3 or 4))
throw new NotSupportedException($"Unsupported bit depth: {wave.Header.BitsPerSample}-bit (AudioFormat={wave.Header.AudioFormat})");
if (wave.Header.AudioFormat != WaveHeader2.AudioFormatPcm && wave.Header.AudioFormat != 0xFFFE)
throw new NotSupportedException("Only PCM WAV is supported for sample reading"); Type guard
static bool IsSupportedReadBitDepth(WaveHeader2 h) => h.BytesPerSample is 1 or 2 or 3 or 4;
Try / catch
try { var reader = GetSampleDataReader(); }
catch (InvalidDataException ex) when (ex.Message.Contains("Cannot read")) { /* convert to 16-bit PCM */ } Prevention
- Check BytesPerSample/BitsPerSample before reading samples.
- Reject non-PCM (float/compressed) audio formats upstream.
- Re-encode exotic bit depths to 16-bit PCM.
- Validate the WAV header immediately after construction.
When it happens
Trigger: Calling any sample-reading API (GeneratePeaks, GenerateSpectrogram, GetSamples) on a WAV whose BitsPerSample is 0, 12, an unusual value, or whose AudioFormat is non-PCM (float/compressed) so BitsPerSample does not map to 8/16/24/32.
Common situations: Opening a 32-bit float WAV (AudioFormat 3) where BitsPerSample is 32 but the value semantics differ; a corrupt fmt chunk yielding BitsPerSample 0; a 20-bit file that rounds up but is otherwise mishandled; header corruption.
Related errors
- Cannot write bits per sample of {Header.BitsPerSample}
- Peaks file must be 16 bits per sample.
- Stream is too small
- Peaks file must have 1 or 2 channels.
- Too many bytes for CCData!
AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13).
Data as JSON: /api/errors/dbdf8af9f8bf1621.
Report an issue: GitHub.