| 1 |
/* |
| 2 |
|
| 3 |
HardwareTestViewController.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 |
import CoreMotion |
| 29 |
|
| 30 |
class HardwareTestViewController: BaseCsoundViewController { |
| 31 |
|
| 32 |
@IBOutlet var mSwitch: UISwitch! |
| 33 |
|
| 34 |
// Labels to display accelerometer, gyro, attitude sensor values |
| 35 |
@IBOutlet var accX: UILabel! |
| 36 |
@IBOutlet var accY: UILabel! |
| 37 |
@IBOutlet var accZ: UILabel! |
| 38 |
|
| 39 |
@IBOutlet var gyroX: UILabel! |
| 40 |
@IBOutlet var gyroY: UILabel! |
| 41 |
@IBOutlet var gyroZ: UILabel! |
| 42 |
|
| 43 |
@IBOutlet var roll: UILabel! |
| 44 |
@IBOutlet var pitch: UILabel! |
| 45 |
@IBOutlet var yaw: UILabel! |
| 46 |
|
| 47 |
var csoundMotion = CsoundMotion() |
| 48 |
var motionManager = CMMotionManager() |
| 49 |
|
| 50 |
override func viewDidLoad() { |
| 51 |
title = "14, Hardware: Motion Control" |
| 52 |
super.viewDidLoad() |
| 53 |
} |
| 54 |
|
| 55 |
@IBAction func toggleOnOff(_ sender: UISwitch) { |
| 56 |
if sender.isOn { |
| 57 |
let tempFile = Bundle.main.path(forResource: "hardwareTest", ofType: "csd") |
| 58 |
|
| 59 |
csound.stop() |
| 60 |
csound = CsoundObj() |
| 61 |
csound.add(self) |
| 62 |
|
| 63 |
csoundMotion = CsoundMotion(csoundObj: csound) |
| 64 |
|
| 65 |
if csoundMotion.motionManager != nil { |
| 66 |
motionManager = csoundMotion.motionManager // Grab the csoundMotion object's CMMotionManager instance |
| 67 |
|
| 68 |
// Enable accelerometer |
| 69 |
if motionManager.isAccelerometerAvailable { |
| 70 |
csoundMotion.enableAccelerometer() |
| 71 |
motionManager.accelerometerUpdateInterval = 0.1 // Set how quickly updates are pulled |
| 72 |
motionManager.startAccelerometerUpdates(to: .main, withHandler: { [unowned self] (data: CMAccelerometerData?, error: Error?) in |
| 73 |
if data?.acceleration != nil { // Use a closure to publish sensor values to labels |
| 74 |
self.accX.text = String(format: "%.3f", (data?.acceleration.x)!) |
| 75 |
self.accY.text = String(format: "%.3f", (data?.acceleration.y)!) |
| 76 |
self.accZ.text = String(format: "%.3f", (data?.acceleration.z)!) |
| 77 |
} |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
// Enable gyro |
| 82 |
if motionManager.isGyroAvailable { |
| 83 |
csoundMotion.enableGyroscope() |
| 84 |
motionManager.gyroUpdateInterval = 0.1 |
| 85 |
motionManager.startGyroUpdates(to: .main, withHandler: { [unowned self] (data: CMGyroData?, error: Error?) in |
| 86 |
if data?.rotationRate != nil { |
| 87 |
self.gyroX.text = String(format: "%.3f", (data?.rotationRate.x)!) |
| 88 |
self.gyroY.text = String(format: "%.3f", (data?.rotationRate.y)!) |
| 89 |
self.gyroZ.text = String(format: "%.3f", (data?.rotationRate.z)!) |
| 90 |
} |
| 91 |
}) |
| 92 |
} |
| 93 |
|
| 94 |
// Enable attitude |
| 95 |
if motionManager.isDeviceMotionAvailable { |
| 96 |
csoundMotion.enableAttitude() |
| 97 |
motionManager.accelerometerUpdateInterval = 0.1 |
| 98 |
motionManager.startDeviceMotionUpdates(to: .main, withHandler: { [unowned self] (data: CMDeviceMotion?, error: Error?) in |
| 99 |
if data?.attitude != nil { |
| 100 |
self.roll.text = String(format: "%.3f", (data?.attitude.roll)!) |
| 101 |
self.pitch.text = String(format: "%.3f", (data?.attitude.pitch)!) |
| 102 |
self.yaw.text = String(format: "%.3f", (data?.attitude.yaw)!) |
| 103 |
} |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
csound.play(tempFile) |
| 108 |
} else { |
| 109 |
csound.stop() |
| 110 |
|
| 111 |
// Stop sensor updates |
| 112 |
if motionManager == csoundMotion.motionManager { |
| 113 |
motionManager.stopAccelerometerUpdates() |
| 114 |
motionManager.stopGyroUpdates() |
| 115 |
motionManager.stopDeviceMotionUpdates() |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
override func didReceiveMemoryWarning() { |
| 121 |
super.didReceiveMemoryWarning() |
| 122 |
// Dispose of any resources that can be recreated. |
| 123 |
} |
| 124 |
|
| 125 |
@IBAction func showInfo(_ sender: UIButton) { |
| 126 |
infoVC.preferredContentSize = CGSize(width: 300, height: 220) |
| 127 |
infoText = "Hardware: Motion Control shows how to use the device's motion sensor data as a set of controllers for Csound, and also displays this data in a set of UILabels. Accelerometer X controls oscillator frequency, Attitude: Yaw controls filter cutoff, Attitude: Pitch controls amplitude, and Attitude: Roll controls filter resonance." |
| 128 |
displayInfo(sender) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
extension HardwareTestViewController: CsoundObjListener { |
| 133 |
func csoundObjCompleted(_ csoundObj: CsoundObj!) { |
| 134 |
DispatchQueue.main.async { [unowned self] in |
| 135 |
self.mSwitch.isOn = false |
| 136 |
} |
| 137 |
} |
| 138 |
} |