Exemple #1
0
        private bool OpenWaveIn()
        {
            if (_hWaveIn == IntPtr.Zero)
            {
                if (_isWaveInOpened == false)
                {
                    var waveFormatEx = new Win32.WAVEFORMATEX
                    {
                        wFormatTag     = (ushort)Win32.WaveFormatFlags.WAVE_FORMAT_PCM,
                        nChannels      = (ushort)_channels,
                        nSamplesPerSec = (ushort)_samplesPerSecond,
                        wBitsPerSample = (ushort)_bitsPerSample
                    };

                    waveFormatEx.nBlockAlign     = (ushort)((waveFormatEx.wBitsPerSample * waveFormatEx.nChannels) >> 3);
                    waveFormatEx.nAvgBytesPerSec = waveFormatEx.nBlockAlign * waveFormatEx.nSamplesPerSec;

                    var deviceId = WinSound.GetWaveInDeviceIdByName(_waveInDeviceName);
                    Win32.waveInOpen(ref _hWaveIn, deviceId, ref waveFormatEx, _delegateWaveInProc, 0, (int)Win32.WaveProcFlags.CALLBACK_FUNCTION);
                    if (_hWaveIn == IntPtr.Zero)
                    {
                        return(false);
                    }

                    GCHandle.Alloc(_hWaveIn, GCHandleType.Pinned);
                }
            }

            _isWaveInOpened = true;
            return(true);
        }
        /// <summary>
        ///     OpenWaveIn
        /// </summary>
        /// <returns></returns>
        private bool OpenWaveIn()
        {
            if (hWaveIn == IntPtr.Zero)
            {
                //Wenn nicht schon offen
                if (IsWaveInOpened == false)
                {
                    //Format bestimmen
                    var waveFormatEx = new Win32.WAVEFORMATEX();
                    waveFormatEx.wFormatTag      = (ushort)Win32.WaveFormatFlags.WAVE_FORMAT_PCM;
                    waveFormatEx.nChannels       = (ushort)Channels;
                    waveFormatEx.nSamplesPerSec  = (ushort)SamplesPerSecond;
                    waveFormatEx.wBitsPerSample  = (ushort)BitsPerSample;
                    waveFormatEx.nBlockAlign     = (ushort)((waveFormatEx.wBitsPerSample * waveFormatEx.nChannels) >> 3);
                    waveFormatEx.nAvgBytesPerSec = waveFormatEx.nBlockAlign * waveFormatEx.nSamplesPerSec;

                    //WaveIn Gerät ermitteln
                    var deviceId = WinSound.GetWaveInDeviceIdByName(WaveInDeviceName);
                    //WaveIn Gerät öffnen
                    var hr = Win32.waveInOpen(ref hWaveIn, deviceId, ref waveFormatEx, delegateWaveInProc, 0,
                                              (int)Win32.WaveProcFlags.CALLBACK_FUNCTION);

                    //Wenn nicht erfolgreich
                    if (hWaveIn == IntPtr.Zero)
                    {
                        IsWaveInOpened = false;
                        return(false);
                    }

                    //Handle sperren
                    GCHandle.Alloc(hWaveIn, GCHandleType.Pinned);
                }
            }

            IsWaveInOpened = true;
            return(true);
        }