当前位置:   article > 正文

安卓音量设置流程之MasterVolume_android master volume

android master volume

       先附上时序图吧,后面会跟上代码讲解:

        一、概述

        安卓系统的音量主要有MasterVolume和StreamVolume来控制,这对于安卓开发人员来说并不陌生。MasterVolume就是系统音量,这个全局的变量影响着Android系统的所有音频通路;StreamVolume则对应着Android系统各种各样的StreamType音频流类型,比如我们最常用的Audio_Sream_Music。

          插曲:下面大致列出几种比较常用的音频流类型:

  1. AUDIO_STREAM_VOICE_CALL = 0, //电话音
  2. AUDIO_STREAM_SYSTEM = 1, //系统音
  3. AUDIO_STREAM_RING = 2, //铃声音
  4. AUDIO_STREAM_MUSIC = 3, //媒体音
  5. AUDIO_STREAM_ALARM = 4, //闹铃音
  6. AUDIO_STREAM_NOTIFICATION = 5,
  7. AUDIO_STREAM_BLUETOOTH_SCO = 6, //蓝牙通话音

        但是我们的目标并不是研究这些StreamType,这些请看其他文章。本篇主要是捋一捋系统音量的设置流程。

         二、流程

         首先要由我们的设备响应按键事件,一般位于PhoneWindowManager.java(比如电视则是TvWindowManager.java)中, 

在interceptKeyBeforeDispatching中实现各种按键事件,我们只看我们想要的部分(KeyEvent.KEYCODE_VOLUME)这个case里处理系统音量的增减(代码保密,不方便贴出)。

        根据代码我们会来到AudioService.java的adjustSuggestedStreamVolume中,然后会调用的成员函数adjustStreamVolume中。AudioService是整个系统音量处理部分最核心的内容,所有的音量流程都要过它之手,所以务必熟悉。

