2021-02-21 13:04:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Controls;
|
2021-02-24 08:05:11 +00:00
|
|
|
|
using AudioSwitcher.AudioApi;
|
|
|
|
|
using MassiveKnob.Plugin.CoreAudio.Settings;
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
|
|
|
|
namespace MassiveKnob.Plugin.CoreAudio.Actions
|
|
|
|
|
{
|
|
|
|
|
public class DeviceVolumeAction : IMassiveKnobAction
|
|
|
|
|
{
|
|
|
|
|
public Guid ActionId { get; } = new Guid("aabd329c-8be5-4d1e-90ab-5114143b21dd");
|
|
|
|
|
public MassiveKnobActionType ActionType { get; } = MassiveKnobActionType.InputAnalog;
|
|
|
|
|
public string Name { get; } = "Set volume";
|
|
|
|
|
public string Description { get; } = "Sets the volume for the selected device, regardless of the current default device.";
|
|
|
|
|
|
|
|
|
|
|
2021-02-24 08:05:11 +00:00
|
|
|
|
public IMassiveKnobActionInstance Create()
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
return new Instance();
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class Instance : IMassiveKnobAnalogAction
|
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
private IMassiveKnobActionContext actionContext;
|
|
|
|
|
private DeviceVolumeActionSettings settings;
|
|
|
|
|
private IDevice playbackDevice;
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
2021-02-24 08:05:11 +00:00
|
|
|
|
|
|
|
|
|
public void Initialize(IMassiveKnobActionContext context)
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
actionContext = context;
|
|
|
|
|
settings = context.GetSettings<DeviceVolumeActionSettings>();
|
|
|
|
|
ApplySettings();
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
2021-02-24 08:05:11 +00:00
|
|
|
|
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 08:05:11 +00:00
|
|
|
|
|
|
|
|
|
private void ApplySettings()
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
var coreAudioController = CoreAudioControllerInstance.Acquire();
|
|
|
|
|
playbackDevice = settings.DeviceId.HasValue ? coreAudioController.GetDevice(settings.DeviceId.Value) : null;
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-24 08:05:11 +00:00
|
|
|
|
public UserControl CreateSettingsControl()
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
var viewModel = new DeviceVolumeActionSettingsViewModel(settings);
|
|
|
|
|
viewModel.PropertyChanged += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
if (!viewModel.IsSettingsProperty(args.PropertyName))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
actionContext.SetSettings(settings);
|
|
|
|
|
ApplySettings();
|
|
|
|
|
};
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
2021-02-24 08:05:11 +00:00
|
|
|
|
return new DeviceVolumeActionSettingsView(viewModel);
|
|
|
|
|
}
|
2021-02-21 13:04:24 +00:00
|
|
|
|
|
2021-02-24 08:05:11 +00:00
|
|
|
|
|
|
|
|
|
public async ValueTask AnalogChanged(byte value)
|
|
|
|
|
{
|
|
|
|
|
if (playbackDevice == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
await playbackDevice.SetVolumeAsync(value);
|
|
|
|
|
}
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|