-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splits up code and uses high level node building API to implement nod…
…e factory
- Loading branch information
Showing
5 changed files
with
225 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using VL.Core; | ||
using System.Reactive.Linq; | ||
using VL.Lib.Basics.Resources; | ||
|
||
namespace VL.Audio | ||
{ | ||
sealed class AudioSignalNode : FactoryBasedVLNode, IVLNode | ||
{ | ||
AudioSignal FSignalInstance; | ||
IResourceHandle<GlobalEngine> FEngineHandle; | ||
|
||
class MyPin : IVLPin | ||
{ | ||
public object Value { get; set; } | ||
public Type Type { get; set; } | ||
public string Name { get; set; } | ||
} | ||
|
||
readonly NodeDescription description; | ||
|
||
public AudioSignalNode(NodeDescription description, IResourceHandle<GlobalEngine> engineHandle, NodeContext nodeContext) : base(nodeContext) | ||
{ | ||
this.description = description; | ||
FSignalInstance = (AudioSignal)Activator.CreateInstance(description.Signal); | ||
FEngineHandle = engineHandle; | ||
|
||
foreach (var input in description.Inputs) | ||
FInputsMap.Add(input.Name, new MyPin() { Name = input.Name, Type = input.Type, Value = input.DefaultValue }); | ||
|
||
Inputs = FInputsMap.Values.ToArray(); | ||
|
||
foreach (var output in description.Outputs) | ||
FOutputsMap.Add(output.Name, new MyPin() { Name = output.Name, Type = output.Type, Value = output.DefaultValue }); | ||
|
||
Outputs = FOutputsMap.Values.ToArray(); | ||
} | ||
|
||
public IVLNodeDescription NodeDescription => description; | ||
|
||
Dictionary<string, IVLPin> FInputsMap = new Dictionary<string, IVLPin>(); | ||
Dictionary<string, IVLPin> FOutputsMap = new Dictionary<string, IVLPin>(); | ||
|
||
public IVLPin[] Inputs { get; } | ||
|
||
public IVLPin[] Outputs { get; } | ||
|
||
public void Update() | ||
{ | ||
var apply = true; | ||
//if (FInputsMap.TryGetValue("Apply", out var pin)) | ||
// apply = (bool)pin.Value; | ||
|
||
if (apply) | ||
{ | ||
foreach (var param in FSignalInstance.InParams) | ||
if (param.GetValueType() == typeof(double)) | ||
param.SetValue((double)(float)FInputsMap[param.Name].Value); | ||
else | ||
param.SetValue(FInputsMap[param.Name].Value); | ||
Outputs[0].Value = FSignalInstance; | ||
} | ||
else | ||
Outputs[0].Value = FInputsMap["Input"].Value; | ||
|
||
foreach (var param in FSignalInstance.OutParams) | ||
if (param.GetValueType() == typeof(double)) | ||
FOutputsMap[param.Name].Value = (float)(double)param.GetValue(); | ||
else | ||
FOutputsMap[param.Name].Value = param.GetValue(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
FSignalInstance.Dispose(); | ||
FEngineHandle.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using VL.Core; | ||
using System.Reactive.Linq; | ||
using VL.Lib.Basics.Resources; | ||
|
||
namespace VL.Audio | ||
{ | ||
sealed class NodeDescription : IVLNodeDescription, IInfo, ITaggedInfo | ||
{ | ||
readonly IResourceProvider<GlobalEngine> _engineProvider; | ||
readonly ImmutableArray<string> _tags; | ||
readonly List<PinDescription> _inputs = new List<PinDescription>(); | ||
readonly List<PinDescription> _outputs = new List<PinDescription>(); | ||
|
||
public NodeDescription(IVLNodeDescriptionFactory factory, IResourceProvider<GlobalEngine> engineProvider, Type signalType, string name, string category, string summary, string remarks, string tags) | ||
{ | ||
Factory = factory; | ||
_engineProvider = engineProvider; | ||
Signal = signalType; | ||
Name = name; | ||
Category = "Audio." + category; | ||
Summary = summary; | ||
Remarks = remarks; | ||
_tags = tags.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToImmutableArray(); | ||
|
||
using (var signal = (AudioSignal)Activator.CreateInstance(signalType)) | ||
{ | ||
foreach (var input in signal.InParams) | ||
_inputs.Add(new PinDescription(input.Name, DoubleToSingleT(input.GetValueType()), DoubleToSingleV(input.GetDefaultValue()), "")); | ||
|
||
//if (category == "Filter") | ||
// inputs.Add(new PinDescription("Apply", typeof(bool), true, "")); | ||
|
||
_outputs.Add(new PinDescription("Output", typeof(AudioSignal), null, "")); | ||
|
||
foreach (var output in signal.OutParams) | ||
_outputs.Add(new PinDescription(output.Name, DoubleToSingleT(output.GetValueType()), DoubleToSingleV(output.GetDefaultValue()), "")); | ||
} | ||
} | ||
|
||
Type DoubleToSingleT(Type type) | ||
{ | ||
if (type == typeof(double)) | ||
return typeof(float); | ||
else | ||
return type; | ||
} | ||
|
||
object DoubleToSingleV(object value) | ||
{ | ||
if (value != null && value.GetType() == typeof(double)) | ||
return (float)(double)value; | ||
else | ||
return value; | ||
} | ||
|
||
public IVLNodeDescriptionFactory Factory { get; } | ||
|
||
public Type Signal { get; private set; } | ||
|
||
public string Name { get; private set; } | ||
|
||
public string Category { get; private set; } | ||
|
||
public bool Fragmented => false; | ||
|
||
public IReadOnlyList<IVLPinDescription> Inputs => _inputs; | ||
|
||
public IReadOnlyList<IVLPinDescription> Outputs => _outputs; | ||
|
||
public IEnumerable<Core.Diagnostics.Message> Messages | ||
{ | ||
get | ||
{ | ||
yield break; | ||
} | ||
} | ||
|
||
public string Summary { get; private set; } | ||
|
||
public string Remarks { get; private set; } | ||
|
||
public IObservable<object> Invalidated => Observable.Never<object>(); | ||
|
||
ImmutableArray<string> ITaggedInfo.Tags => _tags; | ||
|
||
public IVLNode CreateInstance(NodeContext context) | ||
{ | ||
var engineHandle = _engineProvider.GetHandle(); | ||
return new AudioSignalNode(this, engineHandle, context); | ||
} | ||
|
||
public bool OpenEditor() | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.