AudioRecord.read()比采样长度慢得多

时间:2019-07-16 19:49:02

标签: android audio-recording mediacodec audiorecord android-audiorecord

我在打电话:

mReadBytes = mAudioRecord.read(mBuffer, mConfig.frameSize());

在不需要额外处理的后台线程上。

  • 配置:AudioFormat.CHANNEL_IN_STEREO
  • 采样率:44.1 kHz
  • 编码:AudioFormat.ENCODING_PCM_16BIT

我正在尝试一次读取2048个字节[每个通道1024个字节/每个通道512个样本]。此数量应等于声音的 11609微秒

11609 (msec) = 2048 (bytes) / (44100 (samples/sec) * 2 (bytes/sample/channel) * 2 (channels)) * 1000000 (msec/sec).

由于这个原因,我希望read()呼叫在这段时间过去之后最多返回(如果有的话,还要加上一些开销)。

问题

当我使用MONO配置时,一切正常。我的假设是正确的-read()呼叫平均花费的时间(针对单个频道进行调整),通常少于该时间。

当我使用上面的STEREO配置时,read()调用花费的时间要比应有的要多 ,即使假设每次我调用记录器缓冲区为空时,延迟为< strong>巨大,并在最终文件中造成很大差距。

V/AudioMediaEncoder: read thread - reading took: 37.782295 should be: 11.609 delay: 26.173294
V/AudioMediaEncoder: read thread - reading took: 18.121565 should be: 11.609 delay: 6.5125647
V/AudioMediaEncoder: read thread - reading took: 18.233908 should be: 11.609 delay: 6.6249075
V/AudioMediaEncoder: read thread - reading took: 18.458492 should be: 11.609 delay: 6.849492
V/AudioMediaEncoder: read thread - reading took: 16.850992 should be: 11.609 delay: 5.241992
V/AudioMediaEncoder: read thread - reading took: 17.748648 should be: 11.609 delay: 6.1396475
V/AudioMediaEncoder: read thread - reading took: 19.446096 should be: 11.609 delay: 7.837096
V/AudioMediaEncoder: read thread - reading took: 35.627087 should be: 11.609 delay: 24.018085
V/AudioMediaEncoder: read thread - reading took: 16.654844 should be: 11.609 delay: 5.045844
V/AudioMediaEncoder: read thread - reading took: 19.425213 should be: 11.609 delay: 7.8162127
V/AudioMediaEncoder: read thread - reading took: 19.640837 should be: 11.609 delay: 8.0318365
V/AudioMediaEncoder: read thread - reading took: 15.437971 should be: 11.609 delay: 3.828971
V/AudioMediaEncoder: read thread - reading took: 18.271303 should be: 11.609 delay: 6.662303
V/AudioMediaEncoder: read thread - reading took: 19.074793 should be: 11.609 delay: 7.4657927
V/AudioMediaEncoder: read thread - reading took: 38.72766 should be: 11.609 delay: 27.11866

如您在上面看到的(以毫秒为单位的值),读取操作最多需要 40毫秒才能捕获声音缓冲区,该缓冲区的长度应 11.6毫秒

不用说输出是垃圾。谁能解释原因或帮助?

更多信息

  • AudioRecord内部缓冲区设置为2048 *25。更改此设置只会使所有情况变得更糟。

  • 这是输出上述日志的代码:

    long before = System.nanoTime();
    mReadBytes = mAudioRecord.read(mCurrentBuffer, mConfig.frameSize());
    long after = System.nanoTime();
    float delayMillis = (after - before) / 1000000F;
    float durationMillis = AudioTimestamp.bytesToMillis(mReadBytes, mConfig.byteRate());
    Log.v(TAG, "read thread - reading took: " + delayMillis + " should be: " + durationMillis + " delay: " + (delayMillis - durationMillis));
    

0 个答案:

没有答案