首页 > 代码库 > 操纵声卡

操纵声卡

unit 操纵声卡;

interface

uses Windows, Dialogs, MMSystem;

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

// - 初始化 TMixerLine
function Initial_mxl(DN: TDeviceName): TMixerLine;
begin
  with Result do
  begin
    case DN of
      Master:
        dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
      Microphone:
        dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
      WaveOut:
        dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;
      Synth:
        dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;
    end;
    cbStruct := SizeOf(TMixerLine);
  end;
end;

// - 初始化 MIXERLINECONTROLS
function Initial_mxlc(mxl: TMixerLine; var mxc: MixerControlW; dwCType: DWORD)
  : MIXERLINECONTROLS;
begin
  FillChar(Result, SizeOf(Result), 0);
  with Result do
  begin
    cbStruct := SizeOf(Result);
    dwLineID := mxl.dwLineID;
    dwControlType := dwCType;
    cControls := 1;
    cbmxctrl := SizeOf(mxc);
    pamxctrl := @mxc;
  end;
end;

// - 初始化 TMIXERCONTROLDETAILS
function Initial_mxcd(mxc: MIXERCONTROL; var vol: DWORD): TMIXERCONTROLDETAILS;
begin
  FillChar(Result, SizeOf(Result), 0);
  with Result do
  begin
    cbDetails := SizeOf(DWORD);
    cChannels := 1;
    cbStruct := SizeOf(Result);
    cMultipleItems := 0;
    dwControlID := mxc.dwControlID;
    paDetails := @vol;
  end;
end;

// - 获取音量
function GetVolume(DN: TDeviceName): Word;
var
  hMix: HMIXER;
  mxl: TMixerLine;
  mxlc: MIXERLINECONTROLS;
  mxc: MIXERCONTROL;
  mxcd: TMIXERCONTROLDETAILS;
  vol: TMIXERCONTROLDETAILS_UNSIGNED;
begin
  Result := 0;
  // - 混频器存在且打开成功
  if (mixerGetNumDevs > 0) and (mixerOpen(@hMix, 0, 0, 0, 0) = MMSYSERR_NOERROR)
  then
    try
      mxl := Initial_mxl(DN);
      // - 检取混频器有关特有线路的信息
      if mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE) = MMSYSERR_NOERROR
      then
      begin
        mxlc := Initial_mxlc(mxl, mxc, MIXERCONTROL_CONTROLTYPE_VOLUME);
        // - 获得与音频线路相关控制的通用信息
        if mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE) = MMSYSERR_NOERROR
        then
        begin
          mxcd := Initial_mxcd(mxc, DWORD(vol));
          Result := vol.dwValue;
          // - 获得与某音频线路相关的一个控制的属性
          if mixerGetControlDetails(hMix, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE)
            <> MMSYSERR_NOERROR then
            ShowMessage(‘GetControlDetails Error‘);
        end
        else
          ShowMessage(‘GetLineInfo Error‘);
      end;
    finally
      // - 关闭指定的混频器
      mixerClose(hMix);
    end;
end;

// - 设置音量
procedure SetVolume(DN: TDeviceName; Value: Word);
var
  hMix: HMIXER;
  mxl: TMixerLine;
  mxlc: MIXERLINECONTROLS;
  mxc: MIXERCONTROL;
  mxcd: TMIXERCONTROLDETAILS;
  vol: TMIXERCONTROLDETAILS_UNSIGNED;
begin
  // - 混频器存在且打开成功
  if (mixerGetNumDevs > 0) and (mixerOpen(@hMix, 0, 0, 0, 0) = MMSYSERR_NOERROR)
  then
    try
      mxl := Initial_mxl(DN);
      // - 检取混频器有关特有线路的信息
      if mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE) = MMSYSERR_NOERROR
      then
      begin
        mxlc := Initial_mxlc(mxl, mxc, MIXERCONTROL_CONTROLTYPE_VOLUME);
        // - 获得与音频线路相关控制的通用信息
        if mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE) = MMSYSERR_NOERROR
        then
        begin
          mxcd := Initial_mxcd(mxc, DWORD(vol));
          vol.dwValue := Value;
          // - 设置指定控制的属性
          if mixerSetControlDetails(hMix, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE)
            <> MMSYSERR_NOERROR then
            ShowMessage(‘SetControlDetails Error‘);
        end
        else
          ShowMessage(‘GetLineInfo Error‘);
      end;

    finally
      // - 关闭指定的混频器
      mixerClose(hMix);
    end;
end;

// - 获取静音
function GetVolumeMute(DN: TDeviceName): Boolean;
var
  hMix: HMIXER;
  mxl: TMixerLine;
  mxlc: MIXERLINECONTROLS;
  mxc: MIXERCONTROL;
  mxcd: TMIXERCONTROLDETAILS;
  mcdMute: MIXERCONTROLDETAILS_BOOLEAN;
begin
  Result := False;
  // - 混频器存在且打开成功
  if (mixerGetNumDevs > 0) and (mixerOpen(@hMix, 0, 0, 0, 0) = MMSYSERR_NOERROR)
  then
    try
      mxl := Initial_mxl(DN);
      // - 检取混频器有关特有线路的信息
      if mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE) = MMSYSERR_NOERROR
      then
      begin
        mxlc := Initial_mxlc(mxl, mxc, MIXERCONTROL_CONTROLTYPE_MUTE);
        // - 获得与音频线路相关控制的通用信息
        if mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE) = MMSYSERR_NOERROR
        then
        begin
          mxcd := Initial_mxcd(mxc, DWORD(mcdMute));
          // - 获得与某音频线路相关的一个控制的属性
          if mixerGetControlDetails(hMix, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE)
            <> MMSYSERR_NOERROR then
            ShowMessage(‘SetControlDetails Error‘);
          Result := (mcdMute.fValue <> 0);
        end
        else
          ShowMessage(‘GetLineInfo Error‘);
      end;
    finally
      // - 关闭指定的混频器
      mixerClose(hMix);
    end;
end;

// - 设置静音
procedure SetVolumeMute(DN: TDeviceName; Value: Boolean);
var
  hMix: HMIXER;
  mxl: TMixerLine;
  mxlc: MIXERLINECONTROLS;
  mxc: MIXERCONTROL;
  mxcd: TMIXERCONTROLDETAILS;
  mcdMute: MIXERCONTROLDETAILS_BOOLEAN;
begin
  // - 混频器存在且打开成功
  if (mixerGetNumDevs > 0) and (mixerOpen(@hMix, 0, 0, 0, 0) = MMSYSERR_NOERROR)
  then
    try
      mxl := Initial_mxl(DN);
      // - 检取混频器有关特有线路的信息
      if mixerGetLineInfo(hMix, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE) = MMSYSERR_NOERROR
      then
      begin
        mxlc := Initial_mxlc(mxl, mxc, MIXERCONTROL_CONTROLTYPE_MUTE);
        // - 获得与音频线路相关控制的通用信息
        if mixerGetLineControls(hMix, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE) = MMSYSERR_NOERROR
        then
        begin
          mxcd := Initial_mxcd(mxc, DWORD(mcdMute));
          mcdMute.fValue := Ord(Value);
          // - 设置指定控制的属性
          if mixerSetControlDetails(hMix, @mxcd, MIXER_SETCONTROLDETAILSF_VALUE)
            <> MMSYSERR_NOERROR then
            ShowMessage(‘SetControlDetails Error‘);
        end
        else
          ShowMessage(‘GetLineInfo Error‘);
      end;
    finally
      // - 关闭指定的混频器
      mixerClose(hMix);
    end;
end;

end.