Exemplo n.º 1
0
        public AudioCaptureViewModel()
        {
            _audioInput = new SharedAudioInput();

            _audioInput.DeviceInfoUpdated += (s, e) =>
            {
                Devices = new ObservableCollection <DeviceInfoViewModel>(
                    e.DeviceInfo.FindAll(di => true)//di.DataFlow == EDataFlow.eCapture)
                    .Select(di => new DeviceInfoViewModel(di)));
                addMessage("Devices updated");
            };

            _audioInput.DeviceSelected += (s, e) =>
            {
                addMessage("Device selected: " + e.Device.FriendlyName);
            };
            _audioInput.CaptureStarted += (s, e) =>
            {
                addMessage("Capture started");
            };
            _audioInput.CaptureStopped += (s, e) =>
            {
                addMessage("Capture stopped");

                if (_wavWriters != null)
                {
                    foreach (var wr in _wavWriters)
                    {
                        wr.Close();
                    }
                }
                _wavWriters = null;
                if (_wavRawWriter != null)
                {
                    _wavRawWriter.Close(); _wavRawWriter = null;
                }
                if (_writer != null)
                {
                    _writer.Close(); _writer = null;
                }
            };

            _audioInput.DataUpdated += (s, e) =>
            {
                if (_wavWriters != null)
                {
                    for (int i = 0; i < _wavWriters.Length; i++)
                    {
                        _wavWriters[i].Write(e.Data[i]);
                    }
                }
                if (_wavRawWriter != null)
                {
                    _wavRawWriter.WriteRawData(e.RawData);
                }
                if (_writer != null)
                {
                    _writer.Write(e.RawData);
                }
            };
            _audioInput.VolumeChanged += (s, e) =>
            {
                Volume = e.Master;
            };

            _audioInput.ErrorOccured += (s, e) =>
            {
                addMessage(e.Exception.Message);
            };

            #region initialize commands
            StartCaptureCommand = new DelegateCommand(
                () =>
            {
                if (_wavWriters != null)
                {
                    foreach (var wr in _wavWriters)
                    {
                        wr.Close();
                    }
                }

                _wavWriters = new WaveFileWriter[_audioInput.CapFormat.nChannels];
                WAVEFORMATEXTENSIBLE fmt = new WAVEFORMATEXTENSIBLE(_audioInput.CapFormat);
                fmt.wFormatTag           = WaveFormatTag.WAVE_FORMAT_PCM;
                fmt.nChannels            = 1;
                fmt.wBitsPerSample       = 16;
                fmt.wValidBitsPerSample  = 16;
                fmt.nAvgBytesPerSec      = (uint)(fmt.nChannels * fmt.nSamplesPerSec * fmt.wBitsPerSample / 8.0);

                for (int i = 0; i < _wavWriters.Length; i++)
                {
                    _wavWriters[i] = new WaveFileWriter(fmt.nChannels, (int)fmt.nSamplesPerSec, fmt.wBitsPerSample,
                                                        DateTime.Now.ToString("yyyyMMdd-HHmmss") + string.Format("_{0}.wav", i));
                }

                WAVEFORMATEXTENSIBLE rawFmt = new WAVEFORMATEXTENSIBLE(_audioInput.CapFormat);
                _wavRawWriter = new WaveFileWriter(rawFmt.nChannels, (int)rawFmt.nSamplesPerSec, rawFmt.wBitsPerSample,
                                                   DateTime.Now.ToString("yyyyMMdd-HHmmss") + "_raw.wav");

                _writer = new System.IO.BinaryWriter(new System.IO.FileStream("test.wav", System.IO.FileMode.Create));

                _audioInput.StartCapture();
            },
                () => { return(_selectedDev != null && !_audioInput.Capturing &&
                               _selectedDev.State == DeviceState.DEVICE_STATE_ACTIVE); });

            StopCaptureCommand = new DelegateCommand(
                () =>
            {
                _audioInput.StopCapture();
            },
                () => { return(SelectedDev != null && _audioInput.Capturing); });
            #endregion

            Devices = new ObservableCollection <DeviceInfoViewModel>();
            _audioInput.UpdateDeviceInfo();
        }