public static bool GetMasterVolumeMute()
        {
            IAudioEndpointVolume audioEndpointVolume = null;

            try
            {
                audioEndpointVolume = GetMasterVolumeObject();
                if (audioEndpointVolume == null)
                {
                    return(false);
                }
                audioEndpointVolume.GetMute(out bool isMuted);
                return(isMuted);
            }
            finally
            {
                if (audioEndpointVolume != null)
                {
                    Marshal.ReleaseComObject(audioEndpointVolume);
                }
            }
        }
Example #2
1
 public AEDev()
 {
   IMMDevice _Device = null;
   if (String.IsNullOrEmpty(_devId))
   {
     Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(0, 1, out _Device));
     Marshal.ThrowExceptionForHR(_Device.GetId(out _devId));
   }
   else
   {
     Marshal.ThrowExceptionForHR(_realEnumerator.GetDevice(_devId, out _Device));
   }
   devstatus state;
   Marshal.ThrowExceptionForHR(_Device.GetState(out state));
   if (state != devstatus.DEVICE_STATE_ACTIVE)
     throw new ApplicationException(String.Format("audio device is not active ({0})", state.ToString()));
   _RealDevice = _Device;
   object result;
   Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioEndpointVolume, CTX.ALL, IntPtr.Zero, out result));
   _AudioEndPointVolume = result as IAudioEndpointVolume;
   _CallBack = new AudioEndpointVolumeCallback(this);
   Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack));
 }
Example #3
0
        internal AudioEndpointVolumeChannels(IAudioEndpointVolume parent)
        {
            int ChannelCount;
            _AudioEndPointVolume = parent;

            ChannelCount = Count;
            _Channels = new AudioEndpointVolumeChannel[ChannelCount];
            for (int i = 0; i < ChannelCount; i++)
                _Channels[i] = new AudioEndpointVolumeChannel(_AudioEndPointVolume, i);
        }
        internal AudioEndpointVolumeChannels(IAudioEndpointVolume parent)
        {
            _audioEndPointVolume = parent;

            int channelCount = Count;
            _channels = new AudioEndpointVolumeChannel[channelCount];
            for (int i = 0; i < channelCount; i++)
            {
                _channels[i] = new AudioEndpointVolumeChannel(_audioEndPointVolume, i);
            }
        }
Example #5
0
        internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume)
        {
            uint HardwareSupp;

            _AudioEndPointVolume = realEndpointVolume;
            _Channels = new AudioEndpointVolumeChannels(_AudioEndPointVolume);
            _StepInformation = new AudioEndpointVolumeStepInformation(_AudioEndPointVolume);
            Marshal.ThrowExceptionForHR(_AudioEndPointVolume.QueryHardwareSupport(out HardwareSupp));
            _HardwareSupport = (EEndpointHardwareSupport)HardwareSupp;
            _VolumeRange = new AudioEndPointVolumeVolumeRange(_AudioEndPointVolume);
            _CallBack = new AudioEndpointVolumeCallback(this);
            Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify( _CallBack));
        }
        /// <summary>
        ///     Creates a new Audio endpoint volume
        /// </summary>
        /// <param name="realEndpointVolume">IAudioEndpointVolume COM interface</param>
        internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume)
        {
            uint hardwareSupp;

            audioEndPointVolume = realEndpointVolume;
            channels = new AudioEndpointVolumeChannels(audioEndPointVolume);
            stepInformation = new AudioEndpointVolumeStepInformation(audioEndPointVolume);
            Marshal.ThrowExceptionForHR(audioEndPointVolume.QueryHardwareSupport(out hardwareSupp));
            hardwareSupport = (EndpointHardwareSupport) hardwareSupp;
            volumeRange = new AudioEndpointVolumeVolumeRange(audioEndPointVolume);
            callBack = new AudioEndpointVolumeCallback(this);
            Marshal.ThrowExceptionForHR(audioEndPointVolume.RegisterControlChangeNotify(callBack));
        }
        /// <summary>
        ///     Creates a new Audio endpoint volume
        /// </summary>
        /// <param name="realEndpointVolume">IAudioEndpointVolume COM interface</param>
        internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume)
        {
            ComThread.Assert();
            uint hardwareSupp;

            _audioEndPointVolume = realEndpointVolume;
            _channels = new AudioEndpointVolumeChannels(_audioEndPointVolume);
            _stepInformation = new AudioEndpointVolumeStepInformation(_audioEndPointVolume);
            Marshal.ThrowExceptionForHR(_audioEndPointVolume.QueryHardwareSupport(out hardwareSupp));
            _hardwareSupport = (EndpointHardwareSupport)hardwareSupp;
            _volumeRange = new AudioEndpointVolumeVolumeRange(_audioEndPointVolume);

            _callBack = new AudioEndpointVolumeCallback(this);
            Marshal.ThrowExceptionForHR(_audioEndPointVolume.RegisterControlChangeNotify(_callBack));
        }
