Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Creative Audio Programming with AudioKit and OpenFrameworks

License

Notifications You must be signed in to change notification settings

narner/OpenFrameworks-and-AudioKit-HelloWorld

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenFrameworks-and-AudioKit

Introduction

If you're making an OpenFrameworks project that will only run on an OS X device, it's possible to include AudioKit in your OpenFrameworks project. AudioKit gives you the ability to quickly prototype great sounding instruments, and is written in pure Swift. If you're an OpenFrameworks developer who's not familiar with AudioKit, check out some of the work shown in our gallery. Hopefully they will inspire you to use AudioKit in some of your games or installations! This tutorial will show you how to add AudioKit to your OpenFrameworks project, and create a simple Hello World example. While it's a rather simple sounding oscillator, it's important to remember that anything you hear in the Gallery pages that you like can also be implemented in your Open Frameworks project.

Getting Started

Before we start, make sure that you have:

  1. Xcode 9 installed

  2. OpenFrameworks 0.9.4 or later downloaded for OS X

  3. You've downloaded the AudioKit-macOS-3.6.zip.

We can create our OpenFrameworks project now. Using the Project Generator, go ahead and create a new OS X project. Call it "OF-AK-HelloWorld", and set its location to be inside of the default "My Apps" folder:

Since AudioKit is a Swift framework, there's a few things we'll need to consider. First, any file that we want to call AudioKit code from will need to be an Objective-C++ file. This is because while it's possible to call Swift code from Objective-C++ code, it is not possible to do so from C++. To change the file type, change the file extension from .cpp to .mm. We're going to do this for ourofApp.cpp and our main.cpp files. After you've done this, change the file type to "Objective-C++" source.

Your file information will look like this at first:

Alt Text

...and should look like this once you've changed it's information:

Alt Text

Again, it's important to remember that because we're changing the file types, your OpenFrameworks project will only compile on an OS X device.

Because AudioKit 3.5 and above requires Swift 3.2, our app's Deployment Target needs to be OS X 10.11 or higher. Go to your project's "General" tab

Alt Text

and make sure that the Deployment Target is set to 10.11.

Adding AudioKit

AudioKit comes with pre-compiled frameworks. If you're planning on distributing your app, we recommend copying the framework into your project:

Alt Text

Make sure that the framework is included in your app's Embedded Binaries listing:

Alt Text

Now, we're going to create a Swift file to define our AudioKit instrument in. We're going to call our file OscillatorInstrument.swift.

Xcode will ask you if you want to create a Bridging Header...we don't need one, as we're calling Swift code from Objective-C++, and not the other way around.

Now, build your project. This will auto-generate a Swift.h file, which is what allows us to call into Swift code from Objective-C++. You can find this file by going to Build Settings, and searching for "Interface Header". You should see a listing for an "Objective-C Generated Interface Header Name", and it should be named OF_AK_HelloWorldDebug-Swift.h:

Alt Text

Go to your ofApp.h file and add the import statement below: #import "OF_AK_HelloWorldDebug-Swift.h

NOTE: Occasionally, this may not display the full-text of the Swift.h file, and may display as $(SWIFT_MODULE_NAME)-Swift.h. This appears to be a bug in Xcode.

Build your project again to make sure that you've sucesfully imported this file. If you get an error, you probably forgot to build after you created your OscillatorInstrument.swift file.

Go to OscillatorInstrument.swift, and import AudioKit by adding the line below under import Foundation: import AudioKit

Add the code below to create a simple oscillator instrument, as well as functions for turning it on and off:

import Foundation
import AudioKit

open class OscillatorInstrument: NSObject {
    var oscillator = AKOscillator()
    
    public override init() {
        AudioKit.output = oscillator
        AudioKit.start()
    }
    
    open func startSound() {
        oscillator.start()
    }
    
    open func stopSound(){
        oscillator.stop()
    }
    
}

Creating and Interacting with our Oscillator

Now, go to your ofApp.mm file, and add the line below as a global variable:

#include "ofApp.h"

OscillatorInstrument *instrument = [[OscillatorInstrument alloc] init];

//--------------------------------------------------------------
void ofApp::setup(){
}

This will create an instance of our OscillatorInstrument class called "Instrument" as soon as our app has started.

Now, we want to give our app the ability to start and stop our oscillator. To do that, we'll add a call to our "Stop" method inside of the mousePressed event method:

void ofApp::mousePressed(int x, int y, int button){
    [instrument startSound];
}

And, to stop our oscillator, add a call to our "Stop" method inside of the mouseReleased method:

void ofApp::mouseReleased(int x, int y, int button){
    [instrument stopSound];
}

Go ahead and run the app. When it starts, press down on your mouse. You should hear an oscillator! To stop the sound, just let go of your mouse.

About

Creative Audio Programming with AudioKit and OpenFrameworks

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published