⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wavein.cs

📁 本人觉得C#的软件是不需要源代码的
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System.Windows.Forms;
using System;
using Microsoft.WindowsCE.Forms;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;

namespace SmartAnswerCall.处理类
{


    public class WaveIn
    {
        protected WaveFile m_file = null;
        protected SoundMessageWindow m_msgWindow = null;

        public WaveIn()
        {
            this.m_msgWindow = new SoundMessageWindow(this);
            this.m_file = new WaveFile();
        }

        public void BlockDone()
        {
            this.m_file.BlockDone();
        }

        public void Dispose()
        {
            this.m_msgWindow.Dispose();
            if (this.m_file != null)
            {
                this.m_file.Dispose();
            }
        }

        public bool Done()
        {
            return this.m_file.Done;
        }

        public Wave.MMSYSERR GetDeviceName(uint deviceId, ref string prodName)
        {
            WAVEINCAPS waveincaps = new WAVEINCAPS();
            Wave.MMSYSERR mmsyserr = waveInGetDevCaps(deviceId, (byte[])waveincaps, waveincaps.Size);
            if (mmsyserr != Wave.MMSYSERR.NOERROR)
            {
                return mmsyserr;
            }
            prodName = waveincaps.szPname;
            return Wave.MMSYSERR.NOERROR;
        }

        public uint NumDevices()
        {
            return (uint)waveInGetNumDevs();
        }

        public Wave.MMSYSERR Preload(int maxRecordLength_ms, int bufferSize)
        {
            if (this.m_file != null)
            {
                return this.m_file.Preload(0, this.m_msgWindow.Hwnd, maxRecordLength_ms, bufferSize);
            }
            return Wave.MMSYSERR.NOERROR;
        }

        public Wave.MMSYSERR Save(string fileName)
        {
            if (this.m_file != null)
            {
                return this.m_file.Save(fileName);
            }
            return Wave.MMSYSERR.NOERROR;
        }

        public Wave.MMSYSERR Start()
        {
            if (this.m_file != null)
            {
                return this.m_file.Start();
            }
            return Wave.MMSYSERR.NOERROR;
        }

        public void Stop()
        {
            if (this.m_file != null)
            {
                this.m_file.Stop();
            }
        }

        public static void TestProc(MainTest.DisplayLineDelegate showLine)
        {
            WaveIn @in = new WaveIn();
           
            
                uint num = @in.NumDevices();
                if (num < 1)
                {
                    showLine("FAILURE: No valid sound drivers detected");
                }
                else
                {
                    showLine(string.Format("{0} device{1} detected:", num, (num != 1) ? "s" : ""));
                    for (uint i = 0; i < num; i++)
                    {
                        string prodName = "";
                        if (@in.GetDeviceName(i, ref prodName) != Wave.MMSYSERR.NOERROR)
                        {
                            showLine(string.Format(" {0}: Failed to get name", i));
                        }
                        else
                        {
                            showLine(string.Format(" {0}: {1}", i, prodName));
                        }
                    }
                    showLine("Setting max time to 3 seconds");
                    showLine("Using 256KB buffers");
                    if (@in.Preload(0xbb8, 0x40000) != Wave.MMSYSERR.NOERROR)
                    {
                        showLine("FAILURE: Failed to preload buffers");
                    }
                    showLine("Starting recording...");
                    if (@in.Start() != Wave.MMSYSERR.NOERROR)
                    {
                        showLine("FAILURE: Failed to start recording");
                    }
                    showLine("Waiting for 2 seconds...");
                    Thread.Sleep(0x7d0);
                    showLine("Stopping recording early");
                    @in.Stop();
                    string fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "test.wav");
                    showLine("Saving file test.wav");
                    if (@in.Save(fileName) != Wave.MMSYSERR.NOERROR)
                    {
                        showLine("FAILURE: Failed to save file");
                    }
                }
            
        }

