Nexus 9 主采样率

Nexus 9 primary sample rate

我有一个使用 OpenSL ES 的应用程序。当我尝试在 Nexus9 6.0.1 上使用它时,我听到噪音,好像我的采样率不对。在其他设备上一切正常。

我的SLDataFormat_PCM结构:

SLDataFormat_PCM format_pcm = {
            SL_DATAFORMAT_PCM,
            aChannels,
            48000 * 1000,
            SL_PCMSAMPLEFORMAT_FIXED_16,
            SL_PCMSAMPLEFORMAT_FIXED_16,
            aChannels == 2 ? SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT
                           : SL_SPEAKER_FRONT_CENTER,
            SL_BYTEORDER_LITTLEENDIAN
    };

当我在此结构中更改采样率 (+/- 1Hz) 时,输出听起来不错,但我收到 AudioTrack 调试消息:

W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client; transfer 1, track 47999 Hz, output 48000 Hz

如果 Nexus9 有 48000Hz,为什么我在 FAST 模式下有问题?

我用这个方法检查过:

jclass clazz = env.getEnv()->FindClass("android/media/AudioSystem");
jmethodID mid = env.getEnv()->GetStaticMethodID(clazz, "getPrimaryOutputSamplingRate", "()I");
int nSampleRate = env.getEnv()->CallStaticIntMethod(clazz, mid);
LOGDEBUG << "Sample Rate: " << nSampleRate;

[ DBG:c894860f] 11:16:14.902: Sample Rate: 48000

是否有更好的方法获取设备的采样率?

是的,有一种方法可以找到设备的首选采样率,尽管它适用于 API 级别 > 16。You can have a look at my answer here

关于您的 SLDataFormat_PCM 结构。您已经使用 48k*1k 的采样率进行了初始化!如果您想以 48k 采样 PCM 数据,请尝试使用下面的代码。

// configure audio source
SLDataFormat_PCM format_pcm = {
        SL_DATAFORMAT_PCM,
        aChannels,
        SL_SAMPLINGRATE_48,
        SL_PCMSAMPLEFORMAT_FIXED_16,
        SL_PCMSAMPLEFORMAT_FIXED_16,
        aChannels == 2 ? SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT
                       : SL_SPEAKER_FRONT_CENTER,
        SL_BYTEORDER_LITTLEENDIAN
};

之前没有用过Nexus 9,不知道是否支持48k采样率。但是,无论如何,您可以检查它是否支持。

问题出在回调函数中的互斥锁上。

更新: OpenSLES Readme

Known issues

At 48000Hz, Galaxy Nexus and Nexus 10 produce glitchy output. At 44100Hz, Galaxy Nexus tends to glitch when switching activities or bringing up large dialogs. Touch sounds occasionally cause OpenSL to glitch. It's probably a good idea to disable touch sounds in audio apps. These problems are not specific to opensl_stream and have been reproduced in other settings.