Exemplo n.º 1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (deviceslist.SelectedItems.Count <= 0)
                return;

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "WAV (*.wav)|*.wav";
            sfd.Title = "Speichern";
            sfd.FileName = String.Empty;
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _waveIn = new WaveInEvent(new WaveFormat(44100, 16, _selectedDevice.Channels));
                _waveIn.Device = deviceslist.SelectedItems[0].Index;

                _waveIn.Initialize();
                _waveIn.Start();

                var waveInToSource = new SoundInSource(_waveIn);

                _source = waveInToSource;
                var notifyStream = new SingleBlockNotificationStream(_source);
                notifyStream.SingleBlockRead += OnNotifyStream_SingleBlockRead;

                _source = notifyStream.ToWaveSource(16);
                _writerBuffer = new byte[_source.WaveFormat.BytesPerSecond];

                _writer = new WaveWriter(File.OpenWrite(sfd.FileName), _source.WaveFormat);
                waveInToSource.DataAvailable += OnNewData;

                btnStart.Enabled = false;
                btnStop.Enabled = true;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Record sound made in Mic and save it to a wave file
        /// </summary>
        /// <param name="wavefile">name of the wave file with extension</param>
        public void CaptureMicToWave(string wavefile)
        {
            int i = 0;
            string extension = ".wav";

            foreach (var device in WaveIn.Devices)
            {
                _waveIn = new WaveInEvent(new WaveFormat(44100, 16, device.Channels));
                _waveIn.Device = i++;

                _waveIn.Initialize();
                _waveIn.Start();

                var waveInToSource = new SoundInSource(_waveIn);

                _source = waveInToSource;
                var notifyStream = new SingleBlockNotificationStream(_source);

                _source = notifyStream.ToWaveSource(16);
                _writerBuffer = new byte[_source.WaveFormat.BytesPerSecond];

                wavefile = string.Format("{0}{1}{2}", wavefile.Remove(wavefile.LastIndexOf(extension) - (i > 1 ? 1 : 0)), i, extension);
                _writer = new WaveWriter(wavefile, _source.WaveFormat);
                waveInToSource.DataAvailable += (s, e) =>
                {
                    int read = 0;
                    while ((read = _source.Read(_writerBuffer, 0, _writerBuffer.Length)) > 0)
                    {
                        _writer.Write(_writerBuffer, 0, read);
                    }
                };
            }
        }
Exemplo n.º 3
0
 public void CanCreateWaveInDevice()
 {
     using (WaveIn waveIn = new WaveIn())
     {
         waveIn.Initialize();
         waveIn.Start();
     }
 }
Exemplo n.º 4
0
        public WaveInBuffer(WaveIn waveIn, int bufferSize)
        {
            if (waveIn == null) throw new ArgumentNullException("waveOut");
            if (bufferSize <= 0) throw new ArgumentOutOfRangeException("bufferSize");
            if (waveIn.Handle == IntPtr.Zero)
                throw new InvalidOperationException("Invalid WaveIn-Handle");

            _waveInHandle = waveIn.Handle;
            _bufferSize = bufferSize;
        }
Exemplo n.º 5
0
 public void CanCreateWaveInDevice()
 {
     #pragma warning disable 612
     using (WaveIn waveIn = new WaveIn())
     #pragma warning restore 612
     {
         waveIn.Initialize();
         waveIn.Start();
     }
 }
Exemplo n.º 6
0
        private void btnStop_Click(object sender, EventArgs e)
        {
            if (_soundOut != null)
                _soundOut.Dispose();

            _waveIn.Dispose();
            if(_writer is IDisposable)
                ((IDisposable)_writer).Dispose();

            _waveIn = null;
            _writer = null;
            _soundOut = null;

            btnStart.Enabled = deviceslist.SelectedItems.Count > 0;
            btnStop.Enabled = false;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Stop recording of the Mic started by CaptureMicToWave
        /// </summary>
        public void UnCaptureMicToWave()
        {
            _waveIn.Stop();
            _source.Dispose();
            _waveIn.Dispose();
            if (_writer is IDisposable)
                ((IDisposable)_writer).Dispose();

            _waveIn = null;
            _source = null;
            _writer = null;
        }