Giter Club home page Giter Club logo

openframeworks-and-audiokit-helloworld's Introduction

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.

openframeworks-and-audiokit-helloworld's People

Contributors

aure avatar narner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

mainvolume-hq

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.