コード例 #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);
        }
コード例 #2
0
        /// <summary>
        ///     OpenWaveOuz
        /// </summary>
        /// <returns></returns>
        private bool OpenWaveOut()
        {
            if (hWaveOut == IntPtr.Zero)
            {
                //Wenn nicht schon offen
                if (IsWaveOutOpened == 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;

                    //WaveOut Gerät ermitteln
                    var deviceId = WinSound.GetWaveOutDeviceIdByName(WaveOutDeviceName);
                    //WaveIn Gerät öffnen
                    var hr = Win32.waveOutOpen(ref hWaveOut, deviceId, ref waveFormatEx, delegateWaveOutProc, 0,
                                               (int)Win32.WaveProcFlags.CALLBACK_FUNCTION);

                    //Wenn nicht erfolgreich
                    if (hr != Win32.MMRESULT.MMSYSERR_NOERROR)
                    {
                        IsWaveOutOpened = false;
                        return(false);
                    }

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

            IsWaveOutOpened = true;
            return(true);
        }