Process streaming DSP audio blocks
This commit is contained in:
parent
3931b4c844
commit
91932cf1da
6 changed files with 197 additions and 2 deletions
|
|
@ -7,6 +7,7 @@ public sealed class AquariumAudioDocument
|
|||
private readonly ConcurrentQueue<AquariumPcmAudioChunk> pcmChunks = new();
|
||||
private readonly ConcurrentQueue<AquariumAudioControlFrame> controlFrames = new();
|
||||
private readonly ConcurrentQueue<AquariumStreamingDspProgram> streamingDspPrograms = new();
|
||||
private readonly ConcurrentQueue<AquariumStreamingAudioBlock> streamingAudioBlocks = new();
|
||||
|
||||
public static AquariumAudioDocument Empty { get; } = new();
|
||||
|
||||
|
|
@ -109,6 +110,27 @@ public sealed class AquariumAudioDocument
|
|||
|
||||
return drained;
|
||||
}
|
||||
|
||||
public void EnqueueStreamingAudioBlock(AquariumStreamingAudioBlock block)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(block.ProfileId) || block.FrameCount <= 0 || block.SampleRate <= 0 || block.Channels.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
streamingAudioBlocks.Enqueue(block);
|
||||
}
|
||||
|
||||
public IReadOnlyList<AquariumStreamingAudioBlock> DrainStreamingAudioBlocks(int maxBlocks = 32)
|
||||
{
|
||||
var drained = new List<AquariumStreamingAudioBlock>();
|
||||
while (drained.Count < maxBlocks && streamingAudioBlocks.TryDequeue(out var block))
|
||||
{
|
||||
drained.Add(block);
|
||||
}
|
||||
|
||||
return drained;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record AquariumPcmAudioChunk(float[] MonoSamples, int SampleRate, float LeftGain = 1.0f, float RightGain = 1.0f);
|
||||
|
|
@ -134,3 +156,17 @@ public sealed record AquariumStreamingDspProgram(
|
|||
string FaustSource,
|
||||
int Revision,
|
||||
float ProbeDurationSeconds = 0.05f);
|
||||
|
||||
public sealed record AquariumStreamingAudioBlock(
|
||||
string ProfileId,
|
||||
IReadOnlyList<AquariumStreamingAudioChannel> Channels,
|
||||
int FrameCount,
|
||||
int SampleRate,
|
||||
long Sequence,
|
||||
int MonitorLeftChannel = -1,
|
||||
int MonitorRightChannel = -1);
|
||||
|
||||
public sealed record AquariumStreamingAudioChannel(
|
||||
int ChannelIndex,
|
||||
string SourceId,
|
||||
float[] Samples);
|
||||
|
|
|
|||
|
|
@ -82,6 +82,44 @@ internal sealed class AquariumStreamingDspHost : IDisposable
|
|||
return true;
|
||||
}
|
||||
|
||||
public bool ProcessBlock(AquariumStreamingAudioBlock block, out float[][] outputs)
|
||||
{
|
||||
outputs = [];
|
||||
if (!programs.TryGetValue(block.ProfileId, out var runtime))
|
||||
{
|
||||
LastError = $"Streaming DSP program `{block.ProfileId}` is not loaded.";
|
||||
return false;
|
||||
}
|
||||
|
||||
var inputCount = runtime.Stream.InputCount;
|
||||
var outputCount = runtime.Stream.OutputCount;
|
||||
var inputs = new float[inputCount][];
|
||||
for (var channel = 0; channel < inputCount; channel++)
|
||||
{
|
||||
inputs[channel] = new float[block.FrameCount];
|
||||
}
|
||||
|
||||
foreach (var channel in block.Channels)
|
||||
{
|
||||
if (channel.ChannelIndex < 0 || channel.ChannelIndex >= inputCount)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Array.Copy(channel.Samples, 0, inputs[channel.ChannelIndex], 0, Math.Min(block.FrameCount, channel.Samples.Length));
|
||||
}
|
||||
|
||||
outputs = new float[outputCount][];
|
||||
for (var channel = 0; channel < outputCount; channel++)
|
||||
{
|
||||
outputs[channel] = new float[block.FrameCount];
|
||||
}
|
||||
|
||||
runtime.Stream.ProcessBlock(inputs, outputs, block.FrameCount);
|
||||
LastError = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var runtime in programs.Values)
|
||||
|
|
|
|||
|
|
@ -59,6 +59,21 @@ internal sealed class AquariumSynthHost : IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
foreach (var block in audio.DrainStreamingAudioBlocks())
|
||||
{
|
||||
if (!streamingDsp.ProcessBlock(block, out var outputs))
|
||||
{
|
||||
if (TraceAudio)
|
||||
{
|
||||
Console.WriteLine($"Aquarium streaming DSP block skipped: {streamingDsp.LastError}");
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
PlayMonitorOutputs(block, outputs);
|
||||
}
|
||||
|
||||
if (!synth.Enabled)
|
||||
{
|
||||
return;
|
||||
|
|
@ -91,6 +106,34 @@ internal sealed class AquariumSynthHost : IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
private void PlayMonitorOutputs(AquariumStreamingAudioBlock block, float[][] outputs)
|
||||
{
|
||||
if (block.MonitorLeftChannel < 0 && block.MonitorRightChannel < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var left = block.MonitorLeftChannel >= 0 && block.MonitorLeftChannel < outputs.Length
|
||||
? outputs[block.MonitorLeftChannel]
|
||||
: null;
|
||||
var right = block.MonitorRightChannel >= 0 && block.MonitorRightChannel < outputs.Length
|
||||
? outputs[block.MonitorRightChannel]
|
||||
: null;
|
||||
if (left is null && right is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var frameCount = Math.Min(block.FrameCount, Math.Max(left?.Length ?? 0, right?.Length ?? 0));
|
||||
var mono = new float[frameCount];
|
||||
for (var index = 0; index < frameCount; index++)
|
||||
{
|
||||
mono[index] = Math.Clamp(((left?[index] ?? 0.0f) + (right?[index] ?? 0.0f)) * 0.5f, -1.0f, 1.0f);
|
||||
}
|
||||
|
||||
audioDevice.Play(mono, block.SampleRate);
|
||||
}
|
||||
|
||||
private PatchRuntime GetRuntime(AquariumSynthPatch patch)
|
||||
{
|
||||
if (patches.TryGetValue(patch.Id, out var runtime))
|
||||
|
|
|
|||
|
|
@ -264,3 +264,4 @@
|
|||
{"lesson":"Synchronized camera Texture2D import is debug-view authority only for now. Fensalir can resolve and wait Mimir camera resources, but production visual-fusion or feature-extraction lowerings are explicitly deferred until renderer and audio work are fixed. Do not let resource import success silently choose a camera fusion policy.","timestamp":"2026-05-27T17:53:38+01:00","files":["state/map.yaml"],"source":"camera lowering scope decision","topic":"camera-texture-debug-only-scope"}
|
||||
{"lesson":"AquariumAudioDocument now has an explicit audio control-frame queue for client-published DSP targets. AquariumSynthHost drains and retains the latest frame by profile, but this is contract/control-plane state only; it does not move samples or claim aligned audio until a Faust/native streaming processor consumes the controls. Fensalir build and Aquarium.Engine.Tests passed.","timestamp":"2026-05-27T18:06:07+01:00","files":["src/Aquarium.Engine.Contracts/Audio/AquariumAudio.cs","src/Aquarium.Engine/Audio/AquariumSynthHost.cs","tests/Aquarium.Engine.Tests/SynthPlaybackTests.cs","state/map.yaml"],"source":"Mimir runtime actuator control handoff","topic":"audio-control-frame-contract"}
|
||||
{"lesson":"Fensalir now hosts profile-keyed streaming Faust DSP programs through AquaSynth.Faust 0.1.0-alpha.4. AquariumAudioDocument queues AquariumStreamingDspProgram declarations, AquariumStreamingDspHost compiles raw Faust source into a persistent AquaSynthStreamingPatch, AquariumSynthHost applies matching audio control frames, and Aquarium.Engine.Tests proves a controlled input block passes through a streaming Faust graph. Device I/O/stem routing is still the next cut before this becomes live aligned audio.","timestamp":"2026-05-27T18:53:49+01:00","files":["src/Aquarium.Engine.Contracts/Audio/AquariumAudio.cs","src/Aquarium.Engine/Audio/AquariumStreamingDspHost.cs","src/Aquarium.Engine/Audio/AquariumSynthHost.cs","src/Aquarium.Engine/Aquarium.Engine.csproj","tests/Aquarium.Engine.Tests/SynthPlaybackTests.cs","packages/AquaSynth.Faust.0.1.0-alpha.4.nupkg","state/map.yaml"],"source":"streaming Faust DSP host cut","topic":"streaming-faust-dsp-host"}
|
||||
{"lesson":"AquariumAudioDocument now carries profile-keyed AquariumStreamingAudioBlock input lanes. AquariumStreamingDspHost maps declared channel indices into the persistent Faust DSP input array, processes the block, and AquariumSynthHost can optionally monitor selected output lanes. Aquarium.Engine.Tests proves declared input blocks are processed through a controlled streaming Faust graph. This closes the samples-enter-DSP seam; durable stem routing/publication remains separate.","timestamp":"2026-05-27T20:18:14+01:00","files":["src/Aquarium.Engine.Contracts/Audio/AquariumAudio.cs","src/Aquarium.Engine/Audio/AquariumStreamingDspHost.cs","src/Aquarium.Engine/Audio/AquariumSynthHost.cs","tests/Aquarium.Engine.Tests/SynthPlaybackTests.cs","state/map.yaml"],"source":"streaming DSP input block contract","topic":"streaming-audio-blocks"}
|
||||
|
|
|
|||
|
|
@ -77,8 +77,9 @@ invariants:
|
|||
- Streaming Faust DSP is now an engine audio mechanism. Clients declare a
|
||||
profile-keyed `AquariumStreamingDspProgram`; Fensalir compiles it through
|
||||
AquaSynth.Faust, keeps a persistent DSP instance, applies matching audio
|
||||
control frames, and can process caller-supplied audio blocks. Device I/O
|
||||
and stem routing remain the next host cut.
|
||||
control frames, and processes profile-keyed `AquariumStreamingAudioBlock`
|
||||
input lanes into DSP output lanes. Optional monitor playback is explicit;
|
||||
durable stem routing and OBS-facing publication remain the next host cut.
|
||||
|
||||
next_actions:
|
||||
- Keep origin pointed at https://github.com/GameCult/Fensalir.git.
|
||||
|
|
|
|||
|
|
@ -38,6 +38,27 @@ public sealed class SynthPlaybackTests
|
|||
Assert.Empty(document.DrainControlFrames());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AquariumAudioDocumentDrainsStreamingAudioBlocks()
|
||||
{
|
||||
var document = new AquariumAudioDocument();
|
||||
document.EnqueueStreamingAudioBlock(new AquariumStreamingAudioBlock(
|
||||
"six-source-faust-fractional-delay",
|
||||
[
|
||||
new AquariumStreamingAudioChannel(0, "mic-0", [0.25f, 0.5f])
|
||||
],
|
||||
FrameCount: 2,
|
||||
SampleRate: 48_000,
|
||||
Sequence: 3));
|
||||
|
||||
var drained = document.DrainStreamingAudioBlocks();
|
||||
|
||||
Assert.Single(drained);
|
||||
Assert.Equal(2, drained[0].FrameCount);
|
||||
Assert.Equal("mic-0", drained[0].Channels[0].SourceId);
|
||||
Assert.Empty(document.DrainStreamingAudioBlocks());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StreamingDspHostProcessesControlDrivenInputBlocksWhenToolchainIsAvailable()
|
||||
{
|
||||
|
|
@ -88,6 +109,61 @@ public sealed class SynthPlaybackTests
|
|||
Assert.All(output[0], sample => Assert.InRange(sample, 0.124f, 0.126f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StreamingDspHostProcessesDeclaredAudioBlocksWhenToolchainIsAvailable()
|
||||
{
|
||||
const string source = """
|
||||
import("stdfaust.lib");
|
||||
gain = hslider("source0/gain", 1.0, 0.0, 2.0, 0.001);
|
||||
process = _ * gain;
|
||||
""";
|
||||
using var host = new AquariumStreamingDspHost();
|
||||
var program = new AquariumStreamingDspProgram(
|
||||
"six-source-faust-fractional-delay",
|
||||
"mimir_alignment_block_smoke",
|
||||
source,
|
||||
Revision: 1);
|
||||
|
||||
if (!host.UpsertProgram(program))
|
||||
{
|
||||
if (host.LastError?.Contains("Faust toolchain not found", StringComparison.OrdinalIgnoreCase) == true ||
|
||||
host.LastError?.Contains("Faust DLL not found", StringComparison.OrdinalIgnoreCase) == true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.Fail($"Streaming DSP compile failed: {host.LastError}");
|
||||
}
|
||||
|
||||
Assert.True(host.ApplyControls(new AquariumAudioControlFrame(
|
||||
program.ProfileId,
|
||||
"loopback-scarlett-speakers",
|
||||
0.0,
|
||||
[
|
||||
new AquariumAudioControlCommand(
|
||||
"scarlett-host-mic",
|
||||
0.0,
|
||||
1.0,
|
||||
1.0,
|
||||
new Dictionary<string, float>
|
||||
{
|
||||
["source0/gain"] = 0.5f
|
||||
})
|
||||
],
|
||||
TruncatedSourceCount: 0,
|
||||
Sequence: 1)));
|
||||
|
||||
Assert.True(host.ProcessBlock(new AquariumStreamingAudioBlock(
|
||||
program.ProfileId,
|
||||
[
|
||||
new AquariumStreamingAudioChannel(0, "scarlett-host-mic", Enumerable.Repeat(0.25f, 128).ToArray())
|
||||
],
|
||||
FrameCount: 128,
|
||||
SampleRate: 48_000,
|
||||
Sequence: 2), out var outputs));
|
||||
Assert.All(outputs[0], sample => Assert.InRange(sample, 0.124f, 0.126f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AquaSynthPatchCompilerCanRenderAudiblePatchForEnginePlayback()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue