Beispiel #1
0
        private void SetValue(MixerComponentType componentType, MixerControlType controlType, int controlValue)
        {
            MixerNativeMethods.MixerLine mixerLine = new MixerNativeMethods.MixerLine(componentType);

            if (MixerNativeMethods.mixerGetLineInfoA(_handle, ref mixerLine, MixerLineFlags.ComponentType) != MixerError.None)
            {
                throw new InvalidOperationException("Mixer.SetValue.1");
            }

            using (
                MixerNativeMethods.MixerLineControls mixerLineControls =
                    new MixerNativeMethods.MixerLineControls(mixerLine.LineId, controlType))
            {
                if (MixerNativeMethods.mixerGetLineControlsA(_handle, mixerLineControls, MixerLineControlFlags.OneByType) !=
                    MixerError.None)
                {
                    throw new InvalidOperationException("Mixer.SetValue.2");
                }

                MixerNativeMethods.MixerControl mixerControl =
                    (MixerNativeMethods.MixerControl)
                    Marshal.PtrToStructure(mixerLineControls.Data, typeof(MixerNativeMethods.MixerControl));

                using (
                    MixerNativeMethods.MixerControlDetails mixerControlDetails =
                        new MixerNativeMethods.MixerControlDetails(mixerControl.ControlId))
                {
                    Marshal.WriteInt32(mixerControlDetails.Data, controlValue);
                    MixerNativeMethods.mixerSetControlDetails(_handle, mixerControlDetails, 0);
                }
            }
        }
Beispiel #2
0
        private MixerNativeMethods.MixerControlDetails GetControl(MixerComponentType componentType,
                                                                  MixerControlType controlType)
        {
            MixerNativeMethods.MixerLine mixerLine = new MixerNativeMethods.MixerLine(componentType);

            if (MixerNativeMethods.mixerGetLineInfoA(_handle, ref mixerLine, MixerLineFlags.ComponentType) != MixerError.None)
            {
                throw new InvalidOperationException("Mixer.GetControl.1");
            }

            using (
                MixerNativeMethods.MixerLineControls mixerLineControls =
                    new MixerNativeMethods.MixerLineControls(mixerLine.LineId, controlType))
            {
                if (MixerNativeMethods.mixerGetLineControlsA(_handle, mixerLineControls, MixerLineControlFlags.OneByType) !=
                    MixerError.None)
                {
                    throw new InvalidOperationException("Mixer.GetControl.2");
                }

                MixerNativeMethods.MixerControl mixerControl =
                    (MixerNativeMethods.MixerControl)
                    Marshal.PtrToStructure(mixerLineControls.Data, typeof(MixerNativeMethods.MixerControl));

                return(new MixerNativeMethods.MixerControlDetails(mixerControl.ControlId));
            }
        }
        public void Open(int mixerIndex, bool isDigital)
        {
            lock (this)
            {
                if (isDigital)
                {
                    _componentType = MixerComponentType.SourceWave;
                }
                // not enough to change this..

                if (_mixerEventListener == null)
                {
                    _mixerEventListener = new MixerEventListener();
                    _mixerEventListener.Start();
                }
                _mixerEventListener.LineChanged    += new MixerEventHandler(OnLineChanged);
                _mixerEventListener.ControlChanged += new MixerEventHandler(OnControlChanged);

                MixerNativeMethods.MixerControl mc = new MixerNativeMethods.MixerControl();

                mc.Size          = 0;
                mc.ControlId     = 0;
                mc.ControlType   = MixerControlType.Volume;
                mc.fdwControl    = 0;
                mc.MultipleItems = 0;
                mc.ShortName     = string.Empty;
                mc.Name          = string.Empty;
                mc.Minimum       = 0;
                mc.Maximum       = 0;
                mc.Reserved      = 0;

                IntPtr handle = IntPtr.Zero;

                if (
                    MixerNativeMethods.mixerOpen(ref handle, mixerIndex, _mixerEventListener.Handle, 0, MixerFlags.CallbackWindow) !=
                    MixerError.None)
                {
                    throw new InvalidOperationException();
                }

                _handle = handle;

                _mixerControlDetailsVolume = GetControl(_componentType, MixerControlType.Volume);
                _mixerControlDetailsMute   = GetControl(_componentType, MixerControlType.Mute);

                _isMuted = (int)GetValue(_componentType, MixerControlType.Mute) == 1;
                _volume  = (int)GetValue(_componentType, MixerControlType.Volume);
            }
        }
