-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
RecordingViewController.swift
106 lines (89 loc) · 3.55 KB
/
RecordingViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import Foundation
import AVFoundation
import UIKit
import DSWaveformImage
import DSWaveformImageViews
class RecordingViewController: UIViewController {
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var waveformView: WaveformLiveView!
@IBOutlet weak var styleSelector: UISegmentedControl!
private let audioManager: SCAudioManager!
private let imageDrawer: WaveformImageDrawer!
required init?(coder: NSCoder) {
audioManager = SCAudioManager()
imageDrawer = WaveformImageDrawer()
super.init(coder: coder)
audioManager.recordingDelegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
waveformView.configuration = waveformView.configuration.with(
style: styleForSelection(index: styleSelector.selectedSegmentIndex)
)
audioManager.prepareAudioRecording()
}
@IBAction func didChangeStyle(_ sender: UISegmentedControl) {
waveformView.configuration = waveformView.configuration.with(
style: styleForSelection(index: sender.selectedSegmentIndex)
)
}
@IBAction func didChangeSilence(_ sender: UISwitch) {
waveformView.shouldDrawSilencePadding = sender.isOn
}
@IBAction func didChangeDampingPercentage(_ sender: UISlider) {
waveformView.configuration = waveformView.configuration.with(
damping: waveformView.configuration.damping?.with(percentage: sender.value)
)
}
@IBAction func didChangeDampingSides(_ sender: UISegmentedControl) {
waveformView.configuration = waveformView.configuration.with(
damping: waveformView.configuration.damping?.with(
sides: sideForSelection(index: sender.selectedSegmentIndex)
)
)
}
@IBAction func didTapRecording() {
if audioManager.recording() {
audioManager.stopRecording()
recordButton.setTitle("Start Recording", for: .normal)
} else {
waveformView.reset()
audioManager.startRecording()
recordButton.setTitle("Stop Recording", for: .normal)
}
}
private func styleForSelection(index: Int) -> Waveform.Style {
switch index {
case 0: return .filled(.red)
case 1: return .gradient([.red, .yellow])
case 2: return .striped(.init(color: .red, width: 3, spacing: 3))
default: fatalError()
}
}
private func sideForSelection(index: Int) -> Waveform.Damping.Sides {
switch index {
case 0: return .left
case 1: return .right
case 2: return .both
default: fatalError()
}
}
}
extension RecordingViewController: RecordingDelegate {
func audioManager(_ manager: SCAudioManager!, didAllowRecording success: Bool) {
if !success {
preconditionFailure("Recording must be allowed in Settings to work.")
}
}
func audioManager(_ manager: SCAudioManager!, didFinishRecordingSuccessfully success: Bool) {
print("did finish recording with success=\(success)")
recordButton.setTitle("Start Recording", for: .normal)
}
func audioManager(_ manager: SCAudioManager!, didUpdateRecordProgress progress: CGFloat) {
print("current power: \(manager.lastAveragePower()) dB")
let linear = 1 - pow(10, manager.lastAveragePower() / 20)
// Here we add the same sample 3 times to speed up the animation.
// Usually you'd just add the sample once.
waveformView.add(samples: [linear, linear, linear])
}
}