📄 mixer.cs
字号:
using System;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace gowk.multimedia
{
public class Mixer
{
IntPtr m_hmx;
public event System.EventHandler MixerControlChange;
private event System.EventHandler PreMixerControlChange;
private void OnMixerControlChange(int controlid)
{
if(this.PreMixerControlChange!=null)this.PreMixerControlChange(this,System.EventArgs.Empty);
if(this.MixerControlChange!=null)this.MixerControlChange(this,System.EventArgs.Empty);
}
MixerCallbackListener listener;
public Mixer(Control c)
{
this.listener=new MixerCallbackListener(this);
this.listener.AssignHandle(c.Handle);
Mixer.mixerOpen(ref m_hmx,0,(int)c.Handle,0,Mixer.MIXER_OBJECTF_MIXER|0x00010000);
} public IntPtr Handle
{ get{return this.m_hmx;} }
public void Close()
{
// this.listener.ReleaseHandle();
Mixer.mixerClose(this.m_hmx);
}
private class MixerCallbackListener:NativeWindow
{
private Mixer mixer;
public MixerCallbackListener(Mixer mixer)
{
this.mixer=mixer;
}
protected override void WndProc(ref Message m)
{
if(m.Msg==Mixer.MM_MIXM_CONTROL_CHANGE)
{
this.mixer.OnMixerControlChange((int)m.LParam);
}
base.WndProc (ref m);
}
} public class MixerControlDetail
{ private int component_type; Mixer mixer; uint lineid; int mute_controlid,volume_controlid; string name; int max,min; bool mute;int volume; public string Name
{ get{return this.name;} } public int Volume
{ get{return this.volume;} set{this.volume=value;this.SetValue(volume_controlid,value);} } public bool Mute
{ get{return this.mute;} set { if(this.mute!=value)
{ this.mute=value; if(value) this.SetValue(mute_controlid,1); else this.SetValue(mute_controlid,0); } } }
public int Max
{ get{return this.max;} } public int Min
{ get{return this.min;} } public MixerControlDetail(Mixer mixer,int component_type)
{ this.component_type=component_type; this.mixer=mixer; this.Initialize(); mixer.PreMixerControlChange+=new EventHandler(mixer_PreMixerControlChange); } private MIXERLINE GetLine()
{
MIXERLINE ml=new MIXERLINE();
if(this.component_type==MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE)
{
MIXERLINE inLine=new MIXERLINE();
inLine.cbStruct=Marshal.SizeOf(inLine);
inLine.dwComponentType=Mixer.MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
Mixer.mixerGetLineInfo((int)mixer.Handle,ref inLine,Mixer.MIXER_GETLINEINFOF_COMPONENTTYPE);
for(int i=0;i<inLine.cConnections;i++)
{
ml=new MIXERLINE();
ml.cbStruct=Marshal.SizeOf(ml);
ml.dwSource=(uint)i;
ml.dwDestination=inLine.dwDestination;
Mixer.mixerGetLineInfo((int)mixer.Handle,ref ml,Mixer.MIXER_GETLINEINFOF_SOURCE);
if(ml.dwComponentType==this.component_type)break;
}
}
else
{
ml=new MIXERLINE();
ml.cbStruct=Marshal.SizeOf(ml);
ml.dwComponentType=this.component_type;
Mixer.mixerGetLineInfo((int)mixer.Handle,ref ml, Mixer.MIXER_OBJECTF_HMIXER|Mixer.MIXER_GETLINEINFOF_COMPONENTTYPE); } return ml; } private void Initialize()
{ MIXERLINE ml=this.GetLine(); this.lineid=ml.dwLineID; this.name=ml.szName; MIXERCONTROL ctl=this.GetLineControl(Mixer.MIXERCONTROL_CONTROLTYPE_VOLUME); this.min=ctl.Bounds.dwMinimum; this.max=ctl.Bounds.dwMaximum; this.volume_controlid=ctl.dwControlID; ctl=this.GetLineControl(Mixer.MIXERCONTROL_CONTROLTYPE_MUTE); this.mute_controlid=ctl.dwControlID; this.volume=this.GetValue(this.volume_controlid); this.mute=this.GetValue(this.mute_controlid)==0; // System.Windows.Forms.MessageBox.Show(mute.ToString()); // MIXERCONTROLDETAILS_UNSIGNED uo=new MIXERCONTROLDETAILS_UNSIGNED(); // uo.dwValue=0; // MIXERCONTROLDETAILS detail=this.GetMIXERCONTROLDETAILS(this.volume_controlid,ref uo); // this.volume=uo.dwValue; // MIXERCONTROLDETAILS_BOOLEAN bo=new MIXERCONTROLDETAILS_BOOLEAN(); // detail=this.GetMIXERCONTROLDETAILS(this.mute_controlid,bo); // this.mute=bo.fValue; }
private int GetValue(int controlid)
{
IntPtr volumes=Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(int))*2);
Marshal.WriteInt32(volumes,0);
gowk.multimedia.MIXERCONTROLDETAILS details=new MIXERCONTROLDETAILS();
details.cbStruct=Marshal.SizeOf(details);
details.cChannels=1;
details.cbDetails=4;
details.cMultipleItems=0;
details.dwControlID=controlid;
details.paDetails=(int)volumes;
Mixer.mixerGetControlDetails(mixer.Handle,ref details,0);
int ret=Marshal.ReadInt32(volumes);
Marshal.FreeCoTaskMem(volumes);
return ret;
} private void SetValue(int controlid,int value)
{
IntPtr volumes=Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(int))*2);
Marshal.WriteInt32(volumes,value);
gowk.multimedia.MIXERCONTROLDETAILS details=new MIXERCONTROLDETAILS();
details.cbStruct=Marshal.SizeOf(details);
details.cChannels=1;
details.cbDetails=4;
details.cMultipleItems=0;
details.dwControlID=controlid;
details.paDetails=(int)volumes;
int r=Mixer.mixerSetControlDetails(mixer.Handle,ref details,0);
System.Diagnostics.Trace.WriteLine(r.ToString());
int ret=Marshal.ReadInt32(volumes);
System.Diagnostics.Trace.WriteLine(ret.ToString());
Marshal.FreeCoTaskMem(volumes); } private MIXERCONTROL GetLineControl(int controlType)
{
MIXERCONTROL mxc=new MIXERCONTROL();mxc.Bounds=new Volume();
MIXERLINECONTROLS mxlc=new MIXERLINECONTROLS();
IntPtr p=Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(MIXERCONTROL)));
Marshal.StructureToPtr(mxc,p,true);
mxlc.cbStruct = Marshal.SizeOf(typeof(MIXERLINECONTROLS));
mxlc.dwLineID =this.lineid;
mxlc.dwControlType =controlType;
mxlc.cControls = 1;
mxlc.cbmxctrl = Marshal.SizeOf(typeof(MIXERCONTROL));
mxlc.pamxctrl = p;
Mixer.mixerGetLineControls((int)mixer.Handle,ref mxlc,Mixer.MIXER_OBJECTF_HMIXER |Mixer.MIXER_GETLINECONTROLSF_ONEBYTYPE);
mxc=(MIXERCONTROL)Marshal.PtrToStructure(mxlc.pamxctrl,typeof(MIXERCONTROL)); return mxc; }
private void mixer_PreMixerControlChange(object sender, EventArgs e)
{
this.Initialize();
}
} #region /* private void InitialSpeaker()
{
MIXERLINE ml=new MIXERLINE();
ml.cbStruct=Marshal.SizeOf(ml);
ml.dwComponentType=Mixer.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
Mixer.mixerGetLineInfo((int)m_hmx,ref ml, Mixer.MIXER_OBJECTF_HMIXER|Mixer.MIXER_GETLINEINFOF_COMPONENTTYPE);
MIXERCONTROL mxc=new MIXERCONTROL();mxc.Bounds=new Volume();
MIXERLINECONTROLS mxlc=new MIXERLINECONTROLS();
IntPtr p=Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(MIXERCONTROL)));
Marshal.StructureToPtr(mxc,p,true);
mxlc.cbStruct = Marshal.SizeOf(typeof(MIXERLINECONTROLS));
mxlc.dwLineID =ml.dwLineID;
mxlc.dwControlType = Mixer.MIXERCONTROL_CONTROLTYPE_VOLUME;
mxlc.cControls = 1;
mxlc.cbmxctrl = Marshal.SizeOf(typeof(MIXERCONTROL));
mxlc.pamxctrl = p;
Mixer.mixerGetLineControls((int)m_hmx,ref mxlc,Mixer.MIXER_OBJECTF_HMIXER |Mixer.MIXER_GETLINECONTROLSF_ONEBYTYPE);
mxc=(MIXERCONTROL)Marshal.PtrToStructure(mxlc.pamxctrl,typeof(MIXERCONTROL)); this.SpeakerVolumeRange=mxc.Bounds; this.dwControlID_speaker=mxc.dwControlID; } private void InitialMicrophone()
{
MIXERLINE ml=new MIXERLINE();
ml.cbStruct=Marshal.SizeOf(ml);
ml.dwComponentType=Mixer.MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
Mixer.mixerGetLineInfo((int)m_hmx,ref ml, Mixer.MIXER_OBJECTF_HMIXER|Mixer.MIXER_GETLINEINFOF_COMPONENTTYPE);
MIXERCONTROL mxc=new MIXERCONTROL();mxc.Bounds=new Volume();
MIXERLINECONTROLS mxlc=new MIXERLINECONTROLS();
IntPtr p=Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(MIXERCONTROL)));
Marshal.StructureToPtr(mxc,p,true);
mxlc.cbStruct = Marshal.SizeOf(typeof(MIXERLINECONTROLS));
mxlc.dwLineID =ml.dwLineID;
mxlc.dwControlType = Mixer.MIXERCONTROL_CONTROLTYPE_VOLUME;
mxlc.cControls = 1;
mxlc.cbmxctrl = Marshal.SizeOf(typeof(MIXERCONTROL));
mxlc.pamxctrl = p;
Mixer.mixerGetLineControls((int)m_hmx,ref mxlc,Mixer.MIXER_OBJECTF_HMIXER |Mixer.MIXER_GETLINECONTROLSF_ONEBYTYPE);
mxc=(MIXERCONTROL)Marshal.PtrToStructure(mxlc.pamxctrl,typeof(MIXERCONTROL));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -