internal void Close() { if (_hMixer != IntPtr.Zero) { MixerNative.mixerClose(_hMixer); } _audioLines.Clear(); }
private void ReloadAudioDevices() { if (_reloadDevicesInProgress) { return; } MMErrors errorCode = 0; IntPtr hMixer = IntPtr.Zero; try { _reloadDevicesInProgress = true; var newAudioDevices = new List <AudioDeviceDescriptor>(); MIXERCAPS wic = new MIXERCAPS(); int iNumDevs = MixerNative.mixerGetNumDevs(); for (int i = 0; i < iNumDevs; i++) { // Get info about the next device errorCode = (MMErrors)MixerNative.mixerGetDevCaps(i, ref wic, Marshal.SizeOf(wic)); if (errorCode != MMErrors.MMSYSERR_NOERROR) { throw new MixerException(errorCode, GetErrorDescription(FuncName.fnMixerGetDevCaps, errorCode)); } errorCode = (MMErrors)MixerNative.mixerOpen(out hMixer, i, IntPtr.Zero, IntPtr.Zero, 0); if (errorCode != MMErrors.MMSYSERR_NOERROR) { throw new MixerException(errorCode, GetErrorDescription(FuncName.fnMixerOpen, errorCode)); } MIXERLINE mxl = new MIXERLINE(); mxl.cbStruct = (uint)Marshal.SizeOf(mxl); var deviceDescriptor = new AudioDeviceDescriptor() { DeviceId = i, Name = wic.szPname }; foreach (var mixerlineComponenttype in AudioDevice.RecordingLineTypes) { mxl.dwComponentType = mixerlineComponenttype; if (MixerNative.mixerGetLineInfo(hMixer, ref mxl, MIXER_GETLINEINFOF.COMPONENTTYPE) == 0) { deviceDescriptor.RecordingSupport = true; deviceDescriptor.RecordingComponentType = mixerlineComponenttype; break; } } foreach (var mixerlineComponenttype in AudioDevice.PlaybackLineTypes) { mxl.dwComponentType = mixerlineComponenttype; if (MixerNative.mixerGetLineInfo(hMixer, ref mxl, MIXER_GETLINEINFOF.COMPONENTTYPE) == 0) { deviceDescriptor.PlaybackSupport = true; deviceDescriptor.PlaybackComponentType = mixerlineComponenttype; break; } } if (deviceDescriptor.PlaybackSupport || deviceDescriptor.RecordingSupport) { if (deviceDescriptor.PlaybackSupport && deviceDescriptor.RecordingSupport) { var playbackDeviceDescriptor = new AudioDeviceDescriptor() { DeviceId = deviceDescriptor.DeviceId, Name = deviceDescriptor.Name, PlaybackSupport = true, RecordingSupport = false, PlaybackComponentType = deviceDescriptor.PlaybackComponentType }; var recordingDeviceDescriptor = new AudioDeviceDescriptor() { DeviceId = deviceDescriptor.DeviceId, Name = deviceDescriptor.Name, PlaybackSupport = false, RecordingSupport = true, RecordingComponentType = deviceDescriptor.RecordingComponentType }; newAudioDevices.Add(playbackDeviceDescriptor); newAudioDevices.Add(recordingDeviceDescriptor); } else { newAudioDevices.Add(deviceDescriptor); } } } if (newAudioDevices.Count > 0 || (_audioDevices.Count > 0 && iNumDevs == 0)) { UpdateAudioDevicesLists(newAudioDevices); } } finally { if (hMixer != IntPtr.Zero) { MixerNative.mixerClose(hMixer); } _reloadDevicesInProgress = false; } }
private AudioDevice GetDefaultDevice(MixerType mixerType) { MMErrors errorCode = 0; IntPtr hWave = IntPtr.Zero; IntPtr hMixer = IntPtr.Zero; try { WAVEFORMATEX waveFormat = WAVEFORMATEX.Empty; waveFormat.formatTag = WaveFormatTag.PCM; waveFormat.nChannels = 1; waveFormat.nSamplesPerSec = 8000; waveFormat.wBitsPerSample = 8; waveFormat.nBlockAlign = (short)(waveFormat.nChannels * (waveFormat.wBitsPerSample / 8)); waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign; waveFormat.cbSize = 0; // Get default mixer HWND if (mixerType == MixerType.Recording) { errorCode = (MMErrors)WaveNative.waveInOpen(out hWave, WaveNative.WAVE_MAPPER, ref waveFormat, CallbackForm.Handle, IntPtr.Zero, (int)CallBackFlag.CALLBACK_WINDOW); } else if (mixerType == MixerType.Playback) { errorCode = (MMErrors)WaveNative.waveOutOpen(out hWave, WaveNative.WAVE_MAPPER, ref waveFormat, CallbackForm.Handle, IntPtr.Zero, (int)CallBackFlag.CALLBACK_WINDOW); } if (errorCode != MMErrors.MMSYSERR_NOERROR) { // TODO: Log error!!! //throw new MixerException(errorCode, Audio.GetErrorDescription(FuncName.fnWaveInOpen, errorCode)); return(null); } if (mixerType == MixerType.Recording) { errorCode = (MMErrors)MixerNative.mixerOpen(out hMixer, hWave, IntPtr.Zero, IntPtr.Zero, ((uint)MIXER_OBJECTFLAG.HWAVEIN)); } else if (mixerType == MixerType.Playback) { errorCode = (MMErrors)MixerNative.mixerOpen(out hMixer, hWave, IntPtr.Zero, IntPtr.Zero, ((uint)MIXER_OBJECTFLAG.HWAVEOUT)); } if (errorCode != MMErrors.MMSYSERR_NOERROR) { // TODO: Log error!!! //throw new MixerException(errorCode, Mixers.GetErrorDescription(FuncName.fnMixerOpen, errorCode)); return(null); } int deviceId = -1; errorCode = (MMErrors)MixerNative.mixerGetID(hMixer, ref deviceId, MIXER_OBJECTFLAG.MIXER); if (errorCode != MMErrors.MMSYSERR_NOERROR) { // TODO: Log error!!! //throw new MixerException(errorCode, Mixers.GetErrorDescription(FuncName.fnMixerGetID, errorCode)); return(null); } MIXERCAPS wic = new MIXERCAPS(); errorCode = (MMErrors)MixerNative.mixerGetDevCaps(deviceId, ref wic, Marshal.SizeOf(wic)); if (errorCode != MMErrors.MMSYSERR_NOERROR) { // TODO: Log error!!! //throw new MixerException(errorCode, GetErrorDescription(FuncName.fnMixerGetDevCaps, errorCode)); return(null); } return(GetAudioDeviceByName(wic.szPname, mixerType == MixerType.Playback)); } finally { if (hMixer != IntPtr.Zero) { MixerNative.mixerClose(hMixer); } if (hWave != IntPtr.Zero && mixerType == MixerType.Playback) { WaveNative.waveOutClose(hWave); } if (hWave != IntPtr.Zero && mixerType == MixerType.Recording) { WaveNative.waveInClose(hWave); } } }