| 1 |
/* |
| 2 |
|
| 3 |
RecordTestViewController.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 // For AVAudioPlayer |
| 29 |
|
| 30 |
class RecordTestViewController: BaseCsoundViewController { |
| 31 |
|
| 32 |
@IBOutlet var mSwitch: UISwitch! |
| 33 |
@IBOutlet var mGainSlider: UISlider! |
| 34 |
@IBOutlet var mGainLabel: UILabel! |
| 35 |
@IBOutlet var mLevelMeter: LevelMeterView! |
| 36 |
@IBOutlet var mPlayButton: UIButton! |
| 37 |
|
| 38 |
var hasRecorded = false |
| 39 |
var mPlayer = AVAudioPlayer() // To play recorded file |
| 40 |
|
| 41 |
override func viewDidLoad() { |
| 42 |
title = "13. Mic: Recording" |
| 43 |
super.viewDidLoad() |
| 44 |
} |
| 45 |
|
| 46 |
@IBAction func toggleOnOff(_ sender: UISwitch) { |
| 47 |
if sender.isOn { |
| 48 |
let tempFile = Bundle.main.path(forResource: "recordTest", ofType: "csd") |
| 49 |
|
| 50 |
csound.stop() |
| 51 |
csound = CsoundObj() |
| 52 |
csound.useAudioInput = true // We must set this property of CsoundObj to true to use audio input |
| 53 |
csound.add(self) |
| 54 |
|
| 55 |
let csoundUI = CsoundUI(csoundObj: csound) |
| 56 |
csoundUI?.add(mGainSlider, forChannelName: "gain") |
| 57 |
|
| 58 |
mLevelMeter.add(to: csound, for: "meter") |
| 59 |
csound.play(tempFile) |
| 60 |
} else { |
| 61 |
csound.stopRecording() |
| 62 |
csound.stop() |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
@IBAction func changeGain(_ sender: UISlider) { |
| 67 |
mGainLabel.text = String(format: "%.2f", sender.value) |
| 68 |
} |
| 69 |
|
| 70 |
@IBAction func play(_ sender: UIButton) { |
| 71 |
if hasRecorded { |
| 72 |
mPlayer.prepareToPlay() |
| 73 |
mPlayer.play() |
| 74 |
sender.removeTarget(self, action: #selector(play(_:)), for: .touchUpInside) |
| 75 |
sender.addTarget(self, action: #selector(stop(_:)), for: .touchUpInside) |
| 76 |
sender.setTitle("Stop", for: .normal) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
@IBAction func stop(_ sender: UIButton) { |
| 81 |
mPlayer.stop() |
| 82 |
mPlayer.currentTime = 0 |
| 83 |
sender.removeTarget(self, action: #selector(stop(_:)), for: .touchUpInside) |
| 84 |
sender.addTarget(self, action: #selector(play(_:)), for: .touchUpInside) |
| 85 |
sender.setTitle("Play", for: .normal) |
| 86 |
} |
| 87 |
|
| 88 |
override func didReceiveMemoryWarning() { |
| 89 |
super.didReceiveMemoryWarning() |
| 90 |
// Dispose of any resources that can be recreated. |
| 91 |
} |
| 92 |
|
| 93 |
func recordingURL() -> URL { |
| 94 |
let docDirPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] |
| 95 |
return docDirPath.appendingPathComponent("recording.wav") |
| 96 |
} |
| 97 |
|
| 98 |
@IBAction func showInfo(_ sender: UIButton) { |
| 99 |
infoVC.preferredContentSize = CGSize(width: 300, height: 120) |
| 100 |
infoText = "This example uses a custom level-meter widget, and demonstrates efficient use of concurrency for an audio application." |
| 101 |
displayInfo(sender) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
extension RecordTestViewController: CsoundObjListener { |
| 106 |
func csoundObjStarted(_ csoundObj: CsoundObj!) { |
| 107 |
csound.record(to: recordingURL()) |
| 108 |
} |
| 109 |
|
| 110 |
func csoundObjCompleted(_ csoundObj: CsoundObj!) { |
| 111 |
DispatchQueue.main.async { [unowned self] in |
| 112 |
self.mSwitch.isOn = false |
| 113 |
} |
| 114 |
|
| 115 |
do { |
| 116 |
mPlayer = try AVAudioPlayer(contentsOf: recordingURL()) |
| 117 |
} catch { |
| 118 |
print(error.localizedDescription) |
| 119 |
} |
| 120 |
|
| 121 |
mPlayer.delegate = self |
| 122 |
hasRecorded = true |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
extension RecordTestViewController: AVAudioPlayerDelegate { |
| 127 |
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { |
| 128 |
stop(mPlayButton) |
| 129 |
} |
| 130 |
} |