{"record":{"id":"d89c5b7acf701060","repo":"SubtitleEdit/subtitleedit","slug":"peaks-file-must-be-16-bits-per-sample","errorCode":null,"errorMessage":"Peaks file must be 16 bits per sample.","messagePattern":"Peaks file must be 16 bits per sample\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/Media/WaveToVisualizer2.cs","lineNumber":787,"sourceCode":"\n        for (var i = 1; i < count; i++)\n        {\n            float value = chunk[i];\n            max = Math.Max(max, value);\n            min = Math.Min(min, value);\n        }\n\n        return new WavePeak2((short)(short.MaxValue * max), (short)(short.MaxValue * min));\n    }\n\n    /// <summary>\n    /// Loads previously generated peaks from disk.\n    /// </summary>\n    internal WavePeakData2 LoadPeaks()\n    {\n        if (Header.BitsPerSample != 16)\n        {\n            throw new Exception(\"Peaks file must be 16 bits per sample.\");\n        }\n\n        if (Header.NumberOfChannels != 1 && Header.NumberOfChannels != 2)\n        {\n            throw new Exception(\"Peaks file must have 1 or 2 channels.\");\n        }\n\n        // load data\n        byte[] data = new byte[Header.DataChunkSize];\n        _stream.Position = Header.DataStartPosition;\n        _ = _stream.Read(data, 0, data.Length);\n\n        // read peak values\n        WavePeak2[] peaks = new WavePeak2[Header.LengthInSamples + 5];\n        int peakIndex = 0;\n        if (Header.NumberOfChannels == 2)\n        {\n            // max value in left channel, min value in right channel","sourceCodeStart":769,"sourceCodeEnd":805,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/Media/WaveToVisualizer2.cs#L769-L805","documentation":"Thrown by WavePeakData2.LoadPeaks when the cached peaks WAV file's header does not report exactly 16 bits per sample. The peaks cache is itself stored as a WAV whose samples are pre-computed min/max peak shorts, so only 16-bit data is meaningful for the peak-reading loop that follows. Any other bit depth means the file is not a SubtitleEdit-generated peaks cache.","triggerScenarios":"Calling LoadPeaks() on a Wave object whose stream points at a regular audio WAV (8/24/32-bit) instead of the generated peaks cache, or on a peaks file produced by a different/older tool version that wrote a different bit depth.","commonSituations":"The peaks cache path was overwritten with the source audio; a user manually swapped the .wav; an upgrade changed the cache format and the stale file was not regenerated; pointing LoadPeaks at the original media instead of the generated peaks file.","solutions":["Regenerate the peaks cache (delete the existing peaks file and re-run peak generation) so it is written as 16-bit.","Ensure LoadPeaks() is only called on the dedicated peaks cache path, never on the source audio file.","If the peaks file format changed between versions, clear the cache directory.","Guard the call: check Header.BitsPerSample == 16 before invoking LoadPeaks and fall back to generation."],"exampleFix":"// before\nvar peaks = wave.LoadPeaks();\n\n// after\nWavePeakData2 peaks;\nif (wave.Header.BitsPerSample == 16)\n    peaks = wave.LoadPeaks();\nelse\n    peaks = wave.GeneratePeaks(peakSampleRate);","handlingStrategy":"validation","validationCode":"// before LoadPeaks\nif (wave.Header.BitsPerSample != 16)\n    throw new InvalidOperationException($\"Peaks cache must be 16-bit, got {wave.Header.BitsPerSample}-bit\");","typeGuard":"static bool IsPeaksCacheBitDepth(WaveHeader2 h) => h.BitsPerSample == 16;","tryCatchPattern":"try { var peaks = wave.LoadPeaks(); }\ncatch (Exception ex) when (ex.Message.Contains(\"16 bits per sample\")) { peaks = wave.GeneratePeaks(peaksPerSecond); }","preventionTips":["Only call LoadPeaks on the generated peaks cache, never on source audio.","Regenerate the cache after upgrades that change the peaks format.","Delete stale peaks files when the source audio changes.","Guard BitsPerSample before calling LoadPeaks."],"tags":["wav","audio","peaks","validation","bit-depth"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}