Example #8
0
        /// <summary>
        /// Mute or unmute the master volume
        /// </summary>
        /// <param name="isMuted">true to mute the master volume, false to unmute</param>
        public static void SetMasterVolumeMute(bool isMuted)
        {
            IAudioEndpointVolume masterVol = null;

            try
            {
                masterVol = GetMasterVolumeObject();
                if (masterVol == null)
                {
                    return;
                }

                masterVol.SetMute(isMuted, Guid.Empty);
            }
            finally
            {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }
Example #9
0
        /// <summary>
        /// Sets the master volume to a specific level
        /// </summary>
        /// <param name="newLevel">Value between 0 and 100 indicating the desired scalar value of the volume</param>
        public static void SetMasterVolume(float newLevel)
        {
            IAudioEndpointVolume masterVol = null;

            try
            {
                masterVol = GetMasterVolumeObject();
                if (masterVol == null)
                {
                    return;
                }

                masterVol.SetMasterVolumeLevelScalar(newLevel / 100, Guid.Empty);
            }
            finally
            {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }
        public static float GetMasterVolume()
        {
            IAudioEndpointVolume audioEndpointVolume = null;

            try
            {
                audioEndpointVolume = GetMasterVolumeObject();
                if (audioEndpointVolume == null)
                {
                    return(-1f);
                }
                audioEndpointVolume.GetMasterVolumeLevelScalar(out float level);
                return(level * 100f);
            }
            finally
            {
                if (audioEndpointVolume != null)
                {
                    Marshal.ReleaseComObject(audioEndpointVolume);
                }
            }
        }
Example #11
0
        public void Dispose()
        {
            keepgoing = false;
            Thread.Sleep(20);
            if (iAudioCaptureClient != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(iAudioCaptureClient);
                iAudioCaptureClient = null;
            }
            if (iAudioClient != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(iAudioClient);
                iAudioClient = null;
            }
            if (iAudioEndpoint != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(iAudioEndpoint);
                iAudioEndpoint = null;
            }

            if (imd != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(imd);
                imd = null;
            }

            if (iMde != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(iMde);
                iMde = null;
            }

            if (oEnumerator != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oEnumerator);
                oEnumerator = null;
            }
        }
Example #12
0
        public virtual void Dispose()
        {
            if (iAudioEndpoint != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(iAudioEndpoint);
                iAudioEndpoint = null;
            }

            if (oEndPoint != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oEndPoint);
                oEndPoint = null;
            }

            if (imd != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(imd);
                imd = null;
            }

            if (oDevice != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oDevice);
                oDevice = null;
            }

            if (iMde != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(iMde);
                iMde = null;
            }

            if (oEnumerator != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oEnumerator);
                oEnumerator = null;
            }
        }
Example #13
0
        /// <summary>
        /// Gets the current master volume in scalar values (percentage)
        /// </summary>
        /// <returns>-1 in case of an error, if successful the value will be between 0 and 100</returns>
        public static float GetMasterVolume()
        {
            IAudioEndpointVolume masterVol = null;

            try
            {
                masterVol = GetMasterVolumeObject();
                if (masterVol == null)
                {
                    return(-1);
                }

                masterVol.GetMasterVolumeLevelScalar(out var volumeLevel);
                return(volumeLevel * 100);
            }
            finally
            {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }
Example #14
0
        /// <summary>
        /// Gets the mute state of the master volume.
        /// While the volume can be muted the <see cref="GetMasterVolume"/> will still return the pre-muted volume value.
        /// </summary>
        /// <returns>false if not muted, true if volume is muted</returns>
        public static bool GetMasterVolumeMute()
        {
            IAudioEndpointVolume masterVol = null;

            try
            {
                masterVol = GetMasterVolumeObject();
                if (masterVol == null)
                {
                    return(false);
                }

                masterVol.GetMute(out var isMuted);
                return(isMuted);
            }
            finally
            {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }
        /// <summary>
        /// Switches between the master volume mute states depending on the current state
        /// </summary>
        /// <returns>the current mute state, true if the volume was muted, false if unmuted</returns>
        public static bool ToggleMasterVolumeMute()
        {
            IAudioEndpointVolume masterVol = null;

            try {
                masterVol = GetMasterVolumeObject();
                if (masterVol == null)
                {
                    return(false);
                }

                bool isMuted;
                masterVol.GetMute(out isMuted);
                masterVol.SetMute(!isMuted, Guid.Empty);

                return(!isMuted);
            } finally {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }
Example #16
0
        /// <summary>
        /// Disposes an instance of the <see cref="AudioCapture"/> class.
        /// </summary>
        public void Dispose()
        {
            this.StopCapture();

            if (this.volume != null)
            {
                if (this.volumeCallback != null)
                {
                    // Unregister the callback before releasing.
                    this.volume.UnregisterControlChangeNotify(this.volumeCallback);
                    this.volumeCallback = null;
                }

                Marshal.ReleaseComObject(this.volume);
                this.volume = null;
            }

            if (this.audioDevice != null)
            {
                Marshal.ReleaseComObject(this.audioDevice);
                this.audioDevice = null;
            }
        }
        public static float GetMasterVolume()
        {
            // retrieve audio device...
            IMMDeviceEnumerator useenumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
            IMMDevice           speakers;

            //retrieve the actual endpoint

            useenumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);

            object o;

            //retrieve the actual interface instance to retrieve the volume information from.
            speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out o);
            IAudioEndpointVolume aepv = (IAudioEndpointVolume)o;
            float result;

            aepv.GetMasterVolumeLevelScalar(out result);
            Marshal.ReleaseComObject(aepv);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(useenumerator);
            return(result);
        }
        /// <summary>
        /// Set the volume on the victims pc
        /// </summary>
        /// <param name="percentage">0-100</param>
        private static void SetVolume(string percentage)
        {
            try
            {
                float volume = float.Parse(percentage) / 100;

                keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY, 0);
                keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

                IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
                IMMDevice           speakers;
                const int           eRender     = 0;
                const int           eMultimedia = 1;
                deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out speakers);

                object aepv_obj;
                speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj);
                IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj;
                Guid ZeroGuid             = new Guid();
                int  res = aepv.SetMasterVolumeLevelScalar(volume, ZeroGuid);
            }
            catch (Exception) { }
        }
        public int getVolume()
        {
            IAudioEndpointVolume masterVol = null;

            try
            {
                masterVol = GetMasterVolumeObject();
                if (masterVol == null)
                {
                    return(-1);
                }

                float volumeLevel;
                masterVol.GetMasterVolumeLevelScalar(out volumeLevel);
                return((int)Math.Round((volumeLevel * 100)));
            }
            finally
            {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }
Example #20
0
        /// <summary>
        /// Increments or decrements the current volume level by the [stepAmount].
        /// </summary>
        /// <param name="stepAmount">Value between -100 and 100 indicating the desired step amount. Use negative numbers to decrease
        /// the volume and positive numbers to increase it.</param>
        /// <returns>the new volume level assigned, or -1 if failed.</returns>
        public static float StepMasterVolume(float stepAmount)
        {
            IAudioEndpointVolume masterVol = null;

            try
            {
                masterVol = GetMasterVolumeObject();
                if (masterVol == null)
                {
                    return(-1);
                }

                float stepAmountScaled = stepAmount / 100;

                // Get the level
                float volumeLevel;
                masterVol.GetMasterVolumeLevelScalar(out volumeLevel);

                // Calculate the new level
                float newLevel = volumeLevel + stepAmountScaled;
                newLevel = Math.Min(1, newLevel);
                newLevel = Math.Max(0, newLevel);

                masterVol.SetMasterVolumeLevelScalar(newLevel, Guid.Empty);

                // Return the new volume level that was set
                return(newLevel * 100);
            }
            finally
            {
                if (masterVol != null)
                {
                    Marshal.ReleaseComObject(masterVol);
                }
            }
        }
 internal AudioEndpointVolumeChannel(IAudioEndpointVolume parent, int channel)
 {
     _channel             = (uint)channel;
     _audioEndpointVolume = parent;
 }
 internal AudioEndpointVolumeStepInformation(IAudioEndpointVolume parent)
 {
     Marshal.ThrowExceptionForHR(parent.GetVolumeStepInfo(out _Step, out _StepCount));
 }
Example #23
0
 internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume)
 {
     _AudioEndPointVolume = realEndpointVolume;
     _CallBack = new AudioEndpointVolumeCallback(this);
     Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify( _CallBack));
 }
        /// <summary>

        /// Call this method to release all com objetcs

        /// </summary>

        public virtual void Dispose()
        {

            /*

            if (delMixerChange != null && iAudioEndpoint != null)

            {

            iAudioEndpoint.UnregisterControlChangeNotify(delMixerChange);

            }

            */

            if (iAudioEndpoint != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(iAudioEndpoint);

                iAudioEndpoint = null;

            }

            if (oEndPoint != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(oEndPoint);

                oEndPoint = null;

            }

            if (imd != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(imd);

                imd = null;

            }

            if (oDevice != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(oDevice);

                oDevice = null;

            }

            //System.Runtime.InteropServices.Marshal.ReleaseComObject(pCollection);

            if (iMde != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(iMde);

                iMde = null;

            }

            if (oEnumerator != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(oEnumerator);

                oEnumerator = null;

            }

        }