Beispiel #4
0
        private object GetValue(MixerComponentType componentType, MixerControlType controlType)
        {
            try
            {
                MixerNativeMethods.MixerLine mixerLine = new MixerNativeMethods.MixerLine(componentType);

                if (MixerNativeMethods.mixerGetLineInfoA(_handle, ref mixerLine, MixerLineFlags.ComponentType) !=
                    MixerError.None)
                {
                    throw new InvalidOperationException("Mixer.OpenControl.1");
                }

                using (
                    MixerNativeMethods.MixerLineControls mixerLineControls =
                        new MixerNativeMethods.MixerLineControls(mixerLine.LineId, controlType))
                {
                    MixerNativeMethods.mixerGetLineControlsA(_handle, mixerLineControls, MixerLineControlFlags.OneByType);
                    MixerNativeMethods.MixerControl mixerControl =
                        (MixerNativeMethods.MixerControl)
                        Marshal.PtrToStructure(mixerLineControls.Data, typeof(MixerNativeMethods.MixerControl));

                    using (
                        MixerNativeMethods.MixerControlDetails mixerControlDetails =
                            new MixerNativeMethods.MixerControlDetails(mixerControl.ControlId))
                    {
                        MixerNativeMethods.mixerGetControlDetailsA(_handle, mixerControlDetails, 0);

                        return(Marshal.ReadInt32(mixerControlDetails.Data));
                    }
                }
            }
            catch (Exception ex)
            {
                // Catch exception when audio device is disconnected
                Log.Error("Mixer: GetValue {0}", ex.Message);
            }
            // Set Volume to 30000 when audio recover
            return(30000);
        }
Beispiel #5
0
        private MixerNativeMethods.MixerControlDetails GetControl(MixerComponentType componentType,
                                                                  MixerControlType controlType)
        {
            try
            {
                MixerNativeMethods.MixerLine mixerLine = new MixerNativeMethods.MixerLine(componentType);

                if (MixerNativeMethods.mixerGetLineInfoA(_handle, ref mixerLine, MixerLineFlags.ComponentType) !=
                    MixerError.None)
                {
                    throw new InvalidOperationException("Mixer.GetControl.1");
                }

                using (
                    MixerNativeMethods.MixerLineControls mixerLineControls =
                        new MixerNativeMethods.MixerLineControls(mixerLine.LineId, controlType))
                {
                    if (MixerNativeMethods.mixerGetLineControlsA(_handle, mixerLineControls, MixerLineControlFlags.OneByType) !=
                        MixerError.None)
                    {
                        throw new InvalidOperationException("Mixer.GetControl.2");
                    }

                    MixerNativeMethods.MixerControl mixerControl =
                        (MixerNativeMethods.MixerControl)
                        Marshal.PtrToStructure(mixerLineControls.Data, typeof(MixerNativeMethods.MixerControl));

                    return(new MixerNativeMethods.MixerControlDetails(mixerControl.ControlId));
                }
            }
            catch (Exception ex)
            {
                // Catch exception when audio device is disconnected
                Log.Error("Mixer: GetControl {0}", ex.Message);
            }
            return(null);
        }
