我正在使用 Android 双簧管库在音乐游戏中获得高性能音频。
在资产文件夹中,我有 2 个 .raw 文件(48000Hz 16 位 PCM wav 和大约 60kB)std_kit_sn.raw std_kit_ht.raw
这些作为 SoundRecordings 加载到内存中并添加到混音器中。kSampleRateHz 为 48000:
stdSN= SoundRecording::loadFromAssets(mAssetManager, "std_kit_sn.raw");
stdHT= SoundRecording::loadFromAssets(mAssetManager, "std_kit_ht.raw");
mMixer.addTrack(stdSN);
mMixer.addTrack(stdFT);
// Create a builder
AudioStreamBuilder builder;
builder.setFormat(AudioFormat::I16);
builder.setChannelCount(1);
builder.setSampleRate(kSampleRateHz);
builder.setCallback(this);
builder.setPerformanceMode(PerformanceMode::LowLatency);
builder.setSharingMode(SharingMode::Exclusive);
LOGD("After creating a builder");
// Open stream
Result result = builder.openStream(&mAudioStream);
if (result != Result::OK){
LOGE("Failed to open stream. Error: %s", convertToText(result));
}
LOGD("After openstream");
// Reduce stream latency by setting the buffer size to a multiple of the burst size
mAudioStream->setBufferSizeInFrames(mAudioStream->getFramesPerBurst() * 2);
// Start the stream
result = mAudioStream->requestStart();
if (result != Result::OK){
LOGE("Failed to start stream. Error: %s", convertToText(result));
}
LOGD("After starting stream");
它们被适当地调用以在需要的时间使用标准代码(根据 Google 教程):
stdSN->setPlaying(true);
stdHT->setPlaying(true); //Nasty Sound
音频回调是标准的(根据谷歌教程):
DataCallbackResult SoundFunctions::onAudioReady(AudioStream *mAudioStream, void *audioData, int32_t numFrames) {
// Play the stream
mMixer.renderAudio(static_cast<int16_t*>(audioData), numFrames);
return DataCallbackResult::Continue;
}
std_kit_sn.raw 播放良好。但是 std_kit_ht.raw 有一个令人讨厌的失真。两者都以低延迟播放。为什么一个播放得很好,而另一个有令人讨厌的失真?