Esempio n. 1
0
        public void StartCapture(string devId)
        {
            SharedAudioInput input = getInput(devId);

            if (input != null)
            {
                input.StartCapture();
            }
        }
Esempio n. 2
0
        public WAVEFORMATEXTENSIBLE GetCapFormat(string devId)
        {
            SharedAudioInput input = getInput(devId);

            if (input != null)
            {
                return(input.CapFormat);
            }
            return(null);
        }
Esempio n. 3
0
        private SharedAudioInput getInput(string devId)
        {
            SharedAudioInput input = null;

            lock (_inputs)
            {
                input = _inputs.Find(di => di.SelectedDeviceId == devId);
            }
            return(input);
        }
Esempio n. 4
0
        public bool Capturing(string devId)
        {
            SharedAudioInput input = getInput(devId);

            if (input != null)
            {
                return(input.Capturing);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 5
0
        public void ReleaseDevice(string devId)
        {
            SharedAudioInput input = getInput(devId);

            if (input != null)
            {
                input.ReleaseDevice();
            }
            lock (_inputs)
            {
                if (input != _defaultInput)
                {
                    input.Dispose();
                    _inputs.Remove(input);
                }
            }
        }
Esempio n. 6
0
 public void SelectDevice(string devId)
 {
     lock (_inputs)
     {
         if (_inputs.Count == 0)
         {
             _defaultInput.SelectDevice(devId);
             _inputs.Add(_defaultInput);
         }
         else
         {
             SharedAudioInput input = new SharedAudioInput();
             _inputs.Add(input);
             setupInput(input);
             input.SelectDevice(devId);
         }
     }
 }
Esempio n. 7
0
        public SharedMultiAudioInput()
        {
            _defaultInput = new SharedAudioInput();

            _defaultInput.DeviceInfoUpdated += (s, e) =>
            {
                _deviceInfos = e.DeviceInfo;

                // イベント発火
                var del = DeviceInfoUpdated;
                if (del != null)
                {
                    List <DeviceInfo> info = _deviceInfos;
                    del.Invoke(this, new DeviceInfoUpdatedEventArgs(info));
                }
            };

            setupInput(_defaultInput);
        }
        public SharedMultiAudioInput()
        {
            _defaultInput = new SharedAudioInput();

            _defaultInput.DeviceInfoUpdated += (s, e) =>
            {
                _deviceInfos = e.DeviceInfo;

                // イベント発火
                var del = DeviceInfoUpdated;
                if (del != null)
                {
                    List<DeviceInfo> info = _deviceInfos;
                    del.Invoke(this, new DeviceInfoUpdatedEventArgs(info));
                }
            };

            setupInput(_defaultInput);
        }
Esempio n. 9
0
        private void setupInput(SharedAudioInput input)
        {
            input.InputDisposed += (s, e) =>
            {
                var h = InputDisposed;
                if (h != null)
                {
                    h(this, new InputDisposedEventArgs(e.DeviceId));
                }
            };

            input.DeviceSelected += (s, e) =>
            {
                var h = DeviceSelected;
                if (h != null)
                {
                    h(this, new DeviceSelectedEventArgs(e.Device, e.Index));
                }
            };
            input.CaptureStarted += (s, e) =>
            {
                var h = CaptureStarted;
                if (h != null)
                {
                    h(this, new CaptureStartedEventArgs(e.DeviceId));
                }
            };
            input.CaptureStopped += (s, e) =>
            {
                var h = CaptureStopped;
                if (h != null)
                {
                    h(this, new CaptureStoppedEventArgs(e.DeviceId));
                }
            };

            input.DataUpdated += (s, e) =>
            {
                var h = DataUpdated;
                if (h != null)
                {
                    byte[] rawData = new byte[e.RawData.Length];
                    Array.Copy(e.RawData, rawData, e.RawData.Length);
                    List <double[]> data = new List <double[]>();
                    foreach (var d in e.Data)
                    {
                        double[] ds = new double[d.Length];
                        Array.Copy(d, ds, d.Length);
                        data.Add(ds);
                    }
                    h(this, new DataUpdatedEventArgs(e.DeviceId, e.SampleNum, rawData, data.ToArray()));
                }
            };
            input.VolumeChanged += (s, e) =>
            {
                var h = VolumeChanged;
                if (h != null)
                {
                    h(this, new VolumeChangedEventArgs(e.DeviceId, e.Master, e.Channels));
                }
            };

            input.ErrorOccured += (s, e) =>
            {
                var h = ErrorOccured;
                if (h != null)
                {
                    h(this, new ErrorEventArgs(e.DeviceId, e.Exception));
                }
            };
        }
        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();
        }
        private void setupInput(SharedAudioInput input)
        {
            input.InputDisposed += (s, e) =>
            {
                var h = InputDisposed;
                if (h != null)
                {
                    h(this, new InputDisposedEventArgs(e.DeviceId));
                }
            };

            input.DeviceSelected += (s, e) =>
            {
                var h = DeviceSelected;
                if (h != null)
                {
                    h(this, new DeviceSelectedEventArgs(e.Device, e.Index));
                }
            };
            input.CaptureStarted += (s, e) =>
            {
                var h = CaptureStarted;
                if (h != null)
                {
                    h(this, new CaptureStartedEventArgs(e.DeviceId));
                }
            };
            input.CaptureStopped += (s, e) =>
            {
                var h = CaptureStopped;
                if (h != null)
                {
                    h(this, new CaptureStoppedEventArgs(e.DeviceId));
                }
            };

            input.DataUpdated += (s, e) =>
            {
                var h = DataUpdated;
                if (h != null)
                {
                    byte[] rawData = new byte[e.RawData.Length];
                    Array.Copy(e.RawData, rawData, e.RawData.Length);
                    List<double[]> data = new List<double[]>();
                    foreach (var d in e.Data)
                    {
                        double[] ds = new double[d.Length];
                        Array.Copy(d, ds, d.Length);
                        data.Add(ds);
                    }
                    h(this, new DataUpdatedEventArgs(e.DeviceId, e.SampleNum, rawData, data.ToArray()));
                }
            };
            input.VolumeChanged += (s, e) =>
            {
                var h = VolumeChanged;
                if (h != null)
                {
                    h(this, new VolumeChangedEventArgs(e.DeviceId, e.Master, e.Channels));
                }
            };

            input.ErrorOccured += (s, e) =>
            {
                var h = ErrorOccured;
                if (h != null)
                {
                    h(this, new ErrorEventArgs(e.DeviceId, e.Exception));
                }
            };
        }
 public void SelectDevice(string devId)
 {
     lock (_inputs)
     {
         if (_inputs.Count == 0)
         {
             _defaultInput.SelectDevice(devId);
             _inputs.Add(_defaultInput);
         }
         else
         {
             SharedAudioInput input = new SharedAudioInput();
             _inputs.Add(input);
             setupInput(input);
             input.SelectDevice(devId);
         }
     }
 }