public MorphicResult <MMDevice, WindowsComError> GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role) { IMMDevice?immDevice; var result = _mmDeviceEnumerator.GetDefaultAudioEndpoint(dataFlow, role, out immDevice); if (result != ExtendedPInvoke.S_OK) { if (result == ExtendedPInvoke.E_NOTFOUND) { throw new NoDeviceIsAvailableException(); } else { // TODO: consider throwing more granular exceptions here var comException = new COMException("IMMDeviceEnumerator.GetDefaultAudioEndpoint failed", Marshal.GetExceptionForHR(result)); return(MorphicResult.ErrorResult(WindowsComError.ComException(comException))); } } if (immDevice is null) { // NOTE: this code should never be executed since GetDefaultAudioEndpoint should have returned an HRESULT of E_POINTER if it failed var comException = new COMException("IMMDeviceEnumerator.GetDefaultAudioEndpoint returned a null pointer", new NullReferenceException()); return(MorphicResult.ErrorResult(WindowsComError.ComException(comException))); } var mmDevice = MMDevice.CreateFromIMMDevice(immDevice !); return(MorphicResult.OkResult(mmDevice)); }
public static MorphicResult <MMDeviceEnumerator, WindowsComError> CreateNew() { // get a Type reference for MMDeviceEnumerator Type MMDeviceEnumeratorType; try { MMDeviceEnumeratorType = Type.GetTypeFromCLSID(new Guid(CLSID_MMDeviceEnumerator), true) !; } catch { // TODO: consider providing a more specific exception result return(MorphicResult.ErrorResult(WindowsComError.ComException(new COMException()))); } MMDeviceEnumerator result; try { // NOTE: objects created by Activator.CreateInstance do not need to be manually freed var mmDeviceEnumeratorAsNullable = Activator.CreateInstance(MMDeviceEnumeratorType) as IMMDeviceEnumerator; if (mmDeviceEnumeratorAsNullable is null) { throw new COMException(); } result = new MMDeviceEnumerator(mmDeviceEnumeratorAsNullable !); } catch { // TODO: in the future, consider throwing different exceptions for different failure conditions throw new COMException(); } return(MorphicResult.OkResult(result)); }
public MorphicResult <MorphicUnit, WindowsComError> SetMasterMuteState(bool muteState) { // set the master mute state var result = this._audioEndpointVolume.SetMute(muteState ? 1 : 0, IntPtr.Zero); if (result != ExtendedPInvoke.S_OK) { // TODO: consider throwing more granular exceptions here var comException = new COMException("IAudioEndpointVolume.SetMute failed", Marshal.GetExceptionForHR(result)); return(MorphicResult.ErrorResult(WindowsComError.ComException(comException))); } return(MorphicResult.OkResult()); }
public MorphicResult <float, WindowsComError> GetMasterVolumeLevel() { // get the master volume level Single volumeLevelScalar; var result = this._audioEndpointVolume.GetMasterVolumeLevelScalar(out volumeLevelScalar); if (result != ExtendedPInvoke.S_OK) { // TODO: consider throwing more granular exceptions here var comException = new COMException("IAudioEndpointVolume.GetMasterVolumeLevelScalar failed", Marshal.GetExceptionForHR(result)); return(MorphicResult.ErrorResult(WindowsComError.ComException(comException))); } _lastMasterVolumeLevel = volumeLevelScalar; return(MorphicResult.OkResult(volumeLevelScalar)); }
public MorphicResult <bool, WindowsComError> GetMasterMuteState() { // get the master mute state Int32 isMutedAsInt32; var result = this._audioEndpointVolume.GetMute(out isMutedAsInt32); if (result != ExtendedPInvoke.S_OK) { // TODO: consider throwing more granular exceptions here var comException = new COMException("IAudioEndpointVolume.GetMute failed", Marshal.GetExceptionForHR(result)); return(MorphicResult.ErrorResult(WindowsComError.ComException(comException))); } var muteState = (isMutedAsInt32 != 0) ? true : false; _lastMasterMuteState = muteState; return(MorphicResult.OkResult(muteState)); }
public MorphicResult <MorphicUnit, WindowsComError> SetMasterVolumeLevel(float volumeLevel) { if (volumeLevel < 0.0 || volumeLevel > 1.0) { throw new ArgumentOutOfRangeException(nameof(volumeLevel)); } // set the master volume level var result = this._audioEndpointVolume.SetMasterVolumeLevelScalar(volumeLevel, IntPtr.Zero); if (result != ExtendedPInvoke.S_OK) { // TODO: consider throwing more granular exceptions here var comException = new COMException("IAudioEndpointVolume.GetMasterVolumeLevelScalar failed", Marshal.GetExceptionForHR(result)); return(MorphicResult.ErrorResult(WindowsComError.ComException(comException))); } return(MorphicResult.OkResult()); }
public MorphicResult <Object, WindowsComError> Activate(Guid iid, CLSCTX clsCtx) { Object? @interface; var result = _immDevice.Activate(iid, clsCtx, IntPtr.Zero, out @interface); if (result != ExtendedPInvoke.S_OK) { // TODO: consider throwing more granular exceptions here var comException = new COMException("IMMDeviceEnumerator.GetDefaultAudioEndpoint failed", Marshal.GetExceptionForHR(result)); return(MorphicResult.ErrorResult(WindowsComError.ComException(comException))); } if (@interface is null) { // NOTE: this code should never be executed since Activate should have returned an HRESULT of E_POINTER if it failed var comException = new COMException("IMMDevice.Activate returned a null pointer", new NullReferenceException()); return(MorphicResult.ErrorResult(WindowsComError.ComException(comException))); } return(MorphicResult.OkResult(@interface !)); }