1. 程式人生 > >android audio 音量設定分析

android audio 音量設定分析

audiod 中經常遇到的場景是音量調整與輸出裝置的切換,下面兩篇文章 針對這兩個場景分別分析一下
1,音量調整場景
android 音量調整,可以使用兩種方式: 
軟體mixer的時候修改PCM data  
控制DAC硬體的增益

第一種情況,如果是多路mix的情況,就是MixerThread進行軟體mixer,然後在mixer計算的時候來縮放PCM data,
首先,JNI層呼叫了AudioFlinger::setStreamVolume。
status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
        audio_io_handle_t output)
{
    AutoMutex lock(mLock);
    PlaybackThread *thread = NULL;
    if (output) {
        thread = checkPlaybackThread_l(output);  //獲得對應的PlaybackThread
        if (thread == NULL) {
            return BAD_VALUE;
        }
    }
    if (thread == NULL) {
        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
            mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
        }
    } else {
        thread->setStreamVolume(stream, value);  //繼續向下層設定
    }
    return NO_ERROR;
}
可以看到,最終是呼叫了PlaybackThread::setStreamVolume來繼續設定音量
void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
{
    Mutex::Autolock _l(mLock);
    mStreamTypes[stream].volume = value;//把音量資料存起來
    broadcast_l();
}
AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l()
{
                float typeVolume = mStreamTypes[track->streamType()].volume;  //取出暫存的音量資料
                float v = masterVolume * typeVolume;
                AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy;
                uint32_t vlr = proxy->getVolumeLR();
                vl = vlr & 0xFFFF;
                vr = vlr >> 16;
                vl = (uint32_t)(v * vl) << 12;
                vr = (uint32_t)(v * vr) << 12;
            mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl);  //設定給audioMixer
            mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr);
}
可以看到上述函式把引數設定到了audioMixer裡面,在audioMixer章節裡面,
我們介紹過在audioMixer的實際操作函式是track__16BitsStereo這種函式,
對track__16BitsStereo的分析中,我們可以看到根據音量對PCM data進行實際的縮放
不再繼續贅述了。

對於第二種方式,控制DAC硬體的增益,主要用在了DirectOutputThread,中,因為DirectOutputThread只有一路音訊,直接寫入HAL層,直接寫入硬體的,
所以需要直接呼叫硬體DAC晶片的控制介面來調整音量。
其主要流程如下:
和mixerThread的流程一樣,上層在呼叫了AudioFlinger::setStreamVolume之後,會呼叫prepareTracks_l函式
AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l(
    Vector< sp<Track> > *tracksToRemove
)
{
            // compute volume for this track
            processVolume_l(track, last);
}
AT章節中,我們提到過prepareTracks_l函式,其中會呼叫processVolume_l來處理音量
void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack)
{
    audio_track_cblk_t* cblk = track->cblk();
    float left, right;
        float typeVolume = mStreamTypes[track->streamType()].volume;  //和mixerThread一樣,也是從mStreamTypes裡面取出音量資料
        float v = mMasterVolume * typeVolume;
        AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy;
        uint32_t vlr = proxy->getVolumeLR();
        float v_clamped = v * (vlr & 0xFFFF);
        if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
        left = v_clamped/MAX_GAIN;
        v_clamped = v * (vlr >> 16);
        if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
        right = v_clamped/MAX_GAIN;
    if (lastTrack) {
        if (left != mLeftVolFloat || right != mRightVolFloat) {
            mLeftVolFloat = left;
            mRightVolFloat = right;
            uint32_t vl = (uint32_t)(left * (1 << 24));
            uint32_t vr = (uint32_t)(right * (1 << 24));
            if (mOutput->stream->set_volume) {
                mOutput->stream->set_volume(mOutput->stream, left, right);  //向下層設定音量
            }
        }
    }
}
mOutput->stream->set_volume實際上呼叫的是 libhardware_legacy中的函式
static int out_set_volume(struct audio_stream_out *stream, float left,  //呼叫libhardware_legacy中的函式
                          float right)
{
    struct legacy_stream_out *out =
        reinterpret_cast<struct legacy_stream_out *>(stream);
    return out->legacy_out->setVolume(left, right);
}
然後就進入了HAL層程式碼,HAL層程式碼中最終呼叫了 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
寫入驅動
由於商業機密,HAL層程式碼不能貼出來。
到了核心中,則是按照以下呼叫序列,最終通過IIC匯流排,將音量控制命令寫入了DAC晶片中
ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
snd_ctl_elem_write_user
snd_ctl_elem_write_user
snd_ctl_elem_write
wm8523_controls
snd_soc_put_volsw
snd_soc_update_bits_locked
IIC匯流排寫入命令
關於驅動的流程我們在這裡不展開講,後續會單獨講解