{"record":{"id":"f278777d67f09ad5","repo":"iflytek/astron-agent","slug":"pcm-pcmsamplerate-testsamplerate","errorCode":null,"errorMessage":"不应该出现pcm采样率{pcmSampleRate}和需要的采样率{testSampleRate}不一致","messagePattern":"不应该出现pcm采样率(.+?)和需要的采样率(.+?)不一致","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"console/frontend/src/utils/record/media.js","lineNumber":100,"sourceCode":"\n        //清理已处理完的缓冲数据，释放内存以支持长时间录音，最后完成录音时不能调用stop，因为数据已经被清掉了\n        for (\n          var i = this.realTimeSendTryChunk\n            ? this.realTimeSendTryChunk.index\n            : 0;\n          i < chunk.index;\n          i++\n        ) {\n          buffers[i] = null;\n        }\n        this.realTimeSendTryChunk = chunk; //此时的chunk.data就是原始的音频16位pcm数据（小端LE），直接保存即为16位pcm文件、加个wav头即为wav文件、丢给mp3编码器转一下码即为mp3文件\n\n        pcm = chunk.data;\n        pcmSampleRate = chunk.sampleRate;\n\n        if (pcmSampleRate != testSampleRate)\n          //除非是onProcess给的bufferSampleRate低于testSampleRate\n          throw new Error(\n            '不应该出现pcm采样率' +\n              pcmSampleRate +\n              '和需要的采样率' +\n              testSampleRate +\n              '不一致'\n          );\n      }\n\n      //将pcm数据丢进缓冲，凑够一帧发送，缓冲内的数据可能有多帧，循环切分发送\n      if (pcm.length > 0) {\n        this.realTimeSendTryChunks.push({\n          pcm: pcm,\n          pcmSampleRate: pcmSampleRate,\n        });\n      }\n\n      //从缓冲中切出一帧数据\n      var chunkSize = SendFrameSize / (testBitRate / 8); //8位时需要的采样数和帧大小一致，16位时采样数为帧大小的一半","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/frontend/src/utils/record/media.js#L82-L118","documentation":"This error is thrown by the real-time sending pipeline (RealTimeSendTry) in the media recording wrapper. Recorder.SampleData is asked to resample incoming PCM buffers to testSampleRate, and the library asserts that the resulting chunk's sampleRate equals testSampleRate. If they differ, an internal invariant of the resampler was violated, so it aborts instead of sending corrupt audio.","triggerScenarios":"Calling RealTimeSendTry (during onProcess streaming) where Recorder.SampleData returns chunk.sampleRate != testSampleRate — typically when onProcess supplies buffers whose bufferSampleRate is lower than testSampleRate, or testSampleRate/bitRate config is changed after recorder creation so buffers and target rate disagree.","commonSituations":"Configuring a fixed testSampleRate (e.g. 16000) while the browser's getUserMedia track reports a different native rate; reusing a recorder instance across open calls with different sample-rate configs; custom onProcess hooks feeding buffers captured at another rate.","solutions":["Ensure testSampleRate matches the rate you pass as bufferSampleRate to RealTimeSendTry and the rate configured on the Recorder instance","Re-create the Recorder instance whenever the sample-rate configuration changes instead of reusing buffers across configs","Check that onProcess does not feed buffers from a different recorder/source with a mismatched sampleRate","Verify Recorder.SampleData usage follows the recorder-core contract (buffers from the same recorder, target rate >= buffer rate)"],"exampleFix":"// before\nif (pcmSampleRate != testSampleRate)\n  throw new Error('不应该出现pcm采样率' + pcmSampleRate + '和需要的采样率' + testSampleRate + '不一致');\n// after\nif (pcmSampleRate != testSampleRate) {\n  console.warn('采样率不一致，跳过本帧: got', pcmSampleRate, 'want', testSampleRate);\n  return; // drop the frame instead of crashing the recording loop\n}","handlingStrategy":"validation","validationCode":"if (typeof testSampleRate !== 'number' || typeof bufferSampleRate !== 'number' || bufferSampleRate > testSampleRate) {\n  throw new Error('bufferSampleRate must be a number <= testSampleRate');\n}","typeGuard":"function isValidRate(r) { return typeof r === 'number' && r > 0 && Number.isFinite(r); }","tryCatchPattern":null,"preventionTips":["Keep testSampleRate in sync with onProcess bufferSampleRate","Recreate the Recorder instance after any config change","Never feed buffers from one recorder into another instance's send pipeline"],"tags":["audio","sample-rate-mismatch","internal-invariant","webm-recording"],"backgroundTag":"internal-invariant-violation","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}