internal void Close() { if (_hMixer != IntPtr.Zero) { MixerNative.mixerClose(_hMixer); } _audioLines.Clear(); }
private void ReloadLines() { MMErrors errorCode = 0; _audioLines.Clear(); MIXERLINE mxl = new MIXERLINE(); uint dwDestination; unchecked { dwDestination = (uint)-1; } mxl.cbStruct = (uint)Marshal.SizeOf(mxl); mxl.dwComponentType = _componentType; errorCode = (MMErrors)MixerNative.mixerGetLineInfo(_hMixer, ref mxl, MIXER_GETLINEINFOF.COMPONENTTYPE); if (errorCode != MMErrors.MMSYSERR_NOERROR) { throw new MixerException(errorCode, WMME.Audio.GetErrorDescription(FuncName.fnMixerGetLineInfo, errorCode)); } dwDestination = mxl.dwDestination; var mixLine = new AudioLine(this, mxl); if (mixLine.CControls != 0 && !(mixLine.CControls == 1 && mixLine.Controls[0].ControlType == MIXERCONTROL_CONTROLTYPE.MUX)) { _audioLines.Add(mixLine); } int cConnections = (int)mxl.cConnections; for (int i = 0; i < cConnections; i++) { mxl.cbStruct = (uint)Marshal.SizeOf(mxl); mxl.dwDestination = dwDestination; mxl.dwSource = (uint)i; errorCode = (MMErrors)MixerNative.mixerGetLineInfo(Handle, ref mxl, MIXER_GETLINEINFOF.SOURCE); if (errorCode != MMErrors.MMSYSERR_NOERROR) { throw new MixerException(errorCode, WMME.Audio.GetErrorDescription(FuncName.fnMixerGetLineInfo, errorCode)); } var mixLineNew = new AudioLine(this, mxl); if (mixLineNew.CControls != 0) { _audioLines.Add(mixLineNew); } } }
private unsafe void ReloadAudioLineControls() { MMErrors errorCode = 0; MIXERLINECONTROLS mlc = new MIXERLINECONTROLS(); IntPtr pmc = IntPtr.Zero; try { _audioLineControls.Clear(); if (CControls == 0) { return; } pmc = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(MIXERCONTROL)) * CControls)); mlc.cbStruct = (uint)sizeof(MIXERLINECONTROLS); mlc.dwLineID = Id; mlc.cControls = CControls; mlc.pamxctrl = pmc; mlc.cbmxctrl = (uint)(Marshal.SizeOf(typeof(MIXERCONTROL))); errorCode = (MMErrors)MixerNative.mixerGetLineControls(AudioDeviceInternal.Handle, ref mlc, MIXER_GETLINECONTROLSFLAG.ALL); if (errorCode != MMErrors.MMSYSERR_NOERROR) { throw new MixerException(errorCode, Audio.GetErrorDescription(FuncName.fnMixerGetLineControls, errorCode)); } for (int i = 0; i < mlc.cControls; i++) { var mc = (MIXERCONTROL)Marshal.PtrToStructure((IntPtr)(((byte *)pmc) + (Marshal.SizeOf(typeof(MIXERCONTROL)) * i)), typeof(MIXERCONTROL)); AudioLineControl lineControl; switch ((MIXERCONTROL_CONTROLTYPE)mc.dwControlType) { case MIXERCONTROL_CONTROLTYPE.MUTE: lineControl = new MuteAudioLineControl(this, mc, new Action <AudioLine>(RaiseMuteChangedEvent)); break; default: lineControl = new VolumeAudioLineControl(this, mc, new Action <AudioLine>(RaiseVolumeChangedEvent)); break; } _audioLineControls.Add(lineControl); } } finally { if (pmc != IntPtr.Zero) { Marshal.FreeHGlobal((IntPtr)pmc); } } }
internal void Open() { var errorCode = (MMErrors)MixerNative.mixerOpen(out _hMixer, DeviceId, _audio.CallbackForm.Handle, IntPtr.Zero, MixerNative.CALLBACK_WINDOW); if (errorCode != MMErrors.MMSYSERR_NOERROR) { throw new MixerException(errorCode, WMME.Audio.GetErrorDescription(FuncName.fnMixerOpen, errorCode)); } ReloadLines(); if (RecordingSupport) { DefaultLine = GetAudioLine(RecordingLineTypes); } else if (PlaybackSupport) { DefaultLine = GetAudioLine(PlaybackLineTypes); } }
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); } } }