Ejemplo n.º 1
0
        private AudioController()
        {
            LoggerHelper.Trace("AudioController instance created.");

            IEnumerable <CoreAudioDevice> audioDevices = audioController.GetDevices();

            foreach (CoreAudioDevice device in audioDevices)
            {
                LoggerHelper.TraceLoop("Audio Device - ID: {0}, Name: {1}, Volume: {2}, Default: {3}, Default Comm: {4}, Capture Device: {5}, Playback Device: {6}",
                                       device.Id, device.FullName, device.Volume, device.IsDefaultDevice, device.IsDefaultCommunicationsDevice, device.IsCaptureDevice, device.IsPlaybackDevice);

                OnDeviceChanged(device, DeviceChangedType.DeviceAdded);
            }

            audioController.AudioDeviceChanged.Subscribe(x => OnDeviceChanged((CoreAudioDevice)x.Device, x.ChangedType));

            if (commsPlayback == null)
            {
                LoggerHelper.Info("No communication audio device found.");
            }

            if (mediaPlayback == null)
            {
                LoggerHelper.Info("No playback audio device found.");
            }
        }
Ejemplo n.º 2
0
        protected override void OnStart(string[] args)
        {
            SetServiceState(ServiceState.SERVICE_START_PENDING, 100000);
            _logger.Info("Service Started.");


            _controller.LoadDevices(false, false, false);
            _logger.Info("Successfully found {0} audio devices.", _controller.GetDevices().Count());

            _selectedDevice = _controller.DefaultPlaybackDevice;
            if (_selectedDevice == null)
            {
                throw new InvalidOperationException("Error loading default playback device");
            }
            _selectedDevice.ReloadAudioEndpointVolume();

            _selectedDevice.VolumeChanged.Subscribe(this);
            _selectedDevice.MuteChanged.Subscribe(this);

            _logger.Info("Selected Default Audio Device ({0}) for volume tracking.", _selectedDevice.FullName);
            _requestTimer.OnTimer = _requestTimer_Elapsed;

            CommandStack.Add(new PowerChangeCommandItem(true));
            CommandStack.Add(new LoadSceneCommandItem(3));
            PerformCommandStack().Wait();
            var volumeTask = _selectedDevice.GetVolumeAsync();

            volumeTask.Wait();
            CommandStack.Add(new VolumeChangeCommandItem(volumeTask.Result));
            PerformCommandStack().Wait();
            SetServiceState(ServiceState.SERVICE_RUNNING);
        }
        public static void FindSoundDeviceByName(string sType, string sName)
        {
            CoreAudioController Controller = new CoreAudioController();
            CoreAudioDevice     device     = null;

            if (sType == "input")
            {
                var devices = Controller.GetCaptureDevices(DeviceState.Active);
                foreach (var d in devices)
                {
                    if (d.FullName == sName)
                    {
                        device = d;
                    }
                }
            }
            else
            {
                var devices = Controller.GetDevices(DeviceState.Active);
                foreach (var d in devices)
                {
                    if (d.FullName == sName)
                    {
                        device = d;
                    }
                }
            }
            if (device != null)
            {
                ChangeStandardSoundDevice(device);
            }
        }
        public static void UpdateLevel(bool reportFix)
        {
            if (audio == null)
            {
                return;
            }

            //Communication capture
            {
                var dev = audio.GetDefaultDevice(AudioSwitcher.AudioApi.DeviceType.Capture, AudioSwitcher.AudioApi.Role.Communications);
                UpdateLevel(dev, reportFix);
            }
            foreach (var dev in audio.GetDevices())
            {
                if (dev.State != AudioSwitcher.AudioApi.DeviceState.Active)
                {
                    continue;
                }

                if ((dev.DeviceType & AudioSwitcher.AudioApi.DeviceType.Capture) != 0)
                {
                    UpdateLevel(dev, reportFix);
                }
            }
        }
