Audio笔记之AudioMixer

http://blog.csdn.net/u010681466/article/details/40263255


[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. bool AudioMixer::isMultichannelCapable = false;//是否支持大于两路的音频  
  2. //downmix effect,用于处理大于两路音频的特效,主要是做向下变换  
  3. effect_descriptor_t AudioMixer::dwnmFxDesc;  
  4. // Ensure mConfiguredNames bitmask is initialized properly on all architectures.  
  5. // The value of 1 << x is undefined in C when x >= 32.  
  6. // 1、初始化state_t对象mState,最多保存32个track,对应thread中的track对象  
  7. // 2、加载支持的effect lib和effect  
  8. // 3、通过类型EFFECT_UIID_DOWNMIX寻找处理多路音频特效的downmix effect对象  
  9. AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks)  
  10.     :   mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1),  
  11.         mSampleRate(sampleRate)  
  12. {  
  13.     // AudioMixer is not yet capable of multi-channel beyond stereo  
  14.     COMPILE_TIME_ASSERT_FUNCTION_SCOPE(2 == MAX_NUM_CHANNELS);  
  15.   
  16.     ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u",  
  17.             maxNumTracks, MAX_NUM_TRACKS);  
  18.   
  19.     // AudioMixer is not yet capable of more than 32 active track inputs  
  20.     ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS);  
  21.   
  22.     // AudioMixer is not yet capable of multi-channel output beyond stereo  
  23.     ALOG_ASSERT(2 == MAX_NUM_CHANNELS, "bad MAX_NUM_CHANNELS %d", MAX_NUM_CHANNELS);  
  24.   
  25.     LocalClock lc;  
  26.   
  27.     pthread_once(&sOnceControl, &sInitRoutine);  
  28.   
  29.     mState.enabledTracks= 0;  
  30.     mState.needsChanged = 0;  
  31.     mState.frameCount   = frameCount;  
  32.     mState.hook         = process__nop;  
  33.     mState.outputTemp   = NULL;  
  34.     mState.resampleTemp = NULL;  
  35.     mState.mLog         = &mDummyLog;  
  36.     // mState.reserved  
  37.   
  38.     // FIXME Most of the following initialization is probably redundant since  
  39.     // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0  
  40.     // and mTrackNames is initially 0.  However, leave it here until that's verified.  
  41.     //目前tracks应该是没有被引用,因为mTrackNames = 0,所以不初始化也可以。  
  42.     track_t* t = mState.tracks;  
  43.     for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {  
  44.         t->resampler = NULL;  
  45.         t->downmixerBufferProvider = NULL;  
  46.         t++;  
  47.     }  
  48.   
  49.     // find multichannel downmix effect if we have to play multichannel content  
  50.     //通过类型EFFECT_UIID_DOWNMIX寻找处理多路音频特效的 downmix effect 对象。  
  51.     uint32_t numEffects = 0;  
  52.     //加载所有的effect lib,并创建effect对象  
  53.     int ret = EffectQueryNumberEffects(&numEffects);  
  54.     if (ret != 0) {  
  55.         ALOGE("AudioMixer() error %d querying number of effects", ret);  
  56.         return;  
  57.     }  
  58.     ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects);  
  59.   
  60.     for (uint32_t i = 0 ; i < numEffects ; i++) {  
  61.         if (EffectQueryEffect(i, &dwnmFxDesc) == 0) {  
  62.             ALOGV("effect %d is called %s", i, dwnmFxDesc.name);  
  63.             if (memcmp(&dwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {  
  64.                 ALOGI("found effect \"%s\" from %s",  
  65.                         dwnmFxDesc.name, dwnmFxDesc.implementor);  
  66.                 isMultichannelCapable = true;  
  67.                 break;  
  68.             }  
  69.         }  
  70.     }  
  71.     ALOGE_IF(!isMultichannelCapable, "unable to find downmix effect");  
  72. }  
  73.   
  74. //根据sessionId获得一个空闲的track,其中mTrackNames每一位代表了一个track的状态,  
  75. //1表示已经分配,0表示未分配,从右至左开始分配(... 0000 1111),设置任意一位为1,返回分配的位数(0到31)  
  76. //mTrackNames初始化为0,mConfiguredNames初始化为-1  
  77. int AudioMixer::getTrackName(audio_channel_mask_t channelMask, int sessionId)  
  78. {,  
  79.     uint32_t names = (~mTrackNames) & mConfiguredNames;  
  80.     if (names != 0) {  
  81.         int n = __builtin_ctz(names);//返回右起第一个‘1’之后的0的个数,如1111 0010 0000 返回5。  
  82.         ALOGV("add track (%d)", n);  
  83.         mTrackNames |= 1 << n;  
  84.         // assume default parameters for the track, except where noted below  
  85.         track_t* t = &mState.tracks[n];  
  86.         t->needs = 0;  
  87.         t->volume[0] = UNITY_GAIN;  
  88.         t->volume[1] = UNITY_GAIN;  
  89.         // no initialization needed  
  90.         // t->prevVolume[0]  
  91.         // t->prevVolume[1]  
  92.         t->volumeInc[0] = 0;  
  93.         t->volumeInc[1] = 0;  
  94.         t->auxLevel = 0;  
  95.         t->auxInc = 0;  
  96.         // no initialization needed  
  97.         // t->prevAuxLevel  
  98.         // t->frameCount  
  99.         t->channelCount = 2;  
  100.         t->enabled = false;  
  101.         t->format = 16;  
  102.         t->channelMask = AUDIO_CHANNEL_OUT_STEREO;  
  103.         t->sessionId = sessionId;  
  104.         // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)  
  105.         t->bufferProvider = NULL;  
  106.         t->buffer.raw = NULL;  
  107.         // no initialization needed  
  108.         // t->buffer.frameCount  
  109.         t->hook = NULL;  
  110.         t->in = NULL;  
  111.         t->resampler = NULL;  
  112.         t->sampleRate = mSampleRate;  
  113.         // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)  
  114.         t->mainBuffer = NULL;  
  115.         t->auxBuffer = NULL;  
  116.         t->downmixerBufferProvider = NULL;  
  117.         //检查是否支持多路音频,不支持返回OK,否则准备downmix effect调用  
  118.         status_t status = initTrackDownmix(&mState.tracks[n], n, channelMask);  
  119.         if (status == OK) {  
  120.             return TRACK0 + n;  
  121.         }  
  122.         ALOGE("AudioMixer::getTrackName(0x%x) failed, error preparing track for downmix",  
  123.                 channelMask);  
  124.     }  
  125.     return -1;  
  126. }  
  127. //释放一个已经分配的track,根据name找到track,并设置mTackNames中任意对应位为0。  
  128. void AudioMixer::deleteTrackName(int name)  
  129. {  
  130.     ALOGV("AudioMixer::deleteTrackName(%d)", name);  
  131.     name -= TRACK0;  
  132.     ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);  
  133.     ALOGV("deleteTrackName(%d)", name);  
  134.     track_t& track(mState.tracks[ name ]);  
  135.     if (track.enabled) {  
  136.         track.enabled = false;  
  137.         invalidateState(1<<name);  
  138.     }  
  139.     // delete the resampler  
  140.     delete track.resampler;  
  141.     track.resampler = NULL;  
  142.     // delete the downmixer  
  143.     unprepareTrackForDownmix(&mState.tracks[name], name);  
  144.   
  145.     mTrackNames &= ~(1<<name);  
  146. }  
  147.   
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值