2021-03-06 09:53:38 +00:00
|
|
|
|
using System;
|
2021-03-07 09:38:56 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.Remoting.Channels;
|
2021-03-06 09:53:38 +00:00
|
|
|
|
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 09:38:56 +00:00
|
|
|
|
|
|
|
|
|
|
2021-03-06 09:53:38 +00:00
|
|
|
|
public IMassiveKnobActionInstance Create(ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
return new Instance();
|
|
|
|
|
}
|
2021-03-07 09:38:56 +00:00
|
|
|
|
|
|
|
|
|
|
2021-03-06 09:53:38 +00:00
|
|
|
|
private class Instance : IMassiveKnobActionInstance, IVoiceMeeterAction
|
|
|
|
|
{
|
|
|
|
|
private IMassiveKnobActionContext actionContext;
|
|
|
|
|
private VoiceMeeterGetParameterActionSettings settings;
|
2021-03-07 09:38:56 +00:00
|
|
|
|
private VoiceMeeterGetParameterActionSettingsViewModel viewModel;
|
2021-03-06 09:53:38 +00:00
|
|
|
|
private Parameters parameters;
|
|
|
|
|
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();
|
|
|
|
|
parameters?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ApplySettings()
|
|
|
|
|
{
|
2021-03-07 09:38:56 +00:00
|
|
|
|
if (InstanceRegister.Version == RunVoicemeeterParam.None || string.IsNullOrEmpty(settings.Parameter))
|
2021-03-06 09:53:38 +00:00
|
|
|
|
{
|
|
|
|
|
parameterChanged?.Dispose();
|
|
|
|
|
parameterChanged = null;
|
2021-03-07 09:38:56 +00:00
|
|
|
|
|
|
|
|
|
parameters?.Dispose();
|
|
|
|
|
parameters = null;
|
|
|
|
|
return;
|
2021-03-06 09:53:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-07 09:38:56 +00:00
|
|
|
|
if (parameters == null)
|
|
|
|
|
parameters = new Parameters();
|
|
|
|
|
|
2021-03-06 09:53:38 +00:00
|
|
|
|
if (parameterChanged == null)
|
|
|
|
|
parameterChanged = parameters.Subscribe(x => ParametersChanged());
|
|
|
|
|
|
2021-03-07 09:38:56 +00:00
|
|
|
|
ParametersChanged();
|
2021-03-06 09:53:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public UserControl CreateSettingsControl()
|
|
|
|
|
{
|
2021-03-07 09:38:56 +00:00
|
|
|
|
viewModel = new VoiceMeeterGetParameterActionSettingsViewModel(settings);
|
2021-03-06 09:53:38 +00:00
|
|
|
|
viewModel.PropertyChanged += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
if (!viewModel.IsSettingsProperty(args.PropertyName))
|
|
|
|
|
return;
|
2021-03-07 09:38:56 +00:00
|
|
|
|
|
2021-03-06 09:53:38 +00:00
|
|
|
|
actionContext.SetSettings(settings);
|
|
|
|
|
ApplySettings();
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-07 09:38:56 +00:00
|
|
|
|
viewModel.Disposed += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
if (sender == viewModel)
|
|
|
|
|
viewModel = null;
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-06 09:53:38 +00:00
|
|
|
|
return new VoiceMeeterGetParameterActionSettingsView(viewModel);
|
|
|
|
|
}
|
2021-03-07 09:38:56 +00:00
|
|
|
|
|
2021-03-06 09:53:38 +00:00
|
|
|
|
|
|
|
|
|
public void VoiceMeeterVersionChanged()
|
|
|
|
|
{
|
2021-03-07 09:38:56 +00:00
|
|
|
|
viewModel?.VoiceMeeterVersionChanged();
|
2021-03-06 09:53:38 +00:00
|
|
|
|
|
|
|
|
|
actionContext.SetSettings(settings);
|
2021-03-07 09:38:56 +00:00
|
|
|
|
ApplySettings();
|
2021-03-06 09:53:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ParametersChanged()
|
|
|
|
|
{
|
|
|
|
|
if (InstanceRegister.Version == RunVoicemeeterParam.None || string.IsNullOrEmpty(settings.Parameter))
|
|
|
|
|
return;
|
|
|
|
|
|
2021-03-07 09:38:56 +00:00
|
|
|
|
InstanceRegister.InitializeVoicemeeter().ContinueWith(t =>
|
2021-03-06 09:53:38 +00: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 09:38:56 +00:00
|
|
|
|
}
|