一个操作系统声音的类。控制系统声音

简介:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace WaveLib
{
    public class AudioMixerHelper
    {
        public const int MMSYSERR_NOERROR = 0;
        public const int MAXPNAMELEN = 32;
        public const int MIXER_LONG_NAME_CHARS = 64;
        public const int MIXER_SHORT_NAME_CHARS = 16;
        public const int MIXER_GETLINEINFOF_COMPONENTTYPE = 0x3;
        public const int MIXER_GETCONTROLDETAILSF_VALUE = 0x0;
        public const int MIXER_GETLINECONTROLSF_ONEBYTYPE = 0x2;
        public const int MIXER_SETCONTROLDETAILSF_VALUE = 0x0;
        public const int MIXERLINE_COMPONENTTYPE_DST_FIRST = 0x0;
        public const int MIXERLINE_COMPONENTTYPE_SRC_FIRST = 0x1000;
        public const int MIXERLINE_COMPONENTTYPE_DST_SPEAKERS =
             (MIXERLINE_COMPONENTTYPE_DST_FIRST + 4);
        public const int MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE =
             (MIXERLINE_COMPONENTTYPE_SRC_FIRST + 3);
        public const int MIXERLINE_COMPONENTTYPE_SRC_LINE =
             (MIXERLINE_COMPONENTTYPE_SRC_FIRST + 2);
        public const int MIXERCONTROL_CT_CLASS_FADER = 0x50000000;
        public const int MIXERCONTROL_CT_UNITS_UNSIGNED = 0x30000;
        public const int MIXERCONTROL_CONTROLTYPE_FADER =
             (MIXERCONTROL_CT_CLASS_FADER | MIXERCONTROL_CT_UNITS_UNSIGNED);
        public const int MIXERCONTROL_CONTROLTYPE_VOLUME =
             (MIXERCONTROL_CONTROLTYPE_FADER + 1);
        [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
        private static extern int mixerClose(int hmx);
        [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
        private static extern int mixerGetControlDetailsA(int hmxobj, ref 
              MIXERCONTROLDETAILS pmxcd, int fdwDetails);
        [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
        private static extern int mixerGetDevCapsA(int uMxId, MIXERCAPS
             pmxcaps, int cbmxcaps);
        [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
        private static extern int mixerGetID(int hmxobj, int pumxID, int
             fdwId);
        [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
        private static extern int mixerGetLineControlsA(int hmxobj, ref 
              MIXERLINECONTROLS pmxlc, int fdwControls);
        [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
        private static extern int mixerGetLineInfoA(int hmxobj, ref 
              MIXERLINE pmxl, int fdwInfo);
        [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
        private static extern int mixerGetNumDevs();
        [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
        private static extern int mixerMessage(int hmx, int uMsg, int
             dwParam1, int dwParam2);
        [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
        private static extern int mixerOpen(out int phmx, int uMxId,
             int dwCallback, int dwInstance, int fdwOpen);
        [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
        private static extern int mixerSetControlDetails(int hmxobj, ref 
              MIXERCONTROLDETAILS pmxcd, int fdwDetails);
        public struct MIXERCAPS
        {
            public int wMid;
            public int wPid;
            public int vDriverVersion;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAXPNAMELEN)]
            public string szPname;
            public int fdwSupport;
            public int cDestinations;
        }
        public struct MIXERCONTROL
        {
            public int cbStruct;
            public int dwControlID;
            public int dwControlType;
            public int fdwControl;
            public int cMultipleItems;
            [MarshalAs(UnmanagedType.ByValTStr,
                  SizeConst = MIXER_SHORT_NAME_CHARS)]
            public string szShortName;
            [MarshalAs(UnmanagedType.ByValTStr,
                  SizeConst = MIXER_LONG_NAME_CHARS)]
            public string szName;
            public int lMinimum;
            public int lMaximum;
            [MarshalAs(UnmanagedType.U4, SizeConst = 10)]
            public int reserved;
        }
        public struct MIXERCONTROLDETAILS
        {
            public int cbStruct;
            public int dwControlID;
            public int cChannels;
            public int item;
            public int cbDetails;
            public IntPtr paDetails;
        }
        public struct MIXERCONTROLDETAILS_UNSIGNED
        {
            public int dwValue;
        }
        public struct MIXERLINE
        {
            public int cbStruct;
            public int dwDestination;
            public int dwSource;
            public int dwLineID;
            public int fdwLine;
            public int dwUser;
            public int dwComponentType;
            public int cChannels;
            public int cConnections;
            public int cControls;
            [MarshalAs(UnmanagedType.ByValTStr,
                  SizeConst = MIXER_SHORT_NAME_CHARS)]
            public string szShortName;
            [MarshalAs(UnmanagedType.ByValTStr,
                  SizeConst = MIXER_LONG_NAME_CHARS)]
            public string szName;
            public int dwType;
            public int dwDeviceID;
            public int wMid;
            public int wPid;
            public int vDriverVersion;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAXPNAMELEN)]
            public string szPname;
        }
        public struct MIXERLINECONTROLS
        {
            public int cbStruct;
            public int dwLineID;
            public int dwControl;
            public int cControls;
            public int cbmxctrl;
            public IntPtr pamxctrl;
        }
        private static bool GetVolumeControl(int hmixer, int componentType,
             int ctrlType, out MIXERCONTROL mxc, out int vCurrentVol)
        {
            // This function attempts to obtain a mixer control. 
            // Returns True if successful. 
            MIXERLINECONTROLS mxlc = new MIXERLINECONTROLS();
            MIXERLINE mxl = new MIXERLINE();
            MIXERCONTROLDETAILS pmxcd = new MIXERCONTROLDETAILS();
            MIXERCONTROLDETAILS_UNSIGNED du = new
                 MIXERCONTROLDETAILS_UNSIGNED();
            mxc = new MIXERCONTROL();
            int rc;
            bool retValue;
            vCurrentVol = -1;
            mxl.cbStruct = Marshal.SizeOf(mxl);
            mxl.dwComponentType = componentType;
            rc = mixerGetLineInfoA(hmixer, ref mxl,
                 MIXER_GETLINEINFOF_COMPONENTTYPE);
            if (MMSYSERR_NOERROR == rc)
            {
                int sizeofMIXERCONTROL = 152;
                int ctrl = Marshal.SizeOf(typeof(MIXERCONTROL));
                mxlc.pamxctrl = Marshal.AllocCoTaskMem(sizeofMIXERCONTROL);
                mxlc.cbStruct = Marshal.SizeOf(mxlc);
                mxlc.dwLineID = mxl.dwLineID;
                mxlc.dwControl = ctrlType;
                mxlc.cControls = 1;
                mxlc.cbmxctrl = sizeofMIXERCONTROL;
                // Allocate a buffer for the control 
                mxc.cbStruct = sizeofMIXERCONTROL;
                // Get the control 
                rc = mixerGetLineControlsA(hmixer, ref mxlc,
                    MIXER_GETLINECONTROLSF_ONEBYTYPE);
                if (MMSYSERR_NOERROR == rc)
                {
                    retValue = true;
                    // Copy the control into the destination structure 
                    mxc = (MIXERCONTROL) Marshal.PtrToStructure(
                         mxlc.pamxctrl, typeof(MIXERCONTROL));
                }
                else
                {
                    retValue = false;
                }
                int sizeofMIXERCONTROLDETAILS =
                    Marshal.SizeOf(typeof(MIXERCONTROLDETAILS));
                int sizeofMIXERCONTROLDETAILS_UNSIGNED =
                    Marshal.SizeOf(typeof(MIXERCONTROLDETAILS_UNSIGNED));
                pmxcd.cbStruct = sizeofMIXERCONTROLDETAILS;
                pmxcd.dwControlID = mxc.dwControlID;
                pmxcd.paDetails =
                    Marshal.AllocCoTaskMem(sizeofMIXERCONTROLDETAILS_UNSIGNED);
                pmxcd.cChannels = 1;
                pmxcd.item = 0;
                pmxcd.cbDetails = sizeofMIXERCONTROLDETAILS_UNSIGNED;
                rc = mixerGetControlDetailsA(hmixer, ref pmxcd,
                    MIXER_GETCONTROLDETAILSF_VALUE);
                du = (MIXERCONTROLDETAILS_UNSIGNED) Marshal.PtrToStructure(
                    pmxcd.paDetails, typeof(MIXERCONTROLDETAILS_UNSIGNED));
                vCurrentVol = du.dwValue;
                return retValue;
            }
            retValue = false;
            return retValue;
        }
        private static bool SetVolumeControl(int hmixer, MIXERCONTROL mxc,
             int volume)
        {
            // This function sets the value for a volume control. 
            // Returns True if successful 
            bool retValue;
            int rc;
            MIXERCONTROLDETAILS mxcd = new MIXERCONTROLDETAILS();
            MIXERCONTROLDETAILS_UNSIGNED vol = new
                 MIXERCONTROLDETAILS_UNSIGNED();
            mxcd.item = 0;
            mxcd.dwControlID = mxc.dwControlID;
            mxcd.cbStruct = Marshal.SizeOf(mxcd);
            mxcd.cbDetails = Marshal.SizeOf(vol);
            // Allocate a buffer for the control value buffer 
            mxcd.cChannels = 1;
            vol.dwValue = volume;
            // Copy the data into the control value buffer 
            mxcd.paDetails = Marshal.AllocCoTaskMem(Marshal.SizeOf(
                 typeof(MIXERCONTROLDETAILS_UNSIGNED)));
            Marshal.StructureToPtr(vol, mxcd.paDetails, false);
            // Set the control value 
            rc = mixerSetControlDetails(hmixer, ref mxcd,
                 MIXER_SETCONTROLDETAILSF_VALUE);
            if (MMSYSERR_NOERROR == rc)
            {
                retValue = true;
            }
            else
            {
                retValue = false;
            }
            return retValue;
        }
        public static int GetVolume()
        {
            int mixer;
            MIXERCONTROL volCtrl = new MIXERCONTROL();
            int currentVol;
            mixerOpen(out mixer, 0, 0, 0, 0);
            int type = MIXERCONTROL_CONTROLTYPE_VOLUME;
            GetVolumeControl(mixer,
                 MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, out volCtrl, out 
                   currentVol);
            mixerClose(mixer);
            return currentVol;
        }
        public static void SetVolume(int vVolume)
        {
            int mixer;
            MIXERCONTROL volCtrl = new MIXERCONTROL();
            int currentVol;
            mixerOpen(out mixer, 0, 0, 0, 0);
            int type = MIXERCONTROL_CONTROLTYPE_VOLUME;
            GetVolumeControl(mixer,
                 MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, out volCtrl, out 
                   currentVol);
            if (vVolume > volCtrl.lMaximum)
                vVolume = volCtrl.lMaximum;
            if (vVolume < volCtrl.lMinimum)
                vVolume = volCtrl.lMinimum;
            SetVolumeControl(mixer, volCtrl, vVolume);
            GetVolumeControl(mixer,
                 MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, out volCtrl, out 
                   currentVol);
            if (vVolume != currentVol)
            {
                throw new Exception("Cannot Set Volume");
            }
            mixerClose(mixer);
        }
    }
}本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/09/07/1561809.html,如需转载请自行联系原作者
 

相关文章
|
Windows 数据安全/隐私保护 移动开发
Windows 10 操作系统下利用USB无限网卡创建虚拟WirelessNetwork 类的封装【二】
根据前面的一个内容对功能和合理性上做了扩充和优化。 代码如下: using System; using System.Collections.
782 0
|
数据安全/隐私保护 Windows C#
Windows 10 操作系统下利用USB无限网卡创建虚拟WirelessNetwork 类的封装
最近因为公司的业务要求,在没有网络的情况下,没有路由器支持的情况下,需要组建一个小局域网,可以同时支持多个终端的相互之间的通信,这里的解决方案如下。
755 0
|
Unix Linux Shell
不同的类UNIX操作系统密码破解方法介绍
(一)Linux 系统密码破解 1.在grub选项菜单按E进入编辑模式 2.编辑kernel那行 /init 1 (或/single) 3.按B重启 4.进入后执行下列命令 root@#passwd root (设置root的密码) Enter new unix password:输入新的密码 root@#init 6   (二)debian linux 系统密码破解 1.
760 0
|
18天前
|
监控 Unix Linux
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
31 0
|
19天前
|
Linux 编译器 开发者
Linux设备树解析:桥接硬件与操作系统的关键架构
在探索Linux的庞大和复杂世界时🌌,我们经常会遇到许多关键概念和工具🛠️,它们使得Linux成为了一个强大和灵活的操作系统💪。其中,"设备树"(Device Tree)是一个不可或缺的部分🌲,尤其是在嵌入式系统🖥️和多平台硬件支持方面🔌。让我们深入了解Linux设备树是什么,它的起源,以及为什么Linux需要它🌳。
Linux设备树解析:桥接硬件与操作系统的关键架构
|
1月前
|
Linux 数据安全/隐私保护 虚拟化
Linux技术基础(1)——操作系统的安装
本文是龙蜥操作系统(Anolis OS) 8.4 的安装指南,用户可以从[龙蜥社区下载页面](https://openanolis.cn/download)获取ISO镜像。安装方法包括物理机的光驱和USB闪存方式,以及虚拟机中的VMware Workstation Pro设置。安装过程涉及选择语言、配置安装目标、选择软件集合和内核,设置Root密码及创建新用户。安装完成后,可通过文本模式或图形化界面验证系统版本,如Anolis OS 8.4,标志着安装成功。
|
1月前
|
存储 缓存 算法
Linux--系统结构与操作系统
Linux--系统结构与操作系统