Пример #1
0
 public void Dispose()
 {
   if (_audioDefaultDevice != null)
   {
     _audioDefaultDevice.SafeDispose();
     _audioDefaultDevice = null;
   }
 }
Пример #2
0
    public MasterVolume()
    {
      bool useWaveVolume;

      // If "Upmix stereo to 5.1/7.1" is enabled or the MP volume control is set to "Wave", the setting
      // "digital" is true and the MP volume handler will control the wave volume (not on Windows XP).
      using (Settings reader = new MPSettings())
      {
        useWaveVolume = Environment.OSVersion.Version.Major >= 6 && reader.GetValueAsBool("volume", "digital", false);
      }

      if (useWaveVolume)
      {
        // Since MP's volume handler controls the wave volume, we have to access the Audio Endpoint Device directly
        Log.Debug("PS: Control the master volume by accessing the Audio Endpoint Device directly");
        _audioDefaultDevice = new AEDev();
      }
      else
      {
        // We use MP's volume handler, since it controls the master volume
        Log.Debug("PS: Control the master volume by using MP's volume handler");
      }
    }
Пример #3
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);
        }
      }
    }
Пример #4
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);
      }
    }
Пример #5
0
 internal AudioEndpointVolumeCallback(AEDev parent)
 {
   _Parent = parent;
 }
Пример #6
0
 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;
   }
 }
Пример #7
0
 internal AudioEndpointVolumeCallback(AEDev parent)
 {
     _Parent = parent;
 }
Пример #8
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);
            }
        }