AudioService.java:

  1. xref: /frameworks/base/services/core/java/com/android/server/audio/AudioService.java
  2. private void adjustStreamVolume(int streamType, int direction, int flags,
  3. String callingPackage, String caller, int uid) {
  4. /* */
  5. //再次确认参数的正确性
  6. ensureValidDirection(direction);
  7. ensureValidStreamType(streamType);
  8. // checkForRingerModeChange() in place of STREAM_RING or STREAM_NOTIFICATION)
  9. //根据当前对应的streamType确定当前设备的StreamAlias
  10. int streamTypeAlias = mStreamVolumeAlias[streamType];
  11. VolumeStreamState streamState = mStreamStates[streamTypeAlias];
  12. // reset any pending volume command
  13. // 当前音量未处理完成时忽略其他音量设置
  14. synchronized (mSafeMediaVolumeState) {
  15. mPendingVolumeCommand = null;
  16. }
  17. //省略大部分不太重要的代码,太多了
  18. // If the ringermode is suppressing media, prevent changes
  19. if (!volumeAdjustmentAllowedByDnd(streamTypeAlias, flags)) {
  20. adjustVolume = false;
  21. }
  22. int oldIndex = mStreamStates[streamType].getIndex(device);
  23. if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) {
  24. mAudioHandler.removeMessages(MSG_UNMUTE_STREAM);
  25. //check current all device if contained BT sink device
  26. //set bludtooth volume flag
  27. flags &= ~AudioManager.FLAG_BLUETOOTH_ABS_VOLUME;
  28. if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0) {
  29. flags |= AudioManager.FLAG_BLUETOOTH_ABS_VOLUME;
  30. }
  31. if (isMuteAdjust) {
  32. //静音处理部分,省略
  33. } else if ((direction == AudioManager.ADJUST_RAISE) &&
  34. !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) {
  35. Log.e(TAG, "adjustStreamVolume() safe volume index = " + oldIndex);
  36. mVolumeController.postDisplaySafeVolumeWarning(flags);
  37. //通过步长计算音量值(因为系统音量一般只有七格或者12格)
  38. } else if (streamState.adjustIndex(direction * step, device, caller)
  39. || streamState.mIsMuted) {
  40. // Post message to set system volume (it in turn will post a
  41. // message to persist).
  42. if (streamState.mIsMuted) {
  43. // Unmute the stream if it was previously muted
  44. if (direction == AudioManager.ADJUST_RAISE) {
  45. // unmute immediately for volume up
  46. streamState.mute(false);
  47. } else if (direction == AudioManager.ADJUST_LOWER) {
  48. if (mIsSingleVolume) {
  49. sendMsg(mAudioHandler, MSG_UNMUTE_STREAM, SENDMSG_QUEUE,
  50. streamTypeAlias, flags, null, UNMUTE_STREAM_DELAY);
  51. }
  52. }
  53. }
  54. //真正处理音量的地方,给Device设值音量值
  55. sendMsg(mAudioHandler,
  56. MSG_SET_DEVICE_VOLUME,
  57. SENDMSG_QUEUE,
  58. device,
  59. 0,
  60. streamState,
  61. 0);
  62. }
  63. // Check if volume update should be sent to Hdmi system audio.
  64. // UI 更新相关部分,略
  65. int newIndex = mStreamStates[streamType].getIndex(device);
  66. if (streamTypeAlias == AudioSystem.STREAM_MUSIC) {
  67. setSystemAudioVolume(oldIndex, newIndex, getStreamMaxVolume(streamType), flags);
  68. }
  69. if (mHdmiManager != null) {
  70. synchronized (mHdmiManager) {
  71. // mHdmiCecSink true => mHdmiPlaybackClient != null
  72. if (mHdmiCecSink &&
  73. streamTypeAlias == AudioSystem.STREAM_MUSIC &&
  74. oldIndex != newIndex) {
  75. }
  76. }
  77. }
  78. }
  79. int index = mStreamStates[streamType].getIndex(device);
  80. sendVolumeUpdate(streamType, oldIndex, index, flags);
  81. }

        删除了大量的代码,只留下了我们感兴趣的。大部分逻辑都是处理各中环境下音量的,我们直接直奔主题。最后会通过消息队列sendMsg(对消息队列感兴趣可以看另一篇博客:安卓消息队列详解)为Device设值。

        插曲:有必要说一下消息队列的另一个参数StreamState。因为安卓各种各样的设备的原因,不同的设备可能存在的音频输入输出设备也不一样(比如电视设备就不存在通话音量)。所以我们可以通过StreamStates这个数组来保存不同设备上的对应音频流类型。当然真正使用的时候只能取一组。

          所以大道敞开,我们直接在AudioService.java中寻找MSG_SET_DEVICE_VOLUME这个case,看来是调用的setDeviceVolume方法:

  1. public void handleMessage(Message msg) {
  2. switch (msg.what) {
  3. case MSG_SET_DEVICE_VOLUME:
  4. setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1);
  5. break;

        插曲:Device是Android系统中的抽象设备,因为不同设备厂商的借口很难统一,所以Android使用HAL层对各种硬件设备厂商的接口进行封装抽象,大大减少了应用开发人员对底层的交互。常见的设备列表如下:

  1. AUDIO_DEVICE_NONE = 0u, // 0x0 空设备
  2. AUDIO_DEVICE_OUT_SPEAKER = 2u, // 0x2 系统扬声器
  3. AUDIO_DEVICE_OUT_WIRED_HEADPHONE = 8u, // 0x8 耳机
  4. AUDIO_DEVICE_OUT_BLUETOOTH_A2DP = 128u, // 0x80 A2DP蓝牙
  5. AUDIO_DEVICE_OUT_SPDIF = 524288u, // 0x80000 外接功放

            好的,我们接着看setDeviceVolume()函数:

  1. private void setDeviceVolume(VolumeStreamState streamState, int device) {
  2. synchronized (VolumeStreamState.class) {
  3. // Apply volume
  4. // 为当前音频流设备设置音量
  5. streamState.applyDeviceVolume_syncVSS(device);
  6. // Apply change to all streams using this one as alias
  7. // 为当前StreamAlias的其他StreamType设置更新音量
  8. int numStreamTypes = AudioSystem.getNumStreamTypes();
  9. for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) {
  10. if (streamType != streamState.mStreamType &&
  11. mStreamVolumeAlias[streamType] == streamState.mStreamType) {
  12. // Make sure volume is also maxed out on A2DP device for aliased stream
  13. // that may have a different device selected
  14. int streamDevice = getDeviceForStream(streamType);
  15. if ((device != streamDevice) && mAvrcpAbsVolSupported &&
  16. ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0)) {
  17. mStreamStates[streamType].applyDeviceVolume_syncVSS(device);
  18. }
  19. umStreamStates[streamType].applyDeviceVolume_syncVSS(streamDevice);
  20. }
  21. }
  22. }
  23. }

        首先为我们的目标device设置音量,如果当前设备的其他streamType的音量可以更新,也更新之。接着看applyDeviceVolume_syncVSS()函数:

  1. // must be called while synchronized VolumeStreamState.class
  2. public void applyDeviceVolume_syncVSS(int device) {
  3. int index;
  4. if (mIsMuted) {
  5. index = 0;
  6. } else if ((device & AudioSystem.DEVICE_OUT_ALL_A2DP) != 0 && mAvrcpAbsVolSupported) {
  7. index = (getIndex(device) + 5)/10;
  8. } else if ((device & mFullVolumeDevices) != 0) {
  9. index = (mIndexMax + 5)/10;
  10. } else {
  11. index = (getIndex(device) + 5)/10;
  12. }
  13. AudioSystem.setStreamVolumeIndex(mStreamType, index, device);
  14. }

        这个地方初次看可能比较困惑,哪儿来的index?其实我们UI更新音量的时候,就已经根据音量曲线为我们的抽象device设置好的index,所以我们现在的任务是获取index,并将这个值设入底层。代码很简单,不赘述了。

        AudioSystem.java:   public static native int setStreamVolumeIndex(int stream, int index, int device);

       调用了AudioSystem的native方法,根据java的JNI机制,如果没猜错C++层应该有个AudioSystem.cpp类与之对应,并且实现了AudioSystem.java的所有方法。关于JNI请看:JNI介绍

       下面正式进入framworkC++层的代码:

       AudioSystem.cpp:

  1. status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
  2. int index,
  3. audio_devices_t device)
  4. {
  5. const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
  6. if (aps == 0) return PERMISSION_DENIED;
  7. return aps->setStreamVolumeIndex(stream, index, device);
  8. }

        直接调用了AudioPolicyService的setStreamVolumeIndex()函数。在Android源码中,IXXX类一般是XXX类的代理,调用的都是XXX里的函数,所以我们直奔主题,去AudioPolicyService里找setStreamVolumeIndex()函数。

        果然在这儿,不过遗憾的是AudioPolicyService的实现并不全是在AudioPolicyService.cpp中,比如这个函数的实现是在AudioPolicyInterfaceImpl.cpp 中。

AudioPolicyInterfaceImpl.cpp:

  1. status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
  2. int index,
  3. audio_devices_t device)
  4. {
  5. if (mAudioPolicyManager == NULL) {
  6. return NO_INIT;
  7. }
  8. if (!settingsAllowed()) {
  9. return PERMISSION_DENIED;
  10. }
  11. if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
  12. return BAD_VALUE;
  13. }
  14. Mutex::Autolock _l(mLock);
  15. return mAudioPolicyManager->setStreamVolumeIndex(stream,
  16. index,
  17. device);
  18. }

        又是一层检查调用!好吧,我们去AudioPolicyManager中看看:

AudioPolicyManager:

  1. status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
  2. int index,
  3. audio_devices_t device)
  4. {
  5. // 超出我们音量曲线设定的值就返回
  6. if ((index < mVolumeCurves->getVolumeIndexMin(stream)) ||
  7. (index > mVolumeCurves->getVolumeIndexMax(stream))) {
  8. return BAD_VALUE;
  9. }
  10. // 如果不是输出设备也返回,设什么音量??
  11. if (!audio_is_output_device(device)) {
  12. return BAD_VALUE;
  13. }
  14. bool applyVolume;
  15. if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) {
  16. curStreamDevice |= device;
  17. applyVolume = (curDevice & curStreamDevice) != 0;
  18. } else {
  19. applyVolume = !mVolumeCurves->hasVolumeIndexForDevice(
  20. stream, Volume::getDeviceForVolume(curStreamDevice));
  21. }
  22. if (applyVolume) {
  23. //FIXME: workaround for truncated touch sounds
  24. // delayed volume change for system stream to be removed when the problem is
  25. // handled by system UI
  26. status_t volStatus =
  27. checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
  28. (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
  29. if (volStatus != NO_ERROR) {
  30. status = volStatus;
  31. }
  32. }
  33. return status;
  34. }

         一大堆的条件判断,然后进入我们的checkAndSetVolume()中,顾名思义,检查并设值。

  1. status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
  2. int index,
  3. const sp<AudioOutputDescriptor>& outputDesc,
  4. audio_devices_t device,
  5. int delayMs,
  6. bool force)
  7. {
  8. // do not change actual stream volume if the stream is muted
  9. if (outputDesc->mMuteCount[stream] != 0) {
  10. ALOGVV("checkAndSetVolume() stream %d muted count %d",
  11. stream, outputDesc->mMuteCount[stream]);
  12. return NO_ERROR;
  13. }
  14. audio_policy_forced_cfg_t forceUseForComm =
  15. mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION);
  16. // do not change in call volume if bluetooth is connected and vice versa
  17. if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) ||
  18. (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) {
  19. ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
  20. stream, forceUseForComm);
  21. return INVALID_OPERATION;
  22. }
  23. //如果设备为空,则默认为输出描述符的device
  24. if (device == AUDIO_DEVICE_NONE) {
  25. device = outputDesc->device();
  26. }
  27. // 将Volume转换为Db音量(-115~0)
  28. float volumeDb = computeVolume(stream, index, device);
  29. if (outputDesc->isFixedVolume(device)) {
  30. volumeDb = 0.0f;
  31. }
  32. // 为设备输出描述符设置音量值
  33. outputDesc->setVolume(volumeDb, stream, device, delayMs, force);
  34. // 处理特殊情况。蓝牙通话和手机通话,显然我们目前不会走这边
  35. if (stream == AUDIO_STREAM_VOICE_CALL ||
  36. stream == AUDIO_STREAM_BLUETOOTH_SCO) {
  37. float voiceVolume;
  38. // Force voice volume to max for bluetooth SCO as volume is managed by the headset
  39. if (stream == AUDIO_STREAM_VOICE_CALL) {
  40. voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream);
  41. } else {
  42. voiceVolume = 1.0;
  43. }
  44. if (voiceVolume != mLastVoiceVolume) {
  45. mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
  46. mLastVoiceVolume = voiceVolume;
  47. }
  48. }
  49. return NO_ERROR;
  50. }

