6

When I use AudioKit's AKMicrophoneTracker on a physical device, the the frequency and amplitude are always both 0. But in the playground and in the iOS simulator, it works perfectly.

Here's a rough example:

class AppDelegate: UIResponder, UIApplicationDelegate {

    let tracker = AKMicrophoneTracker()

    func application(_ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // start the tracker and show frequency information
        tracker.start()
        Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { _ in
            print(tracker.frequency)
            print(tracker.amplitude)
        })
    }

}

I've reset my physical device's privacy permissions, and iOS is correctly prompting me to allow microphone access. It still doesn't work even though I'm allowing microphone access.

How can I get AKMicrophoneTracker to actually read these values?

I'm using AudioKit 4.0.3. It works as expected when using:

  • the AudioKit playground on my Mac
  • a simulator iPhone 7 Plus running iOS 11.1

It does not work when using:

  • A physical iPhone 7 Plus running iOS 11.1.1 (and also occurs on iOS 11.1)

I originally posted this as a bug on AudioKit's GitHub issue tracker. However, Aure (the project maintainer) encouraged me to post here instead.

John Ellmore
  • 2,209
  • 1
  • 10
  • 22
  • why you have try to track on 0.1 second. i think you should use 1 or 2 minimum second hope will helpful to you – BuLB JoBs Nov 29 '17 at 13:39
  • do you checked in someother device which have iOS 11 or 10 – R. Mohan Nov 30 '17 at 10:16
  • @Tubelight longer time between samples might give better results, but even a time as long as 0.1 seconds should give me _some_ data, even if it's not accurate. But I'm just getting zeros. – John Ellmore Nov 30 '17 at 16:05
  • @R.Mohan I have not checked on a physical iOS 10 device. I unfortunately don't have one. – John Ellmore Nov 30 '17 at 16:07

2 Answers2

6

The following worked for me on 7+, 6s:

In AppDelegate:

import UIKit
import AudioKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
let tracker = AKMicrophoneTracker()



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    // start the tracker and show frequency information
    tracker.start()
    Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { _ in
        print(self.tracker.frequency)
        print(self.tracker.amplitude)
    })


    return true
}
...

info.plist

...
<key>NSMicrophoneUsageDescription</key>
<string>WE need your microfone to contact the aliens</string>
...

enter image description here

RLoniello
  • 2,309
  • 2
  • 19
  • 26
  • You're totally correct--turns out my problem was caused by some other code that I accidentally didn't disable. I've upvoted your answer and awarded you the bounty, but I've marked my answer below as correct as I think it contains the actual relevant info to the problem. – John Ellmore Dec 05 '17 at 18:03
0

Ercell0's answer is correct--his code works perfectly. My specific problem appears to have been caused by some other AudioKit functionality which I thought I had disabled during testing. It was equivalent to running:

AudioKit.output = AKMixer()
AudioKit.start()

after the AKMicrophoneTracker initialization. This caused the AKMicrophoneTracker problem I was experiencing.

It looks like this might be a bug in AudioKit. I've opened issue #1142 on Github.

John Ellmore
  • 2,209
  • 1
  • 10
  • 22