示例#1
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));
 }
示例#2
0
        public AudioDevice(IAudioDeviceManager deviceManager, IMMDevice device, Dispatcher foregroundDispatcher)
        {
            _device        = device;
            _deviceManager = new WeakReference <IAudioDeviceManager>(deviceManager);
            _dispatcher    = foregroundDispatcher;
            _id            = device.GetId();

            Trace.WriteLine($"AudioDevice Create {_id}");

            if (_device.GetState() == DeviceState.ACTIVE)
            {
                _deviceVolume = device.Activate <IAudioEndpointVolume>();
                _deviceVolume.RegisterControlChangeNotify(this);
                _deviceVolume.GetMasterVolumeLevelScalar(out _volume);
                _isMuted       = _deviceVolume.GetMute() != 0;
                _isRegistered  = true;
                _meter         = device.Activate <IAudioMeterInformation>();
                _channels      = new AudioDeviceChannelCollection(_deviceVolume, _dispatcher);
                _sessions      = new AudioDeviceSessionCollection(this, _device, _dispatcher);
                _sessionFilter = new FilteredCollectionChain <IAudioDeviceSession>(_sessions.Sessions, _dispatcher);
                Groups         = _sessionFilter.Items;
            }
            else
            {
                Groups = new ObservableCollection <IAudioDeviceSession>();
            }

            ReadProperties();
        }
        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));
        }
示例#4
0
        public DS4Audio(DataFlow audioFlags = DataFlow.Render,
                        string searchName   = "Wireless Controller")
        {
            var audioEnumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDeviceCollection audioDevices;

            audioEnumerator.EnumAudioEndpoints(audioFlags, DeviceState.Active, out audioDevices);

            int numAudioDevices;

            Marshal.ThrowExceptionForHR(audioDevices.GetCount(out numAudioDevices));

            for (int deviceNumber = 0; deviceNumber < numAudioDevices; ++deviceNumber)
            {
                IMMDevice audioDevice;
                Marshal.ThrowExceptionForHR(audioDevices.Item(deviceNumber, out audioDevice));
                string deviceName = GetAudioDeviceName(ref audioDevice);

                if (deviceName.Contains(searchName))
                {
                    object interfacePointer;
                    Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer));
                    endpointVolume = interfacePointer as IAudioEndpointVolume;
                    endpointVolume.RegisterControlChangeNotify(this);
                }

                RefreshVolume();
                Marshal.ReleaseComObject(audioDevice);
            }

            Marshal.ReleaseComObject(audioDevices);
            Marshal.ReleaseComObject(audioEnumerator);
        }
示例#5
0
        /// <summary>
        /// The Initialize Audio Client
        /// </summary>
        /// <param name="audioFlow"></param>
        /// <param name="_deviceEnumerator"></param>
        private void InitializeAudioClient()
        {
            //Get Audio Client from device
            COMResult result = _audioDevice.Activate(typeof(IAudioClient).GUID, 0, IntPtr.Zero, out object obj);

            _audioClient = (IAudioClient)obj;
            //Get Audio Meter from device
            result      = _audioDevice.Activate(typeof(IAudioMeterInformation).GUID, 0, IntPtr.Zero, out obj);
            _audioMeter = (IAudioMeterInformation)obj;
            //Get Audio End Point
            result = _audioDevice.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out obj);
            _audioEndpointVolume = (IAudioEndpointVolume)obj;
            _audioEndpointVolume.RegisterControlChangeNotify(classCallBack);
            //Initialize Audio Client.
            _sessionGuid = new Guid();
            result       = _audioClient.GetMixFormat(out waveFormat);
            AudioClientStreamFlags streamFlag = AudioClientStreamFlags.None;

            if (_audioDataFlow == AudioDataFlow.eRender)
            {
                streamFlag = AudioClientStreamFlags.Loopback;
            }
            result = _audioClient.Initialize(AudioClientMode.Shared, streamFlag, 10000000, 0, waveFormat, ref _sessionGuid);
            result = _audioClient.Start();
            //Change wave format here
            SetupWaveFormat(waveFormat);

            result = _audioEndpointVolume.GetChannelCount(out _channelCount);
        }
        public AEDev(bool resetDevice = false)
        {
            IMMDevice _Device = null;

            if (resetDevice)
            {
                _devId = "";
            }

            _realEnumerator = new _AEDeviceEnumerator() as IMMDeviceEnumerator;
            try
            {
                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($"audio device is not active ({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));
            }
            catch (Exception)
            {
                // Catch if no device is found or changed device
                try
                {
                    Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(0, 1, out _Device));
                    Marshal.ThrowExceptionForHR(_Device.GetId(out _devId));
                    devstatus state;
                    Marshal.ThrowExceptionForHR(_Device.GetState(out state));
                    if (state != devstatus.DEVICE_STATE_ACTIVE)
                    {
                        throw new ApplicationException($"audio device is not active ({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));
                }
                catch (Exception)
                {
                    // Catch if no device is found
                }
            }
        }
示例#7
0
        public AudioEndpointVolume(IAudioEndpointVolume audioEndpointVolume)
        {
            _audioEndpointVolume = audioEndpointVolume;

            _callBack = new AudioEndpointVolumeCallback(this);
            Marshal.ThrowExceptionForHR(_audioEndpointVolume.RegisterControlChangeNotify(_callBack));
        }
示例#8
0
 internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume)
 {
     _AudioEndPointVolume = realEndpointVolume;
     _Channels            = new AudioEndpointVolumeChannels(_AudioEndPointVolume);
     _StepInformation     = new AudioEndpointVolumeStepInformation(_AudioEndPointVolume);
     Marshal.ThrowExceptionForHR(_AudioEndPointVolume.QueryHardwareSupport(out uint HardwareSupp));
     _HardwareSupport = (EEndpointHardwareSupport)HardwareSupp;
     _VolumeRange     = new AudioEndPointVolumeVolumeRange(_AudioEndPointVolume);
     _CallBack        = new AudioEndpointVolumeCallback(this);
     Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack));
 }
