Add Faust WebAssembly compile lane
This commit is contained in:
parent
90649b3de2
commit
d7b49d7248
4 changed files with 130 additions and 1 deletions
13
README.md
13
README.md
|
|
@ -63,6 +63,9 @@ published packages.
|
|||
from `AquaSynth.Faust`.
|
||||
- `FaustCompiler.CompileAsync(source, options)` writes generated backend code
|
||||
through an installed or resolved Faust compiler from `AquaSynth.Faust`.
|
||||
- `FaustCompiler.CompileWebAssemblyScriptAsync(...)` and
|
||||
`CompileWebAssemblyAsync(...)` emit browser-ready `.dsp`, `.wasm`, and Faust
|
||||
JSON UI metadata artifacts.
|
||||
- `AquaSynthPatchCompiler` from `AquaSynth.Faust` is the live host-facing
|
||||
boundary for compiling `.aqua` scripts into hosted DSP factories.
|
||||
- `AquaSynthNativeCompiler` remains the lower native `libfaust` compiler
|
||||
|
|
@ -92,6 +95,16 @@ await FaustCompiler.CompileAsync(
|
|||
new FaustCompileOptions(FaustTargetLanguage.CSharp, "Generated/Bass.cs"));
|
||||
```
|
||||
|
||||
The browser lane uses Faust's WebAssembly backend and keeps the same ownership:
|
||||
AquaSynth emits the `.dsp`, `.wasm`, and JSON UI description; the host supplies
|
||||
WebAudio scheduling and parameter binding.
|
||||
|
||||
```csharp
|
||||
await FaustCompiler.CompileWebAssemblyScriptAsync(
|
||||
"v w=sin f=880 g=.12 sustain=.04 decay=.2",
|
||||
new FaustWebAssemblyCompileOptions("Generated/Web", "ui_select"));
|
||||
```
|
||||
|
||||
The runtime lane is different. AquaSynth should own Faust toolchain selection,
|
||||
compile cache identity, generated artifacts, parameter manifests, and dynamic
|
||||
patch compilation outside the realtime path. Aquarium Engine should load and
|
||||
|
|
|
|||
|
|
@ -66,6 +66,9 @@ ordinary patch controls move through the compiled DSP parameter API.
|
|||
|
||||
- Authoring/tests may use Faust-generated C# because it is easy to render,
|
||||
inspect, and compare inside the .NET test suite.
|
||||
- Browser-hosted tools should use Faust-generated WebAssembly plus Faust JSON UI
|
||||
metadata. AquaSynth owns those artifacts; the web app owns WebAudio loading,
|
||||
scheduling, user gesture unlocking, and parameter binding.
|
||||
- Shipping builds should prefer Faust-generated native targets when the platform
|
||||
permits it.
|
||||
- A bundled Faust compiler belongs in an AquaSynth toolchain package or
|
||||
|
|
|
|||
|
|
@ -10,13 +10,31 @@ public enum FaustTargetLanguage
|
|||
C,
|
||||
Cpp,
|
||||
CSharp,
|
||||
Rust
|
||||
Rust,
|
||||
WebAssembly,
|
||||
WebAssemblyText
|
||||
}
|
||||
|
||||
public sealed record FaustCompileOptions(FaustTargetLanguage Language, string OutputPath);
|
||||
|
||||
public sealed record FaustValidation(string Command, bool Success, int? StatusCode, string Stdout, string Stderr);
|
||||
|
||||
public sealed record FaustWebAssemblyCompileOptions(
|
||||
string OutputDirectory,
|
||||
string BaseName = "aquasynth_patch",
|
||||
bool Stereo = false);
|
||||
|
||||
public sealed record FaustWebAssemblyManifest(
|
||||
string Name,
|
||||
string DspPath,
|
||||
string WasmPath,
|
||||
string JsonPath,
|
||||
string FaustPath,
|
||||
bool Success,
|
||||
int? StatusCode,
|
||||
string Stdout,
|
||||
string Stderr);
|
||||
|
||||
public sealed record FaustRenderOptions(int SampleRate = 44100, float DurationSeconds = 1);
|
||||
|
||||
public sealed record FaustRender(float[] Samples, int SampleRate, string Command, string Stdout, string Stderr);
|
||||
|
|
@ -65,6 +83,64 @@ public static class FaustCompiler
|
|||
}
|
||||
}
|
||||
|
||||
public static async Task<FaustWebAssemblyManifest?> CompileWebAssemblyAsync(
|
||||
SynthPatch patch,
|
||||
FaustWebAssemblyCompileOptions options,
|
||||
string? faustPath = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var export = FaustEmitter.Emit(patch, new FaustExportOptions(options.BaseName, options.Stereo));
|
||||
return await CompileWebAssemblySourceAsync(export.Source, options, faustPath, cancellationToken);
|
||||
}
|
||||
|
||||
public static async Task<FaustWebAssemblyManifest?> CompileWebAssemblyScriptAsync(
|
||||
string script,
|
||||
FaustWebAssemblyCompileOptions options,
|
||||
string? faustPath = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var export = FaustEmitter.EmitScript(script, new FaustExportOptions(options.BaseName, options.Stereo));
|
||||
return await CompileWebAssemblySourceAsync(export.Source, options, faustPath, cancellationToken);
|
||||
}
|
||||
|
||||
public static async Task<FaustWebAssemblyManifest?> CompileWebAssemblySourceAsync(
|
||||
string source,
|
||||
FaustWebAssemblyCompileOptions options,
|
||||
string? faustPath = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
faustPath ??= FindFaust();
|
||||
if (faustPath is null) return null;
|
||||
|
||||
Directory.CreateDirectory(options.OutputDirectory);
|
||||
var baseName = SafeArtifactName(options.BaseName);
|
||||
var dspPath = Path.Combine(options.OutputDirectory, $"{baseName}.dsp");
|
||||
var wasmPath = Path.Combine(options.OutputDirectory, $"{baseName}.wasm");
|
||||
var jsonPath = Path.Combine(options.OutputDirectory, $"{baseName}.json");
|
||||
await File.WriteAllTextAsync(dspPath, source, cancellationToken);
|
||||
|
||||
var result = await RunAsync(
|
||||
faustPath,
|
||||
[
|
||||
"-lang", Language(FaustTargetLanguage.WebAssembly),
|
||||
"-json",
|
||||
"-o", wasmPath,
|
||||
dspPath
|
||||
],
|
||||
cancellationToken);
|
||||
|
||||
return new FaustWebAssemblyManifest(
|
||||
options.BaseName,
|
||||
dspPath,
|
||||
wasmPath,
|
||||
jsonPath,
|
||||
faustPath,
|
||||
result.ExitCode == 0 && File.Exists(wasmPath),
|
||||
result.ExitCode,
|
||||
result.Stdout,
|
||||
result.Stderr);
|
||||
}
|
||||
|
||||
public static async Task<FaustValidation?> ValidateAsync(
|
||||
string source,
|
||||
string? faustPath = null,
|
||||
|
|
@ -140,9 +216,18 @@ public static class FaustCompiler
|
|||
FaustTargetLanguage.Cpp => "cpp",
|
||||
FaustTargetLanguage.CSharp => "csharp",
|
||||
FaustTargetLanguage.Rust => "rust",
|
||||
FaustTargetLanguage.WebAssembly => "wasm",
|
||||
FaustTargetLanguage.WebAssemblyText => "wast",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(language), language, null)
|
||||
};
|
||||
|
||||
private static string SafeArtifactName(string value)
|
||||
{
|
||||
var sanitized = new string(value.Select(character =>
|
||||
char.IsAsciiLetterOrDigit(character) || character is '-' or '_' ? character : '_').ToArray());
|
||||
return string.IsNullOrWhiteSpace(sanitized) ? "aquasynth_patch" : sanitized;
|
||||
}
|
||||
|
||||
private static async Task<string?> FaustArchDirAsync(string faustPath, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await RunAsync(faustPath, ["-archdir"], cancellationToken);
|
||||
|
|
|
|||
|
|
@ -887,6 +887,34 @@ public sealed class PatchScriptTests
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FaustCompilerCanEmitWebAssemblyWhenInstalled()
|
||||
{
|
||||
var outputDir = Path.Combine(Path.GetTempPath(), $"aquasynth-wasm-{Guid.NewGuid():N}");
|
||||
try
|
||||
{
|
||||
var manifest = await FaustCompiler.CompileWebAssemblyScriptAsync(
|
||||
"v w=sin f=440 g=.2 s=.1 d=.2",
|
||||
new FaustWebAssemblyCompileOptions(outputDir, "ui_bloom"));
|
||||
|
||||
if (manifest is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.True(manifest.Success, manifest.Stderr);
|
||||
Assert.True(File.Exists(manifest.DspPath));
|
||||
Assert.True(File.Exists(manifest.WasmPath));
|
||||
Assert.True(File.Exists(manifest.JsonPath));
|
||||
Assert.True(new FileInfo(manifest.WasmPath).Length > 0);
|
||||
Assert.Contains("\"name\"", await File.ReadAllTextAsync(manifest.JsonPath));
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (Directory.Exists(outputDir)) Directory.Delete(outputDir, recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FaustCompilerRendersGeneratedSourceWhenInstalled()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue