Debian Codegraph @

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

raw
1 /*
2
3 TrappedGeneratorViewController.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 AVFoundation
29
30 class TrappedGeneratorViewController: BaseCsoundViewController {
31
32 var hasRendered = false
33 var localFileURL: URL?
34 var player: AVAudioPlayer?
35
36 override func viewDidLoad() {
37 title = "06. Render: Trapped in Convert"
38 super.viewDidLoad()
39 }
40
41 // Render Trapped in Convert, using trapped.csd, to a file
42 @IBAction func generateTrappedToDocumentsFolder(_ sender: UIButton) {
43 let csdPath = Bundle.main.path(forResource: "trapped", ofType: "csd")
44 let docsDirURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
45 localFileURL = docsDirURL.appendingPathComponent("trapped.wav")
46
47 csound.stop()
48 csound = CsoundObj()
49 csound.add(self)
50 // Render offline (there are other methods to record in realtime)
51 if localFileURL != nil {
52 csound.record(csdPath, toFile: localFileURL!.path)
53 }
54 }
55
56 override func didReceiveMemoryWarning() {
57 super.didReceiveMemoryWarning()
58 // Dispose of any resources that can be recreated.
59 }
60
61 @IBAction func play(_ sender: UIButton) {
62 if hasRendered {
63 if localFileURL != nil {
64 do {
65 player = try AVAudioPlayer(contentsOf: localFileURL!)
66 } catch {
67 print(error.localizedDescription)
68 }
69 }
70
71 player?.prepareToPlay()
72 player?.play()
73 } else {
74 let alert = UIAlertController(title: "Not Rendered", message: "Please render the file before playing it.", preferredStyle: .alert)
75 let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
76 alert.addAction(defaultAction)
77
78 present(alert, animated: true, completion: nil)
79 }
80 }
81
82 @IBAction func stop(_ sender: UIButton) {
83 if player != nil {
84 if player!.isPlaying {
85 player!.stop()
86 }
87 }
88 }
89
90 @IBAction func showInfo(_ sender: UIButton) {
91 infoVC.preferredContentSize = CGSize(width: 300, height: 140)
92 infoText = "This example demonstrates creating a virtual Csound console, and implementing a method to update this virtual console with information accessed from CsoundObj."
93 displayInfo(sender)
94 }
95 }
96
97 extension TrappedGeneratorViewController: CsoundObjListener {
98 func csoundObjCompleted(_ csoundObj: CsoundObj!) {
99 // Display a UIAlertController to the user notifying them of render completion
100 let alert = UIAlertController(title: "Render Complete", message: "File generated as trapped.wav in application Documents Folder.", preferredStyle: .alert)
101 let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
102 alert.addAction(defaultAction)
103
104 present(alert, animated: true, completion: nil)
105 hasRendered = true
106 }
107 }