Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 73 additions & 75 deletions AVAudioUnitSamplerFrobs/DrumMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,116 +10,114 @@ import Foundation
import AVFoundation

class DrumMachine: NSObject {
var engine: AVAudioEngine!
var sampler: AVAudioUnitSampler!

var engine: AVAudioEngine

var sampler: AVAudioUnitSampler

var sequencer: AVAudioSequencer!

override init() {
super.init()

engine = AVAudioEngine()

sampler = AVAudioUnitSampler()
super.init()

engine.attach(sampler)
engine.connect(sampler, to: engine.mainMixerNode, format: nil)

setupSequencer()

loadPreset()

addObservers()

startEngine()

print(self.engine)

setSessionPlayback()
}


func setupSequencer() {

self.sequencer = AVAudioSequencer(audioEngine: self.engine)

let options = AVMusicSequenceLoadOptions()

if let fileURL = Bundle.main.url(forResource: "chromatic", withExtension: "mid") {
do {
try sequencer.load(from: fileURL, options: options)
try sequencer.load(from: fileURL, options: [])
print("loaded \(fileURL)")
} catch {
print("something screwed up \(error)")
return
}
}

sequencer.prepareToPlay()
print(sequencer)
}

func play() {
if sequencer.isPlaying {
stop()
}
sequencer.currentPositionInBeats = TimeInterval(0)

sequencer.currentPositionInBeats = 0

do {
try sequencer.start()
} catch {
print("cannot start \(error)")
}
}

func stop() {
sequencer.stop()
}

// load from the bundle or documents directory only
// https://forums.developer.apple.com/message/20748#20643
func loadPreset() {

guard let preset = Bundle.main.url(forResource: "Drums", withExtension: "aupreset") else {
print("could not load aupreset")
return
}
print("loaded preset \(preset)")

do {
try sampler.loadInstrument(at: preset)
} catch {
print("error loading preset \(error)")
}
}


func setSessionPlayback() {
let audioSession = AVAudioSession.sharedInstance()
do {
try
audioSession.setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers)
audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
} catch {
print("couldn't set category \(error)")
return
}

do {
try audioSession.setActive(true)
} catch {
print("couldn't set category active \(error)")
return
}
}

func startEngine() {

if engine.isRunning {
print("audio engine already started")
return
}

do {
try engine.start()
print("audio engine started")
Expand All @@ -128,52 +126,52 @@ class DrumMachine: NSObject {
print("could not start audio engine")
}
}

// MARK: - Notifications

func addObservers() {
NotificationCenter.default.addObserver(self,
selector: #selector(DrumMachine.engineConfigurationChange(_:)),
name: NSNotification.Name.AVAudioEngineConfigurationChange,
selector: #selector(engineConfigurationChange),
name: .AVAudioEngineConfigurationChange,
object: engine)

NotificationCenter.default.addObserver(self,
selector: #selector(DrumMachine.sessionInterrupted(_:)),
name: NSNotification.Name.AVAudioSessionInterruption,
selector: #selector(sessionInterrupted),
name: .AVAudioSessionInterruption,
object: engine)

NotificationCenter.default.addObserver(self,
selector: #selector(DrumMachine.sessionRouteChange(_:)),
name: NSNotification.Name.AVAudioSessionRouteChange,
selector: #selector(sessionRouteChange),
name: .AVAudioSessionRouteChange,
object: engine)
}

func removeObservers() {
NotificationCenter.default.removeObserver(self,
name: NSNotification.Name.AVAudioEngineConfigurationChange,
name: .AVAudioEngineConfigurationChange,
object: nil)

NotificationCenter.default.removeObserver(self,
name: NSNotification.Name.AVAudioSessionInterruption,
name: .AVAudioSessionInterruption,
object: nil)

NotificationCenter.default.removeObserver(self,
name: NSNotification.Name.AVAudioSessionRouteChange,
name: .AVAudioSessionRouteChange,
object: nil)
}


// MARK: notification callbacks
@objc func engineConfigurationChange(_ notification: Notification) {
print("engineConfigurationChange")
}

@objc func sessionInterrupted(_ notification: Notification) {
print("audio session interrupted")
if let engine = notification.object as? AVAudioEngine {
engine.stop()
}

if let userInfo = notification.userInfo as? [String: Any?] {
if let reason = userInfo[AVAudioSessionInterruptionTypeKey] as? AVAudioSessionInterruptionType {
switch reason {
Expand All @@ -185,19 +183,19 @@ class DrumMachine: NSObject {
}
}
}

@objc func sessionRouteChange(_ notification: Notification) {
print("sessionRouteChange")
if let engine = notification.object as? AVAudioEngine {
engine.stop()
}

if let userInfo = notification.userInfo as? [String: Any?] {

if let reason = userInfo[AVAudioSessionRouteChangeReasonKey] as? AVAudioSessionRouteChangeReason {

print("audio session route change reason \(reason)")

switch reason {
case .categoryChange: print("CategoryChange")
case .newDeviceAvailable:print("NewDeviceAvailable")
Expand All @@ -209,31 +207,31 @@ class DrumMachine: NSObject {
case .routeConfigurationChange:print("RouteConfigurationChange")
}
}

if let previous = userInfo[AVAudioSessionRouteChangePreviousRouteKey] {
print("audio session route change previous \(String(describing: previous))")
}
}
}


func createMusicSequence() -> MusicSequence? {

var s: MusicSequence?
var status = NewMusicSequence(&s)
if status != noErr {
print("\(#line) bad status \(status) creating sequence")
}

if let musicSequence = s {

// add a track
var t: MusicTrack?
status = MusicSequenceNewTrack(musicSequence, &t)
if status != noErr {
print("error creating track \(status)")
}

if let track = t {
// bank select msb
var chanmess = MIDIChannelMessage(status: 0xB0, data1: 0, data2: 0, reserved: 0)
Expand All @@ -247,14 +245,14 @@ class DrumMachine: NSObject {
if status != noErr {
print("creating bank select event \(status)")
}

// program change. first data byte is the patch, the second data byte is unused for program change messages.
chanmess = MIDIChannelMessage(status: 0xC0, data1: 0, data2: 0, reserved: 0)
status = MusicTrackNewMIDIChannelEvent(track, 0, &chanmess)
if status != noErr {
print("creating program change event \(status)")
}

// now make some notes and put them on the track
var beat = MusicTimeStamp(0.0)
for i: UInt8 in 60...72 {
Expand All @@ -272,14 +270,14 @@ class DrumMachine: NSObject {
}
// associate the AUGraph with the sequence.
// MusicSequenceSetAUGraph(musicSequence, self.processingGraph)

// Let's see it
CAShow(UnsafeMutablePointer<MusicSequence>(musicSequence))

return musicSequence
}

return nil
}

}
Loading