iOS AudioKit AKAmplitudeTracker

Viewed 435

I'm trying to get something like this playground working on iOS: http://audiokit.io/playgrounds/Analysis/Tracking%20Amplitude/

This is my view controller, where I use the mandolin physical model to create notes and then run an fft and an amplitudeTracker. But I get no values from them. You can see the output below:

var fft: AKFFTTap!
var amplitudeTracker: AKAmplitudeTracker!


override func viewDidLoad() {
    super.viewDidLoad()

    let mandolin = AKMandolin()
    mandolin.detune = 1
    mandolin.bodySize = 1
    let pluckPosition = 0.2

    let scale: [MIDINoteNumber] = [72, 74, 76, 77, 79, 81, 83, 84]
    let delay = AKDelay(mandolin)
    let mix = AKMixer()
    mix.connect(delay)

    let reverb = AKReverb(mix)
    amplitudeTracker = AKAmplitudeTracker(mix)
    fft = AKFFTTap(mix)

    AudioKit.output = reverb
    AudioKit.start()

  for note in scale {

        let note1: MIDINoteNumber = note
        let octave1: MIDINoteNumber = 4
        let course1 = 2
        let count = 25

        mandolin.fret(noteNumber: note1 + octave1, course: course1 - 1)
        mandolin.pluck(course: course1 - 1, position: pluckPosition, velocity: 127)

        print("plying note")

        let fftData = self.fft.fftData
        let lowMax = fftData[0 ... (count / 2) - 1].max() ?? 0
        let hiMax = fftData[count / 2 ... count - 1].max() ?? 0
        let hiMin = fftData[count / 2 ... count - 1].min() ?? 0
        let amplitude = Float(self.amplitudeTracker.amplitude * 65)
        print("amplitude \(amplitude)")
        print("lowMax \(lowMax)")
        print("hiMax \(hiMax)")
        print("hiMin \(hiMin)")

        sleep(1)
    }

}

This is the output I get when I run it :

2017-09-26 12:43:27.724706-0700 AK[9467:1161171] 957: AUParameterSet 2 (1/8): err -10867
2017-09-26 12:43:28.177699-0700 AK[9467:1161171] 957: AUParameterSet 2 (1/8): err -10867
playing note
amplitude 0.0
lowMax 0.0
hiMax 0.0
hiMin 0.0
playing note
amplitude 0.0
lowMax 0.0
hiMax 0.0
hiMin 0.0
...
2 Answers

The main problem here is that Frequency Tracker node is not part of the signal chain. AudioKit (and Apple's underlying AVAudioEngine) works on a pull model in that audio will not be pulled through a node unless it is requested by a downstream node. This basically means everything up from the AudioKit.output node will get bytes pulled through them.

However, here, the reverb is made to be the output, so the tracker itself doesn't get any data coming through it. Changing it to AudioKit.output = amplitudeTracker will get the data going through the node.

The amplitudeTracker acts as a passthrough so the audio comes through as well. If you would not want the audio, you'd then stick the output of the tracker through a booster which would lower the volume down to zero.

I was getting this -10867 error while trying to reinitialize a AKSequencer variable that had a bunch of tracks/samplers/etc.

I stored them in arrays, called the following before reinitializing, and the -10867 errors went away:

private var samplers = [AKMIDISampler]()
private var tracks = [AKMusicTrack]()
private var mixer = AKMixer()

...

public func cleanSequencer() {
    for track in tracks { 
        track.clear() 
    }
    for sample in samplers {
        sample.disconnectOutput()
        sample.destroyEndpoint()
    }
    mixer.detach()
}

Hope this helps!

------- UPDATE: 01 -------

This produced some unexpected effects, mainly with no sound being played after using this method.

But, now curious if anyone knows why the -10867 would go away and sound too?

Related