Example #25
0
 internal AudioEndpointVolumeChannel(IAudioEndpointVolume parent, int channel)
 {
     _Channel = (uint)channel;
     _AudioEndpointVolume = parent;
 }
Example #26
0
 public MasterVolumeControlCoreAudio()
 {
     endPointVol = GetAudioEndpointVolume();
 }
        /// <summary>
        ///     Dispose
        /// </summary>
        public void Dispose()
        {
            if (_callBack != null)
            {
                ComThread.BeginInvoke(() =>
                {
                    _audioEndPointVolume.UnregisterControlChangeNotify(_callBack);
                    _callBack = null;
                    _audioEndPointVolume = null;
                });
            }

            GC.SuppressFinalize(this);
        }
Example #28
0
        /* Set the Volume on Windows Vista by obtaining an
         * IAudioEndpointVolume COM object.
         */
        public static void SetVolumeWindowsVista(int value)
        {
            object enumerator           = null;
            Guid   devEnumeratorGuid    = new Guid("BCDE0395-E52F-467C-8E3D-C4579291692E");
            Guid   idevEnumeratorGuid   = new Guid("00000000-0000-0000-C000-000000000046");
            int    CLSCTX_INPROC_SERVER = 1;

            CoInitialize(null);
            int result = CoCreateInstance(ref devEnumeratorGuid, null,
                                          CLSCTX_INPROC_SERVER, ref idevEnumeratorGuid,
                                          out enumerator);

            if (result != 0 || enumerator == null)
            {
                return;
            }
            IMMDeviceEnumerator devEnum = enumerator as IMMDeviceEnumerator;

            if (devEnum == null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum);
                return;
            }
            IntPtr devicePtr = IntPtr.Zero;
            int    eRender = 0; int eConsole = 0;
            int    ret = devEnum.GetDefaultAudioEndpoint(eRender, eConsole, ref devicePtr);

            if (ret != 0)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum);
                return;
            }
            object    obj    = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(devicePtr);
            IMMDevice device = obj as IMMDevice;

            if (device == null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(device);
            }
            Guid   iAudioEndPointVolGuid = new Guid("5CDF2C82-841E-4546-9722-0CF74078229A");
            uint   context          = (uint)0x17;
            IntPtr activationParams = IntPtr.Zero;
            IntPtr endPoint         = IntPtr.Zero;

            ret = device.Activate(ref iAudioEndPointVolGuid, context,
                                  activationParams, ref endPoint);
            if (ret != 0)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(device);
                return;
            }
            obj = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(endPoint);
            IAudioEndpointVolume audioEndpoint = obj as IAudioEndpointVolume;

            if (audioEndpoint == null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(device);
                return;
            }
            Guid empty = Guid.Empty;

            audioEndpoint.SetMasterVolumeLevelScalar((float)(value / 100.0), empty);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(audioEndpoint);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(device);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum);
        }
 public override void Dispose()
 {
     Marshal.ReleaseComObject(endPointVol);
     endPointVol = null;
 }
 public MasterAudioSessionVolume(IAudioEndpointVolume aVol)
 => audioVolume = aVol;
 internal AudioEndpointVolumeVolumeRange(IAudioEndpointVolume parent)
 {
     Marshal.ThrowExceptionForHR(parent.GetVolumeRange(out volumeMinDecibels,out volumeMaxDecibels,out volumeIncrementDecibels));
 }
 internal AudioEndpointVolumeStepInformation(IAudioEndpointVolume parent)
 {
     Marshal.ThrowExceptionForHR(parent.GetVolumeStepInfo(out step, out stepCount));
 }
        //public event MixerChangedEventHandler MixerChanged;



        #region Class Constructor and Dispose public methods

        /// <summary>

        /// Constructor

        /// </summary>

        public EndpointVolume()
        {

            const uint CLSCTX_INPROC_SERVER = 1;

            Guid clsid = new Guid("BCDE0395-E52F-467C-8E3D-C4579291692E");

            Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");

            oEnumerator = null;

            uint hResult = CoCreateInstance(ref clsid, null, CLSCTX_INPROC_SERVER, ref IID_IUnknown, out oEnumerator);

            if (hResult != 0 || oEnumerator == null)
            {

                throw new Exception("CoCreateInstance() pInvoke failed");

            }

            iMde = oEnumerator as IMMDeviceEnumerator;

            if (iMde == null)
            {

                throw new Exception("COM cast failed to IMMDeviceEnumerator");

            }

            IntPtr pDevice = IntPtr.Zero;

            int retVal = iMde.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole, ref pDevice);

            if (retVal != 0)
            {

                throw new Exception("IMMDeviceEnumerator.GetDefaultAudioEndpoint()");

            }

            int dwStateMask = DEVICE_STATE_ACTIVE | DEVICE_STATE_NOTPRESENT | DEVICE_STATE_UNPLUGGED;

            IntPtr pCollection = IntPtr.Zero;

            retVal = iMde.EnumAudioEndpoints(EDataFlow.eRender, dwStateMask, ref pCollection);

            if (retVal != 0)
            {

                throw new Exception("IMMDeviceEnumerator.EnumAudioEndpoints()");

            }

            oDevice = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pDevice);

            imd = oDevice as IMMDevice;

            if (imd == null)
            {

                throw new Exception("COM cast failed to IMMDevice");

            }

            Guid iid = new Guid("5CDF2C82-841E-4546-9722-0CF74078229A");

            uint dwClsCtx = (uint)CLSCTX.CLSCTX_ALL;

            IntPtr pActivationParams = IntPtr.Zero;

            IntPtr pEndPoint = IntPtr.Zero;

            retVal = imd.Activate(ref iid, dwClsCtx, pActivationParams, ref pEndPoint);

            if (retVal != 0)
            {

                throw new Exception("IMMDevice.Activate()");

            }

            oEndPoint = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pEndPoint);

            iAudioEndpoint = oEndPoint as IAudioEndpointVolume;

            if (iAudioEndpoint == null)
            {

                throw new Exception("COM cast failed to IAudioEndpointVolume");

            }

            /*

            delMixerChange = new DelegateMixerChange(MixerChange);

            retVal = iAudioEndpoint.RegisterControlChangeNotify(delMixerChange);

            if (retVal != 0)

            {

            throw new Exception("iAudioEndpoint.RegisterControlChangeNotify(delMixerChange)");

            }

            */

        }
Example #34
0
 internal AudioEndpointVolumeVolumeRange(IAudioEndpointVolume parent)
 {
     Marshal.ThrowExceptionForHR(parent.GetVolumeRange(out volumeMinDecibels, out volumeMaxDecibels, out volumeIncrementDecibels));
 }
Example #35
0
        public CoreAudio(bool microphone = false)
        {
            const uint REFTIMES_PER_SEC     = 10000000;
            const uint CLSCTX_INPROC_SERVER = 1;
            Guid       clsid        = new Guid("BCDE0395-E52F-467C-8E3D-C4579291692E");
            Guid       IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");

            oEnumerator = null;
            uint hResult = CoCreateInstance(ref clsid, null, CLSCTX_INPROC_SERVER, ref IID_IUnknown, out oEnumerator);

            if (hResult != 0 || oEnumerator == null)
            {
                throw new Exception("CoCreateInstance() pInvoke failed");
            }

            iMde = oEnumerator as IMMDeviceEnumerator;
            if (iMde == null)
            {
                throw new Exception("COM cast failed to IMMDeviceEnumerator");
            }

            IntPtr pDevice = IntPtr.Zero;

            //iMde.EnumAudioEndpoints(EDataFlow.eCapture, DEVICE_STATE_ACTIVE,ref pDevice);
            int retVal;

            if (microphone)
            {
                retVal = iMde.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eConsole, ref pDevice);
            }
            else
            {
                retVal = iMde.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole, ref pDevice);
            }

            if (retVal != 0)
            {
                throw new Exception("IMMDeviceEnumerator.GetDefaultAudioEndpoint()");
            }
            //int dwStateMask = DEVICE_STATE_ACTIVE | DEVICE_STATE_NOTPRESENT | DEVICE_STATE_UNPLUGGED;
            //IntPtr pCollection = IntPtr.Zero;
            //retVal = iMde.EnumAudioEndpoints(EDataFlow.eRender, dwStateMask, ref pCollection);
            //if (retVal != 0)
            //{
            //    throw new Exception("IMMDeviceEnumerator.EnumAudioEndpoints()");
            //}
            imd = (IMMDevice)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pDevice);
            if (imd == null)
            {
                throw new Exception("COM cast failed to IMMDevice");
            }

            Guid   iid               = new Guid("5CDF2C82-841E-4546-9722-0CF74078229A");
            uint   dwClsCtx          = (uint)CLSCTX.CLSCTX_ALL;
            IntPtr pActivationParams = IntPtr.Zero;
            IntPtr pEndPoint         = IntPtr.Zero;

            retVal = imd.Activate(iid, dwClsCtx, pActivationParams, out pEndPoint);
            if (retVal != 0)
            {
                throw new Exception("IMMDevice.Activate()");
            }
            iAudioEndpoint = (IAudioEndpointVolume)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pEndPoint);
            if (iAudioEndpoint == null)
            {
                throw new Exception("COM cast failed to IAudioEndpointVolume");
            }

            iid               = new Guid("1CB9AD4C-DBFA-4c32-B178-C2F568A703B2");
            dwClsCtx          = (uint)CLSCTX.CLSCTX_ALL;
            pActivationParams = IntPtr.Zero;
            pEndPoint         = IntPtr.Zero;
            retVal            = imd.Activate(iid, dwClsCtx, pActivationParams, out pEndPoint);
            if (retVal != 0)
            {
                throw new Exception("IAudioClient.Activate() " + Convert.ToString(retVal, 2));
            }

            iAudioClient = (IAudioClient)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pEndPoint);
            if (iAudioClient == null)
            {
                throw new Exception("COM cast failed to iAudioClient");
            }
            ulong processInterval;
            ulong minimumInterval;

            retVal = iAudioClient.GetDevicePeriod(out processInterval, out minimumInterval);
            if (retVal != 0)
            {
                throw new Exception("iAudioClient.GetDevicePeriod()");
            }

            waveformat                 = new WAVEFORMATEX();
            waveformat.wFormatTag      = (ushort)WaveFormatEncoding.Pcm;
            waveformat.nChannels       = 2;
            waveformat.nBlockAlign     = 4;
            waveformat.wBitsPerSample  = 16;
            waveformat.nSamplesPerSec  = 44100;
            waveformat.cbSize          = 0;
            waveformat.nAvgBytesPerSec = 176400;
            IntPtr reqForm = Marshal.AllocHGlobal(Marshal.SizeOf(waveformat));

            Marshal.StructureToPtr(waveformat, reqForm, false);

            IntPtr propForm = Marshal.AllocHGlobal(Marshal.SizeOf(waveformat));

            retVal = iAudioClient.IsFormatSupported(AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_SHARED, reqForm, out propForm);
            if (retVal != 0)
            {
                throw new Exception("IAudioClient.IsFormatSupported()");
            }

            if (microphone)
            {
                retVal = iAudioClient.Initialize(AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_EXCLUSIVE, 0, 2000000, 0, reqForm, Guid.Empty);
            }
            else
            {
                retVal = iAudioClient.Initialize(AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, 2000000, 0, reqForm, Guid.Empty);
            }

            if (retVal != 0)
            {
                throw new Exception("IAudioClient.Initialize() " + retVal);
            }
            uint buffersize = 0;

            retVal = iAudioClient.GetBufferSize(out buffersize);
            if (retVal != 0)
            {
                throw new Exception("IAudioClient.GetBufferSize()");
            }
            iid = new Guid("C8ADBD64-E71E-48a0-A4DE-185C395CD317");
            IntPtr capclient = IntPtr.Zero;

            retVal = iAudioClient.GetService(iid, out capclient);
            if (retVal != 0)
            {
                throw new Exception("IAudioClient.GetService()");
            }
            iAudioCaptureClient = (IAudioCaptureClient)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(capclient);
            if (iAudioCaptureClient == null)
            {
                throw new Exception("COM cast failed to iAudioCaptureClient");
            }
            hnsActualDuration            = (double)(REFTIMES_PER_SEC * buffersize / waveformat.nSamplesPerSec); // 8391 smallest possible value
            recordingthread              = new Thread(recordingloop);
            recordingthread.IsBackground = false;
            recordingthread.Start();
        }
 internal AudioEndpointVolumeVolumeRange(IAudioEndpointVolume parent)
 {
     parent.GetVolumeRange(out volumeMinDecibels, out volumeMaxDecibels, out volumeIncrementDecibels);
 }
 internal AudioEndPointVolumeVolumeRange(IAudioEndpointVolume parent)
 {
     Marshal.ThrowExceptionForHR(parent.GetVolumeRange(out _VolumeMindB,out _VolumeMaxdB,out _VolumeIncrementdB));
 }
 internal AudioEndpointVolumeChannel(IAudioEndpointVolume parent, int channel)
 {
     this.channel = (uint)channel;
     audioEndpointVolume = parent;
 }
