Debian Codegraph @

iOS/Csound iOS Swift Examples/Csound iOS SwiftExamples/SimpleTest1ViewController.swift

raw
1 /*
2
3 SimpleTest1ViewController.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 SimpleTest1ViewController: BaseCsoundViewController {
30
31 @IBOutlet var uiSlider: UISlider!
32 @IBOutlet var uiSwitch: UISwitch!
33 @IBOutlet var uiLabel: UILabel!
34
35 override func viewDidLoad() {
36 title = "01. Simple Test 1"
37 super.viewDidLoad()
38 }
39
40 override func didReceiveMemoryWarning() {
41 super.didReceiveMemoryWarning()
42 }
43
44 @IBAction func toggleOnOff(_ sender: UISwitch) {
45 print("Status: \(sender.isOn)")
46
47 if sender.isOn {
48 let csdFile = Bundle.main.path(forResource: "test", ofType: "csd")
49
50 csound = CsoundObj()
51 csound.add(self) // Add self as a 'listener', a kind of delegate, to be notified when Csound starts and/or stops
52 let csoundUI = CsoundUI(csoundObj: csound)
53 csoundUI?.labelPrecision = 2 // Set label value display precision before adding UILabel binding
54 csoundUI?.add(uiLabel, forChannelName: "slider")
55 csoundUI?.add(uiSlider, forChannelName: "slider")
56
57 csound.play(csdFile)
58 } else {
59 csound.stop()
60 }
61 }
62
63 // Present info popover
64 @IBAction func showInfo(_ sender: UIButton) {
65 infoVC.preferredContentSize = CGSize(width: 200, height: 200)
66 infoText = "Flip the switch to begin rendering Csound. Use the slider to control pitch."
67 displayInfo(sender) // Call inherited method to display info popover after setting specifics
68 }
69 }
70
71 // CsoundObjListener method: if Csound finishes running, it will call this method
72 extension SimpleTest1ViewController: CsoundObjListener {
73 func csoundObjCompleted(_ csoundObj: CsoundObj!) {
74 DispatchQueue.main.async { [unowned self] in // Use the main thread for UI operation
75 self.uiSwitch.isOn = false // Turn the switch off
76 self.uiLabel.text = ""
77 }
78 }
79 }