-
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.
Add audio processing nodes and fixup binding generator to handle Prox…
…yVoidNode
- Loading branch information
Showing
7 changed files
with
791 additions
and
139 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
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,159 @@ | ||
using System; | ||
using ProtoFlux.Core; | ||
using ProtoFlux.Runtimes.Execution; | ||
using FrooxEngine.ProtoFlux; | ||
using FrooxEngine; | ||
using Elements.Assets; | ||
|
||
namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Audio | ||
{ | ||
public class AudioAdderProxy : ProtoFluxEngineProxy, IAudioSource | ||
{ | ||
public IAudioSource AudioInput; | ||
|
||
public IAudioSource AudioInput2; | ||
|
||
public bool IsActive => true; | ||
|
||
public int ChannelCount => 1; | ||
|
||
public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S> | ||
{ | ||
Span<S> newBuffer = stackalloc S[buffer.Length]; | ||
newBuffer = buffer; | ||
Span<S> newBuffer2 = stackalloc S[buffer.Length]; | ||
if (AudioInput != null) | ||
{ | ||
AudioInput.Read(newBuffer); | ||
} | ||
else | ||
{ | ||
newBuffer.Fill(default); | ||
} | ||
if (AudioInput2 != null) | ||
{ | ||
AudioInput2.Read(newBuffer2); | ||
} | ||
else | ||
{ | ||
newBuffer2.Fill(default); | ||
} | ||
for (int i = 0; i < buffer.Length; i++) | ||
{ | ||
newBuffer[i] = newBuffer[i].Add(newBuffer2[i]); | ||
} | ||
} | ||
} | ||
[NodeCategory("Obsidian/Audio")] | ||
public class AudioAdder : ProxyVoidNode<FrooxEngineContext, AudioAdderProxy>, IExecutionChangeListener<FrooxEngineContext> | ||
{ | ||
[ChangeListener] | ||
public readonly ObjectInput<IAudioSource> AudioInput; | ||
|
||
[ChangeListener] | ||
public readonly ObjectInput<IAudioSource> AudioInput2; | ||
|
||
public readonly ObjectOutput<IAudioSource> AudioOutput; | ||
|
||
private ObjectStore<Action<IChangeable>> _enabledChangedHandler; | ||
|
||
private ObjectStore<SlotEvent> _activeChangedHandler; | ||
|
||
public bool ValueListensToChanges { get; private set; } | ||
|
||
private bool ShouldListen(AudioAdderProxy proxy) | ||
{ | ||
if (proxy.Enabled) | ||
{ | ||
return proxy.Slot.IsActive; | ||
} | ||
return false; | ||
} | ||
|
||
protected override void ProxyAdded(AudioAdderProxy proxy, FrooxEngineContext context) | ||
{ | ||
base.ProxyAdded(proxy, context); | ||
NodeContextPath path = context.CaptureContextPath(); | ||
ProtoFluxNodeGroup group = context.Group; | ||
context.GetEventDispatcher(out var dispatcher); | ||
Action<IChangeable> enabledHandler = delegate | ||
{ | ||
dispatcher.ScheduleEvent(path, delegate (FrooxEngineContext c) | ||
{ | ||
UpdateListenerState(c); | ||
}); | ||
}; | ||
SlotEvent activeHandler = delegate | ||
{ | ||
dispatcher.ScheduleEvent(path, delegate (FrooxEngineContext c) | ||
{ | ||
UpdateListenerState(c); | ||
}); | ||
}; | ||
proxy.EnabledField.Changed += enabledHandler; | ||
proxy.Slot.ActiveChanged += activeHandler; | ||
_enabledChangedHandler.Write(enabledHandler, context); | ||
_activeChangedHandler.Write(activeHandler, context); | ||
ValueListensToChanges = ShouldListen(proxy); | ||
} | ||
|
||
protected override void ProxyRemoved(AudioAdderProxy proxy, FrooxEngineContext context, bool inUseByAnotherInstance) | ||
{ | ||
if (!inUseByAnotherInstance) | ||
{ | ||
proxy.EnabledField.Changed -= _enabledChangedHandler.Read(context); | ||
proxy.Slot.ActiveChanged -= _activeChangedHandler.Read(context); | ||
_enabledChangedHandler.Clear(context); | ||
_activeChangedHandler.Clear(context); | ||
} | ||
} | ||
|
||
protected void UpdateListenerState(FrooxEngineContext context) | ||
{ | ||
AudioAdderProxy proxy = GetProxy(context); | ||
if (proxy != null) | ||
{ | ||
bool shouldListen = ShouldListen(proxy); | ||
if (shouldListen != ValueListensToChanges) | ||
{ | ||
ValueListensToChanges = shouldListen; | ||
context.Group.MarkChangeTrackingDirty(); | ||
} | ||
} | ||
} | ||
|
||
public void Changed(FrooxEngineContext context) | ||
{ | ||
AudioAdderProxy proxy = GetProxy(context); | ||
if (proxy == null) | ||
{ | ||
return; | ||
} | ||
if (!proxy.IsValid) | ||
{ | ||
return; | ||
} | ||
try | ||
{ | ||
context.World.UpdateManager.NestCurrentlyUpdating(proxy); | ||
proxy.AudioInput = AudioInput.Evaluate(context); | ||
proxy.AudioInput2 = AudioInput2.Evaluate(context); | ||
} | ||
finally | ||
{ | ||
context.World.UpdateManager.PopCurrentlyUpdating(proxy); | ||
} | ||
} | ||
|
||
protected override void ComputeOutputs(FrooxEngineContext context) | ||
{ | ||
AudioAdderProxy proxy = GetProxy(context); | ||
AudioOutput.Write(proxy, context); | ||
} | ||
|
||
public AudioAdder() | ||
{ | ||
AudioOutput = new ObjectOutput<IAudioSource>(this); | ||
} | ||
} | ||
} |
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,159 @@ | ||
using System; | ||
using ProtoFlux.Core; | ||
using ProtoFlux.Runtimes.Execution; | ||
using FrooxEngine.ProtoFlux; | ||
using FrooxEngine; | ||
using Elements.Assets; | ||
|
||
namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Audio | ||
{ | ||
public class AudioSubtractorProxy : ProtoFluxEngineProxy, IAudioSource | ||
{ | ||
public IAudioSource AudioInput; | ||
|
||
public IAudioSource AudioInput2; | ||
|
||
public bool IsActive => true; | ||
|
||
public int ChannelCount => 1; | ||
|
||
public void Read<S>(Span<S> buffer) where S : unmanaged, IAudioSample<S> | ||
{ | ||
Span<S> newBuffer = stackalloc S[buffer.Length]; | ||
newBuffer = buffer; | ||
Span<S> newBuffer2 = stackalloc S[buffer.Length]; | ||
if (AudioInput != null) | ||
{ | ||
AudioInput.Read(newBuffer); | ||
} | ||
else | ||
{ | ||
newBuffer.Fill(default); | ||
} | ||
if (AudioInput2 != null) | ||
{ | ||
AudioInput2.Read(newBuffer2); | ||
} | ||
else | ||
{ | ||
newBuffer2.Fill(default); | ||
} | ||
for (int i = 0; i < buffer.Length; i++) | ||
{ | ||
newBuffer[i] = newBuffer[i].Subtract(newBuffer2[i]); | ||
} | ||
} | ||
} | ||
[NodeCategory("Obsidian/Audio")] | ||
public class AudioSubtractor : ProxyVoidNode<FrooxEngineContext, AudioSubtractorProxy>, IExecutionChangeListener<FrooxEngineContext> | ||
{ | ||
[ChangeListener] | ||
public readonly ObjectInput<IAudioSource> AudioInput; | ||
|
||
[ChangeListener] | ||
public readonly ObjectInput<IAudioSource> AudioInput2; | ||
|
||
public readonly ObjectOutput<IAudioSource> AudioOutput; | ||
|
||
private ObjectStore<Action<IChangeable>> _enabledChangedHandler; | ||
|
||
private ObjectStore<SlotEvent> _activeChangedHandler; | ||
|
||
public bool ValueListensToChanges { get; private set; } | ||
|
||
private bool ShouldListen(AudioSubtractorProxy proxy) | ||
{ | ||
if (proxy.Enabled) | ||
{ | ||
return proxy.Slot.IsActive; | ||
} | ||
return false; | ||
} | ||
|
||
protected override void ProxyAdded(AudioSubtractorProxy proxy, FrooxEngineContext context) | ||
{ | ||
base.ProxyAdded(proxy, context); | ||
NodeContextPath path = context.CaptureContextPath(); | ||
ProtoFluxNodeGroup group = context.Group; | ||
context.GetEventDispatcher(out var dispatcher); | ||
Action<IChangeable> enabledHandler = delegate | ||
{ | ||
dispatcher.ScheduleEvent(path, delegate (FrooxEngineContext c) | ||
{ | ||
UpdateListenerState(c); | ||
}); | ||
}; | ||
SlotEvent activeHandler = delegate | ||
{ | ||
dispatcher.ScheduleEvent(path, delegate (FrooxEngineContext c) | ||
{ | ||
UpdateListenerState(c); | ||
}); | ||
}; | ||
proxy.EnabledField.Changed += enabledHandler; | ||
proxy.Slot.ActiveChanged += activeHandler; | ||
_enabledChangedHandler.Write(enabledHandler, context); | ||
_activeChangedHandler.Write(activeHandler, context); | ||
ValueListensToChanges = ShouldListen(proxy); | ||
} | ||
|
||
protected override void ProxyRemoved(AudioSubtractorProxy proxy, FrooxEngineContext context, bool inUseByAnotherInstance) | ||
{ | ||
if (!inUseByAnotherInstance) | ||
{ | ||
proxy.EnabledField.Changed -= _enabledChangedHandler.Read(context); | ||
proxy.Slot.ActiveChanged -= _activeChangedHandler.Read(context); | ||
_enabledChangedHandler.Clear(context); | ||
_activeChangedHandler.Clear(context); | ||
} | ||
} | ||
|
||
protected void UpdateListenerState(FrooxEngineContext context) | ||
{ | ||
AudioSubtractorProxy proxy = GetProxy(context); | ||
if (proxy != null) | ||
{ | ||
bool shouldListen = ShouldListen(proxy); | ||
if (shouldListen != ValueListensToChanges) | ||
{ | ||
ValueListensToChanges = shouldListen; | ||
context.Group.MarkChangeTrackingDirty(); | ||
} | ||
} | ||
} | ||
|
||
public void Changed(FrooxEngineContext context) | ||
{ | ||
AudioSubtractorProxy proxy = GetProxy(context); | ||
if (proxy == null) | ||
{ | ||
return; | ||
} | ||
if (!proxy.IsValid) | ||
{ | ||
return; | ||
} | ||
try | ||
{ | ||
context.World.UpdateManager.NestCurrentlyUpdating(proxy); | ||
proxy.AudioInput = AudioInput.Evaluate(context); | ||
proxy.AudioInput2 = AudioInput2.Evaluate(context); | ||
} | ||
finally | ||
{ | ||
context.World.UpdateManager.PopCurrentlyUpdating(proxy); | ||
} | ||
} | ||
|
||
protected override void ComputeOutputs(FrooxEngineContext context) | ||
{ | ||
AudioSubtractorProxy proxy = GetProxy(context); | ||
AudioOutput.Write(proxy, context); | ||
} | ||
|
||
public AudioSubtractor() | ||
{ | ||
AudioOutput = new ObjectOutput<IAudioSource>(this); | ||
} | ||
} | ||
} |
Oops, something went wrong.