SubtitleEdit/subtitleedit · error · InvalidDataException
Cannot write bits per sample of {Header.BitsPerSample}
Error message
Cannot write bits per sample of {Header.BitsPerSample} What it means
Thrown by GetSampleDataWriter when Header.BytesPerSample is not 1, 2, 3, or 4. Symmetric with the reader: only 8/16/24/32-bit write paths exist, so writing peaks to a cache whose bit depth maps to an unsupported byte count is impossible. The message reports BitsPerSample for developer clarity.
Source
Thrown at src/ui/Logic/Media/WaveToVisualizer2.cs:931
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);
}
}
public void Dispose()
{
Close();
}
public void Close()
{
if (_stream != null)
{
_stream.Close();
}
}
//////////////////////////////////////// SPECTRUM ///////////////////////////////////////////////////////////
View on GitHub (pinned to 17a9f07487)
Solutions
- Confirm Header.BitsPerSample is one of 8/16/24/32 before writing.
- Ensure the destination cache is created as 16-bit PCM (the peaks format).
- Reject non-PCM AudioFormat before reaching the writer.
- Validate the header right after construction and fail early with a clear message.
Example fix
// before
var writer = GetSampleDataWriter();
// after
if (Header.BytesPerSample is not (1 or 2 or 3 or 4))
throw new NotSupportedException("Cannot write peaks for this bit depth");
var writer = GetSampleDataWriter(); 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 for writing: {wave.Header.BitsPerSample}-bit"); Type guard
static bool IsSupportedWriteBitDepth(WaveHeader2 h) => h.BytesPerSample is 1 or 2 or 3 or 4;
Try / catch
try { var writer = GetSampleDataWriter(); }
catch (InvalidDataException ex) when (ex.Message.Contains("Cannot write")) { /* create a fresh 16-bit PCM cache */ } Prevention
- Create the peaks cache as 16-bit PCM explicitly.
- Validate header bit depth before writing.
- Reject corrupt/non-PCM headers early.
- Re-initialize the cache when bit depth is unsupported.
When it happens
Trigger: Invoking the sample-writing path (e.g. SavePeaks / writing a peaks cache) for a WAV whose BitsPerSample does not round to 1-4 bytes per sample, or whose header was parsed from corrupt/float/non-PCM data.
Common situations: Attempting to write a peaks cache for a source whose header is corrupt (BitsPerSample 0); float audio misidentified as PCM; a header written by a non-standard encoder.
Related errors
- Cannot read 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/4123398e0355c29a.
Report an issue: GitHub.