-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
ChannelDeviceRouting.swift
113 lines (107 loc) · 3.9 KB
/
ChannelDeviceRouting.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
107
108
109
110
111
112
113
import AudioKit
import AudioKitEX
import AudioKitUI
import AVFoundation
import SwiftUI
class ChannelDeviceRoutingConductor: ObservableObject, HasAudioEngine {
let engine = AudioEngine()
var input: AudioEngine.InputNode?
let inputDevices = Settings.session.availableInputs
var inputDeviceList = [String]()
init() {
do {
try Settings.setSession(category: .playAndRecord,
with: [.defaultToSpeaker,
.mixWithOthers,
.allowBluetooth,
.allowBluetoothA2DP])
try Settings.session.setActive(true)
} catch let err {
Log(err.localizedDescription)
}
if let input = engine.input {
self.input = input
if let inputAudio = self.input {
engine.output = Mixer(inputAudio)
}
} else {
input = nil
engine.output = Mixer()
}
if let existingInputs = inputDevices {
for device in existingInputs {
inputDeviceList.append(device.portName)
}
}
}
func switchInput(number: Int?) {
stop()
if let inputs = Settings.session.availableInputs {
let newInput = inputs[number ?? 0]
do {
try Settings.session.setPreferredInput(newInput)
try Settings.session.setActive(true)
} catch let err {
Log(err)
}
}
}
}
struct ChannelDeviceRoutingView: View {
@StateObject var conductor = ChannelDeviceRoutingConductor()
@State var isPlaying = false
@State var inputDevice: String = ""
@State private var showingAlert = false
@State private var headphonesIn = Settings.headPhonesPlugged
var body: some View {
VStack {
Text("Input Devices")
.font(.largeTitle)
Picker("Input Device", selection: $inputDevice) {
ForEach(conductor.inputDeviceList, id: \.self) { input in
Text(input).tag(input)
}
}
.frame(width: 100, height: 200, alignment: .center)
.onChange(of: inputDevice) { _ in
let index = Int(inputDevice)
conductor.switchInput(number: index)
}
Button(action: {
if isPlaying {
self.isPlaying ? conductor.stop() : conductor.start()
self.isPlaying.toggle()
} else {
if headphonesIn {
self.isPlaying ? conductor.stop() : conductor.start()
self.isPlaying.toggle()
showingAlert = false
} else {
showingAlert = true
}
}
}, label: {
Image(systemName: isPlaying ? "mic.circle.fill" : "mic.circle")
.resizable()
.frame(minWidth: 25,
idealWidth: 50,
maxWidth: 100,
minHeight: 25,
idealHeight: 50,
maxHeight: 100,
alignment: .center)
.foregroundColor(.primary)
})
.keyboardShortcut(.space, modifiers: [])
}
.cookbookNavBarTitle("Channel/Device Routing")
.alert(isPresented: $showingAlert) {
Alert(title: Text("Warning: Check your levels!"),
message: Text("Audio feedback may occur!"),
dismissButton: .destructive(Text("Proceed"), action: {
self.isPlaying ? conductor.stop() : conductor.start()
self.isPlaying.toggle()
}))
}
}
}