示例#1
0
        /// <summary>
        /// Cleans up any resources being used.
        /// </summary>
        public void Dispose()
        {
            if (m_IsDisposed)
            {
                return;
            }
            m_IsDisposed = true;

            // Release events.
            this.BufferFull = null;

            try{
                // If recording, we need to reset wav device first.
                WavMethods.waveInReset(m_pWavDevHandle);

                // If there are unprepared wav headers, we need to unprepare these.
                foreach (BufferItem item in m_pBuffers)
                {
                    WavMethods.waveInUnprepareHeader(m_pWavDevHandle, item.HeaderHandle.AddrOfPinnedObject(), Marshal.SizeOf(item.Header));
                    item.Dispose();
                }

                // Close input device.
                WavMethods.waveInClose(m_pWavDevHandle);

                m_pInDevice     = null;
                m_pWavDevHandle = IntPtr.Zero;
            }
            catch {
            }
        }
示例#2
0
        protected virtual void Dispose(bool disposing)
        {
            if (!m_IsDisposed)
            {
                // Release events.
                this.BufferFull = null;

                try
                {
                    // If recording, we need to reset wav device first.
                    WavMethods.waveInReset(m_pWavDevHandle);

                    // If there are unprepared wav headers, we need to unprepare these.
                    lock (m_pBuffers)
                    {
                        foreach (BufferItem item in m_pBuffers)
                        {
                            try
                            {
                                WavMethods.waveInUnprepareHeader(m_pWavDevHandle, item.HeaderHandle.AddrOfPinnedObject(), Marshal.SizeOf(item.Header));
                            }
                            catch
                            {
                                Debug.WriteLine("WaveIn.Dispose() - WavMethods.waveInUnprepareHeader()");
                            }
                            item.Dispose();
                        }
                    }

                    // Close input device.
                    Debug.WriteLine("WaveIn.Dispose() - waveInClose");
                    try
                    {
                        WavMethods.waveInClose(m_pWavDevHandle);
                    }
                    catch
                    {
                        Debug.WriteLine("WaveIn.Dispose() - WavMethods.waveInClose()");
                    }
                    finally
                    {
                        m_pInDevice     = null;
                        m_pWavDevHandle = IntPtr.Zero;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("WaveIn.Dispose() - " + ex.Message);
                }
            }
            m_IsDisposed = true;
        }
示例#3
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="outputDevice">Input device.</param>
        /// <param name="samplesPerSec">Sample rate, in samples per second (hertz). For PCM common values are
        /// 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz.</param>
        /// <param name="bitsPerSample">Bits per sample. For PCM 8 or 16 are the only valid values.</param>
        /// <param name="channels">Number of channels.</param>
        /// <param name="bufferSize">Specifies recording buffer size.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>outputDevice</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the aruments has invalid value.</exception>
        public WaveIn(WavInDevice device, int samplesPerSec, int bitsPerSample, int channels, int bufferSize)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            if (samplesPerSec < 8000)
            {
                throw new ArgumentException("Argument 'samplesPerSec' value must be >= 8000.");
            }
            if (bitsPerSample < 8)
            {
                throw new ArgumentException("Argument 'bitsPerSample' value must be >= 8.");
            }
            if (channels < 1)
            {
                throw new ArgumentException("Argument 'channels' value must be >= 1.");
            }

            m_pInDevice     = device;
            m_SamplesPerSec = samplesPerSec;
            m_BitsPerSample = bitsPerSample;
            m_Channels      = channels;
            m_BufferSize    = bufferSize;
            m_BlockSize     = m_Channels * (m_BitsPerSample / 8);
            m_pBuffers      = new List <BufferItem>();

            // Try to open wav device.
            WAVEFORMATEX format = new WAVEFORMATEX();

            format.wFormatTag      = WavFormat.PCM;
            format.nChannels       = (ushort)m_Channels;
            format.nSamplesPerSec  = (uint)samplesPerSec;
            format.nAvgBytesPerSec = (uint)(m_SamplesPerSec * m_Channels * (m_BitsPerSample / 8));
            format.nBlockAlign     = (ushort)m_BlockSize;
            format.wBitsPerSample  = (ushort)m_BitsPerSample;
            format.cbSize          = 0;
            // We must delegate reference, otherwise GC will collect it.
            m_pWaveInProc = new waveInProc(this.OnWaveInProc);
            int result = WavMethods.waveInOpen(out m_pWavDevHandle, m_pInDevice.Index, format, m_pWaveInProc, 0, WavConstants.CALLBACK_FUNCTION);

            if (result != MMSYSERR.NOERROR)
            {
                throw new Exception("Failed to open wav device, error: " + result.ToString() + ".");
            }

            EnsureBuffers();
        }
