EZAudio を使用して iOS アプリを作成しています。float**そのデリゲートは、検出されたボリュームを示す float 値を含むバッファーを返します。このデリゲートは常に呼び出され、その作業は別のスレッドで行われます。
私がやろうとしているのは、EZAudio から float 値を取得し、それをデシベルに変換することです。
EZAudioDelegate
マイク データを取得するための簡略化された EZAudio デリゲートを次に示します。
- (void)microphone:(EZMicrophone *)microphone hasAudioReceived:(float **)buffer withBufferSize:(UInt32)bufferSize withNumberOfChannels:(UInt32)numberOfChannels {
/*
* Returns a float array called buffer that contains the stereo signal data
* buffer[0] is the left audio channel
* buffer[1] is the right audio channel
*/
// Using a separate audio thread to not block the main UI thread
dispatch_async(dispatch_get_main_queue(), ^{
float decibels = [self getDecibelsFromVolume:buffer withBufferSize:bufferSize];
NSLog(@"Decibels: %f", decibels);
});
}
問題
問題は、以下のリンクからソリューションを実装した後、それがどのように機能するのか理解できないことです. 誰かが音量をデシベルに変換する方法を説明できれば、とても感謝しています
コード
このソリューションでは、Accelerate Frameworkの次のメソッドを使用して、音量をデシベルに変換します。
以下はgetDecibelsFromVolume、EZAudio Delegate から呼び出されるメソッドです。デリゲートからfloat** bufferandが渡されます。bufferSize
- (float)getDecibelsFromVolume:(float**)buffer withBufferSize:(UInt32)bufferSize {
// Decibel Calculation.
float one = 1.0;
float meanVal = 0.0;
float tiny = 0.1;
float lastdbValue = 0.0;
vDSP_vsq(buffer[0], 1, buffer[0], 1, bufferSize);
vDSP_meanv(buffer[0], 1, &meanVal, bufferSize);
vDSP_vdbcon(&meanVal, 1, &one, &meanVal, 1, 1, 0);
// Exponential moving average to dB level to only get continous sounds.
float currentdb = 1.0 - (fabs(meanVal) / 100);
if (lastdbValue == INFINITY || lastdbValue == -INFINITY || isnan(lastdbValue)) {
lastdbValue = 0.0;
}
float dbValue = ((1.0 - tiny) * lastdbValue) + tiny * currentdb;
lastdbValue = dbValue;
return dbValue;
}