{"record":{"id":"dbdf8af9f8bf1621","repo":"SubtitleEdit/subtitleedit","slug":"cannot-read-bits-per-sample-of-header-bitspersamp","errorCode":null,"errorMessage":"Cannot read bits per sample of {Header.BitsPerSample}","messagePattern":"Cannot read bits per sample of (.+?)","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/Media/WaveToVisualizer2.cs","lineNumber":914,"sourceCode":"    private double GetSampleAndChannelScale()\n    {\n        return GetSampleScale() / Header.NumberOfChannels;\n    }\n\n    private ReadSampleDataValue GetSampleDataReader()\n    {\n        switch (Header.BytesPerSample)\n        {\n            case 1:\n                return ReadValue8Bit;\n            case 2:\n                return ReadValue16Bit;\n            case 3:\n                return ReadValue24Bit;\n            case 4:\n                return ReadValue32Bit;\n            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        }","sourceCodeStart":896,"sourceCodeEnd":932,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/Media/WaveToVisualizer2.cs#L896-L932","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar reader = GetSampleDataReader();\n\n// after\nif (Header.BytesPerSample is not (1 or 2 or 3 or 4))\n    throw new NotSupportedException($\"Unsupported bit depth: {Header.BitsPerSample}-bit\");\nvar reader = GetSampleDataReader();","handlingStrategy":"validation","validationCode":"if (wave.Header.BytesPerSample is not (1 or 2 or 3 or 4))\n    throw new NotSupportedException($\"Unsupported bit depth: {wave.Header.BitsPerSample}-bit (AudioFormat={wave.Header.AudioFormat})\");\nif (wave.Header.AudioFormat != WaveHeader2.AudioFormatPcm && wave.Header.AudioFormat != 0xFFFE)\n    throw new NotSupportedException(\"Only PCM WAV is supported for sample reading\");","typeGuard":"static bool IsSupportedReadBitDepth(WaveHeader2 h) => h.BytesPerSample is 1 or 2 or 3 or 4;","tryCatchPattern":"try { var reader = GetSampleDataReader(); }\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"Cannot read\")) { /* convert to 16-bit PCM */ }","preventionTips":["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."],"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"}