实时采集并显示录音波形miniform.cs

using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Runtime.InteropServices;
using  System.Text;


namespace  cswavrec
{
    
/// <summary>
    
/// Summary description for Form1.
    
/// </summary>

    public class MainForm : System.Windows.Forms.Form
    
{
        
/// <summary>
        
/// Required designer variable.
        
/// </summary>

        private System.ComponentModel.Container components = null;
        
private System.Windows.Forms.Button StopButton;
        
private System.Windows.Forms.Button StartButton;
        
private Button button1;
        
private PictureBox pictureBox1;
        
private TextBox textBox1;
        
private System.Windows.Forms.OpenFileDialog OpenDlg;

        
public MainForm()
        
{
            
//
            
// Required for Windows Form Designer support
            
//
            InitializeComponent();
        }


        
/// <summary>
        
/// Clean up any resources being used.
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows Form Designer generated code

        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new MainForm());
        }


        
        
private WaveLib.WaveInRecorder m_Recorder;
        
//private WaveLib.FifoStream m_Fifo = new WaveLib.FifoStream();

        
        
private byte[] m_RecBuffer;

        
/*private void Filler(IntPtr data, int size)
        {
            if (m_PlayBuffer == null || m_PlayBuffer.Length < size)
                m_PlayBuffer = new byte[size];
            if (m_Fifo.Length >= size)
                m_Fifo.Read(m_PlayBuffer, 0, size);
            else
                for (int i = 0; i < m_PlayBuffer.Length; i++)
                    m_PlayBuffer[i] = 0;
            System.Runtime.InteropServices.Marshal.Copy(m_PlayBuffer, 0, data, size);
        }
*/


        
private void DataArrived(IntPtr data, int size)
        
{
           
int 音频数据 = 0;
            
            
if (m_RecBuffer == null || m_RecBuffer.Length < size)
                m_RecBuffer 
= new byte[size];

            WaveLib.WaveNative.FillRect(hd内存画板1句柄, 
out 填充范围, hd刷子句柄);
            WaveLib.WaveNative.MoveToEx(hd内存画板1句柄, 
064 + m_RecBuffer[1/ 2560);
            
            
for (int i = 1; i <256; i++)
            
{
                
                音频数据 
= (BitConverter.ToInt16(m_RecBuffer, i * 4 - 3+ BitConverter.ToInt16(m_RecBuffer, i * 4 - 1)) / 2;
                音频数据 
= 音频数据 / size;
                WaveLib.WaveNative.LineTo(hd内存画板1句柄, i, 
64+音频数据);
                
            }

            WaveLib.WaveNative.BitBlt(hd画板1句柄, 
00256127, hd内存画板1句柄, 000xcc0020);

            Marshal.Copy(data, m_RecBuffer, 
0, size);
        }



       

        
private void Stop()
        
{
            
            
if (m_Recorder != null)
                
try
                
{
                    m_Recorder.Dispose();
                }

                
finally
                
{
                    m_Recorder 
= null;
                }

            
        }


        
private void Start()
        
{
            Stop();
            
try
            
{
                WaveLib.WaveFormat fmt 
= new WaveLib.WaveFormat();
                
                m_Recorder 
= new WaveLib.WaveInRecorder(-1, fmt,10242new WaveLib.BufferDoneEventHandler(DataArrived));
                
            }

            
catch
            
{
                Stop();
                
throw;
            }

        }


        
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        
{
            Stop();
        }


        
private void StartButton_Click(object sender, System.EventArgs e)
        
{
            StartButton.Enabled 
= false;
            StopButton.Enabled 
= true;
            Start();
        }


        
private void StopButton_Click(object sender, System.EventArgs e)
        
{
            StartButton.Enabled 
= true;
            StopButton.Enabled 
= false;
            Stop();
        }


        
private void button1_Click(object sender, EventArgs e)
        
{


/* MIXERLINE_COMPONENTTYPE_SRC_DIGITAL     数码
 MIXERLINE_COMPONENTTYPE_SRC_LINE     Line In
 MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE     麦克风
 MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER     电子合成器(MIDI)
 MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC     CD
 MIXERLINE_COMPONENTTYPE_SRC_TELEPHONE     电话(调制解调器)
 MIXERLINE_COMPONENTTYPE_SRC_PCSPEAKER     PC
 MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT      波形
 MIXERLINE_COMPONENTTYPE_SRC_AUXILIARY      AUX
 MIXERLINE_COMPONENTTYPE_SRC_ANALOG     辅助
*/


            SetInLine设录制线路(WaveLib.WaveNative.MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE);


        }


        
public bool SetInLine设录制线路(uint line)
        
{
            
int fmixerhandle=new int ();
            fmixerhandle 
= 0;
            
int mixerID;
            WaveLib.WaveNative.MIXERCAPS MIXERCAPS
=new WaveLib.WaveNative.MIXERCAPS ();
            WaveLib.WaveNative.MIXERLINE MIXERLINE1
=new WaveLib.WaveNative.MIXERLINE ();
            WaveLib.WaveNative.MIXERLINE MIXERLINE2
=new WaveLib.WaveNative.MIXERLINE ();
            WaveLib.WaveNative.MIXERLINECONTROLS MIXERLINECONTROLS
=new WaveLib.WaveNative.MIXERLINECONTROLS ();
            WaveLib.WaveNative.MIXERCONTROLDETAILS MIXERCONTROLDETAILS
=new WaveLib.WaveNative.MIXERCONTROLDETAILS ();
            
byte[] tmp;
            MIXERCAPS.szPname 
= new byte[32];
            
            MIXERLINE1.szShortName 
= new byte[16];
            MIXERLINE1.szName 
= new byte[64];
            MIXERLINE1.szPname 
= new byte[32];

            MIXERLINE2.szShortName 
= new byte[16];
            MIXERLINE2.szName 
= new byte[64];
            MIXERLINE2.szPname 
= new byte[32];

            MIXERLINECONTROLS.pamxctrl 
= new byte[1];


           
// int LineSelet线路索引;
           
// int ControlSelet控制索引;
            int Controlindex控制位置;
            Controlindex控制位置 
= 0;
           
            
if (WaveLib.WaveNative.mixerGetNumDevs() == 0)
            
{
                
return false;
            }

            mixerID 
= 0;
            
if (WaveLib.WaveNative.mixerOpen(out fmixerhandle, mixerID, 000!= 0)
            
{
                
return false;
            }


            
if (WaveLib.WaveNative.mixerGetDevCaps(mixerID, ref MIXERCAPS, 48!= 0)
            
{
                WaveLib.WaveNative.mixerClose(fmixerhandle);
                
return false;
            }

            MIXERLINE1.cbStruct
=168;
            MIXERLINE1.dwDestination
=WaveLib.WaveNative.MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
            
if(WaveLib .WaveNative.mixerGetLineInfo(fmixerhandle,out MIXERLINE1,WaveLib.WaveNative.MIXER_GETLINEINFOF_DESTINATION)!=0)
            
{
                WaveLib.WaveNative.mixerClose(fmixerhandle);
                
return false;
            }


            
if (MIXERLINE1.cControls <= 0 || MIXERLINE1.cConnections <= 0)
            
{
                WaveLib.WaveNative.mixerClose(fmixerhandle);
                
return false;
            }


            MIXERLINE2.cbStruct 
= 168;
            MIXERLINE2.dwDestination 
= WaveLib.WaveNative.MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
            
for (int i = 0; i < MIXERLINE1.cConnections - 1; i++)
            
{
                MIXERLINE2.dwSource 
= i;
                
if (WaveLib.WaveNative.mixerGetLineInfo(fmixerhandle, out MIXERLINE2, WaveLib.WaveNative.MIXER_GETLINEINFOF_SOURCE) != 0)
                
{
                    WaveLib.WaveNative.mixerClose(fmixerhandle);
                    
return false;
                }

                
if (MIXERLINE2.dwComponentType == line)
                
{
                    Controlindex控制位置 
= MIXERLINE1.cConnections - i;
                    
break;
                }

            }


            
if (Controlindex控制位置 == 0)
            
{
                
if (line == WaveLib.WaveNative.MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT)
                
{
                    Controlindex控制位置 
= MIXERLINE1.cConnections;
                }

                
else
                
{
                    WaveLib.WaveNative.mixerClose(fmixerhandle);
                    
return false;
                }

            }


            MIXERLINECONTROLS.cbStruct 
= 24;
            MIXERLINECONTROLS.dwLineID 
= MIXERLINE1.dwLineID;
            MIXERLINECONTROLS.cControls 
= MIXERLINE1.cControls;
            MIXERLINECONTROLS.cbmxctrl 
= 148;
            MIXERLINECONTROLS.pamxctrl 
= new byte[148 * MIXERLINE1.cControls];
            
if (WaveLib.WaveNative.mixerGetLineControls(fmixerhandle, out MIXERLINECONTROLS, WaveLib.WaveNative.MIXER_GETLINECONTROLSF_ALL) != 0)
            
{
                WaveLib.WaveNative.mixerClose(fmixerhandle);
                
return false;
            }

            tmp 
= MIXERLINECONTROLS.pamxctrl;
            
for (int j = 0; j < MIXERLINECONTROLS.cControls; j++)
            
{
                
if (BitConverter.ToUInt32(tmp, GetStuctNum(j, "dwControlType")) == WaveLib.WaveNative.MIXERCONTROL_CONTROLTYPE_MUX)
                
{
                    MIXERCONTROLDETAILS.cbStruct 
= 24;
                    MIXERCONTROLDETAILS.dwControlID 
= BitConverter.ToUInt32(tmp, GetStuctNum(j, "dwControlID"));
                    MIXERCONTROLDETAILS.cChannels 
= 1;
                    MIXERCONTROLDETAILS.union 
= BitConverter.ToUInt32(tmp, GetStuctNum(j, "cMultipleItems"));
                    MIXERCONTROLDETAILS.cbDetails 
= 4;

                    MIXERCONTROLDETAILS.paDetails 
= new int[MIXERLINE1.cConnections];
                    MIXERCONTROLDETAILS.paDetails[j] 
= 1;
                    
if (WaveLib.WaveNative.mixerSetControlDetails(fmixerhandle, out MIXERCONTROLDETAILS, WaveLib.WaveNative.MIXER_OBJECTF_HMIXER + WaveLib.WaveNative.MIXER_GETCONTROLDETAILSF_VALUE) == 0)
                    
{
                        WaveLib.WaveNative.mixerClose(fmixerhandle);
                        
return true;
                    }

                    
else
                    
{
                        WaveLib.WaveNative.mixerClose(fmixerhandle);
                        
return false;
                    }


                }


                WaveLib.WaveNative.mixerClose(fmixerhandle);
                
return false;
            }


                
return true;

        }



        
public int GetStuctNum(int selectindex, string StructName)
        
{
            
if (StructName == "cbStruct"return 148 * (selectindex - 1+ 1;
            
if (StructName == "dwControlID"return 148 * (selectindex - 1+ 5;
            
if (StructName == "dwControlType"return 148 * (selectindex - 1+ 9;
            
if (StructName == "fdwControl"return 148 * (selectindex - 1+ 13;
            
if (StructName == "cMultipleItems"return 148 * (selectindex - 1+ 17;
            
if (StructName == "szShortName"return 148 * (selectindex - 1+ 21;
            
if (StructName == "szName"return 148 * (selectindex - 1+ 37;
            
if (StructName == "Bounds"return 148 * (selectindex - 1+ 101;
            
if (StructName == "Metrics"return 148 * (selectindex - 1+ 125;
            
return -1;

        }

        
public IntPtr hd画板1句柄;
        
public IntPtr hd内存画板;
        
public IntPtr hd内存画板1句柄;
        
public IntPtr hd位图句柄1;
        
public IntPtr hd画笔句柄;
        
public IntPtr hd刷子句柄;
        
public Rectangle 填充范围;

        
private void MainForm_Load(object sender, EventArgs e)
        
{
            hd画板1句柄 
= WaveLib.WaveNative.GetDC(pictureBox1.Handle);
            hd内存画板1句柄 
= WaveLib.WaveNative.CreateCompatibleDC(hd画板1句柄);
            hd位图句柄1 
= WaveLib.WaveNative.CreateCompatibleBitmap(hd画板1句柄, pictureBox1.Width, pictureBox1.Height);
            WaveLib.WaveNative.SelectObject(hd内存画板1句柄, hd位图句柄1);
            hd画笔句柄 
= WaveLib.WaveNative.CreatePen(0165280);
            WaveLib.WaveNative.SelectObject(hd内存画板1句柄, hd画笔句柄);
            
            
           
            hd刷子句柄
=WaveLib .WaveNative.CreateSolidBrush(0);
            
/*填充范围.left=0;
            填充范围.top=0;
            填充范围.right=pictureBox1.Width;
            填充范围.bottom = pictureBox1.Height;
*/

            填充范围.X 
= 0;
            填充范围.Y 
= 0;
            填充范围.Width 
= pictureBox1.Width;
            填充范围.Height 
= pictureBox1.Height;


        }

    }

}

 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值