设备输出描述符有一个简单的继承关系:

一个是软解输出设备描述符,一个是硬解输出设备描述符,我们的音频解码一般都用SwAudioOutputDescriptor:

  1. bool SwAudioOutputDescriptor::setVolume(float volume,
  2. audio_stream_type_t stream,
  3. audio_devices_t device,
  4. uint32_t delayMs,
  5. bool force)
  6. {
  7. bool changed = AudioOutputDescriptor::setVolume(volume, stream, device, delayMs, force);
  8. if (changed) {
  9. // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
  10. // enabled
  11. float volume = Volume::DbToAmpl(mCurVolume[stream]);
  12. if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
  13. mClientInterface->setStreamVolume(
  14. AUDIO_STREAM_VOICE_CALL, volume, mIoHandle, delayMs);
  15. }
  16. mClientInterface->setStreamVolume(stream, volume, mIoHandle, delayMs);
  17. }
  18. return changed;
  19. }

        代码很容易理解,根据Device获取对应的IOHandle(每个handle对应了底层的一个回放线程)。并设备输出描述符设置音量,如果音量有改变,就调用AudioPolicyClientInterface的setStreamVolume方法。当然我们找不到它的实现,根据继承关系AudioPolicyClient->AudioPolicyClientInterface,我们去AudioPolicyClient看看:

  1. status_t AudioPolicyService::AudioPolicyClient::setStreamVolume(audio_stream_type_t stream,
  2. float volume, audio_io_handle_t output,
  3. int delay_ms)
  4. {
  5. return mAudioPolicyService->setStreamVolume(stream, volume, output,
  6. delay_ms);
  7. }

