When multimedia software is developed, the volume and setting of various devices are often adjusted. I have written a unit, four functions, which are used to obtain volume (DN)), set the volume (SETVOLUME (DN, Value) ), Get mute (DN)) and set the mute (DN, Value).
Unit funvolume;
Interface
Uses MMSystem, Dialogs;
TYPE TDEVICENAME = (Master, Microphone, Waveout, Synth);
function GetVolume (DN: TDeviceName): Word; procedure SetVolume (DN: TDeviceName; Value: Word); function GetVolumeMute (DN: TDeviceName): Boolean; procedure SetVolumeMute (DN: TDeviceName; Value: Boolean);
IMPLEMENTATION
// Get the volume function GetVolume (DN: TDeviceName): Word; var hMix: HMIXER; mxlc: MIXERLINECONTROLS; mxcd: TMIXERCONTROLDETAILS; vol: TMIXERCONTROLDETAILS_UNSIGNED; mxc: MIXERCONTROL; mxl: TMixerLine; intRet: Integer; nMixerDevs: Integer; begin // Check if Mixer Is Available NmixerDevs: = MixergetNumdevs (); IF (NMixerDevs <1).
// open the mixer intRet: = mixerOpen (@hMix, 0, 0, 0, 0); if intRet = MMSYSERR_NOERROR then begin case DN of Master: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS; Microphone: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; WaveOut : mxl.dwcomponenttype: = MXERLINE_COMPONENTTYPE_SRC_WAVEOUT; SYNTH: MXL.DWComponentTyPe: = MXERLINE_COMPONENTTYPE: = MXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; end; mxl.cbstruct: = sizeof (mxl);
// get line info intret: = MixergetLineInfo (HMIX, @mxl, mixer_getlineinfof_componenttype);
if intRet = MMSYSERR_NOERROR then begin FillChar (mxlc, SizeOf (mxlc), 0); mxlc.cbStruct: = SizeOf (mxlc); mxlc.dwLineID: = mxl.dwLineID; mxlc.dwControlType: = MIXERCONTROL_CONTROLTYPE_VOLUME; mxlc.cControls: = 1 Mxlc.cbmxctrl: = sizeof (mxc); mxlc.pamxctrl: = @mxc; intret: = MixergetLineControls (hmix, @MXLC, Mixer_getLineControlsf_onebype);
if intRet = MMSYSERR_NOERROR then begin FillChar (mxcd, SizeOf (mxcd), 0); mxcd.dwControlID: = mxc.dwControlID; mxcd.cbStruct: = SizeOf (mxcd); mxcd.cMultipleItems: = 0; mxcd.cbDetails: = SizeOf (Vol); mxcd.padetails: = @vol; mxcd.cchannels: = 1;
Intret: = MixergetControlDetails (HMIX, @ mxcd, mixer_setcontroldetailsf_value);
Result: = Vol.dwvalue;
IF intret <> mmsyserr_noError Then ShowMessage ('getControlDetails Error'); Else ShowMessage ('getLineInfo Error'); end; intret: = mixerclose (hmix); end;
// set the volume procedure setVolume (DN: TDeviceName; Value: Word); var hMix: HMIXER; mxlc: MIXERLINECONTROLS; mxcd: TMIXERCONTROLDETAILS; vol: TMIXERCONTROLDETAILS_UNSIGNED; mxc: MIXERCONTROL; mxl: TMixerLine; intRet: Integer; nMixerDevs: Integer; begin // Check if Mixer Is Available NMixerdevs: = MixergetNumdevs (); IF (NMixerDevs <1).
// open the mixer intRet: = mixerOpen (@hMix, 0, 0, 0, 0); if intRet = MMSYSERR_NOERROR then begin case DN of Master: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS; Microphone: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; WaveOut : mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; Synth: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; end; mxl.cbStruct: = SizeOf (mxl); // get line info intRet: = mixerGetLineInfo (hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);
if intRet = MMSYSERR_NOERROR then begin FillChar (mxlc, SizeOf (mxlc), 0); mxlc.cbStruct: = SizeOf (mxlc); mxlc.dwLineID: = mxl.dwLineID; mxlc.dwControlType: = MIXERCONTROL_CONTROLTYPE_VOLUME; mxlc.cControls: = 1 Mxlc.cbmxctrl: = sizeof (mxc);
MXLC.PAMXCTRL: = @mxc; intret: = MixergetLineControls (hmix, @mxlc, mixer_getlinecontrolsf_onebytype);
if intRet = MMSYSERR_NOERROR then begin FillChar (mxcd, SizeOf (mxcd), 0); mxcd.dwControlID: = mxc.dwControlID; mxcd.cbStruct: = SizeOf (mxcd); mxcd.cMultipleItems: = 0; mxcd.cbDetails: = SizeOf (Vol); mxcd.padetails: = @vol; mxcd.cchannels: = 1;
Vol.dwvalue: = value;
INTRET: = MixersetControlDetails (hmix, @ mxcd, mixer_setcontroldetailsf_value);
IF intret <> mmsyserr_noError Then ShowMessage ('setLineInfo Error'); end; intret: = mixerclose (hmix); end;
// Get the mute function GetVolumeMute (DN: TDeviceName): Boolean; var hMix: HMIXER; mxlc: MIXERLINECONTROLS; mxcd: TMIXERCONTROLDETAILS; vol: TMIXERCONTROLDETAILS_UNSIGNED; mxc: MIXERCONTROL; mxl: TMixerLine; intRet: Integer; nMixerDevs: Integer; mcdMute: MIXERCONTROLDETAILS_BOOLEAN Begin // Check if Mixer Is Available NMixerdevs: = MixergetNumdevs (); if (NMixerDevs <1) Then Begin Exit; End; // Open THE MIXER INTRET: = Mixeropen (@Hmix, 0, 0, 0); if intRet = MMSYSERR_NOERROR then begin case DN of Master: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS; Microphone: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; WaveOut: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; Synth: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; end; mxl.cbStruct : = SizeOf (MXL);
// MixerLine Info intret: = MixergetLineInfo (HMIX, @mxl, mixer_getlineinfof_componenttype);
if intRet = MMSYSERR_NOERROR then begin FillChar (mxlc, SizeOf (mxlc), 0); mxlc.cbStruct: = SizeOf (mxlc); mxlc.dwLineID: = mxl.dwLineID; mxlc.dwControlType: = MIXERCONTROL_CONTROLTYPE_MUTE; mxlc.cControls: = 1 ; Mxlc.cbmxctrl: = sizeof (mxc); mxlc.pamxctrl: = @mxc;
// get the mute control intret: = MixergetLineControls (hmix, @mxlc, mixer_getlinecontrolsf_onebytype);
if intRet = MMSYSERR_NOERROR then begin FillChar (mxcd, SizeOf (mxcd), 0); mxcd.cbStruct: = SizeOf (TMIXERCONTROLDETAILS); mxcd.dwControlID: = mxc.dwControlID; mxcd.cChannels: = 1; mxcd.cbDetails: = SizeOf (MixerControlDetails); mxcd.padetails: = @mcdmute; // get mute intret: = MixergetControlDetails (hmix, @mxcd, mixer_setcontroldetailsf_value);
IF mcdmute.fvalue = 0 Then Result: = false else result: = true;
IF intret <> mmsyserr_noError Then ShowMessage ('setControlDetails Error'); Else ShowMessage ('getLineInfo Error');
Intret: = MixerClose (HMIX); END;
// set the silence procedure SetVolumeMute (DN: TDeviceName; Value: Boolean); var hMix: HMIXER; mxlc: MIXERLINECONTROLS; mxcd: TMIXERCONTROLDETAILS; vol: TMIXERCONTROLDETAILS_UNSIGNED; mxc: MIXERCONTROL; mxl: TMixerLine; intRet: Integer; nMixerDevs: Integer; mcdMute : MixerControlDetails_Boolean; Begin // Check if Mixer Is Available NMixerDevs: = MixergetNumdevs (); if (NMixerDevs <1).
// open the mixer intRet: = mixerOpen (@hMix, 0, 0, 0, 0); if intRet = MMSYSERR_NOERROR then begin case DN of Master: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS; Microphone: mxl.dwComponentType: = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; WaveOut : mxl.dwcomponenttype: = MXERLINE_COMPONENTTYPE_SRC_WAVEOUT; SYNTH: MXL.DWComponentTyPe: = MXERLINE_COMPONENTTYPE: = MXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; end; mxl.cbstruct: = sizeof (mxl);
// MixerLine Info intret: = MixergetLineInfo (HMIX, @mxl, mixer_getlineinfof_componenttype);
if intRet = MMSYSERR_NOERROR then begin FillChar (mxlc, SizeOf (mxlc), 0); mxlc.cbStruct: = SizeOf (mxlc); mxlc.dwLineID: = mxl.dwLineID; mxlc.dwControlType: = MIXERCONTROL_CONTROLTYPE_MUTE; mxlc.cControls: = 1 Mxlc.cbmxctrl: = sizeof (mxc); mxlc.pamxctrl: = @mxc; // Get the mute control intret: = MixergetLineControls (hmix, @mxlc, mixer_getlinecontrolsf_onebype);
if intRet = MMSYSERR_NOERROR then begin FillChar (mxcd, SizeOf (mxcd), 0); mxcd.cbStruct: = SizeOf (TMIXERCONTROLDETAILS); mxcd.dwControlID: = mxc.dwControlID; mxcd.cChannels: = 1; mxcd.cbDetails: = SizeOf (MixerControlDetails_Boolean); mxcd.padetails: = @mcdmute;
// set and unset mute mcdmute.fValue: = ORD (Value); intret: = MixersetControlDetails (hmix, @mxcd, mixer_setcontroldetailsf_value);
IF intret <> mmsyserr_noError Then ShowMessage ('setControlDetails Error'); Else ShowMessage ('getLineInfo Error');
Intret: = MixerClose (HMIX); END;
End.