示例#9
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));
        }
示例#10
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));
        }
示例#12
0
        /// <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 = (EEndpointHardwareSupport)hardwareSupp;
            volumeRange     = new AudioEndpointVolumeVolumeRange(audioEndPointVolume);
            callBack        = new AudioEndpointVolumeCallback(this);
            Marshal.ThrowExceptionForHR(audioEndPointVolume.RegisterControlChangeNotify(callBack));
        }
示例#13
0
        /// <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));
        }
示例#14
0
        /// <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));
        }
示例#15
0
        public AudioDevice(IMMDevice device)
        {
            _device     = device;
            _dispatcher = App.Current.Dispatcher;
            _id         = device.GetId();

            Trace.WriteLine($"AudioDevice Create {_id}");

            _deviceVolume = device.Activate <IAudioEndpointVolume>();

            _deviceVolume.RegisterControlChangeNotify(this);
            _meter    = device.Activate <IAudioMeterInformation>();
            _sessions = new AudioDeviceSessionCollection(this, _device);

            _deviceVolume.GetMasterVolumeLevelScalar(out _volume);
            _isMuted = _deviceVolume.GetMute() != 0;

            ReadDisplayName();
        }
示例#16
0
        public static void SetMasterVolumeNotifier(VolumeListener listner)
        {
            IAudioEndpointVolume        masterVol = null;
            AudioEndpointVolumeCallback callBack;

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

                callBack = new AudioEndpointVolumeCallback(listner);
                Marshal.ThrowExceptionForHR(masterVol.RegisterControlChangeNotify(callBack));
            }
            finally
            {
                // if (masterVol != null)
                // Marshal.ReleaseComObject(masterVol);
            }
        }
示例#17
0
        private void UpdateUnmanagedNotificationRegistration()
        {
            var shouldBeRegisteredForNotifications = false;

            // if any events are registered by our caller, we need to be registered for unmanaged volume/mute state change notifications
            //
            // volume level notifications
            if (_masterVolumeLevelChangedEvent is not null)
            {
                shouldBeRegisteredForNotifications = true;
            }
            //
            // mute state notifications
            if (_masterMuteStateChangedEvent is not null)
            {
                shouldBeRegisteredForNotifications = true;
            }

            if (shouldBeRegisteredForNotifications == true)
            {
                // if we have subscribed events and are _not_ already registered for notifications, register for notifications now
                if (_audioEndpointVolumeCallback is null)
                {
                    _audioEndpointVolumeCallback = new AudioEndpointVolumeCallback(this.AudioVolumeNotificationCallback);
                    _audioEndpointVolume.RegisterControlChangeNotify(_audioEndpointVolumeCallback);
                }
            }
            else
            {
                // unregister for notifications if we are already registered...but no longer have any subscribed events
                if (_audioEndpointVolumeCallback is not null)
                {
                    _audioEndpointVolume.UnregisterControlChangeNotify(_audioEndpointVolumeCallback !);
                }
            }
        }
示例#18
0
 internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume)
 {
     _AudioEndPointVolume = realEndpointVolume;
     _CallBack = new AudioEndpointVolumeCallback(this);
     Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify( _CallBack));
 }
 internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume)
 {
     _AudioEndPointVolume = realEndpointVolume;
     _CallBack            = new AudioEndpointVolumeCallback(this);
     Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack));
 }
示例#20
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));
 }