Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to build a simple tuner [closed]

Tags:

ios

swift

swift2

I am trying to build a simple tuner or some simple app that records a sound and then sends back the frequency of the sound. This could be used to find what note the sound is (as in a guitar tuner), but I am mostly looking to simply record a sound and have the app send back the frequency of the sound. Could someone point me in the right direction regarding this? I've read some stuff about FFT or other things, but I'm a bit of a noob. I've looked over google, but wasn't able to find much about this, but someone talking about an app they made and some code which I have no idea what to make of.

Okay so just now I was looking up something that might help: http://audiokit.io/. This might be the simple solution, but again not sure if this is what most people would do.

So I've managed to get audiokit imported. The issue is that I can't seem to get the code to work to just display the frequency when a sound is made. Here is the code

import UIKit
import AudioKit

class ViewController: UIViewController {

    @IBOutlet weak var frequencyLabel: UILabel!

   let mic = AKMicrophone()


    override func viewDidAppear(animated: Bool) {



    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib

        let tracker = AKFrequencyTracker.init(mic, minimumFrequency: 200, maximumFrequency: 2000)
        var silence = AKBooster(tracker, gain: 0)
        AudioKit.output = silence
        AudioKit.start()


        if tracker.amplitude > 0.1 {
            frequencyLabel.text = String(format: "%0.1f", tracker.frequency)
        }


    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
like image 488
bearsworth Avatar asked Oct 29 '22 23:10

bearsworth


1 Answers

Okay so I figured it out... I was missing lots of things as part of the kit. The reason is that from the audio.io website i followed those instructions but there are a ton of other things that are included in the example when you download the actual audio kit. Here is my code to make the simple frequency thing. It works!!!!

import UIKit
import AudioKit

class ViewController: UIViewController {

    @IBOutlet weak var frequencyLabel: UILabel!

    var mic: AKMicrophone!
    var tracker: AKFrequencyTracker!
    var silence: AKBooster!

    override func viewDidLoad() {
        super.viewDidLoad()

        AKSettings.audioInputEnabled = true
        mic = AKMicrophone()
        tracker = AKFrequencyTracker.init(mic, minimumFrequency: 200, maximumFrequency: 2000)
        silence = AKBooster(tracker, gain: 0)


    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        AudioKit.output = silence
        AudioKit.start()
        NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(ViewController.updateUI), userInfo: nil, repeats: true)
    }

    func updateUI() {
        if tracker.amplitude > 0.1 {
            frequencyLabel.text = String(format: "%0.1f", tracker.frequency)
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
like image 106
bearsworth Avatar answered Nov 15 '22 05:11

bearsworth