简体   繁体   中英

Implementing microphone analysis with Audiokit v5

I need to run my app which uses AudioKit as one of its libraries in Xcode 12 so I can prepare it for the upcoming iOS 14 release, since Xcode 12 contains all the changes to classes that need to be adopted by me.

However the current stable release of 4.10.1 does not compile in Xcode 12. I then tried to install the latest v5-develop version 5.0.b1 and that compiles successfully, however the API went through significant changes -> AudioKit.output for example is deprecated. I was unable to find any documentation regarding these changes. I reckon that is because it is still in beta.

I previously used this code I have taken from their microphone analysis example:

func setup(completion: @escaping TaskHandler) {
    // Setup the microphone
    AKSettings.audioInputEnabled = true
    mic = AKMicrophone()
    tracker = AKFrequencyTracker(mic)
    silence = AKBooster(tracker, gain: 0)

    // Start the audio engine
    AudioKit.output = silence
    do {
        try AudioKit.start()
        completion(true, nil)
    } catch {
        completion(false, ErrorMessage.microphoneFailedToStart)
        AKLog("AudioKit did not start!")
    }
}

and then I access the amplitude and frequency like this:

timer?.setEventHandler(handler: {
        let currentAmp = self.tracker.amplitude
        let currentFreq = self.tracker.frequency
        // ... some processing here
}

In the new version I dug around in Xcode and found and tried to get AKMicrophoneTrackerEngine to work and I got it to compile and run:

let engine = AKMicrophoneTrackerEngine()

func setup(completion: @escaping TaskHandler) {
    engine.start()
    completion(true, nil)
}

// ..later in code

timer?.setEventHandler(handler: {
        let currentAmp = self.engine.amplitude
        let currentFreq = self.engine.frequency
        // ... some processing here
}

However the frequency (and possibly amplitude) are slightly off. Playing a 1000Hz test tone using 4.10.1 produced exactly 1000 as an output but my new approach produces a value around 918.

Can anyone guide me to a piece of documentation or advise me how to implement the same functionality using the new API?

Thank you.

https://github.com/AudioKit/AudioKit/issues/2267 the issue should be fixed by updating to AudioKit 4.11.1 pod 'AudioKit' , '~> 4.11.1' . This version successfully compiles in Xcode 12. You also need to replace all AudioKit instances with AKManager for example: AudioKit.output = ... with AKManager.output = ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM