iflytek/astron-agent · error · Error

不应该出现pcm采样率 和需要的采样率 不一致

Error message

不应该出现pcm采样率{pcmSampleRate}和需要的采样率{testSampleRate}不一致

What it means

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.

Solutions

  1. Ensure testSampleRate matches the rate you pass as bufferSampleRate to RealTimeSendTry and the rate configured on the Recorder instance
  2. Re-create the Recorder instance whenever the sample-rate configuration changes instead of reusing buffers across configs
  3. Check that onProcess does not feed buffers from a different recorder/source with a mismatched sampleRate
  4. Verify Recorder.SampleData usage follows the recorder-core contract (buffers from the same recorder, target rate >= buffer rate)

Example fix

// before
if (pcmSampleRate != testSampleRate)
  throw new Error('不应该出现pcm采样率' + pcmSampleRate + '和需要的采样率' + testSampleRate + '不一致');
// after
if (pcmSampleRate != testSampleRate) {
  console.warn('采样率不一致,跳过本帧: got', pcmSampleRate, 'want', testSampleRate);
  return; // drop the frame instead of crashing the recording loop
}
Defensive patterns

Strategy: validation

Validate before calling

if (typeof testSampleRate !== 'number' || typeof bufferSampleRate !== 'number' || bufferSampleRate > testSampleRate) {
  throw new Error('bufferSampleRate must be a number <= testSampleRate');
}

Type guard

function isValidRate(r) { return typeof r === 'number' && r > 0 && Number.isFinite(r); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/f278777d67f09ad5. Report an issue: GitHub.

Appendix: source

Thrown at console/frontend/src/utils/record/media.js:100

        //清理已处理完的缓冲数据,释放内存以支持长时间录音,最后完成录音时不能调用stop,因为数据已经被清掉了
        for (
          var i = this.realTimeSendTryChunk
            ? this.realTimeSendTryChunk.index
            : 0;
          i < chunk.index;
          i++
        ) {
          buffers[i] = null;
        }
        this.realTimeSendTryChunk = chunk; //此时的chunk.data就是原始的音频16位pcm数据(小端LE),直接保存即为16位pcm文件、加个wav头即为wav文件、丢给mp3编码器转一下码即为mp3文件

        pcm = chunk.data;
        pcmSampleRate = chunk.sampleRate;

        if (pcmSampleRate != testSampleRate)
          //除非是onProcess给的bufferSampleRate低于testSampleRate
          throw new Error(
            '不应该出现pcm采样率' +
              pcmSampleRate +
              '和需要的采样率' +
              testSampleRate +
              '不一致'
          );
      }

      //将pcm数据丢进缓冲,凑够一帧发送,缓冲内的数据可能有多帧,循环切分发送
      if (pcm.length > 0) {
        this.realTimeSendTryChunks.push({
          pcm: pcm,
          pcmSampleRate: pcmSampleRate,
        });
      }

      //从缓冲中切出一帧数据
      var chunkSize = SendFrameSize / (testBitRate / 8); //8位时需要的采样数和帧大小一致,16位时采样数为帧大小的一半

View on GitHub (pinned to 5e758547a8)