- bool AudioMixer::isMultichannelCapable = false;//是否支持大于两路的音频
- //downmix effect,用于处理大于两路音频的特效,主要是做向下变换
- effect_descriptor_t AudioMixer::dwnmFxDesc;
- // Ensure mConfiguredNames bitmask is initialized properly on all architectures.
- // The value of 1 << x is undefined in C when x >= 32.
- // 1、初始化state_t对象mState,最多保存32个track,对应thread中的track对象
- // 2、加载支持的effect lib和effect
- // 3、通过类型EFFECT_UIID_DOWNMIX寻找处理多路音频特效的downmix effect对象
- AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks)
- : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1),
- mSampleRate(sampleRate)
- {
- // AudioMixer is not yet capable of multi-channel beyond stereo
- COMPILE_TIME_ASSERT_FUNCTION_SCOPE(2 == MAX_NUM_CHANNELS);
- ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u",
- maxNumTracks, MAX_NUM_TRACKS);
- // AudioMixer is not yet capable of more than 32 active track inputs
- ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS);
- // AudioMixer is not yet capable of multi-channel output beyond stereo
- ALOG_ASSERT(2 == MAX_NUM_CHANNELS, "bad MAX_NUM_CHANNELS %d", MAX_NUM_CHANNELS);
- LocalClock lc;
- pthread_once(&sOnceControl, &sInitRoutine);
- mState.enabledTracks= 0;
- mState.needsChanged = 0;
- mState.frameCount = frameCount;
- mState.hook = process__nop;
- mState.outputTemp = NULL;
- mState.resampleTemp = NULL;
- mState.mLog = &mDummyLog;
- // mState.reserved
- // FIXME Most of the following initialization is probably redundant since
- // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0
- // and mTrackNames is initially 0. However, leave it here until that's verified.
- //目前tracks应该是没有被引用,因为mTrackNames = 0,所以不初始化也可以。
- track_t* t = mState.tracks;
- for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
- t->resampler = NULL;
- t->downmixerBufferProvider = NULL;
- t++;
- }
- // find multichannel downmix effect if we have to play multichannel content
- //通过类型EFFECT_UIID_DOWNMIX寻找处理多路音频特效的 downmix effect 对象。
- uint32_t numEffects = 0;
- //加载所有的effect lib,并创建effect对象
- int ret = EffectQueryNumberEffects(&numEffects);
- if (ret != 0) {
- ALOGE("AudioMixer() error %d querying number of effects", ret);
- return;
- }
- ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects);
- for (uint32_t i = 0 ; i < numEffects ; i++) {
- if (EffectQueryEffect(i, &dwnmFxDesc) == 0) {
- ALOGV("effect %d is called %s", i, dwnmFxDesc.name);
- if (memcmp(&dwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {
- ALOGI("found effect \"%s\" from %s",
- dwnmFxDesc.name, dwnmFxDesc.implementor);
- isMultichannelCapable = true;
- break;
- }
- }
- }
- ALOGE_IF(!isMultichannelCapable, "unable to find downmix effect");
- }
- //根据sessionId获得一个空闲的track,其中mTrackNames每一位代表了一个track的状态,
- //1表示已经分配,0表示未分配,从右至左开始分配(... 0000 1111),设置任意一位为1,返回分配的位数(0到31)
- //mTrackNames初始化为0,mConfiguredNames初始化为-1
- int AudioMixer::getTrackName(audio_channel_mask_t channelMask, int sessionId)
- {,
- uint32_t names = (~mTrackNames) & mConfiguredNames;
- if (names != 0) {
- int n = __builtin_ctz(names);//返回右起第一个‘1’之后的0的个数,如1111 0010 0000 返回5。
- ALOGV("add track (%d)", n);
- mTrackNames |= 1 << n;
- // assume default parameters for the track, except where noted below
- track_t* t = &mState.tracks[n];
- t->needs = 0;
- t->volume[0] = UNITY_GAIN;
- t->volume[1] = UNITY_GAIN;
- // no initialization needed
- // t->prevVolume[0]
- // t->prevVolume[1]
- t->volumeInc[0] = 0;
- t->volumeInc[1] = 0;
- t->auxLevel = 0;
- t->auxInc = 0;
- // no initialization needed
- // t->prevAuxLevel
- // t->frameCount
- t->channelCount = 2;
- t->enabled = false;
- t->format = 16;
- t->channelMask = AUDIO_CHANNEL_OUT_STEREO;
- t->sessionId = sessionId;
- // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)
- t->bufferProvider = NULL;
- t->buffer.raw = NULL;
- // no initialization needed
- // t->buffer.frameCount
- t->hook = NULL;
- t->in = NULL;
- t->resampler = NULL;
- t->sampleRate = mSampleRate;
- // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)
- t->mainBuffer = NULL;
- t->auxBuffer = NULL;
- t->downmixerBufferProvider = NULL;
- //检查是否支持多路音频,不支持返回OK,否则准备downmix effect调用
- status_t status = initTrackDownmix(&mState.tracks[n], n, channelMask);
- if (status == OK) {
- return TRACK0 + n;
- }
- ALOGE("AudioMixer::getTrackName(0x%x) failed, error preparing track for downmix",
- channelMask);
- }
- return -1;
- }
- //释放一个已经分配的track,根据name找到track,并设置mTackNames中任意对应位为0。
- void AudioMixer::deleteTrackName(int name)
- {
- ALOGV("AudioMixer::deleteTrackName(%d)", name);
- name -= TRACK0;
- ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
- ALOGV("deleteTrackName(%d)", name);
- track_t& track(mState.tracks[ name ]);
- if (track.enabled) {
- track.enabled = false;
- invalidateState(1<<name);
- }
- // delete the resampler
- delete track.resampler;
- track.resampler = NULL;
- // delete the downmixer
- unprepareTrackForDownmix(&mState.tracks[name], name);
- mTrackNames &= ~(1<<name);
- }
Audio笔记之AudioMixer
最新推荐文章于 2024-07-24 18:06:11 发布