private void selectDeviceImpl(string devId) { releaseDevice(); _capDevice = _devices.GetDevice(devId.Trim()); int idx = _deviceInfos.FindIndex((di) => { return(di.DeviceId == devId); }); if (_capDevice == null) { #warning 例外 } _capDeviceId = _capDevice.Id; // デバイスに適した初期化方法を決定 AudioClientStreamFlags streamFlags = AudioClientStreamFlags.NoPersist; if (_capDevice.DataFlow == EDataFlow.eRender) { streamFlags = AudioClientStreamFlags.Loopback | AudioClientStreamFlags.EventCallback; // 実際は発生してくれない } // フォーマット if (_audioClient != null) { _capDevice.ReleaseAudioClient(); } try { _audioClient = _capDevice.AudioClient; _capFormat = _audioClient.MixFormat; _pitchAnalyzer.SampleFrequency = (double)(_capFormat.nSamplesPerSec); // 初期化 _audioClient.Initialize(AudioClientShareMode.Shared, streamFlags, 300 /*ms*/ * 10000, 0, _capFormat, Guid.Empty); _capClient = _audioClient.AudioCaptureClient; // イベント発火 DeviceSelectedEventHandler del = DeviceSelected; if (del != null) { del.Invoke(this, new DeviceSelectedEventArgs(_capDevice, idx)); } } catch (System.Runtime.InteropServices.COMException ex) { #warning エラー処理 _audioClient = null; _capClient = null; } }
public void SelectDevice(string devId) { _capDevice = _devices.GetDevice(devId.Trim()); if (_capDevice == null) { _audioClient = null; _capClient = null; return; } _capDeviceId = _capDevice.Id; // モード AudioClientShareMode shareMode = AudioClientShareMode.Exclusive; AudioClientStreamFlags streamFlags = AudioClientStreamFlags.NoPersist; if (_audioClient != null) _capDevice.ReleaseAudioClient(); try { _audioClient = _capDevice.AudioClient; _capFormat = _audioClient.MixFormat; _capFormat.wFormatTag = WaveFormatTag.WAVE_FORMAT_EXTENSIBLE; _capFormat.nChannels = 2; _capFormat.nSamplesPerSec = 16000; _capFormat.wBitsPerSample = 16; _capFormat.SubFormat = CoreAudioApi.AudioMediaSubtypes.MEDIASUBTYPE_PCM; _capFormat.wValidBitsPerSample = _capFormat.wBitsPerSample; _capFormat.nBlockAlign = (ushort)(_capFormat.wBitsPerSample / 8.0 * _capFormat.nChannels); _capFormat.nAvgBytesPerSec = _capFormat.nSamplesPerSec * _capFormat.nBlockAlign; long tmp1; long tmp2; _audioClient.GetDevicePeriod(out tmp1, out tmp2); // 初期化 try { WAVEFORMATEXTENSIBLE tmpFmt = new WAVEFORMATEXTENSIBLE(); if (!_audioClient.IsFormatSupported(shareMode, _capFormat, ref tmpFmt)) _capFormat = tmpFmt; _audioClient.Initialize(shareMode, streamFlags, tmp2, tmp2, _capFormat, Guid.Empty); } catch (System.Runtime.InteropServices.COMException ex) { try { AudioClientError error = (AudioClientError)ex.ErrorCode; switch (error) { case AudioClientError.BufferSizeNotAligned: uint bufSize = _audioClient.BufferSize; tmp2 = (long)((10000.0 * 1000 / _capFormat.nSamplesPerSec * bufSize) + 0.5); _audioClient.Initialize(shareMode, streamFlags, tmp2, tmp2, _capFormat, Guid.Empty); break; case AudioClientError.UnsupportedFormat: break; } } catch (InvalidCastException) { } } _capClient = _audioClient.AudioCaptureClient; } catch (System.Runtime.InteropServices.COMException ex) { _audioClient = null; _capClient = null; throw ex; } }
private void selectDeviceImpl(string devId) { if (_capDevice != null && _capDevice.Id == devId) { return; } releaseDeviceImpl(); _capDevice = _devices.GetDevice(devId.Trim()); int idx = _deviceInfos.FindIndex((di) => { return di.DeviceId == devId; }); if (_capDevice == null) { #warning 例外 _audioClient = null; _capClient = null; return; } _capDeviceId = _capDevice.Id; // モード AudioClientShareMode shareMode = AudioClientShareMode.Shared; // デバイスに適した初期化方法を決定 AudioClientStreamFlags streamFlags = AudioClientStreamFlags.NoPersist; switch (shareMode) { case AudioClientShareMode.Shared: switch (_capDevice.DataFlow) { case EDataFlow.eCapture: streamFlags = 0; break; case EDataFlow.eRender: streamFlags = AudioClientStreamFlags.Loopback; break; } break; case AudioClientShareMode.Exclusive: streamFlags = AudioClientStreamFlags.NoPersist; break; } // フォーマット if (_audioClient != null) _capDevice.ReleaseAudioClient(); // ボリューム _masterVolume = 0; _channelVolumes = new double[_capDevice.AudioMeterInformation.PeakValues.Count]; var h = VolumeChanged; if (h != null) { h(this, new VolumeChangedEventArgs(_capDeviceId, _masterVolume, _channelVolumes)); } try { _audioClient = _capDevice.AudioClient; _capFormat = _audioClient.MixFormat; _capFormat.wFormatTag = WaveFormatTag.WAVE_FORMAT_EXTENSIBLE; _capFormat.nChannels = 1; _capFormat.nSamplesPerSec = 44100; _capFormat.wBitsPerSample = 16; _capFormat.SubFormat = CoreAudioApi.AudioMediaSubtypes.MEDIASUBTYPE_PCM; _capFormat.wValidBitsPerSample = _capFormat.wBitsPerSample; _capFormat.nBlockAlign = (ushort)(_capFormat.wBitsPerSample / 8.0 * _capFormat.nChannels); _capFormat.nAvgBytesPerSec = _capFormat.nSamplesPerSec * _capFormat.nBlockAlign; long tmp1; long tmp2; _audioClient.GetDevicePeriod(out tmp1, out tmp2); // 初期化 try { WAVEFORMATEXTENSIBLE tmpFmt = new WAVEFORMATEXTENSIBLE(); if (!_audioClient.IsFormatSupported(shareMode, _capFormat, ref tmpFmt)) _capFormat = tmpFmt; _audioClient.Initialize(shareMode, streamFlags, tmp2, tmp2, _capFormat, Guid.Empty); } catch (System.Runtime.InteropServices.COMException ex) { if ((uint)ex.ErrorCode == 0x88890019) { uint bufSize = _audioClient.BufferSize; tmp2 = (long)((10000.0 * 1000 / _capFormat.nSamplesPerSec * bufSize) + 0.5); _audioClient.Initialize(shareMode, streamFlags, tmp2, tmp2, _capFormat, Guid.Empty); } } clearBuffer(); _capClient = _audioClient.AudioCaptureClient; // イベント発火 var del = DeviceSelected; if (del != null) { del.Invoke(this, new DeviceSelectedEventArgs(_capDevice, idx)); } } catch (System.Runtime.InteropServices.COMException ex) { _audioClient = null; _capClient = null; throw ex; } }
public void SelectDevice(string devId) { _capDevice = _devices.GetDevice(devId.Trim()); if (_capDevice == null) { _audioClient = null; _capClient = null; return; } _capDeviceId = _capDevice.Id; // モード AudioClientShareMode shareMode = AudioClientShareMode.Exclusive; AudioClientStreamFlags streamFlags = AudioClientStreamFlags.NoPersist; if (_audioClient != null) { _capDevice.ReleaseAudioClient(); } try { _audioClient = _capDevice.AudioClient; _capFormat = _audioClient.MixFormat; _capFormat.wFormatTag = WaveFormatTag.WAVE_FORMAT_EXTENSIBLE; _capFormat.nChannels = 2; _capFormat.nSamplesPerSec = 16000; _capFormat.wBitsPerSample = 16; _capFormat.SubFormat = CoreAudioApi.AudioMediaSubtypes.MEDIASUBTYPE_PCM; _capFormat.wValidBitsPerSample = _capFormat.wBitsPerSample; _capFormat.nBlockAlign = (ushort)(_capFormat.wBitsPerSample / 8.0 * _capFormat.nChannels); _capFormat.nAvgBytesPerSec = _capFormat.nSamplesPerSec * _capFormat.nBlockAlign; long tmp1; long tmp2; _audioClient.GetDevicePeriod(out tmp1, out tmp2); // 初期化 try { WAVEFORMATEXTENSIBLE tmpFmt = new WAVEFORMATEXTENSIBLE(); if (!_audioClient.IsFormatSupported(shareMode, _capFormat, ref tmpFmt)) { _capFormat = tmpFmt; } _audioClient.Initialize(shareMode, streamFlags, tmp2, tmp2, _capFormat, Guid.Empty); } catch (System.Runtime.InteropServices.COMException ex) { try { AudioClientError error = (AudioClientError)ex.ErrorCode; switch (error) { case AudioClientError.BufferSizeNotAligned: uint bufSize = _audioClient.BufferSize; tmp2 = (long)((10000.0 * 1000 / _capFormat.nSamplesPerSec * bufSize) + 0.5); _audioClient.Initialize(shareMode, streamFlags, tmp2, tmp2, _capFormat, Guid.Empty); break; case AudioClientError.UnsupportedFormat: break; } } catch (InvalidCastException) { } } _capClient = _audioClient.AudioCaptureClient; } catch (System.Runtime.InteropServices.COMException ex) { _audioClient = null; _capClient = null; throw ex; } }
private void selectDeviceImpl(string devId) { if (_capDevice != null && _capDevice.Id == devId) { return; } releaseDeviceImpl(); _capDevice = _devices.GetDevice(devId.Trim()); int idx = _deviceInfos.FindIndex((di) => { return(di.DeviceId == devId); }); if (_capDevice == null) { #warning 例外 _audioClient = null; _capClient = null; return; } _capDeviceId = _capDevice.Id; // モード AudioClientShareMode shareMode = AudioClientShareMode.Shared; // デバイスに適した初期化方法を決定 AudioClientStreamFlags streamFlags = AudioClientStreamFlags.NoPersist; switch (shareMode) { case AudioClientShareMode.Shared: switch (_capDevice.DataFlow) { case EDataFlow.eCapture: streamFlags = 0; break; case EDataFlow.eRender: streamFlags = AudioClientStreamFlags.Loopback; break; } break; case AudioClientShareMode.Exclusive: streamFlags = AudioClientStreamFlags.NoPersist; break; } // フォーマット if (_audioClient != null) { _capDevice.ReleaseAudioClient(); } // ボリューム _masterVolume = 0; _channelVolumes = new double[_capDevice.AudioMeterInformation.PeakValues.Count]; var h = VolumeChanged; if (h != null) { h(this, new VolumeChangedEventArgs(_capDeviceId, _masterVolume, _channelVolumes)); } try { _audioClient = _capDevice.AudioClient; _capFormat = _audioClient.MixFormat; _capFormat.wFormatTag = WaveFormatTag.WAVE_FORMAT_EXTENSIBLE; _capFormat.nChannels = 1; _capFormat.nSamplesPerSec = 44100; _capFormat.wBitsPerSample = 16; _capFormat.SubFormat = CoreAudioApi.AudioMediaSubtypes.MEDIASUBTYPE_PCM; _capFormat.wValidBitsPerSample = _capFormat.wBitsPerSample; _capFormat.nBlockAlign = (ushort)(_capFormat.wBitsPerSample / 8.0 * _capFormat.nChannels); _capFormat.nAvgBytesPerSec = _capFormat.nSamplesPerSec * _capFormat.nBlockAlign; long tmp1; long tmp2; _audioClient.GetDevicePeriod(out tmp1, out tmp2); // 初期化 try { WAVEFORMATEXTENSIBLE tmpFmt = new WAVEFORMATEXTENSIBLE(); if (!_audioClient.IsFormatSupported(shareMode, _capFormat, ref tmpFmt)) { _capFormat = tmpFmt; } _audioClient.Initialize(shareMode, streamFlags, tmp2, tmp2, _capFormat, Guid.Empty); } catch (System.Runtime.InteropServices.COMException ex) { if ((uint)ex.ErrorCode == 0x88890019) { uint bufSize = _audioClient.BufferSize; tmp2 = (long)((10000.0 * 1000 / _capFormat.nSamplesPerSec * bufSize) + 0.5); _audioClient.Initialize(shareMode, streamFlags, tmp2, tmp2, _capFormat, Guid.Empty); } } clearBuffer(); _capClient = _audioClient.AudioCaptureClient; // イベント発火 var del = DeviceSelected; if (del != null) { del.Invoke(this, new DeviceSelectedEventArgs(_capDevice, idx)); } } catch (System.Runtime.InteropServices.COMException ex) { _audioClient = null; _capClient = null; throw ex; } }