-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMixer.pas
306 lines (266 loc) · 8.75 KB
/
Mixer.pas
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
{ Minimalist Media Player
Copyright (C) 2021 Baz Cuda
https://github.com/BazzaCuda/MinimalistMediaPlayer
NB: I didn't write this unit and I don't remember where I got it.
As a courtesy, I have protected its use with this licence on behalf of the original author.
The author is welcome to contact me if I have acted in error and they wish this licence to be removed.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
}
unit Mixer;
interface
type
Tmixer = class
protected
function getMute: boolean; virtual; abstract;
function getVolume: integer; virtual; abstract;
procedure setVolume(Value: integer); virtual; abstract;
procedure setMute(Value: boolean); virtual; abstract;
public
property volume: integer read getVolume write setVolume;
property muted: boolean read getMute write setMute;
end;
function g_mixer: Tmixer;
implementation
uses
Windows, MMSystem, MMDevApi_tlb, ComObj, ActiveX, SysUtils;
type
TxpMixer = class(Tmixer)
private
Fmxct: integer;
Fmixer: HMIXER;
procedure chk(r: MMRESULT);
protected
function getMute: boolean; override;
function getVolume: integer; override;
procedure setVolume(Value: integer); override;
procedure setMute(Value: boolean); override;
public
constructor Create;
destructor Destroy; override;
end;
TVistaMixer = class(Tmixer)
private
FmmDev: IMMDevice;
FmmDevEnum: IMMDeviceEnumerator;
FmmEndpoint: IMMAudioEndpointVolume;
protected
function getMute: boolean; override;
function getVolume: integer; override;
procedure setVolume(Value: integer); override;
procedure setMute(Value: boolean); override;
public
constructor Create;
end;
var
_g_mixer: Tmixer;
function g_mixer: Tmixer;
var
VerInfo: TOSVersioninfo;
begin
if (_g_mixer = nil) then
begin
VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
GetVersionEx(VerInfo);
if (VerInfo.dwMajorVersion >= 6) then
_g_mixer := TVistaMixer.Create
else
_g_mixer := TxpMixer.Create;
end;
result := _g_mixer;
end;
{ TxpMixer }
procedure TxpMixer.chk(r: MMRESULT);
var
s: string;
begin
if (r = MMSYSERR_NOERROR) then
exit;
setLength(s, MMSystem.MAXERRORLENGTH + 1);
waveOutGetErrorText(r, @s[1], MMSystem.MAXERRORLENGTH);
raise Exception.Create(StrPas(pChar(s)));
end;
constructor TxpMixer.Create;
begin
Fmxct := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
chk(mixerOpen(@Fmixer, 0, 0, 0, 0));
end;
destructor TxpMixer.Destroy;
begin
if (Fmixer <> 0) then
mixerClose(Fmixer);
inherited;
end;
function TxpMixer.getMute: boolean;
var
MasterMute: TMixerControl;
Details: TMixerControlDetails;
BoolDetails: TMixerControlDetailsBoolean;
Line: TMixerLine;
Controls: TMixerLineControls;
begin
ZeroMemory(@Line, SizeOf(Line));
Line.cbStruct := SizeOf(Line);
Line.dwComponentType := Fmxct;
chk(mixerGetLineInfo(0, @Line, MIXER_GETLINEINFOF_COMPONENTTYPE));
ZeroMemory(@Controls, SizeOf(Controls));
Controls.cbStruct := SizeOf(Controls);
Controls.dwLineID := Line.dwLineID;
Controls.cControls := 1;
Controls.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE;
Controls.cbmxctrl := SizeOf(MasterMute);
Controls.pamxctrl := @MasterMute;
chk(mixerGetLineControls(0, @Controls, MIXER_GETLINECONTROLSF_ONEBYTYPE));
Details.cbStruct := SizeOf(Details);
Details.dwControlID := MasterMute.dwControlID;
Details.cChannels := 1;
Details.cMultipleItems := 0;
Details.cbDetails := SizeOf(BoolDetails);
Details.paDetails := @BoolDetails;
chk(mixerGetControlDetails(0, @Details, MIXER_GETCONTROLDETAILSF_VALUE));
result := BoolDetails.fValue <> 0;
end;
function TxpMixer.getVolume: integer;
var
Line: TMixerLine;
Controls: TMixerLineControls;
MasterVolume: TMixerControl;
Details: TMixerControlDetails;
UnsignedDetails: TMixerControlDetailsUnsigned;
begin
ZeroMemory(@Line, SizeOf(Line));
Line.cbStruct := SizeOf(Line);
Line.dwComponentType := Fmxct;
chk(mixerGetLineInfo(Fmixer, @Line, MIXER_GETLINEINFOF_COMPONENTTYPE));
ZeroMemory(@Controls, SizeOf(Controls));
Controls.cbStruct := SizeOf(Controls);
Controls.dwLineID := Line.dwLineID;
Controls.cControls := 1;
Controls.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
Controls.cbmxctrl := SizeOf(MasterVolume);
Controls.pamxctrl := @MasterVolume;
chk(mixerGetLineControls(Fmixer, @Controls, MIXER_GETLINECONTROLSF_ONEBYTYPE));
details.cbStruct := SizeOf(Details);
details.dwControlID := MasterVolume.dwControlID;
details.cChannels := 1;
details.cMultipleItems := 0;
details.cbDetails := SizeOf(UnsignedDetails);
details.paDetails := @UnsignedDetails;
chk(mixerGetControlDetails(Fmixer, @Details, MIXER_GETCONTROLDETAILSF_VALUE));
result := UnsignedDetails.dwValue;
end;
procedure TxpMixer.setMute(Value: boolean);
var
Line: TMixerLine;
Controls: TMixerLineControls;
MasterMute: TMixerControl;
Details: TMixerControlDetails;
BoolDetails: TMixerControlDetailsBoolean;
begin
ZeroMemory(@Line, SizeOf(Line));
Line.cbStruct := SizeOf(Line);
Line.dwComponentType := Fmxct;
chk(mixerGetLineInfo(Fmixer, @Line, MIXER_GETLINEINFOF_COMPONENTTYPE));
ZeroMemory(@Controls, SizeOf(Controls));
Controls.cbStruct := SizeOf(Controls);
Controls.dwLineID := Line.dwLineID;
Controls.cControls := 1;
Controls.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE;
Controls.cbmxctrl := SizeOf(masterMute);
Controls.pamxctrl := @masterMute;
chk(mixerGetLineControls(Fmixer, @Controls, MIXER_GETLINECONTROLSF_ONEBYTYPE));
details.cbStruct := SizeOf(Details);
details.dwControlID := MasterMute.dwControlID;
details.cChannels := 1;
details.cMultipleItems := 0;
details.cbDetails := SizeOf(BoolDetails);
details.paDetails := @BoolDetails;
mixerGetControlDetails(0, @Details, MIXER_GETCONTROLDETAILSF_VALUE);
if (Value) then
BoolDetails.fValue := 1
else
BoolDetails.fValue := 0;
chk(mixerSetControlDetails(0, @Details, MIXER_SETCONTROLDETAILSF_VALUE));
end;
procedure TxpMixer.setVolume(Value: integer);
var
Line: TMixerLine;
Controls: TMixerLineControls;
MasterVolume: TMixerControl;
Details: TMixerControlDetails;
UnsignedDetails: TMixerControlDetailsUnsigned;
begin
if (value < 0) then
value := 0;
if (value > 65535) then
value := 65535;
ZeroMemory(@Line, SizeOf(Line));
Line.cbStruct := SizeOf(Line);
Line.dwComponentType := Fmxct;
chk(mixerGetLineInfo(Fmixer, @Line, MIXER_GETLINEINFOF_COMPONENTTYPE));
ZeroMemory(@Controls, SizeOf(Controls));
Controls.cbStruct := SizeOf(Controls);
Controls.dwLineID := Line.dwLineID;
Controls.cControls := 1;
Controls.dwControlType := MIXERCONTROL_CONTROLTYPE_VOLUME;
Controls.cbmxctrl := SizeOf(MasterVolume);
Controls.pamxctrl := @MasterVolume;
chk(mixerGetLineControls(Fmixer, @Controls, MIXER_GETLINECONTROLSF_ONEBYTYPE));
details.cbStruct := SizeOf(Details);
details.dwControlID := MasterVolume.dwControlID;
details.cChannels := 1;
details.cMultipleItems := 0;
details.cbDetails := SizeOf(UnsignedDetails);
details.paDetails := @UnsignedDetails;
UnsignedDetails.dwValue := Value;
chk(mixerSetControlDetails(Fmixer, @Details, MIXER_SETCONTROLDETAILSF_VALUE));
end;
{ TVistaMixer }
constructor TVistaMixer.Create;
begin
CoCreateInstance(CLSID_MMDeviceEnumerator, nil, CLSCTX_ALL, IID_IMMDeviceEnumerator, FmmDevEnum);
FmmDevEnum.GetDefaultAudioEndpoint(eRender, eMultimedia, FmmDev);
FmmDev.Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, nil, FmmEndpoint);
end;
function TVistaMixer.getMute: boolean;
var
vResult: integer;
begin
FmmEndpoint.GetMute(vResult);
result := boolean(vResult);
end;
function TVistaMixer.getVolume: integer;
var
VolLevel: Single;
begin
FmmEndpoint.GetMasterVolumeLevelScalar(VolLevel);
result := Round(VolLevel * 65535);
end;
procedure TVistaMixer.setMute(Value: boolean);
begin
FmmEndpoint.SetMute(integer(Value), nil);
end;
procedure TVistaMixer.setVolume(Value: integer);
var
fValue: Single;
begin
if (value < 0) then
value := 0;
if (value > 65535) then
value := 65535;
fValue := Value / 65535;
FmmEndpoint.SetMasterVolumeLevelScalar(fValue, nil);
end;
initialization
finalization
case assigned(_g_mixer) of TRUE: _g_mixer.Free; end;
end.