Starting with Audiokit on IOS

Hashdog started to work with a new project wich the goal is to record an audio with the smartphone for apply a process and shows in a screen, that is what our client needs. Catch the sound it wasn't a big deal, the complex start with the audio process, increase and decrease gain, capture frequency and amplitude, and show graphs of the audio.
To solve all these requirements we use a IOS library, Audiokit, it has implemented methods to catch audio without problems and generate the output file.

In this post, you going to see the basic steps to start using Audiokit. You will be able to install and create a simple "Hello world" app basically. Enjoy!

We're using Xcode 8.2.1 and we develop in Swift 3.

Start creating a new project in Xcode, choose "Single View Application", because in this project we aren't going to create something complex.

For this example, we're going to use a Swift and iPhone as a target device.

Now, download the library from audiokit.io

On this tutorial, we will use this version of audioKit AudioKit-iOS-3.5.3.zip

In Xcode again, we're going to add the AudioKit framework to the project, then go to the general tab and click on add new binary; after that select the file that just download it.

For this example we're using a simple view make with the storyboard, to build it you just need to drag and drop buttons and the other elements how you know.

Ok, let's go to our controller, in this case, the ViewController.swift

1 . Import the library

//  ViewController.swift
import UIKit  
import AudioKit  

2 . Instance the oscillator, it will be the sound manager, and also instance the microphone that we will need to catch the external sounds.

class ViewController: UIViewController {  
    let oscillator = AKOscillator()
    let mic = AKMicrophone()
    …

3 . We have to add some logic to work with frequency and amplitude.

       ...
        if oscillator.isPlaying {
            oscillator.stop()
            sender.setTitle("Play Sine Wave", for: .normal)
        } else {
            oscillator.amplitude = random(0.5, 1)
            oscillator.frequency = random(220, 880)
            oscillator.start()
            sender.setTitle("Stop Sine Wave at \(Int(oscillator.frequency))Hz", for: .normal)
        }
    }

4 . Now in another action, we use the microphone.

    @IBAction func startMic(_ sender: UIButton) {
...        
        if mic.isPlaying {
            mic.stop()
            sender.setTitle("Start microphone", for: .normal)
            downVolume.isHidden = true
            upVolume.isHidden = true
        } else {
            mic.start()
            sender.setTitle("Stop microphone", for: .normal)
            downVolume.isHidden = false
            upVolume.isHidden = false
        }
    }

5 . Also we add some methods to up/down the volume.

    @IBAction func upVolume(_ sender: Any) {
        volume = volume + 1
        mic.volume = volume
    }

    @IBAction func downVolume(_ sender: Any) {
        volume = volume - 1
        mic.volume = volume
    }

6 . In the final step, we add a plot to show the wave graphics, this is a simple UIView with EZAudioPlotGL class.

    @IBOutlet weak var plot: EZAudioPlotGL!

    override func viewDidLoad() {
        …

        //plot
        let graph = AKOutputWaveformPlot.createView()
        plot.addSubview(graph)

Well, with this you can start to play with AudioKit; now depend on you. I recommend read the official documentation, it’s very complete, has examples and tutorials, check the link http://audiokit.io/docs/index.html

Hope that you enjoy this great library and you can make the apps that you have in mind.

Please, feel you free to comment or contact us. You can take a look our Brochure too.

God bless you!