Android Audio代碼分析17 – setvolume函數 – Android移動開發技術文章_手機開發 Android移動開發教學課程

下面來看看音量設置相關的接口。


*****************************************源碼*************************************************
    //Test case 1: setStereoVolume() with max volume returns SUCCESS
    @LargeTest
    public void testSetStereoVolumeMax() throws Exception {
        // constants for test
        final String TEST_NAME = "testSetStereoVolumeMax";
        final int TEST_SR = 22050;
        final int TEST_CONF = AudioFormat.CHANNEL_OUT_STEREO;
        final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
        final int TEST_MODE = AudioTrack.MODE_STREAM;
        final int TEST_STREAM_TYPE = AudioManager.STREAM_MUSIC;
       
        //——– initialization ————–
        int minBuffSize = AudioTrack.getMinBufferSize(TEST_SR, TEST_CONF, TEST_FORMAT);
        AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT,
                minBuffSize, TEST_MODE);
        byte data[] = new byte[minBuffSize/2];
        //——–    test        ————–
        track.write(data, 0, data.length);
        track.write(data, 0, data.length);
        track.play();
        float maxVol = AudioTrack.getMaxVolume();
        assertTrue(TEST_NAME, track.setStereoVolume(maxVol, maxVol) == AudioTrack.SUCCESS);
        //——– tear down      ————–
        track.release();
    }
**********************************************************************************************
源碼路徑:
frameworks\base\media\tests\mediaframeworktest\src\com\android\mediaframeworktest\functional\MediaAudioTrackTest.java


#######################說明################################
    //Test case 1: setStereoVolume() with max volume returns SUCCESS
    @LargeTest
    public void testSetStereoVolumeMax() throws Exception {
        // constants for test
        final String TEST_NAME = "testSetStereoVolumeMax";
        final int TEST_SR = 22050;
        final int TEST_CONF = AudioFormat.CHANNEL_OUT_STEREO;
        final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
        final int TEST_MODE = AudioTrack.MODE_STREAM;
        final int TEST_STREAM_TYPE = AudioManager.STREAM_MUSIC;
       
        //——– initialization ————–
        int minBuffSize = AudioTrack.getMinBufferSize(TEST_SR, TEST_CONF, TEST_FORMAT);
        AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT,
                minBuffSize, TEST_MODE);
        byte data[] = new byte[minBuffSize/2];
        //——–    test        ————–
        track.write(data, 0, data.length);
        track.write(data, 0, data.length);
        track.play();
        float maxVol = AudioTrack.getMaxVolume();
++++++++++++++++++++++++++++++getMaxVolume++++++++++++++++++++++++++++++++++
    /**
     * Returns the maximum valid volume value. Volume values set above this one will
     * be clamped at this value.
     * @return the maximum volume expressed as a linear attenuation.
     */
    static public float getMaxVolume() {
        return AudioTrack.VOLUME_MAX;
    }
——————————getMaxVolume———————————-
        assertTrue(TEST_NAME, track.setStereoVolume(maxVol, maxVol) == AudioTrack.SUCCESS);
++++++++++++++++++++++++++++++setStereoVolume++++++++++++++++++++++++++++++++++
     /**
     * Sets the specified left/right output volume values on the AudioTrack. Values are clamped
     * to the ({@link #getMinVolume()}, {@link #getMaxVolume()}) interval if outside this range.
     * @param leftVolume output attenuation for the left channel. A value of 0.0f is silence,
     *      a value of 1.0f is no attenuation.
     * @param rightVolume output attenuation for the right channel
     * @return error code or success, see {@link #SUCCESS},
     *    {@link #ERROR_INVALID_OPERATION}
     */
    public int setStereoVolume(float leftVolume, float rightVolume) {
        if (mState != STATE_INITIALIZED) {
            return ERROR_INVALID_OPERATION;
        }


// 確保音量在[min, max]范圍之內。
        // clamp the volumes
        if (leftVolume < getMinVolume()) {
            leftVolume = getMinVolume();
        }
        if (leftVolume > getMaxVolume()) {
            leftVolume = getMaxVolume();
        }
        if (rightVolume < getMinVolume()) {
            rightVolume = getMinVolume();
        }
        if (rightVolume > getMaxVolume()) {
            rightVolume = getMaxVolume();
        }


        native_setVolume(leftVolume, rightVolume);
++++++++++++++++++++++++++++android_media_AudioTrack_set_volume++++++++++++++++++++++++++++++++++++
static void
android_media_AudioTrack_set_volume(JNIEnv *env, jobject thiz, jfloat leftVol, jfloat rightVol )
{
    AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
        thiz, javaAudioTrackFields.nativeTrackInJavaObj);
    if (lpTrack == NULL ) {
        jniThrowException(env, "java/lang/IllegalStateException",
            "Unable to retrieve AudioTrack pointer for setVolume()");
        return;
    }


    lpTrack->setVolume(leftVol, rightVol);
+++++++++++++++++++++++++++++AudioTrack::setVolume++++++++++++++++++++++++++++++++++
status_t AudioTrack::setVolume(float left, float right)
{
// 范圍檢查
    if (left > 1.0f || right > 1.0f) {
        return BAD_VALUE;
    }


// 將音量保存到成員變量中
// 函數AudioTrack::set中會將這兩個音量初始化為1.0f
// 函數AudioTrack::getVolume會取出這兩個音量值
// 函數AudioTrack::createTrack中會將這兩個值賦值給mCblk->volumeLR
    mVolume[LEFT] = left;
    mVolume[RIGHT] = right;


    // write must be atomic
// 從audio_track_cblk_t的定義可知,volume的信息其實是一個union,這兒是作為volumeLR存入的,
// 用的時候其實是作為uint16_t    volume[2]數組來使用的。
+++++++++++++++++++++++++++++union+++++++++++++++++++++++++++++++++++
    volatile    union {
                    uint16_t    volume[2];
                    uint32_t    volumeLR;
                };
—————————–union———————————–
// 類AudioMixer中,將數據從audio_track_cblk_t copy 到main buffer時,會根據音量對數據進行處理。
// 如以前我們看過的process__OneTrack16BitsStereoNoResampling函數。
    mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000);


    return NO_ERROR;
}
—————————–AudioTrack::setVolume———————————-
}
—————————-android_media_AudioTrack_set_volume————————————

        return SUCCESS;
    }
——————————setStereoVolume———————————-
        //——– tear down      ————–
        track.release();
    }
###########################################################


&&&&&&&&&&&&&&&&&&&&&&&總結&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
函數setStereoVolume最終設置的是軟音量。
類AudioMixer中將數據從audio_track_cblk_t copy 到main buffer時,會使用音量對數據進行處理。
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

摘自:江風的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *