-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_AudioEndpointVolume.au3
More file actions
561 lines (534 loc) · 22.2 KB
/
_AudioEndpointVolume.au3
File metadata and controls
561 lines (534 loc) · 22.2 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#include-once
; ===============================================================================================================================
; Title: _AudioEndpointVolume
; Author: Erik Pilsits
; Version: 1.0.0.0
; ===============================================================================================================================
Global Const $CLSID_MMDeviceEnumerator = "{BCDE0395-E52F-467C-8E3D-C4579291692E}"
Global Const $IID_IMMDeviceEnumerator = "{A95664D2-9614-4F35-A746-DE8DB63617E6}"
Global Const $tagIMMDeviceEnumerator = _
"EnumAudioEndpoints hresult(int;dword;ptr*);" & _
"GetDefaultAudioEndpoint hresult(int;int;ptr*);" & _
"GetDevice hresult(wstr;ptr*);" & _
"RegisterEndpointNotificationCallback hresult(ptr);" & _
"UnregisterEndpointNotificationCallback hresult(ptr)"
Global Const $IID_IMMDevice = "{D666063F-1587-4E43-81F1-B948E807363F}"
Global Const $tagIMMDevice = _
"Activate hresult(struct*;dword;ptr;ptr*);" & _
"OpenPropertyStore hresult(dword;ptr*);" & _
"GetId hresult(wstr*);" & _
"GetState hresult(dword*)"
Global Const $eRender = 0, $eConsole = 0
Global Const $IID_IAudioEndpointVolume = "{5CDF2C82-841E-4546-9722-0CF74078229A}"
Global Const $tagIAudioEndpointVolume = _
"RegisterControlChangeNotify hresult(ptr);" & _
"UnregisterControlChangeNotify hresult(ptr);" & _
"GetChannelCount hresult(uint*);" & _
"SetMasterVolumeLevel hresult(float;ptr);" & _
"SetMasterVolumeLevelScalar hresult(float;ptr);" & _
"GetMasterVolumeLevel hresult(float*);" & _
"GetMasterVolumeLevelScalar hresult(float*);" & _
"SetChannelVolumeLevel hresult(uint;float;ptr);" & _
"SetChannelVolumeLevelScalar hresult(uint;float;ptr);" & _
"GetChannelVolumeLevel hresult(uint;float*);" & _
"GetChannelVolumeLevelScalar hresult(uint;float*);" & _
"SetMute hresult(int;ptr);" & _
"GetMute hresult(int*);" & _
"GetVolumeStepInfo hresult(uint*;uint*);" & _
"VolumeStepUp hresult(ptr);" & _
"VolumeStepDown hresult(ptr);" & _
"QueryHardwareSupport hresult(dword*);" & _
"GetVolumeRange hresult(float*;float*;float*)"
Global Const $CLSCTX_INPROC_SERVER = 1
Global $__g_oEndpointVolume = 0
OnAutoItExitRegister("_EndpointVolume_Close")
; #FUNCTION# ====================================================================================================================
; Name ..........: _EndpointVolume_Init
; Description ...: Create global endpoint volume object used by other functions
; Syntax ........: _EndpointVolume_Init()
; Parameters ....:
; Return values .: Failure - Sets @error
; | 1 - Failed to create IMMDeviceEnumerator interface
; | 2 - Failed to get default audio endpoint interface pointer
; | 3 - Failed to create IMMDevice interface
; | 4 - Failed to get endpoint volume interface pointer
; | 5 - Failed to create IAudioEndpointVolume interface
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......: Calling a function before calling Init will automatically call Init. In this case if an error occurs,
; @error is set to -1. Explicitly call Init to get the actual @error code.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _EndpointVolume_Init()
If IsObj($__g_oEndpointVolume) Then Return
$__g_oEndpointVolume = __GetDefaultEndpointVolume()
If @error Then Return SetError(@error)
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _EndpointVolume_Close
; Description ...: Releases global endpoint volume object
; Syntax ........: _EndpointVolume_Close()
; Parameters ....:
; Return values .: None
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _EndpointVolume_Close()
If IsObj($__g_oEndpointVolume) Then $__g_oEndpointVolume = 0
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetChannelCount
; Description ...: Get the number of channels in the default audio endpoint
; Syntax ........: _GetChannelCount()
; Parameters ....:
; Return values .: Success - Number of channels
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GetChannelCount()
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
Local $nCount
If __SUCCEEDED($__g_oEndpointVolume.GetChannelCount($nCount)) Then
Return $nCount
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _SetMasterVolumeLevel
; Description ...: Set master volume level in decibels.
; Syntax ........: _SetMasterVolumeLevel($idB)
; Parameters ....: $idB - Volume level in decibels (float)
; Return values .: Success - 1
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......: Call _GetVolumeRange for possible values. See _GetVolumeRange remarks for more information.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _SetMasterVolumeLevel($idB)
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
If __SUCCEEDED($__g_oEndpointVolume.SetMasterVolumeLevel($idB, 0)) Then
Return 1
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _SetMasterVolumeLevelScalar
; Description ...: Set master volume level in scalar units, 0.0 to 100.0.
; Syntax ........: _SetMasterVolumeLevelScalar($iVol)
; Parameters ....: $iVol - Volume level in scalar units, 0.0 to 100.0 (float)
; Return values .: Success - 1
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _SetMasterVolumeLevelScalar($iVol)
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
If __SUCCEEDED($__g_oEndpointVolume.SetMasterVolumeLevelScalar($iVol / 100, 0)) Then
Return 1
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetMasterVolumeLevel
; Description ...: Get master volume level in decibels
; Syntax ........: _GetMasterVolumeLevel()
; Parameters ....:
; Return values .: Success - Master volume level in decibels (float)
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GetMasterVolumeLevel()
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
Local $idB
If __SUCCEEDED($__g_oEndpointVolume.GetMasterVolumeLevel($idB)) Then
Return $idB
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetMasterVolumeLevelScalar
; Description ...: Get master volume level in scalar units, 0.0 to 100.0.
; Syntax ........: _GetMasterVolumeLevelScalar()
; Parameters ....:
; Return values .: Success - Master volume level in scalar units, 0.0 to 100.0 (float)
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GetMasterVolumeLevelScalar()
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
Local $iVol
If __SUCCEEDED($__g_oEndpointVolume.GetMasterVolumeLevelScalar($iVol)) Then
Return ($iVol * 100)
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _SetChannelVolumeLevel
; Description ...: Set volume level for a specific channel in decibels.
; Syntax ........: _SetChannelVolumeLevel($iChannel, $idB)
; Parameters ....: $iChannel - Channel number to modify, 0 based index
; $idB - Volume level in decibels (float)
; Return values .: Success - 1
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......: Call _GetChannelCount for number of channels, _GetVolumeRange for possible values.
; See _GetVolumeRange remarks for more information.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _SetChannelVolumeLevel($iChannel, $idB)
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
If __SUCCEEDED($__g_oEndpointVolume.SetChannelVolumeLevel($iChannel, $idB, 0)) Then
Return 1
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _SetChannelVolumeLevelScalar
; Description ...: Set volume level for a specific channel in scalar units, 0.0 to 100.0.
; Syntax ........: _SetChannelVolumeLevelScalar($iChannel, $iVol)
; Parameters ....: $iChannel - Channel number to modify, 0 based index
; $iVol - Volume level in scalar units, 0.0 to 100.0 (float)
; Return values .: Success - 1
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......: Call _GetChannelCount for number of channels.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _SetChannelVolumeLevelScalar($iChannel, $iVol)
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
If __SUCCEEDED($__g_oEndpointVolume.SetChannelVolumeLevelScalar($iChannel, $iVol / 100, 0)) Then
Return 1
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetChannelVolumeLevel
; Description ...: Get volume level for a specific channel in decibels.
; Syntax ........: _GetChannelVolumeLevel($iChannel)
; Parameters ....: $iChannel - Channel number to query, 0 based index
; Return values .: Success - Channel volume level in decibels (float)
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......: Call _GetChannelCount for number of channels.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GetChannelVolumeLevel($iChannel)
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
Local $idB
If __SUCCEEDED($__g_oEndpointVolume.GetChannelVolumeLevel($iChannel, $idB)) Then
Return $idB
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetChannelVolumeLevelScalar
; Description ...: Get volume of a specific channel in scalar units, 0.0 to 100.0.
; Syntax ........: _GetChannelVolumeLevelScalar($iChannel)
; Parameters ....: $iChannel - Channel number to query, 0 based index
; Return values .: Success - Channel volume in scalar units, 0.0 to 100.0 (float)
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......: Call _GetChannelCount for number of channels.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GetChannelVolumeLevelScalar($iChannel)
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
Local $iVol
If __SUCCEEDED($__g_oEndpointVolume.GetChannelVolumeLevelScalar($iChannel, $iVol)) Then
Return ($iVol * 100)
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _SetMute
; Description ...: Set mute state
; Syntax ........: _SetMute($bMute)
; Parameters ....: $bMute - Desired mute state: 1 to mute, 0 to unmute
; Return values .: Success - 1
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _SetMute($bMute)
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
If __SUCCEEDED($__g_oEndpointVolume.SetMute($bMute, 0)) Then
Return 1
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetMute
; Description ...: Get current mute state
; Syntax ........: _GetMute()
; Parameters ....:
; Return values .: Success - Current mute state
; | 0 - not muted
; | 1 - muted
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GetMute()
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
Local $bMute
If __SUCCEEDED($__g_oEndpointVolume.GetMute($bMute)) Then
Return $bMute
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetVolumeStepInfo
; Description ...: Get current volume step and range.
; Syntax ........: _GetVolumeStepInfo()
; Parameters ....:
; Return values .: Success - Two element array containing step info
; [0] - Current volume step
; [1] - Volume step range
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......: Volume step values range from 0 to (range-1).
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GetVolumeStepInfo()
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
Local $iCurrentStep, $iStepRange
If __SUCCEEDED($__g_oEndpointVolume.GetVolumeStepInfo($iCurrentStep, $iStepRange)) Then
Local $aRet[2] = [$iCurrentStep, $iStepRange]
Return $aRet
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _VolumeStepUp
; Description ...: Increase volume by one step.
; Syntax ........: _VolumeStepUp()
; Parameters ....:
; Return values .: Success - 1
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......: Call _GetVolumeStepInfo for current step and range.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _VolumeStepUp()
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
If __SUCCEEDED($__g_oEndpointVolume.VolumeStepUp(0)) Then
Return 1
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _VolumeStepDown
; Description ...: Decrease volume by one step.
; Syntax ........: _VolumeStepDown()
; Parameters ....:
; Return values .: Success - 1
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......: Call _GetVolumeStepInfo for current step and range.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _VolumeStepDown()
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
If __SUCCEEDED($__g_oEndpointVolume.VolumeStepDown(0)) Then
Return 1
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _QueryHardwareSupport
; Description ...: Query audio endpoint device for hardware supported functions.
; Syntax ........: _QueryHardwareSupport()
; Parameters ....:
; Return values .: Success - Dword mask of bitwise OR'd hardware function constants.
; Failure - 0 and sets @error
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _QueryHardwareSupport()
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
Local $iMask
If __SUCCEEDED($__g_oEndpointVolume.QueryHardwareSupport($iMask)) Then
Return $iMask
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetVolumeRange
; Description ...: Get volume range information.
; Syntax ........: _GetVolumeRange()
; Parameters ....:
; Return values .: Success - Three element array containing volume range information
; [0] - Minimum volume in decibels (float)
; [1] - Maximum volume in decibels (float)
; [2] - Volume increment value in decibels (float)
; Author ........: Erik Pilsits
; Modified ......:
; Remarks .......: The volume range is divided into equal steps such that nSteps = (max - min) / increment.
; A call to SetMasterVolumeLevel or SetChannelVolumeLevel that falls between steps will set the volume
; to the nearest step.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GetVolumeRange()
_EndpointVolume_Init()
If @error Then Return SetError(-1, 0, 0)
;
Local $iMindB, $iMaxdB, $iIncrementdB
If __SUCCEEDED($__g_oEndpointVolume.GetVolumeRange($iMindB, $iMaxdB, $iIncrementdB)) Then
Local $aRet[3] = [$iMindB, $iMaxdB, $iIncrementdB]
Return $aRet
Else
Return SetError(1, 0, 0)
EndIf
EndFunc
#region INTERNAL FUNCTIONS
Func __GetDefaultEndpointVolume()
Local $oIAudioEndpointVolume = 0, $err = 1
; create device enumerator interface
Local $oDevEnum = ObjCreateInterface($CLSID_MMDeviceEnumerator, $IID_IMMDeviceEnumerator, $tagIMMDeviceEnumerator)
If IsObj($oDevEnum) Then
$err = 2
; get default audio endpoint interface pointer
Local $pDefaultDevice = 0
If __SUCCEEDED($oDevEnum.GetDefaultAudioEndpoint($eRender, $eConsole, $pDefaultDevice)) Then
$err = 3
; create default audio endpoint interface
Local $oIMMDevice = ObjCreateInterface($pDefaultDevice, $IID_IMMDevice, $tagIMMDevice)
If IsObj($oIMMDevice) Then
$err = 4
; get endpoint volume interface pointer
Local $pEndpointVolume = 0
If __SUCCEEDED($oIMMDevice.Activate(__uuidof($IID_IAudioEndpointVolume), $CLSCTX_INPROC_SERVER, 0, $pEndpointVolume)) Then
$err = 5
; create endpoint volume interface
$oIAudioEndpointVolume = ObjCreateInterface($pEndpointVolume, $IID_IAudioEndpointVolume, $tagIAudioEndpointVolume)
EndIf
$oIMMDevice = 0
EndIf
EndIf
$oDevEnum = 0
EndIf
;
If IsObj($oIAudioEndpointVolume) Then
Return $oIAudioEndpointVolume
Else
Return SetError($err, 0, 0)
EndIf
EndFunc
Func __SUCCEEDED($hr)
Return ($hr >= 0)
EndFunc
Func __uuidof($sGUID)
Local $tGUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]")
DllCall("ole32.dll", "long", "CLSIDFromString", "wstr", $sGUID, "struct*", $tGUID)
If @error Then Return SetError(@error, @extended, 0)
Return $tGUID
EndFunc
#endregion INTERNAL FUNCTIONS