博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
操纵声卡
阅读量:6842 次
发布时间:2019-06-26

本文共 5394 字,大约阅读时间需要 17 分钟。

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.

转载于:https://www.cnblogs.com/haim/p/3698421.html

你可能感兴趣的文章
FFmpeg(二) 解封装相关函数理解
查看>>
JS---改变图片大小
查看>>
Codeforces Round #348(VK Cup 2016 - Round 2)
查看>>
ASP.Net TextBox只读时不能通过后台赋值取值
查看>>
文件I/O操作函数 read() write()
查看>>
vue文件上传 vue-simple-upload的使用方法
查看>>
Oracle 一次 锁表 处理小记
查看>>
ES6数组方法
查看>>
Linux学习的常用命令
查看>>
win10下安装lupa
查看>>
线程同步之lock学习
查看>>
深入浅出javascript(五)函数
查看>>
关键字的理解
查看>>
Bootstrap 3支持IE 8遇到的一个小问题
查看>>
jquery中ajax回调函数使用this
查看>>
我记录网站综合系统 -- 技术原理解析[7:CSS类]
查看>>
14. Longest Common Prefix
查看>>
Mac OS Sierra 安装PHP扩展 Operation not permitted
查看>>
集训考试题tents
查看>>
设计模式的学习
查看>>