Example #1
0
        private SoundMixer()
        {
            Channels = new Channel[0x10];
            for (byte i = 0; i < 0x10; i++)
            {
                Channels[i] = new Channel(i);
            }

            Mutes = new bool[0x10];

            buffer = new BufferedWaveProvider(new WaveFormat(65456, 16, 2))
            {
                DiscardOnBufferOverflow = true,
                BufferLength            = 0x5540
            };
            @out = new WasapiOut();
            @out.Init(buffer);
            SessionCollection sessions = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia).AudioSessionManager.Sessions;
            int id = System.Diagnostics.Process.GetCurrentProcess().Id;

            for (int i = 0; i < sessions.Count; i++)
            {
                AudioSessionControl session = sessions[i];
                if (session.GetProcessID == id)
                {
                    appVolume = session;
                    appVolume.RegisterEventClient(this);
                    break;
                }
            }
            @out.Play();
        }
 public SoundDevice(DeviceController deviceController, bool isMasterVolumeForDevice, MMDevice device, AudioSessionControl session)
 {
     this.IsMasterVolumeControlForDevice = isMasterVolumeForDevice;
     this._device      = device;
     this._session     = session;
     _deviceController = deviceController;
     _session.RegisterEventClient(this);
 }
Example #3
0
        public AudioSession(AudioSessionControl session)
        {
            Session = session;
            Session.RegisterEventClient(this);

            SessionIdentifier = Session.GetSessionIdentifier;

            IsSystemSound = Session.IsSystemSoundsSession || Session.GetProcessID == 0;
            string appId = SessionIdentifier.ExtractAppPath();

            Id = IsSystemSound ? int.MinValue : appId.GetHashCode();

            UpdateDisplayName();
        }
 protected void Init(IWaveProvider waveProvider)
 {
     @out = new WasapiOut();
     @out.Init(waveProvider);
     using (var en = new MMDeviceEnumerator())
     {
         SessionCollection sessions = en.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia).AudioSessionManager.Sessions;
         int id = System.Diagnostics.Process.GetCurrentProcess().Id;
         for (int i = 0; i < sessions.Count; i++)
         {
             AudioSessionControl session = sessions[i];
             if (session.GetProcessID == id)
             {
                 appVolume = session;
                 appVolume.RegisterEventClient(this);
                 break;
             }
         }
     }
     @out.Play();
 }
Example #5
0
            public AudioSession(AudioSessionControl Session)
            {
                EventClient eventClient = new EventClient();

                eventClient.VolumeChanged     += OnVolumeChanged;
                eventClient.StateChanged      += OnStateChanged;
                eventClient.SessionDisconnect += OnSessionDisconnect;
                SessionInstanceIdentifier      = Session.GetSessionInstanceIdentifier;
                this.eventClient = eventClient;
                sSessionName     = Session.DisplayName;
                this.Session     = Session;
                Session.RegisterEventClient(eventClient);

                // Refresh if DLL Path
                sSessionName = LoadStringFromDLL.LoadStringFromDLLByPath(Session.DisplayName);
                if (string.IsNullOrEmpty(sSessionName))
                {
                    sSessionName = GetNameByID(Session.GetProcessID);
                }
                isMuted = Session.SimpleAudioVolume.Mute;
                // System.Diagnostics.Debug.WriteLine("New Audio Session: " + sSessionName + " PID:" + Session.GetProcessID.ToString() + " IsMuted:" + this.isMuted);
            }
Example #6
0
        /// <summary>
        /// Constructor for session panel creation.
        /// </summary>
        /// <param name="device">Selected device.</param>
        /// <param name="session">Current session of device.</param>
        public VolumePanel(MMDevice device, AudioSessionControl session)
        {
            this.devicePanel = false;
            this.device      = device;
            this.session     = session;
            InitializeComponent();

            cmbDevice.Visible = false;
            Process process = Process.GetProcessById((int)session.GetProcessID);

            if (session.IsSystemSoundsSession)
            {
                lblName.Text               = "System Sounds";
                pbProcessIcon.Visible      = false;
                btnSoundProperties.Visible = true;
                var iconAddress = session.IconPath.Split(',');
                var icon        = IconExtractor.Extract(iconAddress[0], int.Parse(iconAddress[1]), true);
                if (icon != null)
                {
                    btnSoundProperties.Image = icon.ToBitmap();
                }
                tooltip.SetToolTip(btnSoundProperties, lblName.Text);
            }
            else
            {
                pbProcessIcon.Image        = Icon.ExtractAssociatedIcon(process.MainModule.FileName).ToBitmap();
                lblName.Text               = process.MainWindowTitle != "" ? process.MainWindowTitle : process.ProcessName;
                pbProcessIcon.Visible      = true;
                btnSoundProperties.Visible = false;
                tooltip.SetToolTip(pbProcessIcon, lblName.Text);
            }
            tooltip.SetToolTip(lblName, lblName.Text);

            session.RegisterEventClient(this);
            UpdateVolume();
            UpdateMuted();
        }
Example #7
0
        public void Initialize(int rate = -1) // !!! Must be called from the same thread that was initialized
        {
            if (uiThread.InvokeRequired)
            {
                uiThread.BeginInvoke(new Action(() => Initialize(rate))); return;
            }

            lock (locker)
            {
                if (rate != -1)
                {
                    Rate = rate;
                }

                // Dispose | UnRegister
                if (device != null)
                {
                    device.AudioEndpointVolume.OnVolumeNotification -= OnMasterVolumeChanged;
                }
                if (session != null)
                {
                    session.UnRegisterEventClient(this);
                }
                if (player != null)
                {
                    player.Dispose();
                }
                if (buffer != null)
                {
                    buffer.ClearBuffer();
                }
                if (session != null)
                {
                    session.UnRegisterEventClient(this);
                }

                // Initialize
                format = WaveFormatExtensible.CreateIeeeFloatWaveFormat(Rate, CHANNELS);
                buffer = new BufferedWaveProvider(format);
                buffer.BufferLength = 1000 * 1024;
                player = new WaveOut();
                player.DeviceNumber   = 0;
                player.DesiredLatency = NAUDIO_DELAY_MS;
                player.Init(buffer);
                player.Volume = 1; // Currently we change only Master volume to achieve constants change levels
                player.Play();

                device = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
                device.AudioEndpointVolume.OnVolumeNotification += OnMasterVolumeChanged;

                for (int i = 0; i < device.AudioSessionManager.Sessions.Count; i++)
                {
                    if (processId == device.AudioSessionManager.Sessions[i].GetProcessID) // && !deviceInit.AudioSessionManager.Sessions[i].IsSystemSoundsSession)
                    {
                        session       = device.AudioSessionManager.Sessions[i];
                        player.Volume = session.SimpleAudioVolume.Volume;
                        session.RegisterEventClient(this);
                    }
                }

                VolumeChanged?.BeginInvoke(this, new VolumeChangedArgs((int)(player.Volume * device.AudioEndpointVolume.MasterVolumeLevelScalar * 100), (session != null ? session.SimpleAudioVolume.Mute : false) || device.AudioEndpointVolume.Mute), null, null);
            }
        }