private void DefaultDevice(CommandControlMappingElement command, MidiControlChangeMessage msg)
        {
            var config     = (MidiConfiguration)command.hardwareConfiguration;
            var calcVolume = Current.CalculateVolume(msg.ControlValue, config.MinValue, config.MaxValue, config.ScalingValue);

            if (calcVolume > 50)
            {
                foreach (var device in _audioDeviceManager.Devices)
                {
                    if (device.DisplayName == command.audioDevice)
                    {
                        try
                        {
                            _audioDeviceManager.Default = device;
                        }
                        catch (NotSupportedException)
                        {
                        }


                        return;
                    }
                }
            }
        }
        public void SaveCommandControlMapping()
        {
            if (_hardwareConfiguration == null ||
                string.IsNullOrEmpty(SelectedDevice) ||
                string.IsNullOrEmpty(SelectedCommand) ||
                (ModeSelectionEnabled && string.IsNullOrEmpty(SelectedMode)) ||
                (IndexesApplicationsSelectionEnabled && string.IsNullOrEmpty(SelectedIndexesApplications)) ||
                string.IsNullOrEmpty(SelectedDeviceType))
            {
                // Do nothing if the settings were not done yet.
                MessageBox.Show(Properties.Resources.IncompleteDeviceConfigurationMessage, "EarTrumpet", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            CommandControlMappingElement.Command command = CommandControlMappingElement.Command.None;
            CommandControlMappingElement.Mode    mode    = CommandControlMappingElement.Mode.None;

            if (SelectedCommand == Properties.Resources.AudioDeviceVolumeText)
            {
                command = CommandControlMappingElement.Command.SystemVolume;
            }
            else if (SelectedCommand == Properties.Resources.AudioDeviceMuteText)
            {
                command = CommandControlMappingElement.Command.SystemMute;
            }
            else if (SelectedCommand == Properties.Resources.ApplicationVolumeText)
            {
                command = CommandControlMappingElement.Command.ApplicationVolume;
            }
            else if (SelectedCommand == Properties.Resources.ApplicationMuteText)
            {
                command = CommandControlMappingElement.Command.ApplicationMute;
            }
            else if (SelectedCommand == Properties.Resources.SetAsDefaultDevice)
            {
                command = CommandControlMappingElement.Command.SetDefaultDevice;
            }
            else if (SelectedCommand == Properties.Resources.CycleDefaultDevices)
            {
                command = CommandControlMappingElement.Command.CycleDefaultDevice;
            }

            if (SelectedMode == Properties.Resources.IndexedText)
            {
                mode = CommandControlMappingElement.Mode.Indexed;
            }
            else if (SelectedMode == Properties.Resources.ApplicationSelectionText)
            {
                mode = CommandControlMappingElement.Mode.ApplicationSelection;
            }

            _commandControlMappingElement = new CommandControlMappingElement(_hardwareConfiguration, SelectedDevice,
                                                                             command, mode, SelectedIndexesApplications);

            // Notify the hardware controls page about the new assignment.
            _hardwareControls.ControlCommandMappingSelectedCallback(_commandControlMappingElement);
        }
        public override void AddCommand(CommandControlMappingElement command)
        {
            var config = (MidiConfiguration)command.hardwareConfiguration;

            MidiIn._StartListening(MidiIn.GetDeviceByName(config.MidiDevice)?.Id);

            _commandControlMappings.Add(command);
            SaveSettings(SAVEKEY);
        }
        public override void AddCommand(CommandControlMappingElement command)
        {
            var config = (DeejConfiguration)command.hardwareConfiguration;

            DeejIn._StartListening(config.Port);

            _commandControlMappings.Add(command);
            SaveSettings(SAVEKEY);
        }
        private void SystemMute(CommandControlMappingElement command, MidiControlChangeMessage msg)
        {
            var devices = GetDevicesByName(command.audioDevice);

            foreach (var device in devices)
            {
                device.IsMuted = SetMute((MidiConfiguration)command.hardwareConfiguration, msg, device.IsMuted);
            }
        }
        private void SystemMute(CommandControlMappingElement command, int value)
        {
            var devices = GetDevicesByName(command.audioDevice);

            foreach (var device in devices)
            {
                device.IsMuted = SetMute((DeejConfiguration)command.hardwareConfiguration, value);
            }
        }
        private void FillForm(CommandControlMappingElement data)
        {
            void FillApplication()
            {
                switch (data.mode)
                {
                case CommandControlMappingElement.Mode.Indexed:
                    SelectedMode = Properties.Resources.IndexedText;
                    break;

                case CommandControlMappingElement.Mode.ApplicationSelection:
                    SelectedMode = Properties.Resources.ApplicationSelectionText;
                    break;
                }

                SelectedIndexesApplications = data.indexApplicationSelection;
            }

            SelectedDevice = data.audioDevice;

            switch (data.command)
            {
            case CommandControlMappingElement.Command.ApplicationMute:
                SelectedCommand = Properties.Resources.ApplicationMuteText;
                FillApplication();
                break;

            case CommandControlMappingElement.Command.ApplicationVolume:
                SelectedCommand = Properties.Resources.ApplicationVolumeText;
                FillApplication();
                break;

            case CommandControlMappingElement.Command.SystemMute:
                SelectedCommand = Properties.Resources.AudioDeviceMuteText;
                break;

            case CommandControlMappingElement.Command.SystemVolume:
                SelectedCommand = Properties.Resources.AudioDeviceVolumeText;
                break;

            case CommandControlMappingElement.Command.SetDefaultDevice:
                SelectedCommand = Properties.Resources.SetAsDefaultDevice;
                break;

            case CommandControlMappingElement.Command.CycleDefaultDevice:
                SelectedCommand = Properties.Resources.CycleDefaultDevices;
                break;
            }

            SelectedDeviceType     = HardwareManager.Current.GetConfigType(data);
            SelectedControl        = data.hardwareConfiguration.ToString();
            _hardwareConfiguration = data.hardwareConfiguration;
        }
        public override void ModifyCommandAt(int index, CommandControlMappingElement newCommand)
        {
            if (_commandControlMappings.Count < index)
            {
                return;
            }

            var config = (MidiConfiguration)newCommand.hardwareConfiguration;

            MidiIn._StartListening(MidiIn.GetDeviceByName(config.MidiDevice)?.Id);

            _commandControlMappings[index] = newCommand;
            SaveSettings(SAVEKEY);
        }
        public override void ModifyCommandAt(int index, CommandControlMappingElement newCommand)
        {
            if (_commandControlMappings.Count < index)
            {
                return;
            }

            var config = (DeejConfiguration)newCommand.hardwareConfiguration;

            DeejIn._StartListening(config.Port);

            _commandControlMappings[index] = newCommand;
            SaveSettings(SAVEKEY);
        }
        public void ControlCommandMappingSelectedCallback(CommandControlMappingElement commandControlMappingElement)
        {
            switch (ItemModificationWay)
            {
            case ItemModificationWays.NEW_EMPTY:
            case ItemModificationWays.NEW_FROM_EXISTING:
                HardwareManager.Current.AddCommand(commandControlMappingElement);
                break;

            case ItemModificationWays.EDIT_EXISTING:
                // Notify the hardware controls page about the new assignment.
                HardwareManager.Current.ModifyCommandAt(SelectedIndex, commandControlMappingElement);
                break;
            }

            UpdateCommandControlsList();

            _hardwareSettingsWindow.OpenOrClose();
        }
        private void CycleDefaultDevice(CommandControlMappingElement command, int value)
        {
            var config     = (DeejConfiguration)command.hardwareConfiguration;
            var calcVolume = CalculateVolume(value, config.MinValue, config.MaxValue, config.ScalingValue);

            if (calcVolume >= 50 && lastValues[command] < 50)
            {
                lastValues[command] = calcVolume;

                var index = -1;
                for (var i = 0; i < _audioDeviceManager.Devices.Count; i++)
                {
                    if (_audioDeviceManager.Devices[i] == _audioDeviceManager.Default)
                    {
                        index = i;
                        break;
                    }
                }

                if (index != -1)
                {
                    index = (index + 1 + _audioDeviceManager.Devices.Count) % _audioDeviceManager.Devices.Count;
                }
                else
                {
                    index = 0;
                }

                try
                {
                    _audioDeviceManager.Default = _audioDeviceManager.Devices[index];
                }
                catch (NotSupportedException)
                {
                }
            }

            lastValues[command] = calcVolume;
        }
        private void ApplicationMute(CommandControlMappingElement command, MidiControlChangeMessage msg)
        {
            List <IAppItemViewModel> apps = null;

            if (command.mode == CommandControlMappingElement.Mode.ApplicationSelection)
            {
                apps = GetAppsByName(command.audioDevice, command.indexApplicationSelection);
            }
            else if (command.mode == CommandControlMappingElement.Mode.Indexed)
            {
                apps = GetAppsByIndex(command.audioDevice, command.indexApplicationSelection);
            }

            if (apps == null)
            {
                return;
            }

            foreach (var app in apps)
            {
                app.IsMuted = SetMute((MidiConfiguration)command.hardwareConfiguration, msg, app.IsMuted);
            }
        }
        private void ApplicationVolume(CommandControlMappingElement command, int value)
        {
            List <IAppItemViewModel> apps = null;

            if (command.mode == CommandControlMappingElement.Mode.ApplicationSelection)
            {
                apps = GetAppsByName(command.audioDevice, command.indexApplicationSelection);
            }
            else if (command.mode == CommandControlMappingElement.Mode.Indexed)
            {
                apps = GetAppsByIndex(command.audioDevice, command.indexApplicationSelection);
            }

            if (apps == null)
            {
                return;
            }

            foreach (var app in apps)
            {
                app.Volume = SetVolume((DeejConfiguration)command.hardwareConfiguration, value);
            }
        }
        private void CycleDefaultDevice(CommandControlMappingElement command, MidiControlChangeMessage msg)
        {
            var config     = (MidiConfiguration)command.hardwareConfiguration;
            var calcVolume = CalculateVolume(msg.ControlValue, config.MinValue, config.MaxValue, config.ScalingValue);

            var cycle     = false;
            var direction = 1;

            switch (config.ControllerType)
            {
            case ControllerTypes.Button:
            case ControllerTypes.LinearPotentiometer:
                if (calcVolume > 50)
                {
                    cycle     = true;
                    direction = 1;
                }
                break;

            case ControllerTypes.RotaryEncoder when msg.ControlValue == config.MinValue:
                cycle     = true;
                direction = -1;
                break;

            case ControllerTypes.RotaryEncoder:
                cycle     = true;
                direction = 1;
                break;
            }

            if (cycle)
            {
                var index = -1;
                for (var i = 0; i < _audioDeviceManager.Devices.Count; i++)
                {
                    if (_audioDeviceManager.Devices[i] == _audioDeviceManager.Default)
                    {
                        index = i;
                        break;
                    }
                }

                if (index != -1)
                {
                    index = index + direction;
                    index = (index + _audioDeviceManager.Devices.Count) % _audioDeviceManager.Devices.Count;
                }
                else
                {
                    index = 0;
                }

                try
                {
                    _audioDeviceManager.Default = _audioDeviceManager.Devices[index];
                }
                catch (NotSupportedException)
                {
                }
            }
        }