AudioKit - How to use AKAmplitudeTracker threshold callback?

317 Views Asked by At

AudioKit include a great tool to track signal amplitude: AKAmplitudeTracker

This tracker can be init with a thresholdCallback, I suppose that the callback should trigger when the threshold is reach.

I'm playing with the MicrophoneAnalysis example and I can't find a way to trigger my callback.

Here is my code:

var mic: AKMicrophone!
var trackerAmplitude: AKAmplitudeTracker!
 var silence: AKBooster!

AKSettings.audioInputEnabled = true
mic = AKMicrophone()

trackerAmplitude = AKAmplitudeTracker(mic, halfPowerPoint: 10, threshold: 0.01, thresholdCallback: { (success) in
            print("thresholdCallback: \(success)")
        })
trackerAmplitude.start()

silence = AKBooster(trackerAmplitude, gain: 0)
AudioKit.output = silence

I tried to play with the halfPowerPoint and threshold values, but even with vey low values I cannot find a way to print anything :/

Whereas when I'm printing trackerAmplitude.amplitude, I've got values higher than 0.01

Is there something I'm missing ?

1

There are 1 best solutions below

0
On

The following code works. Tested with AudioKit 4.9, Xcode 11.2, macOS Playground.

This might be an issue of AudioKit, but threshold must be changed via property to activate tracking, as shown below...

import AudioKitPlaygrounds
import AudioKit

let mic = AKMicrophone()

AKSettings.audioInputEnabled = true
let amplitudeTracker = AKAmplitudeTracker(mic, halfPowerPoint: 10, threshold: 1, thresholdCallback: { (success) in
    print("thresholdCallback: \(success)")
})
AudioKit.output = amplitudeTracker

try AudioKit.start()
amplitudeTracker.threshold = 0.01 // !! MUST BE SET VIA PROPERTY
amplitudeTracker.start()
mic?.start()

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true