Beispiel #6
0
    public void Open(int mixerIndex, bool isDigital)
    {
      lock (this)
      {
        _waveVolume = isDigital;
        if (isDigital)
        {
          _componentType = MixerComponentType.SourceWave;
        }
        else
        {
          _componentType = MixerComponentType.DestinationSpeakers;
        }
        // not enough to change this..

        // Use Endpoint Volume API for Vista/Win7 if master volume is selected and always for Win8 to handle muting of master volume
        if ((OSInfo.OSInfo.VistaOrLater() && _componentType == MixerComponentType.DestinationSpeakers) ||
            OSInfo.OSInfo.Win8OrLater())
        {
          try
          {
            _audioDefaultDevice = new AEDev();
            if (_audioDefaultDevice != null)
            {
              _audioDefaultDevice.OnVolumeNotification +=
                new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification);

              _isMuted = _audioDefaultDevice.Muted;
              _volume = (int) Math.Round(_audioDefaultDevice.MasterVolume*VolumeMaximum);
            }
          }
          catch (Exception)
          {
            _isMuted = false;
            _volume = 100;
          }
        }

        // Use Windows Multimedia mixer functions for XP and for Vista and later if wave volume is selected
        if (_componentType == MixerComponentType.SourceWave || !OSInfo.OSInfo.VistaOrLater())
        {
          if (_mixerEventListener == null)
          {
            _mixerEventListener = new MixerEventListener();
            _mixerEventListener.Start();
          }
          _mixerEventListener.LineChanged += new MixerEventHandler(OnLineChanged);
          _mixerEventListener.ControlChanged += new MixerEventHandler(OnControlChanged);

          MixerNativeMethods.MixerControl mc = new MixerNativeMethods.MixerControl();

          mc.Size = 0;
          mc.ControlId = 0;
          mc.ControlType = MixerControlType.Volume;
          mc.fdwControl = 0;
          mc.MultipleItems = 0;
          mc.ShortName = string.Empty;
          mc.Name = string.Empty;
          mc.Minimum = 0;
          mc.Maximum = 0;
          mc.Reserved = 0;

          IntPtr handle = IntPtr.Zero;

          if (
            MixerNativeMethods.mixerOpen(ref handle, mixerIndex, _mixerEventListener.Handle, 0,
                                         MixerFlags.CallbackWindow) !=
            MixerError.None)
          {
            throw new InvalidOperationException();
          }

          _handle = handle;

          _mixerControlDetailsVolume = GetControl(_componentType, MixerControlType.Volume);
          _mixerControlDetailsMute = GetControl(_componentType, MixerControlType.Mute);

          _isMuted = (int) GetValue(_componentType, MixerControlType.Mute) == 1;
          _volume = (int) GetValue(_componentType, MixerControlType.Volume);
        }
      }
    }
