| 1 |
/* |
| 2 |
|
| 3 |
MultiTouchXYViewController.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 MultiTouchXYViewController: BaseCsoundViewController { |
| 30 |
|
| 31 |
private var touchIds = [Int](repeatElement(0, count: 10)) |
| 32 |
private var touchArray = [UITouch?](repeatElement(nil, count: 10)) |
| 33 |
private var touchesCount = 0 |
| 34 |
fileprivate var touchX = [Float](repeatElement(0, count: 10)) |
| 35 |
fileprivate var touchY = [Float](repeatElement(0, count: 10)) |
| 36 |
fileprivate var touchXPtr = [UnsafeMutablePointer<Float>?](repeatElement(nil, count: 10)) |
| 37 |
fileprivate var touchYPtr = [UnsafeMutablePointer<Float>?](repeatElement(nil, count: 10)) |
| 38 |
|
| 39 |
@IBOutlet var touchesLabel: UILabel! |
| 40 |
|
| 41 |
override func viewDidLoad() { |
| 42 |
title = "15. MultiTouch XY Pad" |
| 43 |
super.viewDidLoad() |
| 44 |
} |
| 45 |
|
| 46 |
override func viewDidAppear(_ animated: Bool) { |
| 47 |
let tempFile = Bundle.main.path(forResource: "multiTouchXY", ofType: "csd") |
| 48 |
|
| 49 |
csound.stop() |
| 50 |
csound = CsoundObj() |
| 51 |
|
| 52 |
csound.addBinding(self) |
| 53 |
csound.play(tempFile) |
| 54 |
} |
| 55 |
|
| 56 |
override func didReceiveMemoryWarning() { |
| 57 |
super.didReceiveMemoryWarning() |
| 58 |
} |
| 59 |
|
| 60 |
func cleanup() { |
| 61 |
for i in 0 ..< 10 { |
| 62 |
touchXPtr[i] = nil |
| 63 |
touchYPtr[i] = nil |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func getTouchIdAssignment() -> Int { |
| 68 |
for i in 0 ..< 10 { |
| 69 |
if touchIds[i] == 0 { |
| 70 |
return i |
| 71 |
} |
| 72 |
} |
| 73 |
return -1 |
| 74 |
} |
| 75 |
|
| 76 |
func getTouchId(_ touch: UITouch) -> Int { |
| 77 |
for i in 0 ..< 10 { |
| 78 |
if touchArray[i] == touch { |
| 79 |
return i |
| 80 |
} |
| 81 |
} |
| 82 |
return -1 |
| 83 |
} |
| 84 |
|
| 85 |
// MARK: Touch Handling Code |
| 86 |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { |
| 87 |
for touch in touches { |
| 88 |
let touchId = getTouchIdAssignment() |
| 89 |
if touchId != -1 { |
| 90 |
touchArray[touchId] = touch |
| 91 |
touchIds[touchId] = 1 |
| 92 |
|
| 93 |
let pt = touch.location(in: view) |
| 94 |
touchX[touchId] = Float(pt.x / view.frame.size.width) |
| 95 |
touchY[touchId] = Float(1 - (pt.y / view.frame.size.height)) // Invert Y axis |
| 96 |
|
| 97 |
touchXPtr[touchId]?.pointee = touchX[touchId] |
| 98 |
touchYPtr[touchId]?.pointee = touchY[touchId] |
| 99 |
|
| 100 |
csound.sendScore("i1.\(touchId) 0 -1 \(touchId)") |
| 101 |
touchesCount += touches.count |
| 102 |
touchesLabel.text = "Touches \(touchesCount)" |
| 103 |
|
| 104 |
if touchesCount > (event?.allTouches?.count)! { |
| 105 |
touchesCount = (event?.allTouches?.count)! |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { |
| 112 |
for touch in touches { |
| 113 |
let touchId = getTouchId(touch) |
| 114 |
if touchId != -1 { |
| 115 |
let pt = touch.location(in: view) |
| 116 |
touchX[touchId] = Float(pt.x / view.frame.size.width) |
| 117 |
touchY[touchId] = Float(1 - (pt.y / view.frame.size.height)) |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { |
| 123 |
for touch in touches { |
| 124 |
let touchId = getTouchId(touch) |
| 125 |
if touchId != -1 { |
| 126 |
touchIds[touchId] = 0 |
| 127 |
touchArray[touchId] = nil |
| 128 |
csound.sendScore("i-1.\(touchId) 0 0 \(touchId)") |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { |
| 134 |
for touch in touches { |
| 135 |
let touchId = getTouchId(touch) |
| 136 |
if touchId != -1 { |
| 137 |
touchIds[touchId] = 0 |
| 138 |
touchArray[touchId] = nil |
| 139 |
csound.sendScore("i-1.\(touchId) 0 0 \(touchId)") |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
@IBAction func showInfo(_ sender: UIButton) { |
| 145 |
infoVC.preferredContentSize = CGSize(width: 300, height: 120) |
| 146 |
infoText = "Multitouch XY Pad demonstrates a multitouch performance surface. Each touch is dynamically mapped to a unique instance of a Csound instrument." |
| 147 |
displayInfo(sender) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
extension MultiTouchXYViewController: CsoundBinding { |
| 152 |
func setup(_ csoundObj: CsoundObj!) { |
| 153 |
for i in 0 ..< 10 { |
| 154 |
touchXPtr[i] = csoundObj.getInputChannelPtr("touch.\(i).x", channelType: CSOUND_CONTROL_CHANNEL) |
| 155 |
touchYPtr[i] = csoundObj.getOutputChannelPtr("touch.\(i).y", channelType: CSOUND_CONTROL_CHANNEL) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func updateValuesToCsound() { |
| 160 |
for i in 0 ..< 10 { |
| 161 |
touchXPtr[i]?.pointee = touchX[i] |
| 162 |
touchYPtr[i]?.pointee = touchY[i] |
| 163 |
} |
| 164 |
} |
| 165 |
} |