Created
February 11, 2015 13:37
-
-
Save hysysk/bed2c0a696ec6932093c to your computer and use it in GitHub Desktop.
Drum pad sample using AudioKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
var buttons: [UIButton] = [] | |
var notes: [AKNote] = [] | |
let soundFiles: Array<String> = ["BassC1", "FluteC2", "glockA1", "musicboxC1", "newharpC1", "pianoguitarA2"] | |
func prepareSounds() { | |
for (index, soundFile) in enumerate(soundFiles) { | |
var fileName = NSBundle.mainBundle().pathForResource(soundFile, ofType: "aif") | |
var akSoundFile = AKSoundFile(filename: fileName) | |
var instrument = AKInstrument() | |
instrument.addFunctionTable(akSoundFile) | |
var looper = AKMonoSoundFileLooper(soundFile: akSoundFile) | |
looper.loopMode = AKSoundFileLooperMode.NoLoop | |
instrument.connect(looper) | |
var output = AKAudioOutput(audioSource: looper) | |
instrument.connect(output) | |
var note: AKNote = AKNote() | |
note.instrument = instrument | |
notes.append(note) | |
AKOrchestra.addInstrument(instrument) | |
} | |
AKOrchestra.start() | |
} | |
func playSound(sender: UIButton!) { | |
var tag: Int = sender.tag | |
notes[tag].play() | |
} | |
func setupGUI() { | |
var viewsDict = Dictionary <String, UIView>() | |
var visualFormatVertical = "V:|-30" | |
for var i = 0; i < 5; i++ { | |
var button: UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton | |
button.setTitle(String(i), forState: UIControlState.Normal) | |
button.backgroundColor = UIColor.blackColor() | |
button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) | |
button.setTranslatesAutoresizingMaskIntoConstraints(false) | |
button.addTarget(self, action: "playSound:", forControlEvents: .TouchUpInside) | |
button.tag = i | |
view.addSubview(button) | |
buttons.append(button) | |
viewsDict["Button"+String(i)] = button | |
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( | |
"H:|-[Button"+String(i)+"]-|", options: nil, metrics: nil, views: viewsDict)) | |
visualFormatVertical += "-[Button"+String(i)+"]-10" | |
} | |
visualFormatVertical += "-|" | |
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(visualFormatVertical, options: nil, metrics: nil, views: viewsDict)) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
prepareSounds() | |
setupGUI() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment