iflytek/astron-agent · error · Error

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

Error message

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

What it means

Identical sample-rate invariant check to the one in media.js, thrown from RealTimeSendTry in record.js. Recorder.SampleData must produce PCM at testSampleRate; when the output chunk's sampleRate differs the record wrapper treats it as a programming/config error and throws instead of emitting bad frames.

Solutions

  1. Align testSampleRate with the bufferSampleRate reported by onProcess buffers
  2. Discard stale realTimeSendTryChunk/buffer state when restarting or reconfiguring the recorder
  3. Create a fresh Recorder instance per configuration rather than mutating sample-rate config at runtime
  4. Log the actual buffer sampleRate from onProcess to confirm what the device produces

Example fix

// before
if (pcmSampleRate != testSampleRate)
  throw new Error('不应该出现pcm采样率' + pcmSampleRate + '和需要的采样率' + testSampleRate + '不一致');
// after
if (pcmSampleRate != testSampleRate) {
  console.warn('sampleRate mismatch, dropping chunk', { pcmSampleRate, testSampleRate });
  return;
}
Defensive patterns

Strategy: validation

Validate before calling

const rates = buffers.map((b) => b.sampleRate);
if (rates.some((r) => r !== bufferSampleRate)) {
  console.warn('mixed buffer sample rates', rates);
  return;
}

Type guard

function sameRate(chunk, target) { return chunk && chunk.sampleRate === target; }

Try / catch

try {
  realTimeSendTry(buffers, bufferSampleRate, isClose);
} catch (e) {
  console.warn('sample-rate mismatch, frame skipped:', e.message);
}

Prevention

When it happens

Trigger: bufferSampleRate supplied to RealTimeSendTry differs from testSampleRate (e.g. onProcess delivering native-rate buffers while testSampleRate is fixed at 16000), or recorder config changed between recording sessions leaving stale buffers.

Common situations: Hardcoded testSampleRate not matching the actual device capture rate; mixing chunks from two recorder instances; upgrading recorder-core so SampleData behavior/rate negotiation changed.

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/1f4b40eb92b6f329. Report an issue: GitHub.

Appendix: source

Thrown at console/frontend/src/utils/record/record.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)