Beispiel #7
0
    public void Open(int mixerIndex, bool isDigital)
    {
      lock (this)
      {
        if (isDigital)
        {
          _componentType = MixerComponentType.SourceWave;
        }
        else
        {
          _componentType = MixerComponentType.DestinationSpeakers;
        }
        // not enough to change this..

        if (OSInfo.OSInfo.VistaOrLater() & (_componentType == MixerComponentType.DestinationSpeakers))
        {
          _audioDefaultDevice = new AEDev();
          _audioDefaultDevice.OnVolumeNotification += new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification);

          _isMuted = _audioDefaultDevice.Muted;
          _volume = (int)Math.Round(_audioDefaultDevice.MasterVolume * VolumeMaximum); 

          return;
        }

        if (_mixerEventListener == null)
        {
          _mixerEventListener = new MixerEventListener();
          _mixerEventListener.Start();
        }
        _mixerEventListener.LineChanged += new MixerEventHandler(OnLineChanged);
        _mixerEventListener.ControlChanged += new MixerEventHandler(OnControlChanged);

        MixerNativeMethods.MixerControl mc = new MixerNativeMethods.MixerControl();

        mc.Size = 0;
        mc.ControlId = 0;
        mc.ControlType = MixerControlType.Volume;
        mc.fdwControl = 0;
        mc.MultipleItems = 0;
        mc.ShortName = string.Empty;
        mc.Name = string.Empty;
        mc.Minimum = 0;
        mc.Maximum = 0;
        mc.Reserved = 0;

        IntPtr handle = IntPtr.Zero;

        if (
          MixerNativeMethods.mixerOpen(ref handle, mixerIndex, _mixerEventListener.Handle, 0, MixerFlags.CallbackWindow) !=
          MixerError.None)
        {
          throw new InvalidOperationException();
        }

        _handle = handle;

        _mixerControlDetailsVolume = GetControl(_componentType, MixerControlType.Volume);
        _mixerControlDetailsMute = GetControl(_componentType, MixerControlType.Mute);

        _isMuted = (int)GetValue(_componentType, MixerControlType.Mute) == 1;
        _volume = (int)GetValue(_componentType, MixerControlType.Volume);
      }
    }
 public void Open(int mixerIndex, bool isDigital, bool resetDevice = false)
 {
   try
   {
     lock (this)
     {
       using (MixerLock(lockInfinite))
       {
         Log.Debug("Mixer: Open(), mixerIndex = {0}, isDigital = {1}, resetDevice = {2}", mixerIndex, isDigital, resetDevice);
         _useWave = isDigital;
         if (isDigital)
         {
           _componentType = MixerComponentType.SourceWave;
         }
         else
         {
           _componentType = MixerComponentType.DestinationSpeakers;
         }
 
         // Use Endpoint Volume API for Vista/Win7 if master volume is selected and always for Win8 to handle muting of master volume
         if ((OSInfo.OSInfo.VistaOrLater() && _componentType == MixerComponentType.DestinationSpeakers) ||
             OSInfo.OSInfo.Win8OrLater())
         {
           try
           {
             Log.Debug("Mixer: Open(), Endpoint Volume API for Master");
             _audioDefaultDevice = new AEDev(resetDevice);
             if (_audioDefaultDevice != null)
             {
               _audioDefaultDevice.OnVolumeNotification +=
                 new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification);
 
               _isMuted = _audioDefaultDevice.Muted;
               _volume = (int)Math.Round(_audioDefaultDevice.MasterVolume * VolumeMaximum);
             }
           }
           catch (Exception ex)
           {
             Log.Error($"Mixer: Open(), Exception in Endpoint Volume API for Master: {ex}");
             _isMuted = false;
             _volume = 100;
           }
         }
         else if (OSInfo.OSInfo.VistaOrLater() && _componentType == MixerComponentType.SourceWave)
         {
           try
           {
             Log.Debug("Mixer: Open(), Endpoint Volume API for Wave");
             _audioDefaultDevice = new AEDev(resetDevice);
             if (_audioDefaultDevice != null)
             {
               _audioDefaultDevice.OnVolumeNotification +=
                 new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification);
             }
           }
           catch (Exception ex)
           {
             Log.Error($"Mixer: Open(), Exception in Endpoint Volume API for Wave: {ex}");
           }
         }
 
         // Use Windows Multimedia mixer functions for XP and for Vista and later if wave volume is selected
         if (_componentType == MixerComponentType.SourceWave || !OSInfo.OSInfo.VistaOrLater())
         {
           if (_mixerEventListener == null)
           {
             _mixerEventListener = new MixerEventListener();
             _mixerEventListener.Start();
           }
           _mixerEventListener.LineChanged += new MixerEventHandler(OnLineChanged);
           _mixerEventListener.ControlChanged += new MixerEventHandler(OnControlChanged);
 
           MixerNativeMethods.MixerControl mc = new MixerNativeMethods.MixerControl();
 
           mc.Size = 0;
           mc.ControlId = 0;
           mc.ControlType = MixerControlType.Volume;
           mc.fdwControl = 0;
           mc.MultipleItems = 0;
           mc.ShortName = string.Empty;
           mc.Name = string.Empty;
           mc.Minimum = 0;
           mc.Maximum = 0;
           mc.Reserved = 0;
 
           IntPtr handle = IntPtr.Zero;
 
           if (
             MixerNativeMethods.mixerOpen(ref handle, mixerIndex, _mixerEventListener.Handle, 0,
                                          MixerFlags.CallbackWindow) !=
             MixerError.None)
           {
             throw new InvalidOperationException();
           }
 
           _handle = handle;
 
           _mixerControlDetailsVolume = GetControl(_componentType, MixerControlType.Volume);
           _mixerControlDetailsMute = GetControl(_componentType, MixerControlType.Mute);
 
           _isMuted = (int)GetValue(_componentType, MixerControlType.Mute) == 1;
           _volume = (int)GetValue(_componentType, MixerControlType.Volume);
           _isMutedWave = _isMuted;
           _volumeWave = _volume;
         }
       }
     }
   }
   catch (Exception ex)
   {
     Log.Error($"Mixer: error occured in Open(): {ex}");
     _isMuted = false;
     _volume = 100;
   }
 }
