私は信号処理に関する知識がほとんどなく、現在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)
....
しかし、有意な増加はないようですマイクまれに、環境が人の耳に聞こえる音量が大幅に増加しない場合でも、volume
はmeanVolume
より大きくなります。
AVAudioPCMBufferから音圧レベルを抽出する適切な方法は何ですか?
ウィキペディアは、基準音圧である正方形の音圧とP0を意味pがルートであると、この
ような式を与えます。
しかし、AVAudioPCMBuffer.floatChannelData
の浮動小数点値が何を表しているかわかりません。 The apple pageのみ
浮動小数点値としてのバッファのオーディオサンプル。
どうすればよいですか?
こんにちはアーチ、私はあなたがこの質問に対する答えを理解したと思いますか?あなたが提供できるコードはありますか? – Logan