示例#4
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="outputDevice">Input device.</param>
        /// <param name="samplesPerSec">Sample rate, in samples per second (hertz). For PCM common values are 
        /// 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz.</param>
        /// <param name="bitsPerSample">Bits per sample. For PCM 8 or 16 are the only valid values.</param>
        /// <param name="channels">Number of channels.</param>
        /// <param name="bufferSize">Specifies recording buffer size.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>outputDevice</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the aruments has invalid value.</exception>
        public WaveIn(WavInDevice device, int samplesPerSec, int bitsPerSample, int channels, int bufferSize)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            if (samplesPerSec < 8000)
            {
                throw new ArgumentException("Argument 'samplesPerSec' value must be >= 8000.");
            }
            if (bitsPerSample < 8)
            {
                throw new ArgumentException("Argument 'bitsPerSample' value must be >= 8.");
            }
            if (channels < 1)
            {
                throw new ArgumentException("Argument 'channels' value must be >= 1.");
            }

            m_pInDevice = device;
            m_SamplesPerSec = samplesPerSec;
            m_BitsPerSample = bitsPerSample;
            m_Channels = channels;
            m_BufferSize = bufferSize;
            m_BlockSize = m_Channels * (m_BitsPerSample / 8);
            m_pBuffers = new List<BufferItem>();

            // Try to open wav device.
            WAVEFORMATEX format = new WAVEFORMATEX();
            format.wFormatTag = WavFormat.PCM;
            format.nChannels = (ushort)m_Channels;
            format.nSamplesPerSec = (uint)samplesPerSec;
            format.nAvgBytesPerSec = (uint)(m_SamplesPerSec * m_Channels * (m_BitsPerSample / 8));
            format.nBlockAlign = (ushort)m_BlockSize;
            format.wBitsPerSample = (ushort)m_BitsPerSample;
            format.cbSize = 0;
            // We must delegate reference, otherwise GC will collect it.
            m_pWaveInProc = new waveInProc(this.OnWaveInProc);
            int result = WavMethods.waveInOpen(out m_pWavDevHandle, m_pInDevice.Index, format, m_pWaveInProc, 0, WavConstants.CALLBACK_FUNCTION);
            if (result != MMSYSERR.NOERROR)
            {
                throw new Exception("Failed to open wav device, error: " + result.ToString() + ".");
            }

            EnsureBuffers();
        }
示例#5
0
        /// <summary>
        /// Cleans up any resources being used.
        /// </summary>
        public void Dispose()
        {
            if(m_IsDisposed){
                return;
            }
            m_IsDisposed = true;

            // Release events.
            this.BufferFull = null;

            try{
                // If recording, we need to reset wav device first.
                WavMethods.waveInReset(m_pWavDevHandle);
                
                // If there are unprepared wav headers, we need to unprepare these.
                foreach(BufferItem item in m_pBuffers){
                    WavMethods.waveInUnprepareHeader(m_pWavDevHandle,item.HeaderHandle.AddrOfPinnedObject(),Marshal.SizeOf(item.Header));
                    item.Dispose();
                }
                
                // Close input device.
                WavMethods.waveInClose(m_pWavDevHandle);

                m_pInDevice     = null;
                m_pWavDevHandle = IntPtr.Zero;
            }
            catch{                
            }
        }