示例#1
0
        private void UpdateDropdowns()
        {
            var Outputdevices = Controller.GetPlaybackDevices(DeviceState.Active);
            var Inputdevices  = Controller.GetCaptureDevices(DeviceState.Active);

            InputDropdown.Items.Clear();
            OutputDropdown.Items.Clear();

            foreach (var endpoint in Inputdevices)
            {
                InputDropdown.Items.Add(endpoint.FullName);
                //Console.WriteLine(endpoint.Fullname);
            }

            //Console.WriteLine("These are output devices");
            foreach (var endpoint in Outputdevices)
            {
                OutputDropdown.Items.Add(endpoint.FullName);
                //Console.WriteLine(endpoint.FullName);
            }

            var defaultInputDevice  = Controller.GetDefaultDevice(DeviceType.Capture, Role.Multimedia);
            var defaultOutputDevice = Controller.GetDefaultDevice(DeviceType.Playback, Role.Multimedia);

            OutputDropdown.SelectedItem = defaultOutputDevice;
            OutputDropdown.Text         = defaultOutputDevice.FullName;

            InputDropdown.SelectedItem = defaultInputDevice;
            InputDropdown.Text         = defaultInputDevice.FullName;
        }
示例#2
0
 public void Execute(string arguments)
 {
     arguments = arguments.Replace("%", "");
     string[] arr = arguments.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
     if (arr.Length < 2)
     {
         var value = CommandParser.Int32Parser(arr[0]);
         if (!value.HasValue)
         {
             throw new ArgumentException();
         }
         controller.DefaultPlaybackDevice.Volume = value.Value;
         Program.Connection.SendLine($"Volume set to {value}%");
     }
     else
     {
         var number = CommandParser.Int32Parser(arr[0]);
         if (number.HasValue)
         {
             var volume = CommandParser.Int32Parser(arr[1]);
             if (!volume.HasValue)
             {
                 throw new ArgumentException();
             }
             List <CoreAudioDevice> dev = controller.GetPlaybackDevices().ToList();
             dev[number.Value].Volume = volume.Value;
             Program.Connection.SendLine($"Volume of device {number} set to {volume}%");
         }
         else
         {
             string name = "";
             for (int i = 0; i < arr.Length - 1; i++)
             {
                 name += arr[i] + " ";
             }
             name = name.Remove(name.Length - 1).ToLower();
             var volume = CommandParser.Int32Parser(arr[arr.Length - 1]);
             if (!volume.HasValue)
             {
                 throw new ArgumentException();
             }
             List <CoreAudioDevice> dev = controller.GetPlaybackDevices().ToList();
             foreach (CoreAudioDevice d in dev)
             {
                 if (d.Name.ToLower().Contains(name) || d.Name.ToLower().Contains(name) || d.InterfaceName.ToLower().Contains(name))
                 {
                     d.Volume = volume.Value;
                     Program.Connection.SendLine($"Volume of device {d.FullName} set to {volume}%");
                 }
             }
         }
     }
 }
示例#3
0
        public void Hotkey1()
        {
            var outputDevices = audioController.GetPlaybackDevices(DeviceState.Active);

            foreach (var device in outputDevices)
            {
                if (dropDownMenu.Text == device.FullName)
                {
                    audioController.DefaultPlaybackDevice = device;
                    break;
                }
            }
        }
        public static List <string> GetAudioDevices(string sType)
        {
            List <string> arrReturn        = new List <string> {
            };
            CoreAudioController Controller = new CoreAudioController();

            if (sType == "output")
            {
                var devices = Controller.GetPlaybackDevices(DeviceState.Active);

                foreach (var d in devices.OrderBy(x => x.Name))
                {
                    arrReturn.Add(d.FullName);
                }
            }
            else if (sType == "input")
            {
                var devices = Controller.GetCaptureDevices(DeviceState.Active);

                foreach (var d in devices.OrderBy(x => x.Name))
                {
                    arrReturn.Add(d.FullName);
                }
            }
            return(arrReturn);
        }
