iOS 音量柱的實(shí)現(xiàn)(mic 采集的聲音DB反映成音量柱)

需求 : APP 將麥克風(fēng)采集到的聲音(Audio Queue / Audio Unit) 通過公式轉(zhuǎn)換成DB然后在界面中顯示出來可實(shí)時(shí)檢測(cè)DB變化。

流程:

  • 配置Audio 初始化參數(shù),必須使用Audio Queue 或 Audio Unit
    采集聲音。
  • 在Audio Queue 或 Audio Unit 采集聲音的回調(diào)中將聲音數(shù)據(jù)轉(zhuǎn)為DB。
  • 將拿到每一幀聲音的DB值傳給主控制器的UI以反映聲音的變化

最終的效果如下,黃色柱形會(huì)反映聲音DB的變化:


音量柱的實(shí)現(xiàn)

GitHub地址(附代碼) : 音量柱的實(shí)現(xiàn)

簡(jiǎn)書地址 : 音量柱的實(shí)現(xiàn)

博客地址 : 音量柱的實(shí)現(xiàn)

掘金地址 : 音量柱的實(shí)現(xiàn)


注意點(diǎn)

  • 經(jīng)過測(cè)試如果使用Audio Unit的方式采集聲音, 由于設(shè)置的聲音級(jí)別是audioUnit.componentSubType = kAudioUnitSubType_VoiceProcessingIO; 而采集到的聲音數(shù)據(jù)從512開始變得不正常,數(shù)據(jù)格外大,非正常范圍數(shù)據(jù),所以我們從512開始不處理后面的數(shù)據(jù)。下文有具體說明
  • 如果是采用Audio Queue計(jì)算的數(shù)據(jù)則不需要額外處理

具體實(shí)現(xiàn)

1.初始化Audio Queue / Audio Unit 采集聲音,這里不做說明,如有問題可參考Audio Queue/ Audio Unit 采集聲音
2.在采集聲音回調(diào)中將聲音數(shù)據(jù)轉(zhuǎn)為聲音的DB值。

以Audio Unit 為例,在回調(diào)中處理如下

#pragma mark - AudioUnit
static OSStatus RecordCallback(void *inRefCon,
                               AudioUnitRenderActionFlags *ioActionFlags,
                               const AudioTimeStamp *inTimeStamp,
                               UInt32 inBusNumber,
                               UInt32 inNumberFrames,
                               AudioBufferList *ioData) {

    XDXRecorder *recorder = (XDXRecorder *)inRefCon;
    
    // 將回調(diào)數(shù)據(jù)傳給_buffList
    AudioUnitRender(recorder->_audioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, recorder->_buffList);
    
    void    *bufferData = recorder->_buffList->mBuffers[0].mData;
    UInt32   bufferSize = recorder->_buffList->mBuffers[0].mDataByteSize;
    //    printf("Audio Recoder Render dataSize : %d \n",bufferSize);
    
    float channelValue[2];
    caculate_bm_db(bufferData, bufferSize, 0, k_Mono, channelValue,true);
    recorder.volLDB = channelValue[0];
    recorder.volRDB = channelValue[1];

根據(jù)聲音的計(jì)算公式dB=20?log(A)→A=pow(10,(db/20.0)),我們對(duì)回調(diào)中傳來的聲音數(shù)據(jù)進(jìn)行處理,這里需要注意的是,經(jīng)過測(cè)試如果使用Audio Unit的方式采集聲音, 由于設(shè)置的聲音級(jí)別是audioUnit.componentSubType = kAudioUnitSubType_VoiceProcessingIO; 而采集到的聲音數(shù)據(jù)從512開始變得不正常,數(shù)據(jù)格外大,非正常范圍數(shù)據(jù),所以我們從512開始不處理后面的數(shù)據(jù),具體原因可能是因?yàn)锳udio Unit的kAudioUnitSubType_VoiceProcessingIO分類做了一些聲音的消除回聲等優(yōu)化導(dǎo)致數(shù)據(jù)和正常數(shù)據(jù)略有不同,在AudioQueue中并不存在這樣的情況。

我們的APP中使用的是單聲道,遍歷聲音數(shù)據(jù),如下我們通過遍歷每一幀完整的聲音數(shù)據(jù)(audioData)找到其中最大的值(max)來對(duì)它進(jìn)行處理,處理后的數(shù)據(jù)按照公式可得到一個(gè)聲音的DB值(-40 - 0)

void caculate_bm_db(void * const data ,size_t length ,int64_t timestamp, ChannelCount channelModel,float channelValue[2],bool isAudioUnit) {
    int16_t *audioData = (int16_t *)data;
    
    if (channelModel == k_Mono) {   // 單聲道
        int     sDbChnnel     = 0;
        int16_t curr          = 0;
        int16_t max           = 0;
        size_t traversalTimes = 0;
        
        if (isAudioUnit) {
            traversalTimes = length/2;// 由于512后面的數(shù)據(jù)顯示異常  需要全部忽略掉
        }else{
            traversalTimes = length;
        }
        
        for(int i = 0; i< traversalTimes; i++) {
            curr = *(audioData+i);
            if(curr > max) max = curr;
        }
        
        if(max < 1) {
            sDbChnnel = -100;
        }else {
            sDbChnnel = (20*log10((0.0 + max)/32767) - 0.5);
        }
        
        channelValue[0] = channelValue[1] = sDbChnnel;
        
    } else if (channelModel == k_Stereo){   // 立體聲
        int sDbChA = 0;
        int sDbChB = 0;
        
        int16_t nCurr[2] = {0};
        int16_t nMax[2] = {0};
        
        for(unsigned int i=0; i<length/2; i++) {
            nCurr[0] = audioData[i];
            nCurr[1] = audioData[i + 1];
            
            if(nMax[0] < nCurr[0]) nMax[0] = nCurr[0];
            
            if(nMax[1] < nCurr[1]) nMax[1] = nCurr[0];
        }
        
        if(nMax[0] < 1) {
            sDbChA = -100;
        } else {
            sDbChA = (20*log10((0.0 + nMax[0])/32767) - 0.5);
        }
        
        if(nMax[1] < 1) {
            sDbChB = -100;
        } else {
            sDbChB = (20*log10((0.0 + nMax[1])/32767) - 0.5);
        }
        
        channelValue[0] = sDbChA;
        channelValue[1] = sDbChB;
    }
}
3.將拿到的DB值反映到UI界面上
  • 自定義音量柱的View類

我們?cè)谶@里使用CALayer來實(shí)現(xiàn)音量柱的變化,使用CALayer的好處是其底層自動(dòng)做了動(dòng)畫的處理,所以當(dāng)我們連續(xù)對(duì)其設(shè)置不同的DB值在UI上變化是連續(xù)的。具體UI的處理可在XDXVolumeView.m中查看。

  • 在主控制器中開啟定時(shí)器設(shè)置每隔0.25s更新一次UI,這樣可以保證音量柱連續(xù)變化
  • 注意:我們將拿到的聲音DB值(-40 - 0)轉(zhuǎn)為0 - 40方便UI的顯示
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容