2021-03-06 10:53:38 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Voicemeeter;
|
|
|
|
|
|
|
|
|
|
namespace MassiveKnob.Plugin.VoiceMeeter.GetParameter
|
|
|
|
|
{
|
|
|
|
|
public class VoiceMeeterGetParameterAction : IMassiveKnobAction
|
|
|
|
|
{
|
|
|
|
|
public Guid ActionId { get; } = new Guid("4904fffb-aaec-4f19-88bb-49f6ed38c3ec");
|
|
|
|
|
public MassiveKnobActionType ActionType { get; } = MassiveKnobActionType.OutputDigital;
|
|
|
|
|
public string Name { get; } = Strings.GetParameterName;
|
|
|
|
|
public string Description { get; } = Strings.GetParameterDescription;
|
2021-03-07 10:38:56 +01:00
|
|
|
|
|
|
|
|
|
|
2021-03-06 10:53:38 +01:00
|
|
|
|
public IMassiveKnobActionInstance Create(ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
return new Instance();
|
|
|
|
|
}
|
2021-03-07 10:38:56 +01:00
|
|
|
|
|
|
|
|
|
|
2021-03-06 10:53:38 +01:00
|
|
|
|
private class Instance : IMassiveKnobActionInstance, IVoiceMeeterAction
|
|
|
|
|
{
|
|
|
|
|
private IMassiveKnobActionContext actionContext;
|
|
|
|
|
private VoiceMeeterGetParameterActionSettings settings;
|
2021-03-07 10:38:56 +01:00
|
|
|
|
private VoiceMeeterGetParameterActionSettingsViewModel viewModel;
|
2021-03-06 10:53:38 +01:00
|
|
|
|
private IDisposable parameterChanged;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Initialize(IMassiveKnobActionContext context)
|
|
|
|
|
{
|
|
|
|
|
actionContext = context;
|
|
|
|
|
settings = context.GetSettings<VoiceMeeterGetParameterActionSettings>();
|
|
|
|
|
ApplySettings();
|
|
|
|
|
|
|
|
|
|
InstanceRegister.Register(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
InstanceRegister.Unregister(this);
|
|
|
|
|
parameterChanged?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ApplySettings()
|
|
|
|
|
{
|
2021-03-07 10:38:56 +01:00
|
|
|
|
if (InstanceRegister.Version == RunVoicemeeterParam.None || string.IsNullOrEmpty(settings.Parameter))
|
2021-03-06 10:53:38 +01:00
|
|
|
|
{
|
|
|
|
|
parameterChanged?.Dispose();
|
|
|
|
|
parameterChanged = null;
|
2021-03-07 10:38:56 +01:00
|
|
|
|
return;
|
2021-03-06 10:53:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parameterChanged == null)
|
2021-03-08 20:18:47 +01:00
|
|
|
|
parameterChanged = InstanceRegister.SubscribeToParameterChanges(ParametersChanged);
|
2021-03-06 10:53:38 +01:00
|
|
|
|
|
2021-03-07 10:38:56 +01:00
|
|
|
|
ParametersChanged();
|
2021-03-06 10:53:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public UserControl CreateSettingsControl()
|
|
|
|
|
{
|
2021-03-07 10:38:56 +01:00
|
|
|
|
viewModel = new VoiceMeeterGetParameterActionSettingsViewModel(settings);
|
2021-03-06 10:53:38 +01:00
|
|
|
|
viewModel.PropertyChanged += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
if (!viewModel.IsSettingsProperty(args.PropertyName))
|
|
|
|
|
return;
|
2021-03-07 10:38:56 +01:00
|
|
|
|
|
2021-03-06 10:53:38 +01:00
|
|
|
|
actionContext.SetSettings(settings);
|
|
|
|
|
ApplySettings();
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-07 10:38:56 +01:00
|
|
|
|
viewModel.Disposed += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
if (sender == viewModel)
|
|
|
|
|
viewModel = null;
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-06 10:53:38 +01:00
|
|
|
|
return new VoiceMeeterGetParameterActionSettingsView(viewModel);
|
|
|
|
|
}
|
2021-03-07 10:38:56 +01:00
|
|
|
|
|
2021-03-06 10:53:38 +01:00
|
|
|
|
|
|
|
|
|
public void VoiceMeeterVersionChanged()
|
|
|
|
|
{
|
2021-03-07 10:38:56 +01:00
|
|
|
|
viewModel?.VoiceMeeterVersionChanged();
|
2021-03-06 10:53:38 +01:00
|
|
|
|
|
|
|
|
|
actionContext.SetSettings(settings);
|
2021-03-07 10:38:56 +01:00
|
|
|
|
ApplySettings();
|
2021-03-06 10:53:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ParametersChanged()
|
|
|
|
|
{
|
|
|
|
|
if (InstanceRegister.Version == RunVoicemeeterParam.None || string.IsNullOrEmpty(settings.Parameter))
|
|
|
|
|
return;
|
|
|
|
|
|
2021-03-07 10:38:56 +01:00
|
|
|
|
InstanceRegister.InitializeVoicemeeter().ContinueWith(t =>
|
2021-03-06 10:53:38 +01:00
|
|
|
|
{
|
|
|
|
|
bool on;
|
|
|
|
|
|
|
|
|
|
if (float.TryParse(settings.Value, out var settingsFloatValue))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Even on/off values are returned as floating point "1.000" in text form,
|
|
|
|
|
// so try to compare in native format first
|
|
|
|
|
var floatValue = global::VoiceMeeter.Remote.GetParameter(settings.Parameter);
|
|
|
|
|
on = Math.Abs(settingsFloatValue - floatValue) < 0.001;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Fall back to text comparison
|
|
|
|
|
var value = global::VoiceMeeter.Remote.GetTextParameter(settings.Parameter);
|
|
|
|
|
on = string.Equals(value, settings.Value, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var value = global::VoiceMeeter.Remote.GetTextParameter(settings.Parameter);
|
|
|
|
|
on = string.Equals(value, settings.Value, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actionContext.SetDigitalOutput(settings.Inverted ? !on : on);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-07 10:38:56 +01:00
|
|
|
|
}
|