procedure TAudioVolume.ProcessMsg(
var Msg: TMessage);
var
PVolMuteRec: PVolMute;
Volume: single;
Muted: boolean;
Device: IMMDevice;
DeviceId: WideString;
DeviceInfo: TDeviceInfo;
HR: HResult;
NewState: DWORD;
MyRegistry: TRegistry;
begin
case Msg.Msg
of
WM_EndpointVolume:
begin
PVolMuteRec := PVolMute(Msg.WPARAM);
if Assigned(tbMasterVolume)
then
begin
tbMasterVolume.Position := round((1.0 - PVolMuteRec^.Volume) * tbMasterVolume.Max);
ckMasterMute.Checked := PVolMuteRec^.Muted;
end;
if Assigned(tbMasterBalance)
then
AdjustMasterBalancePos;
if Assigned(FOnMasterVolumeEvent)
then
FOnMasterVolumeEvent(MyVolSet, PVolMuteRec^.Volume, PVolMuteRec^.Muted);
end;
WM_VolumeEvent:
begin
PVolMuteRec := PVolMute(Msg.WPARAM);
if MySessionVolSet
or Assigned(FOnSessionVolumeEvent)
then
begin
if Assigned(tbSessionVolume)
or Assigned(FOnSessionVolumeEvent)
then
begin
tbSessionVolume.Position := round((1.0 - PVolMuteRec^.Volume) * tbSessionVolume.Max);
ckSessionMute.Checked := PVolMuteRec^.Muted;
end;
if (MySessionVolSet
and (abs(PVolMuteRec^.Volume - MyVolVal) < 0.0001))
then
MySessionVolSet := false
else if (MyMuteSet
and (PVolMuteRec^.Muted = MyMuteVal))
then
MyMuteSet := false;
if Assigned(tbSessionBalance)
then
AdjustSessionBalancePos;
end;
end;
WM_SessionStateEvent:
begin
if Assigned(FOnSessionStateEvent)
then
FOnSessionStateEvent(self, Msg.WPARAM);
end;
WM_DeviceStateChange, WM_DeviceAdded, WM_DeviceRemoved:
begin
DeviceId := WideString(PWideChar(Msg.WPARAM));
NewState := Msg.LPARAM;
// Just refresh device list to simplify the logic.
RefreshDeviceList(DeviceEnumerator);
// SetupDefaultAudioEndpoint(DeviceEnumerator);
if Assigned(FOnDeviceStateChange)
then
begin
if Msg.Msg = WM_DeviceStateChange
then
FOnDeviceStateChange(DeviceId, NewState)
else if Msg.Msg = WM_DeviceAdded
then
FOnDeviceStateChange(DeviceId, DEVICE_STATE_ADDED)
else if Msg.Msg = WM_DeviceRemoved
then
FOnDeviceStateChange(DeviceId, DEVICE_STATE_REMOVED);
end;
end;
WM_SessionDisconnected:
begin
FAudioSessionList.SessionDisconnected := cAudioSessionDisconnected
[TAudioSessionDisconnected(Msg.WPARAM)];
if Assigned(FOnSessionDisconnected)
then
FOnSessionDisconnected(self, Msg.WPARAM);
end;
WM_SessionCreate:
begin
if Assigned(FOnSessionCreated)
then
FOnSessionCreated(IAudioSessionControl(@Msg.WPARAM));
end;
WM_DefaultDeviceChange:
begin
HR := DeviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, Device);
if HR <> S_OK
then
raise Exception.Create('
Error : Unable to get DefaultAudioEndpoint Device');
DeviceInfo := GetDeviceInfo(Device);
if DeviceInfo.DeviceId <> FDefaultDevice.DeviceId
then
begin
FDefaultDevice := DeviceInfo;
if Assigned(FOnDefaultDeviceChange)
then
FOnDefaultDeviceChange(DeviceInfo);
end;
end;
MM_MIXM_LINE_CHANGE:
begin
// wParam = (WPARAM) hMixer lParam = (LPARAM) dwLineID
end;
MM_MIXM_CONTROL_CHANGE:
begin
// wParam = (WPARAM) hMixer lParam = (LPARAM) dwControlID
if (DWORD(Msg.LPARAM) <> MasterMute.dwControlID)
and
(DWORD(Msg.LPARAM) <> MasterVol.dwControlID)
then
exit;
Muted := IsMasterMuted;
Volume := GetMasterVolume;
if (DWORD(Msg.LPARAM) = MasterMute.dwControlID)
then
begin
if (MyMuteSet
and (Muted = MyMuteVal))
then
MyMuteSet := false;
end
else
// for (Msg.LParam = MasterVol.dwControlID)
if (MyVolSet
and (abs(Volume - MyVolVal) < 0.0001))
then
MyVolSet := false;
if Assigned(tbMasterVolume)
then
begin
tbMasterVolume.Position := round((1.0 - Volume) * tbMasterVolume.Max);
ckMasterMute.Checked := Muted;
end;
if Assigned(tbMasterBalance)
then
AdjustMasterBalancePos;
end;
else
if Msg.Msg = WINMM_DEVICECHANGE
then
begin
MyRegistry := TRegistry.Create;
MyRegistry.RootKey := HKEY_CURRENT_USER;
if MyRegistry.KeyExists('
Software\Microsoft\Multimedia\Sound Mapper')
then
if MyRegistry.OpenKey('
Software\Microsoft\Multimedia\Sound Mapper', true)
then
FDefaultDevice.DeviceName := MyRegistry.ReadString('
Playback');
MyRegistry.free;
end
else
Msg.Result := DefWindowProc(FEventHandle, Msg.Msg, Msg.WPARAM, Msg.LPARAM);
end;
end;