Beispiel #1
0
        /// <summary>
        /// Open the recorder.
        /// </summary>
        /// <param name="filename">The path and filename of the file to write the audio to.</param>
        /// <param name="format">The audio wave format.</param>
        /// <param name="audioRecordingFormat">The audio recording format.</param>
        public void Open(string filename, WaveFormatProvider format = null, AudioRecordingFormat audioRecordingFormat = AudioRecordingFormat.WaveIn)
        {
            // If not created.
            if (!_waveInCreated)
            {
                _filename       = filename;
                _audioStream    = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read);
                _internalStream = true;
                _isBufferStream = false;

                // Initialise.
                Init(format, audioRecordingFormat);
                _waveInCreated = true;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Initialise the waveIn stream.
        /// </summary>
        /// <param name="format">The audio wave format.</param>
        /// <param name="audioRecordingFormat">The audio recording format.</param>
        private void Init(WaveFormatProvider format, AudioRecordingFormat audioRecordingFormat)
        {
            _audioFormat = audioRecordingFormat;

            // Get all the catpure devices.
            MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
            MMDeviceCollection devices = DevEnum.EnumerateAudioEndPoints(EDataFlow.Capture, EDeviceState.Active);

            // Select the device index.
            _mmDevice = devices[_device.Index];

            // If null the setup defaults.
            if (format == null)
            {
                // Select the provider.
                switch (audioRecordingFormat)
                {
                case AudioRecordingFormat.WasapiLoopback:
                    _waveIn = new WasapiLoopbackCapture(_mmDevice);
                    break;

                case AudioRecordingFormat.Wasapi:
                    _waveIn            = new WasapiCapture(_mmDevice);
                    _waveIn.WaveFormat = new WaveFormatProvider(8000, 16, 1);
                    break;

                case AudioRecordingFormat.WaveInEvent:
                    _waveIn = new WaveInEvent()
                    {
                        DeviceNumber = _device.Index, BufferMilliseconds = _bufferMilliseconds
                    };
                    _waveIn.WaveFormat = new WaveFormatProvider(8000, 16, 1);
                    break;

                case AudioRecordingFormat.WaveIn:
                default:
                    _waveIn = new WaveIn()
                    {
                        DeviceNumber = _device.Index, BufferMilliseconds = _bufferMilliseconds
                    };
                    _waveIn.WaveFormat = new WaveFormatProvider(8000, 16, 1);
                    break;
                }
            }
            else
            {
                // Select the provider.
                switch (audioRecordingFormat)
                {
                case AudioRecordingFormat.WasapiLoopback:
                    _waveIn = new WasapiLoopbackCapture(_mmDevice);
                    break;

                case AudioRecordingFormat.Wasapi:
                    _waveIn            = new WasapiCapture(_mmDevice);
                    _waveIn.WaveFormat = format;
                    break;

                case AudioRecordingFormat.WaveInEvent:
                    _waveIn = new WaveInEvent()
                    {
                        DeviceNumber = _device.Index, BufferMilliseconds = _bufferMilliseconds
                    };
                    _waveIn.WaveFormat = format;
                    break;

                case AudioRecordingFormat.WaveIn:
                default:
                    _waveIn = new WaveIn()
                    {
                        DeviceNumber = _device.Index, BufferMilliseconds = _bufferMilliseconds
                    };
                    _waveIn.WaveFormat = format;
                    break;
                }
            }

            // Set the capture.
            _waveIn.DataAvailable    += _waveIn_DataAvailable;
            _waveIn.RecordingStopped += _waveIn_RecordingStopped;
        }
Beispiel #3
0
        /// <summary>
        /// Open the recorder.
        /// </summary>
        /// <param name="outStream">The stream to write the audio to.</param>
        /// <param name="format">The audio wave format.</param>
        /// <param name="audioRecordingFormat">The audio recording format.</param>
        public void Open(System.IO.Stream outStream, WaveFormatProvider format = null, AudioRecordingFormat audioRecordingFormat = AudioRecordingFormat.WaveIn)
        {
            // If not created.
            if (!_waveInCreated)
            {
                _audioStream    = outStream;
                _internalStream = false;
                _isBufferStream = false;

                // Initialise.
                Init(format, audioRecordingFormat);
                _waveInCreated = true;
            }
        }