Ejemplo n.º 5
0
        public void LoadKnownDevices()
        {
            try
            {
                cbAvailableAudioDevices.Items.Clear();
                cbAvailableAudioDevices.AutoCompleteCustomSource.Clear();
                cbStartupPlaybackDevices.Items.Clear();
                cbStartupPlaybackDevices.AutoCompleteCustomSource.Clear();


                CoreAudioController           _ac           = new CoreAudioController();
                IEnumerable <CoreAudioDevice> _knownDevices = _ac.GetDevices(DeviceType.Playback, DeviceState.Active);

                foreach (var device in _knownDevices)
                {
                    cbAvailableAudioDevices.Items.Add(device.FullName);
                    cbAvailableAudioDevices.AutoCompleteCustomSource.Add(device.FullName);
                    cbStartupPlaybackDevices.Items.Add(device.FullName);
                    cbStartupPlaybackDevices.AutoCompleteCustomSource.Add(device.FullName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occured during GetPlaybackDevices()");
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 6
0
        private IEnumerable <CoreAudioDevice> GetPlaybackDevices()
        {
            IEnumerable <CoreAudioDevice> devices = null;
            var reInitCoreAudioController         = false;

            try
            {
                Log.Debug("Audio Switcher - getting all playback devices");

                devices = _ac.GetDevices(DeviceType.Playback, DeviceState.Active);

                // Check for UNKNOWN devices and re-init CoreAudioControler if needed (AudioSwitcher API bug)
                foreach (var device in devices.Where(device => device.FullName.ToLower().Contains("unknown")))
                {
                    Log.Debug("Found unknown device during GetPlaybackDevices(), gonna re-init CoreAudioControler and update list");
                    Log.Debug("Device ID: " + device.Id);
                    reInitCoreAudioController = true;
                }

                if (reInitCoreAudioController)
                {
                    _ac     = null;
                    devices = null;
                    _ac     = new CoreAudioController();
                    devices = _ac.GetDevices(DeviceType.Playback, DeviceState.Active);
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error occured during GetPlaybackDevices()");
                Log.Error(ex.Message);
            }

            return(devices);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        public void FillComboBox(DeviceType item)
        {
            var audioController = new CoreAudioController();
            var devices         = Task.FromResult(audioController.GetDevices(item, DeviceState.Active));

            foreach (var device in devices.Result)
            {
                GlobalDeviceControl dd = new GlobalDeviceControl();
                dd.Text  = device.FullName;
                dd.Value = device;

                comboBox1.Items.Add(dd);
            }
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        // ------------------------------------------------- //

        public static void Init()
        {
            // Create our controller interface.
            _audioController = new CoreAudioController();

            // Create an observer to monitor for device changes.
            _deviceChangeObserver = new AudioDeviceChangeObserver();
            _audioController.AudioDeviceChanged.Subscribe(_deviceChangeObserver);

            // Get all currently active audio devices
            var activeDevices = _audioController.GetDevices(DeviceType.All, DeviceState.Active);

            /* Create observers for each default audio device type */
            // Default playback device
            var defaultPlaybackDevice = activeDevices.FirstOrDefault(d => d.DeviceType == DeviceType.Playback && d.IsDefaultDevice);

            if (defaultPlaybackDevice != null)
            {
                _playbackDeviceObserver = _CreateObserver(defaultPlaybackDevice);
            }
            else
            {
                LogHelper.Error($"{nameof(DJsAudioStats)} couldn't find an active default playback device on the system.");
            }

            // Default capture device
            var defaultCaptureDevice = activeDevices.FirstOrDefault(d => d.DeviceType == DeviceType.Capture && d.IsDefaultDevice);

            if (defaultCaptureDevice != null)
            {
                _captureDeviceObserver = _CreateObserver(defaultCaptureDevice);
            }
            else
            {
                LogHelper.Error($"{nameof(DJsAudioStats)} couldn't find an active default capture device on the system.");
            }

            // Default communications device
            var defaultCommsDevice = activeDevices.FirstOrDefault(d => d.DeviceType == DeviceType.Capture && d.IsDefaultCommunicationsDevice);

            if (defaultCommsDevice != null)
            {
                _commsDeviceObserver = _CreateObserver(defaultCommsDevice);
            }
            else
            {
                LogHelper.Error($"{nameof(DJsAudioStats)} couldn't find an active default communications device on the system.");
            }
        }
Ejemplo n.º 11
0
        public static CoreAudioDevice getDeviceByGuid(Guid guid)
        {
            CoreAudioDevice result          = null;
            var             audioController = new CoreAudioController();

            var devices = Task.FromResult(audioController.GetDevices(DeviceType.All, DeviceState.Active));

            foreach (CoreAudioDevice device in devices.Result)
            {
                if (device.Id == guid)
                {
                    result = device;
                }
            }
            return(result);
        }
Ejemplo n.º 12
0
        public static void Main(string[] args)
        {
            var rootCommand = new RootCommand
            {
                new Option("--list", "List all audio devices"),
                new Option <string>("--set", "Set device"),
                new Option <string>("--set-comm", "Set communication device")
            };

            rootCommand.Description = "AudioDeviceSelector";

            // Note that the parameters of the handler method are matched according to the names of the options
            rootCommand.Handler = CommandHandler.Create <string, string>(
                (set, setComm) =>
            {
                var controller = new CoreAudioController();
                var devices    = controller.GetDevices();

                if (args.Count() < 1)
                {
                    Console.WriteLine($"{Assembly.GetExecutingAssembly().GetName().Name}.exe --help");
                }

                if (args.Any(x => x.ToString().ToLower() == "--list"))
                {
                    devices
                    .OrderBy(d => d.DeviceType)
                    .ToList()
                    .ForEach(d => Console.WriteLine($"DeviceType: {d.DeviceType}, Id: {d.Id}, InterfaceName: {d.InterfaceName}, Name: {d.Name}"));
                }

                _ = set != null && controller.SetDeviceByName(set);
                _ = setComm != null && controller.SetCommDeviceByName(setComm);
            });

            rootCommand.Invoke(args);
        }
Ejemplo n.º 13
0
        List <CoreAudioDevice> GetAllValidSoundDevices()
        {
            var devices = AudioController.GetDevices();

            return(devices.Where(x => x.IsPlaybackDevice && x.State == DeviceState.Active).ToList());
        }
Ejemplo n.º 14
0
 public static CoreAudioDevice GetDeviceByName(this CoreAudioController controller, string idOrName)
 {
     return(controller.GetDevices().FirstOrDefault(d => d.Id.ToString() == idOrName || d.InterfaceName == idOrName || d.Name == idOrName));
 }