Exemple #1
0
 /// <summary>
 /// Runs automap on any device not already mapped.
 /// </summary>
 public void AutoMap()
 {
     foreach (var device in Available)
     {
         var mapping = GetMappedDeviceById(device.ID);
         if (mapping == null)
         {
             var automap = MidiHardwareTypes.AutoMap(device);
             if (automap != null)
             {
                 Map(device, automap);
             }
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// Maps a device to a new usage.
        /// </summary>
        public void Map(MidiDevice device, string hardwareInterface) // MidiDeviceMapping map)
        {
            lock (Mapped)
            {
                bool             changed = false;
                MappedMidiDevice mapping = null;

                // replace the "empty" marker with null
                if (hardwareInterface == "---")
                {
                    hardwareInterface = null;
                }

                try
                {
                    // either find an existing mapping to change or create a new one
                    mapping = GetMapping(device);
                    if (mapping == null)
                    {
                        mapping = new MappedMidiDevice()
                        {
                            Device = device
                        };
                        Mapped.Add(mapping);
                    }

                    // update mapping
                    if (hardwareInterface != null)
                    {
                        if (mapping.Hardware == null || mapping.Hardware.Name != hardwareInterface)
                        {
                            changed = true;
                        }
                    }
                    else
                    {
                        changed = mapping.Hardware != null;
                    }

                    // SIMULATOR TEMP HACK
                    // TODO: fully integrate this into the midi driver pipeline
                    if (device is LaunchPadSimulator)
                    {
                        // assign virtual driver
                        if (mapping.Driver == null)
                        {
                            mapping.Driver = new LPToolKit.Platform.VirtualMidiDriver();
                            mapping.Driver.SelectedDevice = device;
                        }
                        return;
                    }
                    // END SIMULATOR TEMP HACK

                    // ensure appropriate driver is setup
                    bool needsDriver = hardwareInterface != null; //map != MidiDeviceMapping.None;
                    if (needsDriver)
                    {
                        // create and setup the driver
                        if (mapping.Driver == null)
                        {
                            mapping.Driver = MidiDriver.CreatePlatformInstance();
                        }
                        mapping.Driver.SelectedDevice = mapping.Device;
                    }
                    else
                    {
                        // destroy any used driver if we're not using the device
                        if (mapping.Driver != null)
                        {
                            mapping.Driver.SelectedDevice = null;
                        }
                        mapping.Hardware.Clear();
                        mapping.Hardware = null;
                        mapping.Driver   = null;
                        Mapped.Remove(mapping);
                    }

                    // setting hardware specific settings
                    if (hardwareInterface != null)
                    {
                        mapping.Hardware = MidiHardwareTypes.CreateInstance(hardwareInterface, mapping);
                    }
                    else
                    {
                        mapping.Hardware = null;
                    }

                    // destroy any other drivers mapped to this device
                    foreach (var other in Mapped.ToArray())
                    {
                        if (other == mapping)
                        {
                            continue;
                        }
                        if (other.Device == mapping.Device)
                        {
                            // shut down duplicate device
                            if (mapping.Driver != null)
                            {
                                mapping.Driver.SelectedDevice = null;
                            }
                            mapping.Hardware.Clear();
                            mapping.Hardware = null;
                            mapping.Driver   = null;
                            Mapped.Remove(other);
                        }
                    }
                }
                finally
                {
                    // warn about device change
                    new DeviceChangeImplantEvent()
                    {
                        Mapping = mapping
                    }.ScheduleTask();
                }
            }
        }