{"record":{"id":"4123398e0355c29a","repo":"SubtitleEdit/subtitleedit","slug":"cannot-write-bits-per-sample-of-header-bitspersam","errorCode":null,"errorMessage":"Cannot write bits per sample of {Header.BitsPerSample}","messagePattern":"Cannot write bits per sample of (.+?)","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/Media/WaveToVisualizer2.cs","lineNumber":931,"sourceCode":"            default:\n                throw new InvalidDataException(\"Cannot read bits per sample of \" + Header.BitsPerSample);\n        }\n    }\n\n    private WriteSampleDataValue GetSampleDataWriter()\n    {\n        switch (Header.BytesPerSample)\n        {\n            case 1:\n                return WriteValue8Bit;\n            case 2:\n                return WriteValue16Bit;\n            case 3:\n                return WriteValue24Bit;\n            case 4:\n                return WriteValue32Bit;\n            default:\n                throw new InvalidDataException(\"Cannot write bits per sample of \" + Header.BitsPerSample);\n        }\n    }\n\n    public void Dispose()\n    {\n        Close();\n    }\n\n    public void Close()\n    {\n        if (_stream != null)\n        {\n            _stream.Close();\n        }\n    }\n\n    //////////////////////////////////////// SPECTRUM ///////////////////////////////////////////////////////////\n","sourceCodeStart":913,"sourceCodeEnd":949,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/Media/WaveToVisualizer2.cs#L913-L949","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar writer = GetSampleDataWriter();\n\n// after\nif (Header.BytesPerSample is not (1 or 2 or 3 or 4))\n    throw new NotSupportedException(\"Cannot write peaks for this bit depth\");\nvar writer = GetSampleDataWriter();","handlingStrategy":"validation","validationCode":"if (wave.Header.BytesPerSample is not (1 or 2 or 3 or 4))\n    throw new NotSupportedException($\"Unsupported bit depth for writing: {wave.Header.BitsPerSample}-bit\");","typeGuard":"static bool IsSupportedWriteBitDepth(WaveHeader2 h) => h.BytesPerSample is 1 or 2 or 3 or 4;","tryCatchPattern":"try { var writer = GetSampleDataWriter(); }\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"Cannot write\")) { /* create a fresh 16-bit PCM cache */ }","preventionTips":["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."],"tags":["wav","audio","pcm","bit-depth","validation"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}