Example #39
0
        //public event MixerChangedEventHandler MixerChanged;

        #region Class Constructor and Dispose public methods

        /// <summary>

        /// Constructor

        /// </summary>

        public VistaVolumeControl()
        {
            const uint CLSCTX_INPROC_SERVER = 1;
            Guid       clsid        = new Guid("BCDE0395-E52F-467C-8E3D-C4579291692E");
            Guid       IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");

            oEnumerator = null;
            uint hResult = CoCreateInstance(ref clsid, null, CLSCTX_INPROC_SERVER, ref IID_IUnknown, out oEnumerator);

            if (hResult != 0 || oEnumerator == null)
            {
                Debug.WriteLine("CoCreateInstance() pInvoke failed");
                return;
                //throw new Exception("CoCreateInstance() pInvoke failed");
            }

            iMde = oEnumerator as IMMDeviceEnumerator;

            if (iMde == null)
            {
                Debug.WriteLine("COM cast failed to IMMDeviceEnumerator");
                return;
                //throw new Exception("COM cast failed to IMMDeviceEnumerator");
            }

            IntPtr pDevice = IntPtr.Zero;

            int retVal = iMde.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole, ref pDevice);

            if (retVal != 0)
            {
                Debug.WriteLine("IMMDeviceEnumerator.GetDefaultAudioEndpoint()");
                return;
                //throw new Exception("IMMDeviceEnumerator.GetDefaultAudioEndpoint()");
            }

            int    dwStateMask = DEVICE_STATE_ACTIVE | DEVICE_STATE_NOTPRESENT | DEVICE_STATE_UNPLUGGED;
            IntPtr pCollection = IntPtr.Zero;

            retVal = iMde.EnumAudioEndpoints(EDataFlow.eRender, dwStateMask, ref pCollection);

            if (retVal != 0)
            {
                Debug.WriteLine("IMMDeviceEnumerator.EnumAudioEndpoints()");
                return;
                //throw new Exception("IMMDeviceEnumerator.EnumAudioEndpoints()");
            }

            oDevice = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pDevice);

            imd = oDevice as IMMDevice;

            if (imd == null)
            {
                Debug.WriteLine("COM cast failed to IMMDevice");
                return;
                //throw new Exception("COM cast failed to IMMDevice");
            }

            Guid iid = new Guid("5CDF2C82-841E-4546-9722-0CF74078229A");

            uint dwClsCtx = (uint)CLSCTX.CLSCTX_ALL;

            IntPtr pActivationParams = IntPtr.Zero;

            IntPtr pEndPoint = IntPtr.Zero;

            retVal = imd.Activate(ref iid, dwClsCtx, pActivationParams, ref pEndPoint);

            if (retVal != 0)
            {
                Debug.WriteLine("IMMDevice.Activate()");
                return;
                //throw new Exception("IMMDevice.Activate()");
            }

            oEndPoint = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(pEndPoint);

            iAudioEndpoint = oEndPoint as IAudioEndpointVolume;

            if (iAudioEndpoint == null)
            {
                Debug.WriteLine("COM cast failed to IAudioEndpointVolume");
                return;
                //throw new Exception("COM cast failed to IAudioEndpointVolume");
            }

            /*
             * delMixerChange = new DelegateMixerChange(MixerChange);
             * retVal = iAudioEndpoint.RegisterControlChangeNotify(delMixerChange);
             * if (retVal != 0)
             * {
             * throw new Exception("iAudioEndpoint.RegisterControlChangeNotify(delMixerChange)");
             * }
             */
        }
 internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume)
 {
     _AudioEndPointVolume = realEndpointVolume;
     _CallBack            = new AudioEndpointVolumeCallback(this);
     Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack));
 }
        public virtual void Dispose()
        {
            if (iAudioEndpoint != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(iAudioEndpoint);

                iAudioEndpoint = null;

            }

            if (oEndPoint != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(oEndPoint);

                oEndPoint = null;

            }

            if (imd != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(imd);

                imd = null;

            }

            if (oDevice != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(oDevice);

                oDevice = null;

            }

            if (iMde != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(iMde);

                iMde = null;

            }

            if (oEnumerator != null)
            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(oEnumerator);

                oEnumerator = null;

            }
        }
