/// <summary>
        /// Parse and activate a device.
        /// If successful it is added to the devices list.
        /// </summary>
        /// <param name="device">The device to parse.</param>
        /// <param name="id">The unique id of the device.</param>
        private WasApiAudioDevice ParseDevice(IMMDevice device, string id)
        {
            if (device == null)
            {
                return(null);
            }

            string deviceName = $"Unknown Device ({id})";

            // Try to get the device name.
            device.OpenPropertyStore(StorageAccessMode.Read, out IPropertyStore store);
            if (store != null)
            {
                store.GetValue(ref PropertyKey.PKEY_Device_FriendlyName, out PropVariant deviceNameProp);
                object parsed = deviceNameProp.Value;
                if (parsed is string deviceNameStr)
                {
                    deviceName = deviceNameStr;
                }
            }

            // Add to the list.
            var dev = new WasApiAudioDevice(id, deviceName, device);

            _devices.Add(id, dev);
            Engine.Log.Trace($"Detected audio device - {deviceName}", MessageSource.Win32);

            return(dev);
        }
Пример #2
0
        /// <summary>
        /// Set the audio device the layer will output into.
        /// </summary>
        public void SetDevice(WasApiAudioDevice device)
        {
            if (device == null)
            {
                _layerContext = null;
                return;
            }

            _layerContext         = device.CreateLayerContext(out uint bufferSize);
            _bufferLengthInFrames = (int)bufferSize;
        }
        /// <summary>
        /// Sets the specified id to be the default device.
        /// </summary>
        /// <param name="id">The id of the device to set as default.</param>
        private void SetDefaultDevice(string id)
        {
            _devices.TryGetValue(id, out WasApiAudioDevice defaultDevice);
            if (defaultDevice == null)
            {
                // Default audio device was not found in the device list - query for it.
                defaultDevice = ParseDevice(id);
                if (defaultDevice == null)
                {
                    Win32Platform.CheckError("Default audio device is not in device list.", true);
                }
            }

            // Unset old default.
            foreach (KeyValuePair <string, WasApiAudioDevice> device in _devices)
            {
                device.Value.Default = false;
            }

            if (defaultDevice == null)
            {
                return;
            }

            defaultDevice.Default = true;
            DefaultDevice         = defaultDevice;
            Engine.Log.Trace($"Default audio device is: {defaultDevice.Name}.", MessageSource.Win32);

            // Tell all layers about this change.
            lock (_layers)
            {
                foreach (WasApiLayer layer in _layers)
                {
                    layer.DefaultDeviceChanged(defaultDevice);
                }
            }
        }
Пример #4
0
 public WasApiLayerContext(WasApiAudioDevice parent)
 {
     Parent = parent;
 }
Пример #5
0
 public void DefaultDeviceChanged(WasApiAudioDevice newDevice)
 {
     _updateDevice = true;
 }
Пример #6
0
 private void SetDevice(WasApiAudioDevice device)
 {
     _layerContext = device.CreateLayerContext();
 }