Ejemplo n.º 1
0
        public WasapiRecorder(Stream stream, int sampleRate, int bitRate, int channels, EncodingFormat format, int preset)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (channels != 1 && channels != 2)
            {
                throw new ArgumentException(string.Format("Unsupported number of channels {0}", channels), "channels");
            }

            if (bitRate != 16)
            {
                throw new ArgumentException(string.Format("Unsupported PCM sample size {0}", bitRate), "bitRate");
            }

            if (sampleRate < 8000 || sampleRate > 48000)
            {
                throw new ArgumentException(string.Format("Unsupported Sample Rate {0}", sampleRate), "sampleRate");
            }

            _stream     = stream;
            _sampleRate = sampleRate;
            _bitRate    = bitRate;
            _channels   = channels;
            _format     = format;
            _preset     = preset;

            _capture = new AudioCapture();
            _capture.SetCaptureFormat(_sampleRate, (ushort)_channels, (ushort)_bitRate);

            _size     = 0;
            _duration = 0;
            _state    = RecorderState.Unknown;

            if (_format == EncodingFormat.Wave)
            {
                _writer = new WaveWriter(_stream, _sampleRate, _bitRate, _channels);
            }
            else if (_format == EncodingFormat.MP3)
            {
                _writer = new Mp3Writer(_stream, _sampleRate, _bitRate, _channels, (LAMEPreset)_preset);
            }

            _timer          = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromMilliseconds(33);
            _timer.Tick    += OnSampleReceived;
        }