示例#5
0
        public MainWindow()
        {
            InitializeComponent();

            HotkeysManager.SetupSystemHook();

            GlobalHotkey hotkey1 = new GlobalHotkey(ModifierKeys.Alt, Key.N, Hotkey1);
            GlobalHotkey hotkey2 = new GlobalHotkey(ModifierKeys.Alt, Key.M, Hotkey2);

            HotkeysManager.AddHotkey(hotkey1);
            HotkeysManager.AddHotkey(hotkey2);

            #region send application to tray

            System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
            ni.Icon    = new System.Drawing.Icon("image.ico");
            ni.Visible = true;
            ni.Click  +=
                delegate(object sender, EventArgs e)
            {
                this.Show();
                this.WindowState = System.Windows.WindowState.Normal;
            };
            ni.DoubleClick +=
                delegate(object sender, EventArgs e)
            {
                this.WindowState = System.Windows.WindowState.Normal;
                close            = true;
                this.Close();
            };

            #endregion

            audioController = new CoreAudioController();
            defaultOutput   = audioController.DefaultPlaybackDevice;

            var OutputDevices = audioController.GetPlaybackDevices(DeviceState.Active);

            foreach (var device in OutputDevices)
            {
                dropDownMenu.Items.Add(device.FullName);
                dropDownMenu2.Items.Add(device.FullName);
            }

            data = SaveLoadManager.Load();


            if (data != null)
            {
                dropDownMenu.Text  = data.device1Name;
                dropDownMenu2.Text = data.device2Name;
            }

            AddHotKeys();
        }
示例#6
0
        static public List <string> GetAudioDevices()
        {
            CoreAudioController cac = new CoreAudioController();
            List <string>       tmp = new List <string>();

            foreach (CoreAudioDevice de in cac.GetPlaybackDevices())
            {
                tmp.Add(de.FullName);
            }

            return(tmp);
        }
示例#7
0
文件: Audio.cs 项目: wiuwiu/Win10As
        public void ChangeOutputDevice(string DeviceFullname)
        {
            CoreAudioController cac = new CoreAudioController();

            foreach (CoreAudioDevice de in cac.GetPlaybackDevices())
            {
                if (de.FullName.ToLower(CultureInfo.CurrentCulture) == DeviceFullname.ToLower(CultureInfo.CurrentCulture))
                {
                    defaultPlaybackDevice = de;
                }
            }
        }
示例#8
0
        public IHttpContext GetSound(IHttpContext context)
        {
            if (Authentication.Enabled)
            {
                HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;
                if (!Authentication.VerifyAuthentication(identity))
                {
                    context.Response.SendResponse(JsonConvert.SerializeObject(Authentication.FailedAuthentication)); return(context);
                }
            }

            SoundDevices sd = new SoundDevices();

            CoreAudioController controller = new CoreAudioController();
            var devices = controller.GetPlaybackDevices();

            foreach (CoreAudioDevice device in devices)
            {
                try
                {
                    sd.SoundDevice.Add(new SoundDeviceInfo
                    {
                        ID           = device.RealId,
                        FriendlyName = device.FullName,
                        State        = device.State.ToString()
                    });

                    sd.Response = new ResponseMessage
                    {
                        Message    = "Successfully got active audio device(s).",
                        Successful = true
                    };
                }
                catch (Exception)
                {
                    sd.Response = new ResponseMessage
                    {
                        Message    = "Failed to get audio device(s).",
                        Successful = false
                    };
                    controller.Dispose();
                }
            }

            controller.Dispose();
            context.Response.ContentEncoding = Encoding.Default;
            context.Response.SendResponse(JsonConvert.SerializeObject(sd));
            return(context);
        }
示例#9
0
        public IHttpContext SetSound(IHttpContext context)
        {
            if (Authentication.Enabled)
            {
                HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;
                if (!Authentication.VerifyAuthentication(identity))
                {
                    context.Response.SendResponse(JsonConvert.SerializeObject(Authentication.FailedAuthentication)); return(context);
                }
            }

            ResponseMessage Response = new ResponseMessage();

            Response.Message    = "Please input correct id or name. Get id by using /getid";
            Response.Successful = false;

            var enc = Encoding.GetEncoding(context.Request.ContentEncoding.CodePage);
            var id  = Encoding.UTF8.GetString(enc.GetBytes(context.Request.QueryString["id"])) ?? JsonConvert.SerializeObject(Response);

            CoreAudioController controller = new CoreAudioController();
            var devices = controller.GetPlaybackDevices();

            foreach (CoreAudioDevice device in devices)
            {
                if (device.RealId == id)
                {
                    controller.SetDefaultDevice(device);
                    Response.Message    = $"Switched to audio device: {device.FullName}";
                    Response.Successful = true;
                }
                if (device.FullName == id && device.State == AudioSwitcher.AudioApi.DeviceState.Active)
                {
                    try
                    {
                        controller.SetDefaultDevice(device);
                        Response.Message    = $"Switched to audio device: {device.FullName}";
                        Response.Successful = true;
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            controller.Dispose();
            context.Response.ContentEncoding = Encoding.Default;
            context.Response.SendResponse(JsonConvert.SerializeObject(Response));
            return(context);
        }
示例#10
0
        private void ChargeDevicesList()
        {
            var List = Controller.GetPlaybackDevices();

            string[] fav = GetDeviceList();
            lstDevices.Items.Clear();
            foreach (CoreAudioDevice CAD in List)
            {
                if (CAD.State == DeviceState.Active)
                {
                    lstDevices.Items.Add(CAD, fav.Contains(CAD.Id.ToString()));
                }
            }

            lstDevices.DisplayMember = "FullName";
            lstDevices.ValueMember   = "Id";
        }
示例#11
0
        public ContextMenuStrip Create()
        {
            ContextMenuStrip   menu = new ContextMenuStrip();
            ToolStripMenuItem  item;
            ToolStripSeparator sep;

            CoreAudioController           audio   = new CoreAudioController();
            IEnumerable <CoreAudioDevice> devices = audio.GetPlaybackDevices();

            Guid deviceId = (Guid)Properties.Settings.Default["DeviceId"];

            item      = new ToolStripMenuItem();
            item.Text = "Select device to switch to:";
            menu.Items.Add(item);

            sep = new ToolStripSeparator();
            menu.Items.Add(sep);

            foreach (CoreAudioDevice device in devices)
            {
                item      = new ToolStripMenuItem();
                item.Text = device.FullName;
                item.Tag  = device;

                item.Click += new EventHandler(Device_Click);

                if (device.Id == deviceId)
                {
                    this.currentItem         = item;
                    this.currentItem.Checked = true;
                }

                menu.Items.Add(item);
            }

            sep = new ToolStripSeparator();
            menu.Items.Add(sep);

            item        = new ToolStripMenuItem();
            item.Text   = "Exit";
            item.Click += new System.EventHandler(Exit_Click);
            menu.Items.Add(item);

            return(menu);
        }
示例#12
0
        /*########################  2do ########################
         * Start Voice meeter BANANA
         * Last Volume / Default Vol if no last Volume (RegistryCurrentUser)
         * Hotkeys 2per Device -> +Vol,-Vol,
         * onchanged (numUDInputVaio){InputVaio.Volume = numUDInputVaio.Value}
         *
         *
         *###################### 2do End ########################*/

        public void iwas()
        {   //Get Audio Devices
            CoreAudioController controller = new CoreAudioController();

            foreach (CoreAudioDevice device in controller.GetPlaybackDevices())
            {
                if (device.InterfaceName == "VB-Audio VoiceMeeter VAIO")
                {
                    InputVaio = device;
                }
                else if (device.InterfaceName == "VB-Audio VoiceMeeter AUX VAIO")
                {
                    InputVaioAux = device;
                }
            }

            //initial Value
            numUpDownVol1.Value = 30;
            numUpDownVol2.Value = 30;
        }
示例#13
0
        public static void SetDevice(string deviceFullName = "")
        {
            var cac = new CoreAudioController();

            if ($"{deviceFullName}" == "")
            {
                _defaultPlaybackDevice = cac.DefaultPlaybackDevice;
                return;
            }

            foreach (var de in cac.GetPlaybackDevices())
            {
                if (de.FullName.ToLower() != deviceFullName.ToLower())
                {
                    continue;
                }

                _defaultPlaybackDevice = de;
                break;
            }
        }
示例#14
0
        int API_List(string Type)
        {
            IEnumerable <CoreAudioDevice> Target;

            if (Type.Equals("API-ListInputs"))
            {
                Target = controller.GetPlaybackDevices();
            }
            if (Type.Equals("API-ListOutputs"))
            {
                Target = controller.GetCaptureDevices();
            }
            else
            {
                Target = controller.GetDevices();
            }
            foreach (CoreAudioDevice device in Target)
            {
                output += "Name : " + device.Name + " Type : " + device.DeviceType.ToString() + Environment.NewLine;
            }
            return(0);
        }
示例#15
0
        static int API_List(string Type)
        {
            AllocConsole();
            IEnumerable <CoreAudioDevice> Target;

            if (Type.Equals("API-ListInputs"))
            {
                Target = controller.GetPlaybackDevices();
            }
            if (Type.Equals("API-ListOutputs"))
            {
                Target = controller.GetCaptureDevices();
            }
            else
            {
                Target = controller.GetDevices();
            }
            foreach (CoreAudioDevice device in Target)
            {
                System.Console.WriteLine("Name : " + device.Name + " Type : " + device.DeviceType.ToString());
            }
            return(0);
        }
示例#16
0
        // ReSharper disable once UnusedMethodReturnValue.Local
        private bool updateState(PcAudio audioUpdate)
        {
            //Console.WriteLine("update");

            lock (m_lock)
            {
                var audioState = new PcAudio
                {
                    protocolVersion    = PROTOCOL_VERSION,
                    applicationVersion = APPLICATION_VERSION
                };

                cleanUpSessionKeepers();

                var defaultDevice = m_coreAudioController.GetDefaultDevice(DeviceType.Playback, Role.Multimedia);
                if (defaultDevice == null)
                {
                    return(true);
                }

                var defaultDeviceId = defaultDevice.Id.ToString();

                // Add all available audio devices to our list of device IDs
                var devices = m_coreAudioController.GetPlaybackDevices();
                foreach (var device in devices)
                {
                    if (device.State == DeviceState.Active)
                    {
                        audioState.deviceIds.Add(device.Id.ToString(), device.FullName);
                    }
                }

                // Master device updates
                if (audioUpdate?.defaultDevice != null)
                {
                    if (!audioUpdate.defaultDevice.deviceId.Equals(defaultDeviceId))
                    {
                        var deviceId = Guid.Parse(audioUpdate.defaultDevice.deviceId);

                        var newDefaultAudioDevice = m_coreAudioController.GetDevice(deviceId);
                        if (newDefaultAudioDevice != null)
                        {
                            //Console.WriteLine("Updated default audio device: " + audioUpdate.defaultDevice.deviceId);

                            newDefaultAudioDevice.SetAsDefaultAsync().Wait();
                            newDefaultAudioDevice.SetAsDefaultCommunicationsAsync().Wait();

                            return(false);
                        }
                        //else
                        //{
                        //Console.WriteLine("Failed to update default audio device. Could not find device for ID: " + audioUpdate.defaultDevice.deviceId);
                        //}
                    }
                    else
                    {
                        if (audioUpdate.defaultDevice.masterMuted != null || audioUpdate.defaultDevice.masterVolume != null)
                        {
                            if (audioUpdate.defaultDevice.masterMuted != null)
                            {
                                var muted = audioUpdate.defaultDevice.masterMuted ?? m_coreAudioController.DefaultPlaybackDevice.IsMuted;
                                //Console.WriteLine("Updating master mute: " + muted);

                                m_coreAudioController.DefaultPlaybackDevice.SetMuteAsync(muted).Wait();
                            }

                            // ReSharper disable once InvertIf
                            if (audioUpdate.defaultDevice.masterVolume != null)
                            {
                                const int increment = 2;

                                var deviceAudio = m_coreAudioController.DefaultPlaybackDevice.Volume;
                                var clientAudio = audioUpdate.defaultDevice.masterVolume;

                                var volume = deviceAudio;
                                if (clientAudio < deviceAudio)
                                {
                                    volume -= increment;
                                }
                                else if (clientAudio > deviceAudio)
                                {
                                    volume += increment;
                                }

                                //var volume = audioUpdate.defaultDevice.masterVolume ?? (float)m_coreAudioController.DefaultPlaybackDevice.Volume;
                                //Console.WriteLine("Updating master volume: " + volume);

                                m_coreAudioController.DefaultPlaybackDevice.SetVolumeAsync(volume).Wait();
                            }

                            return(false);
                        }
                    }
                }

                // Create our default audio device and populate it's volume and mute status
                var audioDevice = new AudioDevice(defaultDevice.FullName, defaultDeviceId);
                audioState.defaultDevice = audioDevice;

                var defaultPlaybackDevice = m_coreAudioController.DefaultPlaybackDevice;
                audioDevice.masterVolume = (float)defaultPlaybackDevice.Volume;
                audioDevice.masterMuted  = defaultPlaybackDevice.IsMuted;

                // Go through all audio sessions
                foreach (var session in defaultDevice.GetCapability <IAudioSessionController>())
                {
                    if (session.IsSystemSession)
                    {
                        continue;
                    }

                    // If we haven't seen this before, create our book keeper
                    var sessionId = session.Id;
                    if (!m_sessions.ContainsKey(sessionId))
                    {
                        //Console.WriteLine("Found new audio session");

                        var sessionKeeper = new AudioSessionKeeper(session, m_sessionVolumeListener, m_sessionMuteListener);
                        m_sessions.Add(session.Id, sessionKeeper);
                    }

                    try
                    {
                        // Audio session update
                        if (audioUpdate?.defaultDevice?.deviceId != null)
                        {
                            if (audioUpdate.defaultDevice.deviceId.Equals(defaultDeviceId))
                            {
                                if (audioUpdate.defaultDevice.sessions != null && audioUpdate.defaultDevice.sessions.Count > 0)
                                {
                                    foreach (var sessionUpdate in audioUpdate.defaultDevice.sessions.
                                             Where(sessionUpdate => sessionUpdate.id.Equals(session.Id)))
                                    {
                                        //Console.WriteLine("Adjusting volume: " + sessionUpdate.name + " - " + sessionUpdate.volume);
                                        //Console.WriteLine("Adjusting mute: " + sessionUpdate.muted + " - " + sessionUpdate.muted);

                                        session.SetVolumeAsync(sessionUpdate.volume).Wait();
                                        session.SetMuteAsync(sessionUpdate.muted).Wait();

                                        break;
                                    }
                                }
                            }
                        }

                        var sessionName = session.DisplayName;
                        if (sessionName == null || sessionName.Trim() == "")
                        {
                            using var process = Process.GetProcessById(session.ProcessId);
                        }

                        var audioSession = new AudioSession(sessionName, session.Id, (float)session.Volume, session.IsMuted);
                        audioDevice.sessions.Add(audioSession);
                    }
                    catch (Exception)
                    {
                        //Console.WriteLine(e.Message);
                        //Console.WriteLine(e.StackTrace);
                        //Console.WriteLine("Process in audio session no longer alive");

                        var sessionKeeper = m_sessions[session.Id];
                        m_sessions.Remove(session.Id);
                        sessionKeeper.Dispose();
                    }
                }

                m_audioState = audioState;
            }

            return(true);
        }