| 1 |
/* |
| 2 |
|
| 3 |
MidiTestViewController.swift |
| 4 |
|
| 5 |
Nikhil Singh, Dr. Richard Boulanger |
| 6 |
Adapted from the Csound iOS Examples by Steven Yi and Victor Lazzarini |
| 7 |
|
| 8 |
This file is part of Csound iOS SwiftExamples. |
| 9 |
|
| 10 |
The Csound for iOS Library is free software; you can redistribute it |
| 11 |
and/or modify it under the terms of the GNU Lesser General Public |
| 12 |
License as published by the Free Software Foundation; either |
| 13 |
version 2.1 of the License, or (at your option) any later version. |
| 14 |
|
| 15 |
Csound is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU Lesser General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU Lesser General Public |
| 21 |
License along with Csound; if not, write to the Free Software |
| 22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 23 |
02111-1307 USA |
| 24 |
|
| 25 |
*/ |
| 26 |
|
| 27 |
import UIKit |
| 28 |
|
| 29 |
class MidiTestViewController: BaseCsoundViewController { |
| 30 |
|
| 31 |
@IBOutlet var mAttackSlider: UISlider! |
| 32 |
@IBOutlet var mDecaySlider: UISlider! |
| 33 |
@IBOutlet var mSustainSlider: UISlider! |
| 34 |
@IBOutlet var mReleaseSlider: UISlider! |
| 35 |
|
| 36 |
@IBOutlet var mCutoffSlider: UISlider! |
| 37 |
@IBOutlet var mResonanceSlider: UISlider! |
| 38 |
|
| 39 |
@IBOutlet var mSwitch: UISwitch! |
| 40 |
let widgetsManager = MidiWidgetsManager() |
| 41 |
|
| 42 |
override func viewDidLoad() { |
| 43 |
title = "04. Hardware: MIDI Controller" |
| 44 |
|
| 45 |
// Use a MidiWidgetsManager object to add MIDI CC to UI object bindings |
| 46 |
widgetsManager.add(mAttackSlider, forControllerNumber: 1) |
| 47 |
widgetsManager.add(mDecaySlider, forControllerNumber: 2) |
| 48 |
widgetsManager.add(mSustainSlider, forControllerNumber: 3) |
| 49 |
widgetsManager.add(mReleaseSlider, forControllerNumber: 4) |
| 50 |
widgetsManager.add(mCutoffSlider, forControllerNumber: 5) |
| 51 |
widgetsManager.add(mResonanceSlider, forControllerNumber: 6) |
| 52 |
widgetsManager.openMidiIn() |
| 53 |
|
| 54 |
super.viewDidLoad() |
| 55 |
} |
| 56 |
|
| 57 |
override func viewWillDisappear(_ animated: Bool) { |
| 58 |
widgetsManager.closeMidiIn() // Close MIDI input |
| 59 |
super.viewWillDisappear(animated) |
| 60 |
} |
| 61 |
|
| 62 |
@IBAction func toggleOnOff(_ sender: UISwitch) { |
| 63 |
if sender.isOn { |
| 64 |
let tempFile = Bundle.main.path(forResource: "midiTest", ofType: "csd") |
| 65 |
|
| 66 |
csound.stop() |
| 67 |
csound = CsoundObj() |
| 68 |
csound.add(self) |
| 69 |
|
| 70 |
// Add value bindings for UI objects which are controlled by MIDI CCs |
| 71 |
let csoundUI = CsoundUI(csoundObj: csound) |
| 72 |
csoundUI?.add(mAttackSlider, forChannelName: "attack") |
| 73 |
csoundUI?.add(mDecaySlider, forChannelName: "decay") |
| 74 |
csoundUI?.add(mSustainSlider, forChannelName: "sustain") |
| 75 |
csoundUI?.add(mReleaseSlider, forChannelName: "release") |
| 76 |
csoundUI?.add(mCutoffSlider, forChannelName: "cutoff") |
| 77 |
csoundUI?.add(mResonanceSlider, forChannelName: "resonance") |
| 78 |
|
| 79 |
csound.midiInEnabled = true |
| 80 |
csound.play(tempFile) |
| 81 |
} else { |
| 82 |
csound.stop() |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Turn off all notes using a csound instrument |
| 87 |
@IBAction func midiPanic(_ sender: UIButton) { |
| 88 |
csound.sendScore("i \"allNotesOff\" 0 1") |
| 89 |
} |
| 90 |
|
| 91 |
@IBAction func showInfo(_ sender: UIButton) { |
| 92 |
infoVC.preferredContentSize = CGSize(width: 300, height: 110) |
| 93 |
infoText = "This example demonstrate MIDI input from hardware, as well an on-screen (simulated) MIDI keyboard." |
| 94 |
displayInfo(sender) |
| 95 |
} |
| 96 |
|
| 97 |
override func didReceiveMemoryWarning() { |
| 98 |
super.didReceiveMemoryWarning() |
| 99 |
// Dispose of any resources that can be recreated. |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
extension MidiTestViewController: CsoundObjListener { |
| 104 |
func csoundObjCompleted(_ csoundObj: CsoundObj!) { |
| 105 |
DispatchQueue.main.async { [unowned self] in |
| 106 |
self.mSwitch.isOn = false |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// Virtual keyboard delegate methods |
| 112 |
extension MidiTestViewController: CsoundVirtualKeyboardDelegate { |
| 113 |
func keyDown(_ keybd: CsoundVirtualKeyboard, keyNum: Int) { |
| 114 |
let midikey = 60 + keyNum |
| 115 |
csound.sendScore(String(format: "i1.%003d 0 -1 \(midikey) 0", midikey)) |
| 116 |
} |
| 117 |
|
| 118 |
func keyUp(_ keybd: CsoundVirtualKeyboard, keyNum: Int) { |
| 119 |
let midikey = 60 + keyNum |
| 120 |
csound.sendScore(String(format: "i-1.%003d 0 0", midikey)) |
| 121 |
} |
| 122 |
} |