-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-selected-audio-device.swift
executable file
·178 lines (139 loc) · 4.44 KB
/
get-selected-audio-device.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/swift
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Selected audio devices
// @raycast.mode inline
// @raycast.refreshTime 10m
// Optional parameters:
// @raycast.icon 🔈
// @raycast.packageName Audio
// Documentation:
// @raycast.description Shows the selected audio devices for input and output
// @raycast.author Roland Leth
// @raycast.authorURL https://runtimesharks.com
import Foundation
import CoreAudio
// Based on https://stackoverflow.com/a/58618034/793916
final class AudioDevice {
let audioDeviceID: AudioDeviceID
init(deviceID: AudioDeviceID) {
self.audioDeviceID = deviceID
}
var hasOutput: Bool {
var address: AudioObjectPropertyAddress = AudioObjectPropertyAddress(
mSelector: AudioObjectPropertySelector(kAudioDevicePropertyStreamConfiguration),
mScope: AudioObjectPropertyScope(kAudioDevicePropertyScopeOutput),
mElement: AudioObjectPropertyElement(0))
var propSize: UInt32 = 0
var result = AudioObjectGetPropertyDataSize(
audioDeviceID,
&address,
UInt32(MemoryLayout<AudioObjectPropertyAddress>.size),
nil,
&propSize)
if (result != 0) {
return false
}
let bufferList = UnsafeMutablePointer<AudioBufferList>.allocate(capacity: Int(propSize))
defer {
bufferList.deallocate()
}
result = AudioObjectGetPropertyData(audioDeviceID, &address, 0, nil, &propSize, bufferList)
if (result != 0) {
return false
}
let buffers = UnsafeMutableAudioBufferListPointer(bufferList)
return buffers.contains { $0.mNumberChannels > 0 }
}
var name: String? {
var address = AudioObjectPropertyAddress(
mSelector: AudioObjectPropertySelector(kAudioDevicePropertyDeviceNameCFString),
mScope: AudioObjectPropertyScope(kAudioObjectPropertyScopeGlobal),
mElement: AudioObjectPropertyElement(kAudioObjectPropertyElementMain))
var name: CFString? = nil
var propSize = UInt32(MemoryLayout<CFString?>.size)
let result = AudioObjectGetPropertyData(audioDeviceID, &address, 0, nil, &propSize, &name)
if (result != 0) {
return nil
}
return name as String?
}
}
func findDevices() -> [AudioDevice] {
var address = AudioObjectPropertyAddress(
mSelector: AudioObjectPropertySelector(kAudioHardwarePropertyDevices),
mScope: AudioObjectPropertyScope(kAudioObjectPropertyScopeGlobal),
mElement: AudioObjectPropertyElement(kAudioObjectPropertyElementMain))
var propSize: UInt32 = 0
var result = AudioObjectGetPropertyDataSize(
AudioObjectID(kAudioObjectSystemObject),
&address,
UInt32(MemoryLayout<AudioObjectPropertyAddress>.size),
nil,
&propSize)
if (result != 0) {
print("Error \(result) from AudioObjectGetPropertyDataSize")
return []
}
let numDevices = Int(propSize / UInt32(MemoryLayout<AudioDeviceID>.size))
var devids = Array<AudioDeviceID>(repeating: AudioDeviceID(), count: numDevices)
result = AudioObjectGetPropertyData(
AudioObjectID(kAudioObjectSystemObject),
&address,
0,
nil,
&propSize,
&devids)
if (result != 0) {
print("Error \(result) from AudioObjectGetPropertyData")
return []
}
return (0..<numDevices).compactMap { i in
AudioDevice(deviceID: devids[i])
}
}
let devices = findDevices()
let inputs = devices.filter { !$0.hasOutput }
let outputs = devices.filter { $0.hasOutput }
func selectedDevice(output: Bool) -> String? {
var id = AudioObjectID(kAudioObjectSystemObject)
var idSize = UInt32(MemoryLayout.size(ofValue: id))
let selector = output
? kAudioHardwarePropertyDefaultOutputDevice
: kAudioHardwarePropertyDefaultInputDevice
var idPropertyAddress = AudioObjectPropertyAddress(
mSelector: AudioObjectPropertySelector(selector),
mScope: AudioObjectPropertyScope(kAudioObjectPropertyScopeGlobal),
mElement: AudioObjectPropertyElement(kAudioObjectPropertyElementMain))
let result = AudioObjectGetPropertyData(
id,
&idPropertyAddress,
0,
nil,
&idSize,
&id)
if (result != 0) {
return nil
}
return (output ? outputs : inputs).first { $0.audioDeviceID == id }?.name
}
let outputDevice = selectedDevice(output: true)
let inputDevice = selectedDevice(output: false)
let inputName = inputs
.compactMap(\.name)
.filter { inputDevice == $0 }
.first
let outputName = outputs
.compactMap(\.name)
.filter { outputDevice == $0 }
.first
switch (inputName, outputName) {
case (let inputName?, let outputName?):
print("\(inputName) | \(outputName)")
case (let inputName?, nil):
print("\(inputName)")
case (nil, let outputName?):
print("\(outputName)")
case (nil, nil):
print("No devices found")
}