2016-10-13 24 views
2

私は信号処理に関する知識がほとんどなく、現在Swiftの機能を実装しようとしていますが、sound pressure levelが増加したときにイベントをトリガーします(例えば人間の叫び声)。私はちょうど平均を計算することによって、音圧レベルの概算を取得しようとしたfloat配列にそれを入れた後AVAudioPCMBufferから音圧レベルを抽出するにあたって

let recordingFormat = inputNode.outputFormat(forBus: 0) 
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat){ 
(buffer : AVAudioPCMBuffer?, when : AVAudioTime) in 
    let arraySize = Int(buffer.frameLength) 
    let samples = Array(UnsafeBufferPointer(start: buffer.floatChannelData![0], count:arraySize)) 

    //do something with samples 
    let volume = 20 * log10(floatArray.reduce(0){ $0 + $1}/Float(arraySize)) 
    if(!volume.isNaN){ 
     print("this is the current volume: \(volume)") 
    } 
} 

私はこのようなコールバックでAVAudioEngineの入力ノードを活用しています。

しかし、これは私のiPadはちょうど非常に部屋に座っていた場合でも、多くの変動値が得られます。

this is the current volume: -123.971 
this is the current volume: -119.698 
this is the current volume: -147.053 
this is the current volume: -119.749 
this is the current volume: -118.815 
this is the current volume: -123.26 
this is the current volume: -118.953 
this is the current volume: -117.273 
this is the current volume: -116.869 
this is the current volume: -110.633 
this is the current volume: -130.988 
this is the current volume: -119.475 
this is the current volume: -116.422 
this is the current volume: -158.268 
this is the current volume: -118.933 

私はマイクの近くに拍手する場合は、この値では有意な増加は確かにありますが。

if(!volume.isNaN){ 
    if(isInThePreparingPhase){ 
     print("this is the current volume: \(volume)") 
     volumeSum += volume 
     volumeCount += 1 
    }else if(isInTheEventTriggeringPhase){ 
     if(volume > meanVolume){ 
      //triggers an event 
     } 
     } 
} 

がaverageVolumeがある:イベントトリガー相の間に差の著しい増加がある場合

だから私は、最初の準備段階の間、これらのボリュームの平均値を計算し、比較するような何かを行うことができますトリガイベント相への準備段階からの移行中に計算:私はほかに大音量の音楽を再生する場合meanVolume = volumeSum/Float(volumeCount)

....

しかし、有意な増加はないようですマイクまれに、環境が人の耳に聞こえる音量が大幅に増加しない場合でも、volumemeanVolumeより大きくなります。

AVAudioPCMBufferから音圧レベルを抽出する適切な方法は何ですか?

ウィキペディアは、基準音圧である正方形の音圧とP0を意味pがルートであると、この

math!

ような式を与えます。

しかし、AVAudioPCMBuffer.floatChannelDataの浮動小数点値が何を表しているかわかりません。 The apple pageのみ

浮動小数点値としてのバッファのオーディオサンプル。

どうすればよいですか?

+0

こんにちはアーチ、私はあなたがこの質問に対する答えを理解したと思いますか?あなたが提供できるコードはありますか? – Logan

答えて

1

私は最初のステップは、音のenvelopeを取得することだと思います。単純平均を使ってエンベロープを計算することができますが、整流ステップを追加する必要があります(通常、abs()またはsquare()を使用してすべてのサンプルを陽性にする必要があります)

もっと一般的には、アタックとディケイのさまざまな定数を使った平均化は、labです。そして、あなたが封筒を持っている場合、あなたは追加のフィルタでそれを滑らかにすることができます

1 - exp(-timePerSample*2/smoothingTime) 

ステップ2

、および:これらの定数は、サンプリング周波数に依存し、あなたが定数を計算し、この計算式を使用することができます2つのエンベロープを比較して、ベースレベルよりも大きな音を見つけます。ここでは、より多くの音があります。complete labです。

オーディオの「イベント」を検出することは非常に難しく、予測が難しいことに注意してください。多くのデバッグ支援が必要です。

+0

ラボデモをお寄せいただきありがとうございます。スーパーヘルプ:D –

関連する問題