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

  1. Confirm Header.BitsPerSample is one of 8/16/24/32 before writing.
  2. Ensure the destination cache is created as 16-bit PCM (the peaks format).
  3. Reject non-PCM AudioFormat before reaching the writer.
  4. 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

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


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/4123398e0355c29a. Report an issue: GitHub.