        [DllImport("coredll.dll")]
        private static extern Wave.MMSYSERR waveInAddBuffer(IntPtr hwi, Wave.WAVEHDR pwh, uint cbwh);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveInClose(IntPtr hwi);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveInGetDevCaps(uint uDeviceID, byte[] pwic, uint cbwic);
        [DllImport("coredll.dll")]
        protected static extern int waveInGetNumDevs();
        [DllImport("coredll.dll")]
        private static extern Wave.MMSYSERR waveInOpen(ref IntPtr phwi, uint uDeviceID, Wave.WAVEFORMATEX pwfx, IntPtr dwCallback, uint dwInstance, uint fdwOpen);
        [DllImport("coredll.dll")]
        private static extern Wave.MMSYSERR waveInPrepareHeader(IntPtr hwi, Wave.WAVEHDR pwh, uint cbwh);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveInReset(IntPtr hwi);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveInStart(IntPtr hwi);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveInStop(IntPtr hwi);
        [DllImport("coredll.dll")]
        private static extern Wave.MMSYSERR waveInUnprepareHeader(IntPtr hwi, Wave.WAVEHDR pwh, uint cbwh);

        protected class SoundMessageWindow : MessageWindow
        {
            protected WaveIn m_wi = null;
            public const int MM_WIM_CLOSE = 0x3bf;
            public const int MM_WIM_DATA = 960;
            public const int MM_WIM_OPEN = 0x3be;

            public SoundMessageWindow(WaveIn wi)
            {
                this.m_wi = wi;
            }

            protected override void WndProc(ref Message msg)
            {
                if ((msg.Msg == 960) && (this.m_wi != null))
                {
                    this.m_wi.BlockDone();
                }
                base.WndProc(ref msg);
            }
        }

        protected class WaveFile : IDisposable
        {
            protected uint m_bufferSize;
            protected int m_curBlock;
            protected IntPtr m_hwi = IntPtr.Zero;
            protected bool m_inited = false;
            protected uint m_maxDataLength;
            protected int m_numBlocks;
            protected bool m_recording = false;
            protected Wave.WAVEFORMATEX m_wfmt = null;
            protected Wave.WAVEHDR[] m_whdr = null;

            public void BlockDone()
            {
                this.m_curBlock++;
                if (this.m_curBlock < this.m_numBlocks)
                {
                    this.InitBuffer(this.m_curBlock + 1);
                }
                else if (this.m_curBlock == this.m_numBlocks)
                {
                    this.Stop();
                }
            }

            public void Dispose()
            {
                this.Stop();
                this.FreeWaveBuffers();
            }

            private void FreeWaveBuffers()
            {
                this.m_inited = false;
                if (this.m_whdr != null)
                {
                    for (int i = 0; i < this.m_whdr.Length; i++)
                    {
                        if (this.m_whdr[i] != null)
                        {
                            WaveIn.waveInUnprepareHeader(this.m_hwi, this.m_whdr[i], (uint)Marshal.SizeOf(this.m_whdr[i]));
                            this.m_whdr[i].Dispose();
                            this.m_whdr[i] = null;
                        }
                    }
                    this.m_whdr = null;
                }
                WaveIn.waveInClose(this.m_hwi);
                this.m_hwi = IntPtr.Zero;
            }

            public Wave.MMSYSERR InitBuffer(int bufIndex)
            {
                uint bufferSize = this.m_bufferSize;
                if (bufIndex < this.m_numBlocks)
                {
                    uint num2 = this.m_maxDataLength - ((uint)(bufIndex * this.m_bufferSize));
                    if (this.m_bufferSize > num2)
                    {
                        bufferSize = num2;
                    }
                }
                if (this.m_whdr[bufIndex] == null)
                {
                    this.m_whdr[bufIndex] = new Wave.WAVEHDR();
                }
                Wave.MMSYSERR mmsyserr = this.m_whdr[bufIndex].Init(bufferSize, false);
                if (mmsyserr != Wave.MMSYSERR.NOERROR)
                {
                    return mmsyserr;
                }
                mmsyserr = WaveIn.waveInPrepareHeader(this.m_hwi, this.m_whdr[bufIndex], (uint)Marshal.SizeOf(this.m_whdr[bufIndex]));
                if (mmsyserr != Wave.MMSYSERR.NOERROR)
                {
                    return mmsyserr;
                }
                return WaveIn.waveInAddBuffer(this.m_hwi, this.m_whdr[bufIndex], (uint)Marshal.SizeOf(this.m_whdr[bufIndex]));
            }

            public Wave.MMSYSERR Preload(uint curDevice, IntPtr hwnd, int maxRecordLength_ms, int bufferSize)
            {
                if (this.m_recording)
                {
                    return Wave.MMSYSERR.ERROR;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -