android - 如何使用双簧管正确播放解码的内存 PCM?
问题描述
我使用双簧管在我的 ndk 库中播放声音,并使用带有 Android 扩展的 OpenSL将 wav 文件解码为 PCM。解码后的签名 16 位 PCM 存储在内存中 ( std::forward_list<int16_t>
),然后通过回调将它们发送到双簧管流中。我可以从手机听到的声音在音量级别上与原始 wav 文件相似,但是,这种声音的“质量”却不是——它爆裂和噼啪作响。
我猜我以错误的顺序或格式(采样率?)在音频流中发送 PCM。如何将 OpenSL 解码与双簧管音频流一起使用?
要将文件解码为 PCM,我使用AndroidSimpleBufferQueue作为接收器,使用AndroidFD和AAssetManager作为源:
// Loading asset
AAsset* asset = AAssetManager_open(manager, path, AASSET_MODE_UNKNOWN);
off_t start, length;
int fd = AAsset_openFileDescriptor(asset, &start, &length);
AAsset_close(asset);
// Creating audio source
SLDataLocator_AndroidFD loc_fd = { SL_DATALOCATOR_ANDROIDFD, fd, start, length };
SLDataFormat_MIME format_mime = { SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED };
SLDataSource audio_source = { &loc_fd, &format_mime };
// Creating audio sink
SLDataLocator_AndroidSimpleBufferQueue loc_bq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 1 };
SLDataFormat_PCM pcm = {
.formatType = SL_DATAFORMAT_PCM,
.numChannels = 2,
.samplesPerSec = SL_SAMPLINGRATE_44_1,
.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16,
.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16,
.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT,
.endianness = SL_BYTEORDER_LITTLEENDIAN
};
SLDataSink sink = { &loc_bq, &pcm };
然后我注册回调,将缓冲区排入队列并将 PCM 从缓冲区移动到存储,直到完成。
注意:wav 音频文件也是 2 通道签名 16 位 44.1Hz PCM
我的双簧管流配置是一样的:
AudioStreamBuilder builder;
builder.setChannelCount(2);
builder.setSampleRate(44100);
builder.setCallback(this);
builder.setFormat(AudioFormat::I16);
builder.setPerformanceMode(PerformanceMode::LowLatency);
builder.setSharingMode(SharingMode::Exclusive);
音频渲染是这样工作的:
// Oboe stream callback
audio_engine::onAudioReady(AudioStream* self, void* audio_data, int32_t num_frames) {
auto stream = static_cast<int16_t*>(audio_data);
sound->render(stream, num_frames);
}
// Sound::render method
sound::render(int16_t* audio_data, int32_t num_frames) {
auto iter = pcm_data.begin();
std::advance(iter, cur_frame);
const int32_t rem_size = std::min(num_frames, size - cur_frame);
for(int32_t i = 0; i < rem_size; ++i, std::next(iter), ++cur_frame) {
audio_data[i] += *iter;
}
}
解决方案
看起来您的 render() 方法混淆了样本和帧。一帧是一组同时的样本。在立体声流中,每一帧都有两个样本。
我认为您的迭代器是在样本基础上工作的。换句话说,next(iter) 将前进到下一个样本,而不是下一帧。试试这个(未经测试的)代码。
sound::render(int16_t* audio_data, int32_t num_frames) {
auto iter = pcm_data.begin();
const int samples_per_frame = 2; // stereo
std::advance(iter, cur_sample);
const int32_t num_samples = std::min(num_frames * samples_per_frame,
total_samples - cur_sample);
for(int32_t i = 0; i < num_samples; ++i, std::next(iter), ++cur_sample) {
audio_data[i] += *iter;
}
}
推荐阅读
- javascript - JS代码乱序执行?JS新手
- python - 即使安装了 passlib,Ansible password_hash 也不会使用 bcrypt
- python - 从 excel 中绘制 X 和 Y 图,其中 x 基于行号
- outlook - 获取在共享辅助日历上创建事件的用户的名称
- ruby-on-rails - 我不知道如何在 windows8.1 上的 ruby 上安装 postgresql10.7
- json - 使用 Postgres 触发器仅记录修改字段的 JSON
- android - Firebase 身份验证 - 没有来自谷歌登录提供商的电子邮件
- javascript - 如何从firebase的嵌套文档中删除数组项?
- javascript - getInitialProps 在 Next.js 中导入数据
- numpy - 带有偏移量Python的块对角矩阵