Debian Codegraph @

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

raw
1 /*
2
3 ConsoleOutputViewController.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 ConsoleOutputViewController: BaseCsoundViewController {
30
31 @IBOutlet var renderButton: UIButton!
32 @IBOutlet var mTextView: UITextView!
33
34 var string: String?
35 var csdTable = UITableView()
36
37 fileprivate var csdArray: [String]?
38 fileprivate var csdPath: String?
39 private var folderPath: String?
40 private var csdListVC: UIViewController?
41
42 // This method is called by CsoundObj() to output console messages
43 @objc func messageCallback(_ infoObj: NSValue) {
44 var info = Message() // Create instance of Message (a C Struct)
45 infoObj.getValue(&info) // Store the infoObj value in Message
46 let message = UnsafeMutablePointer<Int8>.allocate(capacity: 1024) // Create an empty C-String
47 let va_ptr: CVaListPointer = CVaListPointer(_fromUnsafeMutablePointer: &(info.valist)) // Get reference to variable argument list
48 vsnprintf(message, 1024, info.format, va_ptr) // Store in our C-String
49 let output = String(cString: message) // Create String object with C-String
50
51 DispatchQueue.main.async { [unowned self] in
52 self.updateUIWithNewMessage(output) // Update UI on main thread
53 }
54 }
55
56 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
57 csdPath = folderPath?.appending((tableView.cellForRow(at: indexPath)?.textLabel?.text)!)
58 csdListVC?.dismiss(animated: true, completion: nil)
59 }
60
61 // MARK: IBActions
62 @IBAction func run(_ sender: UIButton) {
63 if sender.isSelected == false {
64 mTextView.text = ""
65 sender.isSelected = true
66 csound.stop()
67 csound = CsoundObj()
68 csound.setMessageCallback(#selector(messageCallback(_:)), withListener: self)
69 csound.add(self)
70 if csdPath != nil {
71 csound.play(csdPath)
72 }
73 } else {
74 csound.stop()
75 sender.isSelected = false
76 }
77 }
78
79 @IBAction func showCSDs(_ sender: UIButton) {
80 csdListVC = UIViewController()
81 csdListVC?.modalPresentationStyle = .popover
82
83 let popover = csdListVC?.popoverPresentationController
84 popover?.sourceView = sender
85 popover?.sourceRect = sender.bounds
86 csdListVC?.preferredContentSize = CGSize(width: 300, height: 600)
87
88 csdTable = UITableView(frame: CGRect(x: 0, y: 0, width: (csdListVC?.preferredContentSize.width)!, height:
89 (csdListVC?.preferredContentSize.height)!))
90 csdTable.delegate = self
91 csdTable.dataSource = self
92 csdListVC?.view.addSubview(csdTable)
93
94 popover?.delegate = self
95 if csdListVC != nil {
96 present(csdListVC!, animated: true, completion: nil)
97 }
98 }
99
100 @IBAction func showInfo(_ sender: UIButton) {
101 infoVC.preferredContentSize = CGSize(width: 300, height: 160)
102 infoText = "Renders, by default, a simple 5-second 'countdown' csd and publishes information to a virtual Csound console every second. Click the CSD button on the right to select a different csd file."
103 displayInfo(sender)
104 }
105
106 override func viewDidLoad() {
107 folderPath = Bundle.main.resourcePath?.appending("/csdStorage/")
108
109 if folderPath != nil {
110 do {
111 csdArray = try FileManager.default.contentsOfDirectory(atPath: folderPath!)
112 } catch {
113 print(error.localizedDescription)
114 }
115 }
116 csdPath = Bundle.main.path(forResource: "TextOnly - Countdown", ofType: "csd", inDirectory: "csdStorage")
117 super.viewDidLoad()
118 }
119
120 func updateUIWithNewMessage(_ newMessage: String) {
121 mTextView.insertText(newMessage)
122 }
123
124 override func didReceiveMemoryWarning() {
125 super.didReceiveMemoryWarning()
126 // Dispose of any resources that can be recreated.
127 }
128
129 }
130
131 extension ConsoleOutputViewController: CsoundObjListener {
132 func csoundObjCompleted(_ csoundObj: CsoundObj!) {
133 DispatchQueue.main.async { [unowned self] in
134 self.renderButton.isSelected = false
135 }
136 }
137 }
138
139 // MARK: TableView delegate/data source methods
140 extension ConsoleOutputViewController: UITableViewDelegate, UITableViewDataSource {
141 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
142 if csdArray != nil {
143 return csdArray!.count
144 } else {
145 return 0
146 }
147 }
148
149 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
150 tableView.dequeueReusableCell(withIdentifier: "Cell")
151 let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
152 if csdArray != nil {
153 cell.textLabel?.text = csdArray?[indexPath.row]
154 }
155 return cell
156 }
157 }