Example #42
0
 private static void GetGlobalControls(out IMMDeviceEnumerator deviceEnumerator, out IMMDevice speakers, out IAudioEndpointVolume aev)
 {
     GetDeviceEnumerator(out deviceEnumerator);
     GetSpeakers(deviceEnumerator, out speakers);
     GetAudioEndpointVolume(speakers, out aev);
 }
Example #43
0
 private AudioEndpoint(IAudioEndpointVolume audioEndpointVolume)
 {
     this._audioEndpointVolume = audioEndpointVolume;
 }
Example #44
0
 internal AEDev()
 {
   IMMDevice _Device = null;
   Marshal.ThrowExceptionForHR(((IMMDeviceEnumerator)_realEnumerator).GetDefaultAudioEndpoint(0, 1, out _Device));
   _RealDevice = _Device;
   object result;
   Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioEndpointVolume, CTX.ALL, IntPtr.Zero, out result));
   _AudioEndPointVolume = result as IAudioEndpointVolume;
   _CallBack = new AudioEndpointVolumeCallback(this);
   Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack));
 }
 public MasterVolumeNotificationHandler(IAudioEndpointVolume eVolume)
 {
     this.endpointVolume = eVolume;
 }
Example #46
0
 internal AudioEndpointVolumeChannel(IAudioEndpointVolume parent, int channel)
 {
     this._Channel             = (uint)channel;
     this._AudioEndpointVolume = parent;
 }
 public AudioDeviceChannel(IAudioEndpointVolume deviceVolume, uint index)
 {
     _index        = index;
     _deviceVolume = deviceVolume;
     _level        = _deviceVolume.GetChannelVolumeLevelScalar(index);
 }
 internal AudioEndPointVolumeVolumeRange(IAudioEndpointVolume parent)
 {
     Marshal.ThrowExceptionForHR(parent.GetVolumeRange(out this._VolumeMindB, out this._VolumeMaxdB, out this._VolumeIncrementdB));
 }
 public MasterVolumeControlCoreAudio()
 {
     endPointVol = GetAudioEndpointVolume();
 }