接着去AudioPolicyService看看:

  1. int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
  2. float volume,
  3. audio_io_handle_t output,
  4. int delayMs)
  5. {
  6. return (int)mAudioCommandThread->volumeCommand(stream, volume,
  7. output, delayMs);
  8. }

恩,接着去内部类AudioCommandThread中看看:

  1. status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
  2. float volume,
  3. audio_io_handle_t output,
  4. int delayMs)
  5. {
  6. sp<AudioCommand> command = new AudioCommand();
  7. command->mCommand = SET_VOLUME;
  8. sp<VolumeData> data = new VolumeData();
  9. data->mStream = stream;
  10. data->mVolume = volume;
  11. data->mIO = output;
  12. command->mParam = data;
  13. command->mWaitStatus = true;
  14. ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
  15. stream, volume, output);
  16. return sendCommand(command, delayMs);
  17. }

       看来是将我们的SET_VOLUMEcommand打包并发送出去。一般在Thread类中都会启动一个线程循环来接收各种消息指令或者处理大量逻辑事物,AudioCommandThread应该也不例外,我们找到Loop函数看看:

  1. bool AudioPolicyService::AudioCommandThread::threadLoop()
  2. {
  3. nsecs_t waitTime = -1;
  4. mLock.lock();
  5. while (!exitPending())
  6. {
  7. sp<AudioPolicyService> svc;
  8. while (!mAudioCommands.isEmpty() && !exitPending()) {
  9. nsecs_t curTime = systemTime();
  10. // commands are sorted by increasing time stamp: execute them from index 0 and up
  11. if (mAudioCommands[0]->mTime <= curTime) {
  12. sp<AudioCommand> command = mAudioCommands[0];
  13. mAudioCommands.removeAt(0);
  14. mLastCommand = command;
  15. switch (command->mCommand) {
  16. case SET_VOLUME: {
  17. VolumeData *data = (VolumeData *)command->mParam.get();
  18. ALOGV("AudioCommandThread() processing set volume stream %d, \
  19. volume %f, output %d", data->mStream, data->mVolume, data->mIO);
  20. command->mStatus = AudioSystem::setStreamVolume(data->mStream,
  21. data->mVolume,
  22. data->mIO);
  23. }break;
  24. }
  25. }
  26. }

果然没猜错,接收指令并调用了AudioSystem的setStreamVolume函数。然后直接调用AudioFlinger的setStreamVolume函数,简单的调用,不贴代码了。

  1. status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
  2. audio_io_handle_t output)
  3. {
  4. // check calling permissions
  5. if (!settingsAllowed()) {
  6. return PERMISSION_DENIED;
  7. }
  8. status_t status = checkStreamType(stream);
  9. if (status != NO_ERROR) {
  10. return status;
  11. }
  12. ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
  13. AutoMutex lock(mLock);
  14. Vector<VolumeInterface *> volumeInterfaces;
  15. if (output != AUDIO_IO_HANDLE_NONE) {
  16. VolumeInterface *volumeInterface = getVolumeInterface_l(output);
  17. if (volumeInterface == NULL) {
  18. return BAD_VALUE;
  19. }
  20. volumeInterfaces.add(volumeInterface);
  21. }
  22. mStreamTypes[stream].volume = value;
  23. if (volumeInterfaces.size() == 0) {
  24. volumeInterfaces = getAllVolumeInterfaces_l();
  25. }
  26. for (size_t i = 0; i < volumeInterfaces.size(); i++) {
  27. volumeInterfaces[i]->setStreamVolume(stream, value);
  28. }
  29. return NO_ERROR;
  30. }

        简单介绍下,先给我们的目标StreamType(当然是system_stream)设置音量值。然后为volumeInterface设值,volumeInterface是PlayBackThread的父类(在AOSP中,XXXinterface一般都是纯虚函数组成的类,相当于Java中的接口)。AudioFlinger是安卓音量处理的核心,在这个类内部维护一个PlayBackThread类,各种回放线程的派生类都继承于它,比如(MixThread,DumplicatingThread~~)。下面大致给出类图:

        MixThread为我们最多用到的混音类,DirectOutputThread只有我们创建一个Track并且标注Flags为Direct时才会创建(比如音效,或者需要硬解的音频流)。所以,我们调用VolumeInterface的setStreamVolume最终会调到PlayBackThread的setStreamVolume()。因为volume是通用属性,所以底层的线程没有重载,直接调用即可。

  1. void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
  2. {
  3. Mutex::Autolock _l(mLock);
  4. mStreamTypes[stream].volume = value;
  5. broadcast_l();
  6. }

         为当前回放线程的streamType对应的stream设置,那么在哪里调用呢?我们知道每个Thread类都维护了一个Loop函数,并且我们的大部分音频处理都要经过MixThread进行混音才能写出。并且MixThread的每次循环处理都会对当前线程挂载的所有Track进行处理,我们去看看(这一块知识点很多,以后有空总结一下):

  1. AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
  2. Vector< sp<Track> > *tracksToRemove)
  3. {
  4. //获取主音量
  5. float masterVolume = mMasterVolume;
  6. bool masterMute = mMasterMute;
  7. if (masterMute) {
  8. masterVolume = 0;
  9. }
  10. // Delegate master volume control to effect in output mix effect chain if needed
  11. sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
  12. if (chain != 0) {
  13. uint32_t v = (uint32_t)(masterVolume * (1 << 24));
  14. chain->setVolume_l(&v, &v);
  15. masterVolume = (float)((v + (1 << 23)) >> 24);
  16. chain.clear();
  17. }
  18. // compute volume for this track
  19. uint32_t vl, vr; // in U8.24 integer format
  20. float vlf, vrf, vaf; // in [0.0, 1.0] float format
  21. if (track->isPausing() || mStreamTypes[track->streamType()].mute) {
  22. vl = vr = 0;
  23. vlf = vrf = vaf = 0.;
  24. if (track->isPausing()) {
  25. track->setPaused();
  26. }
  27. } else {
  28. // read original volumes with volume control
  29. // 从我们设置的StreamType音量里获取音量
  30. float typeVolume = mStreamTypes[track->streamType()].volume;
  31. typeVolume = 1.0f;
  32. // 获取主音量和StreamVolume的音量乘积
  33. float v = masterVolume * typeVolume;
  34. // 分离出左右声道
  35. sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
  36. gain_minifloat_packed_t vlr = proxy->getVolumeLR();
  37. vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
  38. vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
  39. // track volumes come from shared memory, so can't be trusted and must be clamped
  40. if (vlf > GAIN_FLOAT_UNITY) {
  41. ALOGV("Track left volume out of range: %.3g", vlf);
  42. vlf = GAIN_FLOAT_UNITY;
  43. }
  44. if (vrf > GAIN_FLOAT_UNITY) {
  45. ALOGV("Track right volume out of range: %.3g", vrf);
  46. vrf = GAIN_FLOAT_UNITY;
  47. }
  48. const float vh = track->getVolumeHandler()->getVolume(
  49. track->mAudioTrackServerProxy->framesReleased()).first;
  50. // now apply the master volume and stream type volume and shaper volume
  51. vlf *= v * vh;
  52. vrf *= v * vh;
  53. // assuming master volume and stream type volume each go up to 1.0,
  54. // then derive vl and vr as U8.24 versions for the effect chain
  55. const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT;
  56. vl = (uint32_t) (scaleto8_24 * vlf);
  57. vr = (uint32_t) (scaleto8_24 * vrf);
  58. // vl and vr are now in U8.24 format
  59. uint16_t sendLevel = proxy->getSendLevel_U4_12();
  60. // send level comes from shared memory and so may be corrupt
  61. if (sendLevel > MAX_GAIN_INT) {
  62. ALOGV("Track send level out of range: %04X", sendLevel);
  63. sendLevel = MAX_GAIN_INT;
  64. }
  65. // vaf is represented as [0.0, 1.0] float by rescaling sendLevel
  66. vaf = v * sendLevel * (1. / MAX_GAIN_INT);
  67. }
  68. // 为真正的Mix设置音量参数
  69. mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf);
  70. mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf);
  71. mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf);
  72. }
  73. }

        最终MiaxThread会根据所设的音量参数重新对所有重采样的Track进行振幅的扩大和缩小,最终达到音量增大减小的效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/988838
推荐阅读
相关标签
  

闽ICP备14008679号