forked from bitgapp/eqMac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEQMControl.swift
More file actions
409 lines (370 loc) · 16.3 KB
/
Copy pathEQMControl.swift
File metadata and controls
409 lines (370 loc) · 16.3 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//
// EQMControl.swift
// eqMac
//
// Created by Nodeful on 15/08/2021.
// Copyright © 2021 Bitgapp. All rights reserved.
//
import Foundation
import CoreAudio.AudioServerPlugIn
import Shared
class EQMControl: EQMObject {
static var volume: Float32 = 1
static var muted = false
static func hasProperty (objectID: AudioObjectID?, address: AudioObjectPropertyAddress) -> Bool {
switch(objectID) {
case kObjectID_Volume_Output_Master:
switch(address.mSelector) {
case kAudioObjectPropertyBaseClass,
kAudioObjectPropertyClass,
kAudioObjectPropertyOwner,
kAudioObjectPropertyOwnedObjects,
kAudioControlPropertyScope,
kAudioControlPropertyElement,
kAudioLevelControlPropertyScalarValue,
kAudioLevelControlPropertyDecibelValue,
kAudioLevelControlPropertyDecibelRange,
kAudioLevelControlPropertyConvertScalarToDecibels,
kAudioLevelControlPropertyConvertDecibelsToScalar:
return true
default: return false
}
case kObjectID_Mute_Output_Master:
switch(address.mSelector) {
case kAudioObjectPropertyBaseClass,
kAudioObjectPropertyClass,
kAudioObjectPropertyOwner,
kAudioObjectPropertyOwnedObjects,
kAudioControlPropertyScope,
kAudioControlPropertyElement,
kAudioBooleanControlPropertyValue:
return true
default: return false
}
case kObjectID_DataSource_Output_Master:
switch(address.mSelector) {
case kAudioObjectPropertyBaseClass,
kAudioObjectPropertyClass,
kAudioObjectPropertyOwner,
kAudioObjectPropertyOwnedObjects,
kAudioControlPropertyScope,
kAudioControlPropertyElement,
kAudioSelectorControlPropertyCurrentItem,
kAudioSelectorControlPropertyAvailableItems,
kAudioSelectorControlPropertyItemName:
return true
default: return false
}
default: return false
}
}
static func isPropertySettable (objectID: AudioObjectID?, address: AudioObjectPropertyAddress) -> Bool {
switch(objectID) {
case kObjectID_Volume_Output_Master:
switch(address.mSelector) {
case kAudioLevelControlPropertyScalarValue,
kAudioLevelControlPropertyDecibelValue:
return true
default: return false
}
case kObjectID_Mute_Output_Master:
switch(address.mSelector) {
case kAudioBooleanControlPropertyValue:
return true
default: return false
}
case kObjectID_DataSource_Output_Master:
switch(address.mSelector) {
case kAudioSelectorControlPropertyCurrentItem:
return true
default: return false
}
default: return false
}
}
static func getPropertyDataSize (objectID: AudioObjectID?, address: AudioObjectPropertyAddress) -> UInt32? {
switch objectID {
case kObjectID_Volume_Output_Master:
switch address.mSelector {
case kAudioObjectPropertyBaseClass: return Memory.sizeof(AudioClassID.self)
case kAudioObjectPropertyClass: return Memory.sizeof(AudioClassID.self)
case kAudioObjectPropertyOwner: return Memory.sizeof(AudioObjectID.self)
case kAudioObjectPropertyOwnedObjects: return 0 * Memory.sizeof(AudioObjectID.self)
case kAudioControlPropertyScope: return Memory.sizeof(AudioObjectPropertyScope.self)
case kAudioControlPropertyElement: return Memory.sizeof(AudioObjectPropertyElement.self)
case kAudioLevelControlPropertyScalarValue: return Memory.sizeof(Float32.self)
case kAudioLevelControlPropertyDecibelValue: return Memory.sizeof(Float32.self)
case kAudioLevelControlPropertyDecibelRange: return Memory.sizeof(AudioValueRange.self)
case kAudioLevelControlPropertyConvertScalarToDecibels: return Memory.sizeof(Float32.self)
case kAudioLevelControlPropertyConvertDecibelsToScalar: return Memory.sizeof(Float32.self)
default:
return nil
}
case kObjectID_Mute_Output_Master:
switch address.mSelector {
case kAudioObjectPropertyBaseClass: return Memory.sizeof(AudioClassID.self)
case kAudioObjectPropertyClass: return Memory.sizeof(AudioClassID.self)
case kAudioObjectPropertyOwner: return Memory.sizeof(AudioObjectID.self)
case kAudioObjectPropertyOwnedObjects: return 0 * Memory.sizeof(AudioObjectID.self)
case kAudioControlPropertyScope: return Memory.sizeof(AudioObjectPropertyScope.self)
case kAudioControlPropertyElement: return Memory.sizeof(AudioObjectPropertyElement.self)
case kAudioBooleanControlPropertyValue: return Memory.sizeof(UInt32.self)
default:
return nil
}
case kObjectID_DataSource_Output_Master:
switch address.mSelector {
case kAudioObjectPropertyBaseClass: return Memory.sizeof(AudioClassID.self)
case kAudioObjectPropertyClass: return Memory.sizeof(AudioClassID.self)
case kAudioObjectPropertyOwner: return Memory.sizeof(AudioObjectID.self)
case kAudioObjectPropertyOwnedObjects: return 0 * Memory.sizeof(AudioObjectID.self)
case kAudioControlPropertyScope: return Memory.sizeof(AudioObjectPropertyScope.self)
case kAudioControlPropertyElement: return Memory.sizeof(AudioObjectPropertyElement.self)
case kAudioSelectorControlPropertyCurrentItem: return Memory.sizeof(UInt32.self)
case kAudioSelectorControlPropertyAvailableItems: return kDataSource_NumberItems * Memory.sizeof(UInt32.self)
case kAudioSelectorControlPropertyItemName: return Memory.sizeof(CFString.self)
default:
return nil
}
default:
return nil
}
}
static func getPropertyData (objectID: AudioObjectID?, address: AudioObjectPropertyAddress, inData: UnsafeRawPointer?) -> EQMObjectProperty? {
switch objectID {
case kObjectID_Volume_Output_Master:
switch address.mSelector {
case kAudioObjectPropertyBaseClass:
// The base class for kAudioVolumeControlClassID is kAudioLevelControlClassID
return .audioClassID(kAudioLevelControlClassID)
case kAudioObjectPropertyClass:
// Volume controls are of the class, kAudioVolumeControlClassID
return .audioClassID(kAudioVolumeControlClassID)
case kAudioObjectPropertyOwner:
// The control's owner is the device object
return .audioObjectID(kObjectID_Device)
case kAudioObjectPropertyOwnedObjects:
// Controls do not own any objects
return .objectIDList([])
case kAudioControlPropertyScope:
// This property returns the scope that the control is attached to.
switch objectID {
case kObjectID_Volume_Output_Master: return .scope(kAudioObjectPropertyScopeOutput)
default: return .scope(kAudioObjectPropertyScopeGlobal)
}
case kAudioControlPropertyElement:
// This property returns the element that the control is attached to.
return .element(kAudioObjectPropertyElementMaster)
case kAudioLevelControlPropertyScalarValue:
// This returns the value of the control in the normalized range of 0 to 1.
// Note that we need to take the state lock to examine the value.
let volume = ({ () -> Float32 in
switch objectID {
case kObjectID_Volume_Output_Master: return self.volume
default: return 0
}
})()
return .float32(VolumeConverter.toScalar(volume))
case kAudioLevelControlPropertyDecibelValue:
// This returns the dB value of the control.
// Note that we need to take the state lock to examine the value.
let volume = ({ () -> Float32 in
switch objectID {
case kObjectID_Volume_Output_Master: return self.volume
default: return 0
}
})()
return .float32(VolumeConverter.toDecibel(volume))
case kAudioLevelControlPropertyDecibelRange:
// This returns the dB range of the control.
return .valueRange(
AudioValueRange(
mMinimum: Float64(kMinVolumeDB),
mMaximum: Float64(kMaxVolumeDB)
)
)
case kAudioLevelControlPropertyConvertScalarToDecibels:
// This takes the scalar value in outData and converts it to dB.
guard var scalar = inData?.assumingMemoryBound(to: Float32?.self).pointee else {
return .float32(0)
}
scalar = scalar.clamp(min: 0, max: 1)
let decibel = VolumeConverter.toDecibel(VolumeConverter.fromScalar(scalar))
return .float32(decibel)
case kAudioLevelControlPropertyConvertDecibelsToScalar:
// This takes the dB value in outData and converts it to scalar.
guard var decibel = inData?.assumingMemoryBound(to: Float32?.self).pointee else {
return .float32(0)
}
decibel = decibel.clamp(min: kMinVolumeDB, max: kMaxVolumeDB)
let scalar = VolumeConverter.toScalar(VolumeConverter.fromDecibel(decibel))
return .float32(scalar)
default: return nil
}
case kObjectID_Mute_Output_Master:
switch address.mSelector {
case kAudioObjectPropertyBaseClass:
// The base class for kAudioMuteControlClassID is kAudioBooleanControlClassID
return .audioClassID(kAudioBooleanControlClassID)
case kAudioObjectPropertyClass:
// Mute controls are of the class, kAudioMuteControlClassID
return .audioClassID(kAudioMuteControlClassID)
case kAudioObjectPropertyOwner:
// The control's owner is the device object
return .audioObjectID(kObjectID_Device)
case kAudioObjectPropertyOwnedObjects:
// Controls do not own any objects
return .objectIDList([])
case kAudioControlPropertyScope:
// This property returns the scope that the control is attached to.
switch objectID {
case kObjectID_Mute_Output_Master: return .scope(kAudioObjectPropertyScopeOutput)
default: return .scope(kAudioObjectPropertyScopeGlobal)
}
case kAudioControlPropertyElement:
// This property returns the element that the control is attached to.
return .element(kAudioObjectPropertyElementMaster)
case kAudioBooleanControlPropertyValue:
// This returns the value of the mute control where 0 means that mute is off
// and audio can be heard and 1 means that mute is on and audio cannot be heard.
// Note that we need to take the state lock to examine this value.
let muted = ({ () -> Bool in
switch objectID {
case kObjectID_Mute_Output_Master: return self.muted
default: return false
}
})()
return .integer(muted ? 1 : 0)
default: return nil
}
case kObjectID_DataSource_Output_Master:
// case kObjectID_DataDestination_PlayThru_Master:
switch address.mSelector {
case kAudioObjectPropertyBaseClass:
// The base class for kAudioDataSourceControlClassID is kAudioSelectorControlClassID
return .audioClassID(kAudioSelectorControlClassID)
case kAudioObjectPropertyClass:
// Data Source controls are of the class, kAudioDataSourceControlClassID
return .audioClassID(kAudioDataSourceControlClassID)
case kAudioObjectPropertyOwner:
// The control's owner is the device object
return .audioObjectID(kObjectID_Device)
case kAudioObjectPropertyOwnedObjects:
// Controls do not own any objects
return .objectIDList([])
case kAudioControlPropertyScope:
// This property returns the scope that the control is attached to.
switch objectID {
case kObjectID_DataSource_Output_Master:
return .scope(kAudioObjectPropertyScopeOutput)
default: return nil
}
case kAudioControlPropertyElement:
// This property returns the element that the control is attached to.
return .element(kAudioObjectPropertyElementMaster)
case kAudioSelectorControlPropertyCurrentItem:
// This returns the value of the data source selector.
// Note that we need to take the state lock to examine this value.
return .integer(0)
case kAudioSelectorControlPropertyAvailableItems:
// This returns the IDs for all the items the data source control supports.
// Calculate the number of items that have been requested. Note that this
// number is allowed to be smaller than the actual size of the list. In such
// case, only that number of items will be returned
// eqMac only has 1 Source
return .integer(0)
case kAudioSelectorControlPropertyItemName:
// This returns the user-readable name for the selector item in the qualifier
return .string(EQMDevice.name as CFString)
default: return nil
}
default: return nil
}
}
static func setPropertyData(client: EQMClient?, objectID: AudioObjectID?, address: AudioObjectPropertyAddress, data: UnsafeRawPointer, changedProperties: inout [AudioObjectPropertyAddress]) -> OSStatus {
switch objectID {
case kObjectID_Volume_Output_Master:
switch address.mSelector {
case kAudioLevelControlPropertyScalarValue:
// For the scalar volume, we clamp the new value to [0, 1]. Note that if this
// value changes, it implies that the dB value changed too.
let scalar = data.load(as: Float32.self)
var newVolume = VolumeConverter.fromScalar(scalar)
newVolume = newVolume.clamp(min: 0.0, max: 1.0)
if volume != newVolume {
volume = newVolume
changedProperties.append(
AudioObjectPropertyAddress(
mSelector: kAudioLevelControlPropertyScalarValue,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMaster
)
)
changedProperties.append(
AudioObjectPropertyAddress(
mSelector: kAudioLevelControlPropertyDecibelValue,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMaster
)
)
}
return noErr
case kAudioLevelControlPropertyDecibelValue:
// For the dB value, we first convert it to a scalar value since that is how
// the value is tracked. Note that if this value changes, it implies that the
// scalar value changes as well.
var decibel = data.load(as: Float32.self)
decibel = decibel.clamp(min: kMinVolumeDB, max: kMaxVolumeDB)
var newVolume = VolumeConverter.fromDecibel(decibel)
newVolume = newVolume.clamp(min: 0.0, max: 1.0)
if volume != newVolume {
volume = newVolume
changedProperties.append(
AudioObjectPropertyAddress(
mSelector: kAudioLevelControlPropertyScalarValue,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMaster
)
)
changedProperties.append(
AudioObjectPropertyAddress(
mSelector: kAudioLevelControlPropertyDecibelValue,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMaster
)
)
}
return noErr
default: return kAudioHardwareUnknownPropertyError
}
case kObjectID_Mute_Output_Master:
switch address.mSelector {
case kAudioBooleanControlPropertyValue:
let mutedInt = data.load(as: UInt32.self)
let newMuted = mutedInt == 1
if (muted != newMuted) {
muted = newMuted
changedProperties.append(
AudioObjectPropertyAddress(
mSelector: kAudioBooleanControlPropertyValue,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMaster
)
)
}
return noErr
default: return kAudioHardwareUnknownPropertyError
}
case kObjectID_DataSource_Output_Master:
// case kObjectID_DataDestination_PlayThru_Master:
switch address.mSelector {
case kAudioSelectorControlPropertyCurrentItem:
// For selector controls, we check to make sure the requested value is in the
// available items list and just store the value.
return noErr
default: return kAudioHardwareUnknownPropertyError;
}
default: return kAudioHardwareBadObjectError
}
}
}