osu/osu.Game/Overlays/Settings/Sections/Audio/AudioDevicesSettings.cs

85 lines
2.8 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Graphics;
using System.Collections.Generic;
using System.Linq;
2018-11-14 09:02:38 +00:00
using osu.Game.Graphics.UserInterface;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Overlays.Settings.Sections.Audio
{
public class AudioDevicesSettings : SettingsSubsection
{
protected override string Header => "Devices";
2020-02-14 13:14:00 +00:00
[Resolved]
private AudioManager audio { get; set; }
2020-02-14 13:30:27 +00:00
2018-04-13 09:19:50 +00:00
private SettingsDropdown<string> dropdown;
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (audio != null)
{
audio.OnNewDevice -= onDeviceChanged;
audio.OnLostDevice -= onDeviceChanged;
}
}
private void updateItems()
{
2018-11-14 09:02:38 +00:00
var deviceItems = new List<string> { string.Empty };
deviceItems.AddRange(audio.AudioDeviceNames);
2018-04-13 09:19:50 +00:00
var preferredDeviceName = audio.AudioDevice.Value;
2018-11-14 09:02:38 +00:00
if (deviceItems.All(kv => kv != preferredDeviceName))
deviceItems.Add(preferredDeviceName);
2018-04-13 09:19:50 +00:00
// The option dropdown for audio device selection lists all audio
// device names. Dropdowns, however, may not have multiple identical
// keys. Thus, we remove duplicate audio device names from
// the dropdown. BASS does not give us a simple mechanism to select
// specific audio devices in such a case anyways. Such
// functionality would require involved OS-specific code.
dropdown.Items = deviceItems.Distinct().ToList();
}
private void onDeviceChanged(string name) => updateItems();
protected override void LoadComplete()
{
base.LoadComplete();
Children = new Drawable[]
{
2019-11-20 16:27:34 +00:00
dropdown = new AudioDeviceSettingsDropdown
{
2019-11-21 13:35:15 +00:00
Keywords = new[] { "speaker", "headphone", "output" }
2019-11-20 16:27:34 +00:00
}
2018-04-13 09:19:50 +00:00
};
updateItems();
dropdown.Bindable = audio.AudioDevice;
audio.OnNewDevice += onDeviceChanged;
audio.OnLostDevice += onDeviceChanged;
}
2018-11-14 09:02:38 +00:00
private class AudioDeviceSettingsDropdown : SettingsDropdown<string>
{
protected override OsuDropdown<string> CreateDropdown() => new AudioDeviceDropdownControl();
2018-11-14 09:02:38 +00:00
private class AudioDeviceDropdownControl : DropdownControl
{
protected override string GenerateItemText(string item)
=> string.IsNullOrEmpty(item) ? "Default" : base.GenerateItemText(item);
}
}
2018-04-13 09:19:50 +00:00
}
}