static int adev_open_input_stream(......)
{
......
//requested_rate: app申请的采样率
//pcm_config->rate: 硬件实际采样率
if (in->requested_rate != pcm_config->rate) {
in->buf_provider.get_next_buffer = get_next_buffer;
in->buf_provider.release_buffer = release_buffer;
......
ret = create_resampler(pcm_config->rate,
in->requested_rate,
audio_channel_count_from_in_mask(in->channel_mask),
RESAMPLER_QUALITY_DEFAULT,
&in->buf_provider,
&in->resampler);
......
}
......
}
重采样算法用android自带库
int create_resampler(......)
{
......
rsmp->speex_resampler = speex_resampler_init(channelCount,
inSampleRate,
outSampleRate,
quality,
&error);
......
}
本质上用的是speex开源库
EXPORT SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err)
{
return speex_resampler_init_frac(nb_channels, in_rate, out_rate, in_rate, out_rate, quality, err);
}
speex使用的是cubic插值算法:
status_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters(......)
{
......
//mSrcSampleRate: 从HAL层获取,下面再看
//mDstSampleRate: APP设置的采样率
if (mSrcSampleRate != mDstSampleRate) {
mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT,
mSrcChannelCount, mDstSampleRate);
mResampler->setSampleRate(mSrcSampleRate);
mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT);
}
......
}
AudioResampler* AudioResampler::create(audio_format_t format, int inChannelCount,
int32_t sampleRate, src_quality quality) {
......
//根据不同的quality选择不同的采样算法
case LOW_QUALITY:
ALOGV("Create linear Resampler");
LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
resampler = new AudioResamplerOrder1(inChannelCount, sampleRate);
break;
case MED_QUALITY:
ALOGV("Create cubic Resampler");
LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
resampler = new AudioResamplerCubic(inChannelCount, sampleRate);
break;
case HIGH_QUALITY:
ALOGV("Create HIGH_QUALITY sinc Resampler");
LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
resampler = new AudioResamplerSinc(inChannelCount, sampleRate);
break;
case VERY_HIGH_QUALITY:
ALOGV("Create VERY_HIGH_QUALITY sinc Resampler = %d", quality);
LOG_ALWAYS_FATAL_IF(format != AUDIO_FORMAT_PCM_16_BIT);
resampler = new AudioResamplerSinc(inChannelCount, sampleRate, quality);
break;
case DYN_LOW_QUALITY:
case DYN_MED_QUALITY:
case DYN_HIGH_QUALITY:
......
resampler = new AudioResamplerDyn(inChannelCount,
......
}
mSrcSampleRate的来源:
void AudioFlinger::RecordThread::readInputParameters_l()
{
mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common);
......
}
最终来自audio_hw.c
static uint32_t in_get_sample_rate(const struct audio_stream *stream)
{
struct stream_in *in = (struct stream_in *)stream;
//ALOGV("%s:get requested_rate : %d ",__FUNCTION__,in->requested_rate);
return in->requested_rate;
}
而requested_rate就是从上层app传下来的需要设置的采样率,因此mSrcSampleRate和mDstSampleRate一样,在framework不会做重采样。
各个算法的区别: