String SaveDevice(CfgAudioDevice dev) { return(dev.id + "," + "\"" + dev.name.Replace("\"", "\"\"") + "\"," + dev.relVol + "," + dev.useLocal); }
// Restore configuration settings into the active device list public void RestoreAudioDevices(Dictionary <String, UIWin.AudioDevice> sysdevs) { // Go through the devices found in the configuration. We only // restore settings for devices listed there; devices that are // present in the system but not listed in the configuration // are left with default settings. Every device is disabled // by default except for the device designated in the Windows // audio control panel as the "default" device, which is always // enabled. So the result is that we enable all of the devices // explicitly marked as enabled in the configuration, plus the // Windows default device. foreach (var v in devices) { // If this device exists in the system devices table, set up // the system device with the saved settings. If it doesn't // exist in the system devices table, ignore it. We leave // the entry in the configuration list so that it will still // take effect in future sessions if the device is installed // again later. if (sysdevs.ContainsKey(v.Key)) { // get the config record and system device entry CfgAudioDevice cfgdev = v.Value; UIWin.AudioDevice sysdev = sysdevs[v.Key]; // Copy the config settings to the system device entry. Note // that isActive is always set to true, since a device only // appears in the config list at all if it's active. sysdev.isActive = true; sysdev.relLevel = cfgdev.relVol / 100.0f; // adjust % value to normalized scale sysdev.useLocal = cfgdev.useLocal; } } }
// Update a saved device with new settings. Returns true if any // changes were made. public bool UpdateDevice(String id, String name, bool isActive, float fRelVol, bool useLocal) { // convert the relative volume from a fraction to a percentage int relVol = (int)Math.Round(fRelVol * 100.0f); // assume no changes will be made bool changed = false; // If the device is active, update the existing table entry, // or add a new entry if there isn't one already. If the // device is inactive, delete any existing table entry, since // we only store active devices. if (isActive) { // The device is active. If it's already in the list, // update the existing entry; otherwise add a new entry. if (devices.ContainsKey(id)) { // It's already in the table. If the existing entry // has any differences, update it. CfgAudioDevice dev = devices[id]; changed = dev.Update(id, name, relVol, useLocal); } else { // It's not in the table - add a new entry CfgAudioDevice dev = new CfgAudioDevice(id, name, relVol, useLocal); devices[id] = dev; changed = true; } } else { // The device is inactive. If there's a table entry, // delete it. if (devices.ContainsKey(id)) { changed = true; devices.Remove(id); } } // return the change indication return(changed); }
void ParseDevice(String s) { Match m = Regex.Match(s, @"(?i)(.*),""((.|"""")*)"",(\d+),(true|false)"); if (m.Success) { CfgAudioDevice dev = new CfgAudioDevice( m.Groups[1].Value, m.Groups[2].Value, int.Parse(m.Groups[4].Value), bool.Parse(m.Groups[5].Value)); devices[dev.id] = dev; } else { throw new FormatException("Invalid 'device' value format"); } }