Beispiel #9
0
    public void Open(int mixerIndex, bool isDigital)
    {
      lock (this)
      {
        if (isDigital)
        {
          _componentType = MixerComponentType.SourceWave;
        }
        // not enough to change this..

        if (_mixerEventListener == null)
        {
          _mixerEventListener = new MixerEventListener();
          _mixerEventListener.Start();
        }
        _mixerEventListener.LineChanged += new MixerEventHandler(OnLineChanged);
        _mixerEventListener.ControlChanged += new MixerEventHandler(OnControlChanged);

        MixerNativeMethods.MixerControl mc = new MixerNativeMethods.MixerControl();

        mc.Size = 0;
        mc.ControlId = 0;
        mc.ControlType = MixerControlType.Volume;
        mc.fdwControl = 0;
        mc.MultipleItems = 0;
        mc.ShortName = string.Empty;
        mc.Name = string.Empty;
        mc.Minimum = 0;
        mc.Maximum = 0;
        mc.Reserved = 0;

        IntPtr handle = IntPtr.Zero;

        if (
          MixerNativeMethods.mixerOpen(ref handle, mixerIndex, _mixerEventListener.Handle, 0, MixerFlags.CallbackWindow) !=
          MixerError.None)
        {
          throw new InvalidOperationException();
        }

        _handle = handle;

        _mixerControlDetailsVolume = GetControl(_componentType, MixerControlType.Volume);
        _mixerControlDetailsMute = GetControl(_componentType, MixerControlType.Mute);

        _isMuted = (int)GetValue(_componentType, MixerControlType.Mute) == 1;
        _volume = (int)GetValue(_componentType, MixerControlType.Volume);
      }
    }
Beispiel #10
0
        public void Open(int mixerIndex, bool isDigital)
        {
            lock (this)
            {
                if (isDigital)
                {
                    _componentType = MixerComponentType.SourceWave;
                }
                else
                {
                    _componentType = MixerComponentType.DestinationSpeakers;
                }
                // not enough to change this..

                if (OSInfo.OSInfo.VistaOrLater() & (_componentType == MixerComponentType.DestinationSpeakers))
                {
                    _audioDefaultDevice = new AEDev();
                    _audioDefaultDevice.OnVolumeNotification += new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification);

                    _isMuted = _audioDefaultDevice.Muted;
                    _volume  = (int)Math.Round(_audioDefaultDevice.MasterVolume * VolumeMaximum);

                    return;
                }

                if (_mixerEventListener == null)
                {
                    _mixerEventListener = new MixerEventListener();
                    _mixerEventListener.Start();
                }
                _mixerEventListener.LineChanged    += new MixerEventHandler(OnLineChanged);
                _mixerEventListener.ControlChanged += new MixerEventHandler(OnControlChanged);

                MixerNativeMethods.MixerControl mc = new MixerNativeMethods.MixerControl();

                mc.Size          = 0;
                mc.ControlId     = 0;
                mc.ControlType   = MixerControlType.Volume;
                mc.fdwControl    = 0;
                mc.MultipleItems = 0;
                mc.ShortName     = string.Empty;
                mc.Name          = string.Empty;
                mc.Minimum       = 0;
                mc.Maximum       = 0;
                mc.Reserved      = 0;

                IntPtr handle = IntPtr.Zero;

                if (
                    MixerNativeMethods.mixerOpen(ref handle, mixerIndex, _mixerEventListener.Handle, 0, MixerFlags.CallbackWindow) !=
                    MixerError.None)
                {
                    throw new InvalidOperationException();
                }

                _handle = handle;

                _mixerControlDetailsVolume = GetControl(_componentType, MixerControlType.Volume);
                _mixerControlDetailsMute   = GetControl(_componentType, MixerControlType.Mute);

                _isMuted = (int)GetValue(_componentType, MixerControlType.Mute) == 1;
                _volume  = (int)GetValue(_componentType, MixerControlType.Volume);
            }
        }