Skip to content

armgit / nota

Public Imported from GitHub
0

Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.

(feat) Updated Grain presets and samples

Egor Khindikaynen41ae26d29cb3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 261a4ead..586e2603 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,15 @@ created, and the build-and-publish run starts.
Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.
### Added
+- **Nota Grain has 50 factory presets, and each one brings its own sound.** Until now every
+ Grain preset played the same built-in pad. Now a preset loads a source made for it: a
+ choir, bowed strings, a church bell, a kalimba, a vowel that moves from "a" to "u", a drum
+ break, an arpeggio, ocean waves, a radio between stations and more (29 in all). They're
+ in nine folders: Basics, Pads, Voices & Choirs, Bass, Keys & Plucks, Bells & Mallets,
+ Drones & Atmospheres, Loops & Sequences and Textures & FX. The sources are made on your
+ computer the first time a preset needs one, so they don't add to the download. The
+ Basics presets still keep whatever sample you dropped in. Previews in the browser play
+ each preset with its own source.
- Import MIDI files. `.mid` files now show up in the browser's Files tab, and you can drag
them from there or straight from Finder / Explorer. Drop one on the arrangement and it
becomes a MIDI clip at that spot. A file with several parts spreads out: the first part
diff --git a/FEATURES.md b/FEATURES.md
index 4c8dd9b8..98a7f60e 100644
--- a/FEATURES.md
+++ b/FEATURES.md
@@ -270,8 +270,10 @@ and user presets, automation, persistence and cloning.
Gauss / Tukey / Tri) sprayed around it, coarse/fine pitch, per-grain position, pitch and
pan variation, stereo spread, **Dry/Wet** against the sample itself, an LP/HP/BP filter
and an amp envelope. The card draws the sample with the live grain cloud on it (drag to
- move Position). Drop a sample from the browser to replace the built-in pad. 25 factory
- presets.
+ move Position). Drop a sample from the browser to replace the built-in pad. 50 factory
+ presets in nine folders; every preset outside Basics brings its own source (29 synthesized
+ on first use — choir, strings, bells, kalimba, a drum break, an arpeggio, ocean, radio …),
+ while Basics shape whatever sample is loaded.
- **Nota Volt** — subtractive synth, two paths: two oscillators and noise, each routed to
one of two filters (LP/HP/BP/Notch, 12/24 dB, with Filter 1 able to feed Filter 2) and
its own amp; amp and filter envelopes, two LFOs (shapes, depth, fade-in, tempo sync), a
diff --git a/src/managed/Nota.Application/Engine/IAuditionRig.cs b/src/managed/Nota.Application/Engine/IAuditionRig.cs
index 6b9db4b3..4522aea0 100644
--- a/src/managed/Nota.Application/Engine/IAuditionRig.cs
+++ b/src/managed/Nota.Application/Engine/IAuditionRig.cs
@@ -23,8 +23,9 @@ public interface IAuditionRig : IDisposable
/// <summary>Appends a built-in MIDI effect; its index, or -1.</summary>
int AddMidiEffect(int kind);
bool MidiParam(int index, string name, float value);
- /// <summary>The Sampler's sample; null = a procedural keys tone at C4.</summary>
- bool SetSamplerSample(string? path);
+ /// <summary>The Sampler's or Nota Grain's sample, played at its own pitch on
+ /// <paramref name="rootNote"/>; null = the Sampler's procedural keys tone at C4.</summary>
+ bool SetSamplerSample(string? path, int rootNote = 60);
/// <summary>Effect source: the first <paramref name="maxSeconds"/> of an audio file.</summary>
bool SetSourceFile(string path, double maxSeconds);
/// <summary>A fresh rig at the same rate, to render one part of a demo track.</summary>
diff --git a/src/managed/Nota.Infrastructure/Audition/PresetAudition.cs b/src/managed/Nota.Infrastructure/Audition/PresetAudition.cs
index 3c9b45fa..4babe17c 100644
--- a/src/managed/Nota.Infrastructure/Audition/PresetAudition.cs
+++ b/src/managed/Nota.Infrastructure/Audition/PresetAudition.cs
@@ -95,6 +95,13 @@ public sealed partial class PresetAudition : IPresetAudition
if (!rig.SetInstrument(kind)) return false;
foreach (var (id, v) in named)
if (!InstrumentView.IsViewParam(id)) rig.InstrumentParam(id, v);
+ // A Nota Grain factory preset is heard on its own source.
+ if (kind == 10 && !string.IsNullOrEmpty(doc.GrainSource))
+ {
+ var src = Grain.GrainSources.ById(doc.GrainSource);
+ var path = Grain.GrainSourceLibrary.Ensure(doc.GrainSource);
+ if (src is null || path is null || !rig.SetSamplerSample(path, src.Root)) return false;
+ }
return true;
},
phrase.Notes, Bpm, phrase.Beats, phrase.Tail, phrase.Rolling);
diff --git a/src/managed/Nota.Infrastructure/Grain/GrainSourceLibrary.cs b/src/managed/Nota.Infrastructure/Grain/GrainSourceLibrary.cs
new file mode 100644
index 00000000..0751856b
--- /dev/null
+++ b/src/managed/Nota.Infrastructure/Grain/GrainSourceLibrary.cs
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.
+//
+// Materializes Nota Grain's factory sources on disk, the way KitLibrary does the drum kits:
+// a source is rendered the first time something asks for it (a preset applied, a preview)
+// and after its recipe changes, and read from disk every other time. Each file has a stamp
+// beside it holding the renderer version and the source's revision.
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using Nota.Infrastructure.Kits;
+
+namespace Nota.Infrastructure.Grain;
+
+public static class GrainSourceLibrary
+{
+ /// <summary>Bump when the shared rendering (levelling, format) changes; a single
+ /// source's recipe edit bumps its own <see cref="GrainSource.Rev"/>.</summary>
+ private const int RendererVersion = 1;
+
+ /// <summary>The loudness every source is levelled to: its loud passages sit at the
+ /// built-in pad's RMS, so a preset sounds as loud whichever source it plays.</summary>
+ private const double TargetRms = 0.14;
+ private const double PeakCeiling = 0.89; // −1 dBFS
+
+ private static readonly object Gate = new();
+
+ /// <summary>A managed folder under the Nota data dir — regenerable, so not the user's samples.</summary>
+ public static string Root => NotaPaths.SubDir("grain-sources");
+
+ public static string PathOf(GrainSource s) => Path.Combine(Root, Sanitize(s.Name) + ".wav");
+ private static string StampOf(GrainSource s) => Path.Combine(Root, "." + s.Id + ".stamp");
+ private static string StampFor(GrainSource s) => $"v{RendererVersion}-{s.Id}-r{s.Rev}-{s.Root}-{s.Seconds}-{s.GainDb}";
+
+ public static bool IsRendered(GrainSource s)
+ {
+ try { return File.Exists(PathOf(s)) && File.Exists(StampOf(s)) && File.ReadAllText(StampOf(s)).Trim() == StampFor(s); }
+ catch { return false; }
+ }
+
+ /// <summary>The source's WAV path, rendering it first if it is missing or stale; null for
+ /// an unknown id or a failed render.</summary>
+ public static string? Ensure(string id)
+ {
+ var s = GrainSources.ById(id);
+ if (s is null) return null;
+ lock (Gate)
+ {
+ if (IsRendered(s)) return PathOf(s);
+ try
+ {
+ var path = PathOf(s);
+ var data = Render(s);
+ var tmp = path + ".part";
+ using (var w = new WavWriter(tmp, SourceBuffer.Rate, 2, WavBitDepth.Pcm24))
+ w.WriteFrames(data, data.Length / 2);
+ File.Move(tmp, path, overwrite: true); // never leave a half-written sample behind
+ File.WriteAllText(StampOf(s), StampFor(s));
+ return path;
+ }
+ catch { return null; }
+ }
+ }
+
+ /// <summary>Renders every source that is missing or stale; returns how many were written.</summary>
+ public static int EnsureAll()
+ {
+ int n = 0;
+ foreach (var s in GrainSources.All)
+ {
+ bool was = IsRendered(s);
+ if (Ensure(s.Id) is not null && !was) n++;
+ }
+ return n;
+ }
+
+ /// <summary>Synthesizes the source and levels it; interleaved stereo frames.</summary>
+ public static float[] Render(GrainSource s)
+ {
+ var b = new SourceBuffer(s.Seconds, Fnv(s.Id));
+ s.Render(b);
+ double gain = LevelGain(b.L, b.R, KitDsp.Db(s.GainDb));
+ var inter = new float[b.N * 2];
+ for (int i = 0; i < b.N; i++)
+ {
+ inter[2 * i] = (float)(b.L[i] * gain);
+ inter[2 * i + 1] = (float)(b.R[i] * gain);
+ }
+ return inter;
+ }
+
+ // Loudness by the loud passages (the 85th-percentile 50 ms window) rather than the
+ // whole file, so a decaying pluck is levelled by its note, not by its silence.
+ private static double LevelGain(double[] l, double[] r, double extra)
+ {
+ int win = SourceBuffer.Rate / 20;
+ var rms = new List<double>();
+ double peak = 0;
+ for (int s = 0; s + win <= l.Length; s += win)
+ {
+ double acc = 0;
+ for (int i = s; i < s + win; i++)
+ {
+ acc += (l[i] * l[i] + r[i] * r[i]) * 0.5;
+ peak = Math.Max(peak, Math.Max(Math.Abs(l[i]), Math.Abs(r[i])));
+ }
+ rms.Add(Math.Sqrt(acc / win));
+ }
+ if (rms.Count == 0 || peak < 1e-9) return 1;
+ rms.Sort();
+ double loud = rms[(int)(rms.Count * 0.85)];
+ double g = loud > 1e-9 ? TargetRms * extra / loud : 1;
+ return Math.Min(g, PeakCeiling / peak);
+ }
+
+ private static uint Fnv(string s)
+ {
+ uint h = 2166136261;
+ foreach (char c in s) { h ^= c; h *= 16777619; }
+ return h;
+ }
+
+ private static string Sanitize(string name)
+ {
+ var sb = new StringBuilder(name.Length);
+ foreach (char c in name) sb.Append(Path.GetInvalidFileNameChars().AsSpan().Contains(c) ? '-' : c);
+ return sb.ToString();
+ }
+}
diff --git a/src/managed/Nota.Infrastructure/Grain/GrainSources.cs b/src/managed/Nota.Infrastructure/Grain/GrainSources.cs
new file mode 100644
index 00000000..d4302df4
--- /dev/null
+++ b/src/managed/Nota.Infrastructure/Grain/GrainSources.cs
@@ -0,0 +1,919 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.
+//
+// The source samples Nota Grain's factory presets granulate. Like the drum kits they ship
+// as recipes, not audio: each source is a small synthesis routine that GrainSourceLibrary
+// renders to a WAV in the data folder the first time a preset (or its preview) needs it.
+//
+// A granular patch is mostly its source, so the set covers the ground a preset library
+// needs: sustained tones to freeze (ensembles, choirs, strings, organ), struck and plucked
+// bodies whose attack or ring can be picked out (piano tine, nylon, kalimba, marimba,
+// bells), material that changes along the file for Scan and Key (a vowel morph, a harp
+// run, a music-box tune, a drum break, an arpeggio) and noises (ocean, rain, radio).
+//
+// Every source is rendered at 48 kHz and levelled to the same loudness as the built-in
+// pad (Level), so presets move between sources without jumps. A pitched source sits on
+// its Root note: playing that key plays the recording at its own pitch.
+
+using System;
+using System.Collections.Generic;
+using Nota.Infrastructure.Kits;
+
+namespace Nota.Infrastructure.Grain;
+
+/// <summary>A factory grain source. <see cref="Rev"/> is bumped whenever its recipe
+/// changes, so the rendered file is refreshed.</summary>
+public sealed record GrainSource(string Id, string Name, int Root, double Seconds, int Rev, string Blurb)
+{
+ internal Action<SourceBuffer> Render { get; init; } = _ => { };
+
+ /// <summary>Level above the shared loudness, in dB (the basses: one note has to carry
+ /// what a pad's chord does). The −1 dBFS ceiling still applies.</summary>
+ public double GainDb { get; init; }
+}
+
+/// <summary>A stereo render target for one source.</summary>
+internal sealed class SourceBuffer
+{
+ public const int Rate = 48000;
+ public readonly double[] L, R;
+ public readonly int N;
+ public readonly Rng Rng;
+
+ public SourceBuffer(double seconds, uint seed)
+ {
+ N = Math.Max(1, (int)(seconds * Rate));
+ L = new double[N]; R = new double[N];
+ Rng = new Rng(seed);
+ }
+
+ public static double T(int i) => i / (double)Rate;
+ public int At(double seconds) => Math.Clamp((int)(seconds * Rate), 0, N);
+}
+
+public static class GrainSources
+{
+ private static IReadOnlyList<GrainSource>? _all;
+ private static Dictionary<string, GrainSource>? _byId;
+
+ /// <summary>Every shipped source, in catalog order.</summary>
+ public static IReadOnlyList<GrainSource> All => _all ??= Build();
+
+ public static GrainSource? ById(string id)
+ {
+ _byId ??= BuildIndex();
+ return _byId.TryGetValue(id, out var s) ? s : null;
+ }
+
+ private static Dictionary<string, GrainSource> BuildIndex()
+ {
+ var d = new Dictionary<string, GrainSource>(StringComparer.Ordinal);
+ foreach (var s in All) d[s.Id] = s;
+ return d;
+ }
+
+ private const double Tau = 2 * Math.PI;
+ private static double Hz(double midi) => 440.0 * Math.Pow(2, (midi - 69) / 12.0);
+ private static double Cents(double c) => Math.Pow(2, c / 1200.0);
+
+ private static List<GrainSource> Build() => new()
+ {
+ // ---- sustained tones -----------------------------------------------------------
+ new("saw-ensemble", "Saw Ensemble", 48, 6, 1, "seven detuned saws under a slowly breathing filter") { Render = SawEnsemble },
+ new("glass-pad", "Glass Partials", 60, 6, 1, "shimmering sine partials that beat against each other") { Render = GlassPad },
+ new("choir", "Choir Aah", 60, 5, 1, "a small ensemble singing 'aah'") { Render = Choir },
+ new("vowels", "Vowel Morph", 48, 6, 1, "one voice moving a · e · i · o · u") { Render = Vowels },
+ new("whisper", "Whisper", 60, 5, 1, "breath through moving vowel formants, unpitched") { Render = Whisper },
+ new("strings", "Bowed Strings", 48, 6, 1, "a string section, bow noise and vibrato") { Render = Strings },
+ new("flute", "Breath Flute", 72, 4, 1, "an airy flute tone with a late vibrato") { Render = Flute },
+ new("brass", "Brass Swell", 48, 4, 1, "a brass section swelling open") { Render = Brass },
+ new("organ", "Drawbar Organ", 48, 4, 1, "drawbars through a rotating speaker") { Render = Organ },
+ new("tape-chord", "Tape Chord", 48, 6, 1, "a soft open chord with tape wow, flutter and hiss") { Render = TapeChord },
+ // ---- bass -----------------------------------------------------------------------
+ new("sub", "Sub Sine", 36, 3, 1, "a round, slightly driven sub") { Render = Sub, GainDb = 8 },
+ new("saw-bass", "Saw Bass", 36, 3, 1, "a plucky filtered saw bass") { Render = SawBass, GainDb = 8 },
+ new("fm-growl", "FM Growl", 36, 4, 1, "a wobbling FM bass") { Render = FmGrowl, GainDb = 8 },
+ // ---- struck & plucked ------------------------------------------------------------
+ new("epiano", "Tine Piano", 60, 4, 1, "an electric piano tine with tremolo") { Render = EPiano },
+ new("nylon", "Nylon Pluck", 48, 3, 1, "a plucked nylon string") { Render = Nylon },
+ new("harp-run", "Harp Run", 60, 5, 1, "a two-octave pentatonic harp run") { Render = HarpRun },
+ new("kalimba", "Kalimba", 72, 3, 1, "a thumb-piano tine") { Render = Kalimba },
+ new("marimba", "Marimba", 60, 2.5, 1, "a soft-mallet marimba bar") { Render = Marimba },
+ new("bell", "Church Bell", 60, 6, 1, "a large bell with its hum and tierce") { Render = Bell },
+ new("music-box", "Music Box Tune", 72, 6, 1, "a little music-box melody") { Render = MusicBox },
+ new("chimes", "Wind Chimes", 84, 6, 1, "pentatonic chimes in the breeze") { Render = Chimes },
+ // ---- textures ---------------------------------------------------------------------
+ new("metal", "Bowed Metal", 48, 6, 1, "a bowed metal plate, inharmonic") { Render = Metal },
+ new("drone", "Dark Drone", 36, 8, 1, "a low saw drone under a slow filter") { Render = Drone },
+ new("ocean", "Ocean Air", 60, 8, 1, "waves of filtered noise, unpitched") { Render = Ocean },
+ new("rain", "Rain & Crackle", 60, 6, 1, "droplets and vinyl crackle, unpitched") { Render = Rain },
+ new("radio", "Shortwave", 60, 6, 1, "a radio between stations") { Render = Radio },
+ // ---- rhythm & sequences -------------------------------------------------------------
+ new("drums", "Break Loop", 60, 4, 1, "a two-bar breakbeat at 120 BPM") { Render = Drums },
+ new("arp", "Synth Arp", 48, 4, 1, "a 16th-note minor arpeggio at 120 BPM") { Render = Arp },
+ new("bits", "Digital Bits", 60, 4, 1, "crushed square blips and noise bursts") { Render = Bits },
+ };
+
+ // ======================================================================
+ // Oscillators and filters
+ // ======================================================================
+
+ // PolyBLEP residual: removes most of the aliasing from a naive saw / pulse edge.
+ private static double Blep(double t, double dt)
+ {
+ if (t < dt) { t /= dt; return t + t - t * t - 1; }
+ if (t > 1 - dt) { t = (t - 1) / dt; return t * t + t + t + 1; }
+ return 0;
+ }
+
+ private static double Saw(ref double p, double f)
+ {
+ double dt = f / SourceBuffer.Rate;
+ double v = 2 * p - 1 - Blep(p, dt);
+ p += dt; if (p >= 1) p -= 1;
+ return v;
+ }
+
+ private static double Pulse(ref double p, double f, double pw)
+ {
+ double dt = f / SourceBuffer.Rate;
+ double q = p - pw; if (q < 0) q += 1;
+ double v = (p < pw ? 1 : -1) + Blep(p, dt) - Blep(q, dt);
+ p += dt; if (p >= 1) p -= 1;
+ return v;
+ }
+
+ private static double Sine(ref double p, double f)
+ {
+ double v = Math.Sin(Tau * p);
+ p += f / SourceBuffer.Rate; if (p >= 1) p -= 1;
+ return v;
+ }
+
+ /// <summary>A zero-delay-feedback state-variable filter with a per-sample cutoff.</summary>
+ private sealed class Svf
+ {
+ private double _s1, _s2;
+ public double Lp, Bp, Hp;
+
+ public void Run(double x, double fc, double q)
+ {
+ double g = Math.Tan(Math.PI * Math.Clamp(fc, 10, SourceBuffer.Rate * 0.45) / SourceBuffer.Rate);
+ double k = 1 / Math.Max(0.1, q);
+ double a1 = 1 / (1 + g * (g + k));
+ double v3 = x - _s2;
+ double v1 = a1 * _s1 + a1 * g * v3;
+ double v2 = _s2 + g * v1;
+ _s1 = 2 * v1 - _s1; _s2 = 2 * v2 - _s2;
+ Lp = v2; Bp = v1; Hp = x - k * v1 - v2;
+ }
+ }
+
+ // Vowel formants (F1..F3 Hz) and their levels.
+ private static readonly double[][] VowelF =
+ {
+ new[] { 800.0, 1150, 2900 }, // a
+ new[] { 400.0, 2000, 2700 }, // e
+ new[] { 300.0, 2250, 3000 }, // i
+ new[] { 450.0, 800, 2830 }, // o
+ new[] { 330.0, 700, 2500 }, // u
+ };
+ private static readonly double[] FormantGain = { 1.0, 0.55, 0.3 };
+
+ /// <summary>Three band-passes tuned to a vowel; <c>v</c> morphs 0 (a) … 4 (u).</summary>
+ private sealed class Formants
+ {
+ private readonly Svf[] _f = { new(), new(), new() };
+
+ public double Run(double x, double v)
+ {
+ int i0 = Math.Clamp((int)Math.Floor(v), 0, 4), i1 = Math.Min(4, i0 + 1);
+ double fr = Math.Clamp(v - i0, 0, 1);
+ double y = 0;
+ for (int k = 0; k < 3; k++)
+ {
+ double f = VowelF[i0][k] + (VowelF[i1][k] - VowelF[i0][k]) * fr;
+ _f[k].Run(x, f, f / (60 + 30 * k));
+ y += _f[k].Bp * FormantGain[k];
+ }
+ return y;
+ }
+ }
+
+ /// <summary>A simple ping-pong delay for width.</summary>
+ private static void PingPong(SourceBuffer b, double seconds, double fb, double mix)
+ {
+ int d = Math.Max(1, (int)(seconds * SourceBuffer.Rate));
+ var dl = new double[d]; var dr = new double[d];
+ int p = 0;
+ for (int i = 0; i < b.N; i++)
+ {
+ double ol = dl[p], or = dr[p];
+ dl[p] = (b.L[i] + b.R[i]) * 0.5 + or * fb;
+ dr[p] = ol * fb;
+ if (++p >= d) p = 0;
+ b.L[i] += ol * mix; b.R[i] += or * mix;
+ }
+ }
+
+ /// <summary>Adds the shared small room (the drum kits' Room) as a stereo send.</summary>
+ private static void Space(SourceBuffer b, double send, double size, double decay, double damp = 0.5)
+ {
+ var room = new Room(size, decay, damp, SourceBuffer.Rate);
+ var hp = Biquad.HighPass(150, 0.7071, SourceBuffer.Rate);
+ for (int i = 0; i < b.N; i++)
+ {
+ room.Process(hp.Process((b.L[i] + b.R[i]) * 0.5) * send, out double wl, out double wr);
+ b.L[i] += wl; b.R[i] += wr;
+ }
+ }
+
+ // Fades both ends so a Scan wrapping through the file never clicks.
+ private static void Edges(SourceBuffer b, double fadeIn, double fadeOut)
+ {
+ int a = b.At(fadeIn), z = b.At(fadeOut);
+ for (int i = 0; i < a; i++) { double g = 0.5 - 0.5 * Math.Cos(Math.PI * i / a); b.L[i] *= g; b.R[i] *= g; }
+ for (int i = 0; i < z; i++) { double g = 0.5 - 0.5 * Math.Cos(Math.PI * i / z); b.L[b.N - 1 - i] *= g; b.R[b.N - 1 - i] *= g; }
+ }
+
+ // Soft-limits peaks to <paramref name="crestDb"/> above the file's RMS: clicks and droplets
+ // would otherwise hold the whole source down under the ceiling.
+ private static void Tame(SourceBuffer b, double crestDb)
+ {
+ double acc = 0;
+ for (int i = 0; i < b.N; i++) acc += (b.L[i] * b.L[i] + b.R[i] * b.R[i]) * 0.5;
+ double th = Math.Sqrt(acc / b.N) * KitDsp.Db(crestDb);
+ if (th <= 1e-9) return;
+ for (int i = 0; i < b.N; i++) { b.L[i] = th * Math.Tanh(b.L[i] / th); b.R[i] = th * Math.Tanh(b.R[i] / th); }
+ }
+
+ private static double Smooth(double t, double a) => t >= a ? 1 : 0.5 - 0.5 * Math.Cos(Math.PI * t / a);
+
+ // ======================================================================
+ // Sustained tones
+ // ======================================================================
+
+ private static void SawEnsemble(SourceBuffer b)
+ {
+ double f0 = Hz(48);
+ double[] det = { -14, -9, -4, 0, 4, 9, 14 };
+ var ph = new double[det.Length];
+ for (int k = 0; k < ph.Length; k++) ph[k] = b.Rng.Next01();
+ var fl = new Svf(); var fr = new Svf();
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i), l = 0, r = 0;
+ for (int k = 0; k < det.Length; k++)
+ {
+ double drift = 1 + 0.0015 * Math.Sin(Tau * (0.13 + 0.05 * k) * t + k);
+ double v = Saw(ref ph[k], f0 * Cents(det[k]) * drift);
+ double pan = (k - 3) / 3.0;
+ l += v * (1 - pan * 0.6); r += v * (1 + pan * 0.6);
+ }
+ double cut = 700 + 1500 * (0.5 - 0.5 * Math.Cos(Tau * t / 6.0)) + 150 * Math.Sin(Tau * 0.7 * t);
+ fl.Run(l, cut, 0.9); fr.Run(r, cut * 1.03, 0.9);
+ b.L[i] = fl.Lp * 0.12; b.R[i] = fr.Lp * 0.12;
+ }
+ Space(b, 0.35, 0.7, 0.6);
+ Edges(b, 0.25, 0.4);
+ }
+
+ private static void GlassPad(SourceBuffer b)
+ {
+ double f0 = Hz(60);
+ double[] ratio = { 1, 2, 3, 4, 5, 6, 8, 2.002, 4.003 };
+ double[] amp = { 1, 0.5, 0.28, 0.32, 0.12, 0.1, 0.08, 0.35, 0.18 };
+ var pl = new double[ratio.Length]; var pr = new double[ratio.Length];
+ var lfo = new double[ratio.Length];
+ for (int k = 0; k < ratio.Length; k++) { lfo[k] = b.Rng.Next01() * Tau; pl[k] = b.Rng.Next01(); pr[k] = b.Rng.Next01(); }
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i), l = 0, r = 0;
+ for (int k = 0; k < ratio.Length; k++)
+ {
+ // Each partial swells on its own, differently left and right.
+ double al = amp[k] * (0.65 + 0.35 * Math.Sin(Tau * (0.11 + 0.07 * k) * t + lfo[k]));
+ double ar = amp[k] * (0.65 + 0.35 * Math.Cos(Tau * (0.09 + 0.05 * k) * t + lfo[k]));
+ l += al * Sine(ref pl[k], f0 * ratio[k]);
+ r += ar * Sine(ref pr[k], f0 * ratio[k] * 1.0007);
+ }
+ b.L[i] = l * 0.2; b.R[i] = r * 0.2;
+ }
+ Space(b, 0.4, 0.8, 0.7, 0.3);
+ Edges(b, 0.3, 0.4);
+ }
+
+ private static void Choir(SourceBuffer b)
+ {
+ const int voices = 6;
+ double f0 = Hz(60);
+ var ph = new double[voices]; var det = new double[voices]; var vib = new double[voices];
+ for (int k = 0; k < voices; k++) { ph[k] = b.Rng.Next01(); det[k] = b.Rng.Next() * 12; vib[k] = b.Rng.Next01() * Tau; }
+ var fL = new Formants(); var fR = new Formants();
+ var breath = new Svf();
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i), l = 0, r = 0;
+ double vibDepth = 18 * Smooth(t, 0.8);
+ for (int k = 0; k < voices; k++)
+ {
+ double f = f0 * Cents(det[k] + vibDepth * Math.Sin(Tau * (4.8 + 0.35 * k) * t + vib[k]));
+ double v = Pulse(ref ph[k], f, 0.28 + 0.03 * k);
+ if (k % 2 == 0) l += v; else r += v;
+ }
+ breath.Run(b.Rng.Next(), 3000, 0.7);
+ double n = breath.Bp * 0.25;
+ b.L[i] = fL.Run(l + n, 0.15) * 0.35; b.R[i] = fR.Run(r + n, 0.2) * 0.35;
+ }
+ Space(b, 0.45, 0.75, 0.65);
+ Edges(b, 0.35, 0.4);
+ }
+
+ private static void Vowels(SourceBuffer b)
+ {
+ double f0 = Hz(48), p1 = 0, p2 = 0.3;
+ var fl = new Formants(); var fr = new Formants();
+ double len = SourceBuffer.T(b.N);
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ double f = f0 * Cents(14 * Smooth(t, 0.6) * Math.Sin(Tau * 5.1 * t));
+ double x = Saw(ref p1, f) * 0.7 + Saw(ref p2, f * Cents(6)) * 0.3;
+ double v = 4.0 * Math.Clamp((t - 0.25) / (len - 0.5), 0, 1);
+ b.L[i] = fl.Run(x, v) * 0.5; b.R[i] = fr.Run(x, Math.Min(4, v + 0.05)) * 0.5;
+ }
+ Space(b, 0.25, 0.5, 0.4);
+ Edges(b, 0.12, 0.3);
+ }
+
+ private static void Whisper(SourceBuffer b)
+ {
+ var fl = new Formants(); var fr = new Formants();
+ double len = SourceBuffer.T(b.N);
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ double v = 2 + 2 * Math.Sin(Tau * t / len * 1.5 - 1.2);
+ double syll = 0.55 + 0.45 * Math.Pow(Math.Abs(Math.Sin(Math.PI * 2.2 * t)), 0.6);
+ b.L[i] = fl.Run(b.Rng.Next(), v) * syll * 0.9;
+ b.R[i] = fr.Run(b.Rng.Next(), 4 - v) * syll * 0.9;
+ }
+ Space(b, 0.3, 0.5, 0.5);
+ Edges(b, 0.2, 0.3);
+ }
+
+ private static void Strings(SourceBuffer b)
+ {
+ double f0 = Hz(48);
+ const int n = 8;
+ var ph = new double[n]; var det = new double[n]; var vr = new double[n];
+ for (int k = 0; k < n; k++) { ph[k] = b.Rng.Next01(); det[k] = b.Rng.Next() * 10; vr[k] = b.Rng.Next01() * Tau; }
+ var bodyL = new[] { Biquad.Peaking(280, 1.2, 5, SourceBuffer.Rate), Biquad.Peaking(1100, 1.5, 3, SourceBuffer.Rate), Biquad.Peaking(2800, 2, 2, SourceBuffer.Rate), Biquad.LowPass(5200, 0.7, SourceBuffer.Rate) };
+ var bodyR = new[] { Biquad.Peaking(300, 1.2, 5, SourceBuffer.Rate), Biquad.Peaking(1180, 1.5, 3, SourceBuffer.Rate), Biquad.Peaking(2650, 2, 2, SourceBuffer.Rate), Biquad.LowPass(5000, 0.7, SourceBuffer.Rate) };
+ var bow = new Svf();
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i), l = 0, r = 0;
+ double swell = Smooth(t, 0.6);
+ for (int k = 0; k < n; k++)
+ {
+ double f = f0 * (k >= 6 ? 2 : 1) * Cents(det[k] + 11 * Math.Sin(Tau * (5.2 + 0.3 * k) * t + vr[k]));
+ double v = Saw(ref ph[k], f) * (k >= 6 ? 0.45 : 1);
+ if (k % 2 == 0) l += v; else r += v;
+ }
+ bow.Run(b.Rng.Next(), 3200, 1.2);
+ double noise = bow.Bp * 0.18 * (0.8 + 0.2 * Math.Sin(Tau * 1.3 * t));
+ double xl = l + noise, xr = r + noise;
+ foreach (var q in bodyL) xl = q.Process(xl);
+ foreach (var q in bodyR) xr = q.Process(xr);
+ b.L[i] = xl * swell * 0.08; b.R[i] = xr * swell * 0.08;
+ }
+ Space(b, 0.4, 0.75, 0.55);
+ Edges(b, 0.05, 0.4);
+ }
+
+ private static void Flute(SourceBuffer b)
+ {
+ double f0 = Hz(72), p1 = 0, p2 = 0, p3 = 0;
+ var breath = new Svf(); var air = new Svf();
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ double vib = 16 * Smooth(Math.Max(0, t - 0.6), 0.8) * Math.Sin(Tau * 5 * t);
+ double f = f0 * Cents(vib);
+ double env = Smooth(t, 0.12) * (0.9 + 0.1 * Math.Sin(Tau * 0.4 * t));
+ double tone = Sine(ref p1, f) + 0.25 * Sine(ref p2, 2 * f) + 0.06 * Sine(ref p3, 3 * f);
+ breath.Run(b.Rng.Next(), f * 2, 2.5);
+ air.Run(b.Rng.Next(), 6000, 0.7);
+ double chiff = Math.Exp(-t / 0.05) * 0.6;
+ double x = env * (tone * 0.5 + breath.Bp * (0.25 + chiff)) + air.Hp * 0.03 * env;
+ b.L[i] = x * 0.6; b.R[i] = x * 0.6;
+ }
+ Space(b, 0.35, 0.6, 0.55);
+ Edges(b, 0.005, 0.3);
+ }
+
+ private static void Brass(SourceBuffer b)
+ {
+ double f0 = Hz(48);
+ double[] det = { -7, -2, 3, 8, 1 };
+ var ph = new double[det.Length];
+ for (int k = 0; k < ph.Length; k++) ph[k] = b.Rng.Next01();
+ var fl = new Svf(); var fr = new Svf();
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i), l = 0, r = 0;
+ double swell = Smooth(t, 0.7);
+ double vib = 9 * Smooth(Math.Max(0, t - 1.2), 1) * Math.Sin(Tau * 5.4 * t);
+ for (int k = 0; k < det.Length; k++)
+ {
+ double v = Saw(ref ph[k], f0 * (k == 4 ? 2 : 1) * Cents(det[k] + vib)) * (k == 4 ? 0.4 : 1);
+ if (k % 2 == 0) l += v; else r += v;
+ }
+ double cut = 300 + 2600 * swell * swell + 300 * Math.Sin(Tau * 0.5 * t);
+ fl.Run(l, cut, 1.1); fr.Run(r, cut * 1.02, 1.1);
+ b.L[i] = KitDsp.Saturate(fl.Lp * 0.2 * (0.2 + 0.8 * swell), 0.25, 0.6);
+ b.R[i] = KitDsp.Saturate(fr.Lp * 0.2 * (0.2 + 0.8 * swell), 0.25, 0.6);
+ }
+ Space(b, 0.3, 0.6, 0.5);
+ Edges(b, 0.01, 0.3);
+ }
+
+ private static void Organ(SourceBuffer b)
+ {
+ double f0 = Hz(48);
+ double[] ratio = { 0.5, 1, 1.5, 2, 3, 4, 6 };
+ double[] amp = { 0.25, 1, 0.55, 0.6, 0.35, 0.3, 0.12 }; // a light 16': the pitch stays on 8'
+ var ph = new double[ratio.Length];
+ var click = new Svf();
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i), x = 0;
+ for (int k = 0; k < ratio.Length; k++) x += amp[k] * Sine(ref ph[k], f0 * ratio[k]);
+ click.Run(b.Rng.Next(), 2500, 0.8);
+ x += click.Bp * Math.Exp(-t / 0.008) * 1.5;
+ // The rotor: horn and drum sweep the sound across the room at ~6 Hz.
+ double rot = Tau * (5.6 + 0.4 * Math.Sin(Tau * 0.2 * t)) * t;
+ double am = 0.22 * Math.Sin(rot);
+ b.L[i] = x * (0.78 + am) * 0.2; b.R[i] = x * (0.78 - am) * 0.2;
+ }
+ Space(b, 0.25, 0.45, 0.35);
+ Edges(b, 0.004, 0.3);
+ }
+
+ private static void TapeChord(SourceBuffer b)
+ {
+ int[] notes = { 48, 55, 64, 67 };
+ var pl = new double[notes.Length * 2]; var pr = new double[notes.Length * 2];
+ for (int k = 0; k < pl.Length; k++) { pl[k] = b.Rng.Next01(); pr[k] = b.Rng.Next01(); }
+ var lpL = Biquad.LowPass(3200, 0.6, SourceBuffer.Rate); var lpR = Biquad.LowPass(3000, 0.6, SourceBuffer.Rate);
+ var hiss = Biquad.HighPass(3000, 0.7, SourceBuffer.Rate);
+ double w = b.Rng.Next01() * Tau;
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ double wow = Cents(11 * Math.Sin(Tau * 0.55 * t + w) + 3 * Math.Sin(Tau * 7.3 * t));
+ double l = 0, r = 0;
+ for (int k = 0; k < notes.Length; k++)
+ {
+ double f = Hz(notes[k]) * wow, a = k == 0 ? 1 : 0.7;
+ l += a * (Tri(ref pl[2 * k], f) * 0.7 + Sine(ref pl[2 * k + 1], f * 2.001) * 0.2);
+ r += a * (Tri(ref pr[2 * k], f * Cents(4)) * 0.7 + Sine(ref pr[2 * k + 1], f * 1.999) * 0.2);
+ }
+ double h = hiss.Process(b.Rng.Next()) * 0.012;
+ b.L[i] = KitDsp.Saturate(lpL.Process(l * 0.22), 0.3, 0.8) + h;
+ b.R[i] = KitDsp.Saturate(lpR.Process(r * 0.22), 0.3, 0.8) + h;
+ }
+ Space(b, 0.25, 0.55, 0.6, 0.6);
+ Edges(b, 0.4, 0.5);
+ }
+
+ private static double Tri(ref double p, double f)
+ {
+ double v = 1 - 4 * Math.Abs(p - 0.5);
+ p += f / SourceBuffer.Rate; if (p >= 1) p -= 1;
+ return v;
+ }
+
+ // ======================================================================
+ // Bass
+ // ======================================================================
+
+ private static void Sub(SourceBuffer b)
+ {
+ double f0 = Hz(36), p1 = 0, p2 = 0;
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ double x = Sine(ref p1, f0) + 0.18 * Sine(ref p2, 2 * f0);
+ double v = KitDsp.Saturate(x * 0.5 * Smooth(t, 0.006), 0.35, 0.5);
+ b.L[i] = v; b.R[i] = v;
+ }
+ Edges(b, 0.004, 0.25);
+ }
+
+ private static void SawBass(SourceBuffer b)
+ {
+ double f0 = Hz(36), p1 = 0, p2 = 0.5, p3 = 0;
+ var f = new Svf();
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ double x = Saw(ref p1, f0) + 0.8 * Pulse(ref p2, f0 * Cents(6), 0.5) + 0.8 * Sine(ref p3, f0);
+ double cut = 180 + 2600 * Math.Exp(-t / 0.22) + 220;
+ f.Run(x, cut, 1.6);
+ double amp = 0.72 + 0.28 * Math.Exp(-t / 0.3);
+ double v = KitDsp.Saturate(f.Lp * 0.35 * amp * Smooth(t, 0.003), 0.4, 0.6);
+ b.L[i] = v; b.R[i] = v;
+ }
+ Edges(b, 0.002, 0.25);
+ }
+
+ private static void FmGrowl(SourceBuffer b)
+ {
+ double f0 = Hz(36), pc = 0, pm = 0, ps = 0;
+ var f = new Svf();
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ double index = 2.2 + 2.0 * Math.Sin(Tau * 1.5 * t - Math.PI / 2);
+ double m = Math.Sin(Tau * pm) * index;
+ pm += f0 * 1.0 / SourceBuffer.Rate; if (pm >= 1) pm -= 1;
+ double c = Math.Sin(Tau * pc + m);
+ pc += f0 / SourceBuffer.Rate; if (pc >= 1) pc -= 1;
+ double sub = Sine(ref ps, f0);
+ f.Run(c, 900 + 700 * Math.Sin(Tau * 1.5 * t), 2.2);
+ double v = KitDsp.Saturate((f.Lp * 0.8 + c * 0.2 + sub * 0.45) * 0.4, 0.45, 0.4);
+ b.L[i] = v; b.R[i] = v;
+ }
+ Edges(b, 0.004, 0.25);
+ }
+
+ // ======================================================================
+ // Struck and plucked
+ // ======================================================================
+
+ private static void EPiano(SourceBuffer b)
+ {
+ double f0 = Hz(60), pc = 0, pm = 0, pt = 0;
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ double index = 1.9 * Math.Exp(-t / 0.5) + 0.35;
+ double m = Math.Sin(Tau * pm) * index;
+ pm += f0 / SourceBuffer.Rate; if (pm >= 1) pm -= 1;
+ double c = Math.Sin(Tau * pc + m);
+ pc += f0 / SourceBuffer.Rate; if (pc >= 1) pc -= 1;
+ double tine = Sine(ref pt, f0 * 14.1) * 0.22 * Math.Exp(-t / 0.04);
+ double x = (c + tine) * Math.Exp(-t / 1.7) * Smooth(t, 0.002) * 0.45;
+ double trem = 0.25 * Math.Sin(Tau * 4.6 * t);
+ b.L[i] = x * (1 + trem); b.R[i] = x * (1 - trem);
+ }
+ Space(b, 0.15, 0.4, 0.4);
+ Edges(b, 0, 0.2);
+ }
+
+ /// <summary>A Karplus-Strong string plucked at <paramref name="at"/> into both channels.</summary>
+ private static void Pluck(SourceBuffer b, double midi, double at, double gain, double decay, double pan, double bright)
+ {
+ double f = Hz(midi);
+ int d = Math.Max(2, (int)Math.Round(SourceBuffer.Rate / f - 0.5));
+ var line = new double[d];
+ var ex = Biquad.LowPass(1500 + 6000 * bright, 0.7, SourceBuffer.Rate);
+ for (int k = 0; k < d; k++) line[k] = ex.Process(b.Rng.Next());
+ // Per-period loss that reaches -60 dB after `decay` seconds.
+ double loss = Math.Pow(10, -3.0 / (decay * f));
+ int start = b.At(at), p = 0;
+ double prev = 0, gl = gain * Math.Sqrt(0.5 * (1 - pan)), gr = gain * Math.Sqrt(0.5 * (1 + pan));
+ for (int i = start; i < b.N; i++)
+ {
+ double y = line[p];
+ double nv = (y + prev) * 0.5 * loss;
+ prev = y;
+ line[p] = nv;
+ if (++p >= d) p = 0;
+ b.L[i] += y * gl; b.R[i] += y * gr;
+ }
+ }
+
+ private static void Nylon(SourceBuffer b)
+ {
+ Pluck(b, 48, 0, 0.5, 2.8, -0.1, 0.35);
+ Pluck(b, 48.03, 0.001, 0.25, 2.4, 0.3, 0.2);
+ var bodyL = Biquad.Peaking(210, 1.4, 4, SourceBuffer.Rate); var bodyR = Biquad.Peaking(230, 1.4, 4, SourceBuffer.Rate);
+ for (int i = 0; i < b.N; i++) { b.L[i] = bodyL.Process(b.L[i]); b.R[i] = bodyR.Process(b.R[i]); }
+ Space(b, 0.15, 0.35, 0.3);
+ }
+
+ private static void HarpRun(SourceBuffer b)
+ {
+ int[] pent = { 0, 2, 4, 7, 9 };
+ int n = 0;
+ for (int oct = 0; oct < 2; oct++)
+ foreach (int s in pent)
+ {
+ double at = 0.02 + n * 0.12;
+ Pluck(b, 60 + oct * 12 + s, at, 0.32, 2.4, -0.6 + 0.12 * n, 0.5);
+ n++;
+ }
+ Pluck(b, 84, 0.02 + n * 0.12, 0.32, 2.6, 0.6, 0.5);
+ Space(b, 0.35, 0.65, 0.55);
+ }
+
+ /// <summary>Strikes a modal body: (ratio, amp, decay seconds) partials of a fundamental.</summary>
+ private static void Strike(SourceBuffer b, double midi, double at, double gain, double pan, (double Ratio, double Amp, double Decay)[] modes,
+ double malletMs = 1.5, double detune = 0)
+ {
+ double f0 = Hz(midi);
+ int start = b.At(at);
+ double gl = gain * Math.Sqrt(0.5 * (1 - pan)), gr = gain * Math.Sqrt(0.5 * (1 + pan));
+ var ml = new Modal[modes.Length]; var mr = new Modal[modes.Length];
+ for (int k = 0; k < modes.Length; k++)
+ {
+ ml[k] = new Modal(f0 * modes[k].Ratio, modes[k].Decay, SourceBuffer.Rate);
+ mr[k] = new Modal(f0 * modes[k].Ratio * (1 + detune), modes[k].Decay, SourceBuffer.Rate);
+ }
+ int mallet = Math.Max(1, (int)(malletMs * 0.001 * SourceBuffer.Rate));
+ for (int i = start; i < b.N; i++)
+ {
+ int j = i - start;
+ double x = j < mallet ? Math.Sin(Math.PI * j / mallet) / mallet * 40 : 0;
+ double l = 0, r = 0;
+ for (int k = 0; k < modes.Length; k++) { l += ml[k].Process(x) * modes[k].Amp; r += mr[k].Process(x) * modes[k].Amp; }
+ b.L[i] += l * gl; b.R[i] += r * gr;
+ if (j > 4 * mallet && j % 4800 == 0 && Math.Abs(l) + Math.Abs(r) < 1e-7) break;
+ }
+ }
+
+ private static readonly (double, double, double)[] KalimbaModes = { (1, 1, 1.8), (5.4, 0.3, 0.35), (11.2, 0.12, 0.12) };
+ private static readonly (double, double, double)[] MarimbaModes = { (1, 1, 1.1), (3.93, 0.4, 0.35), (9.2, 0.15, 0.1), (0.5, 0.05, 0.6) };
+ private static readonly (double, double, double)[] BellModes =
+ {
+ (0.5, 0.6, 6), (1, 1, 5), (1.19, 0.55, 4.2), (1.5, 0.35, 3.5), (2, 0.5, 3),
+ (2.51, 0.25, 2), (3.01, 0.2, 1.5), (4.16, 0.14, 1), (5.43, 0.08, 0.6),
+ };
+ private static readonly (double, double, double)[] BoxModes = { (1, 1, 1.4), (2.76, 0.18, 0.3), (5.4, 0.08, 0.1) };
+ private static readonly (double, double, double)[] ChimeModes = { (1, 1, 3.2), (2.76, 0.35, 1.2), (5.4, 0.18, 0.5), (8.93, 0.06, 0.2) };
+
+ private static void Kalimba(SourceBuffer b)
+ {
+ Strike(b, 72, 0, 0.5, 0, KalimbaModes, 1.0, 0.0006);
+ var box = Biquad.Peaking(380, 1.2, 4, SourceBuffer.Rate); var boxR = Biquad.Peaking(400, 1.2, 4, SourceBuffer.Rate);
+ for (int i = 0; i < b.N; i++) { b.L[i] = box.Process(b.L[i]); b.R[i] = boxR.Process(b.R[i]); }
+ Space(b, 0.2, 0.35, 0.35);
+ }
+
+ private static void Marimba(SourceBuffer b)
+ {
+ Strike(b, 60, 0, 0.5, 0, MarimbaModes, 3.0, 0.0004);
+ Space(b, 0.2, 0.4, 0.3);
+ }
+
+ private static void Bell(SourceBuffer b)
+ {
+ Strike(b, 60, 0, 0.35, 0, BellModes, 1.2, 0.0012);
+ Space(b, 0.35, 0.85, 0.75, 0.5);
+ }
+
+ private static void MusicBox(SourceBuffer b)
+ {
+ // A little tune in C: two phrases of eighth notes at 100 BPM.
+ int[] tune = { 72, 76, 79, 84, 83, 79, 76, 74, 72, 74, 76, 79, 77, 74, 71, 72, 76, 72, 67, 72 };
+ for (int k = 0; k < tune.Length; k++)
+ Strike(b, tune[k], 0.02 + k * 0.28, 0.35, (k % 3 - 1) * 0.3, BoxModes, 0.6, 0.0008);
+ Space(b, 0.3, 0.5, 0.5);
+ }
+
+ private static void Chimes(SourceBuffer b)
+ {
+ int[] notes = { 84, 86, 88, 91, 93 };
+ double t = 0.01;
+ while (t < b.N / (double)SourceBuffer.Rate - 0.5)
+ {
+ int k = (int)(b.Rng.Next01() * notes.Length) % notes.Length;
+ Strike(b, notes[k], t, 0.12 + 0.12 * b.Rng.Next01(), (k - 2) * 0.4, ChimeModes, 0.8, 0.0015);
+ t += 0.08 + 0.35 * b.Rng.Next01();
+ }
+ Space(b, 0.35, 0.7, 0.6, 0.4);
+ Edges(b, 0, 0.8);
+ }
+
+ // ======================================================================
+ // Textures
+ // ======================================================================
+
+ private static void Metal(SourceBuffer b)
+ {
+ double f0 = Hz(48);
+ double[] ratio = { 1, 2.32, 4.25, 6.63, 9.38, 12.1 };
+ var ml = new Modal[ratio.Length]; var mr = new Modal[ratio.Length];
+ for (int k = 0; k < ratio.Length; k++)
+ {
+ ml[k] = new Modal(f0 * ratio[k], 3.5 - 0.4 * k, SourceBuffer.Rate);
+ mr[k] = new Modal(f0 * ratio[k] * 1.0021, 3.5 - 0.4 * k, SourceBuffer.Rate);
+ }
+ var exL = new Svf(); var exR = new Svf();
+ double w1 = b.Rng.Next01() * Tau, w2 = b.Rng.Next01() * Tau;
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ double bowAmt = Smooth(t, 0.8) * (0.6 + 0.25 * Math.Sin(Tau * 0.37 * t + w1) + 0.15 * Math.Sin(Tau * 1.1 * t + w2));
+ exL.Run(b.Rng.Next(), 2500, 0.8); exR.Run(b.Rng.Next(), 2600, 0.8);
+ double xl = exL.Lp * bowAmt * 0.02, xr = exR.Lp * bowAmt * 0.02;
+ double l = 0, r = 0;
+ for (int k = 0; k < ratio.Length; k++) { double a = 1.0 / (1 + k * 0.5); l += ml[k].Process(xl) * a; r += mr[k].Process(xr) * a; }
+ b.L[i] = l; b.R[i] = r;
+ }
+ Space(b, 0.4, 0.8, 0.75, 0.4);
+ Edges(b, 0.1, 0.6);
+ }
+
+ private static void Drone(SourceBuffer b)
+ {
+ double f0 = Hz(36);
+ (double Ratio, double Cent, double Amp)[] osc = { (1, -6, 1), (1, 7, 1), (1.5, -3, 0.6), (2, 4, 0.4), (1, 0, 0.7) };
+ var pl = new double[osc.Length]; var pr = new double[osc.Length];
+ for (int k = 0; k < osc.Length; k++) { pl[k] = b.Rng.Next01(); pr[k] = b.Rng.Next01(); }
+ var fl = new Svf(); var fr = new Svf(); var rumble = Biquad.LowPass(120, 0.7, SourceBuffer.Rate);
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i), l = 0, r = 0;
+ for (int k = 0; k < osc.Length; k++)
+ {
+ double f = f0 * osc[k].Ratio * Cents(osc[k].Cent);
+ if (k == 4) { l += osc[k].Amp * Sine(ref pl[k], f) * 2; r += osc[k].Amp * Sine(ref pr[k], f) * 2; continue; }
+ l += osc[k].Amp * Saw(ref pl[k], f); r += osc[k].Amp * Saw(ref pr[k], f * Cents(3));
+ }
+ double cut = 220 + 700 * (0.5 - 0.5 * Math.Cos(Tau * t / 8.0 * 1.5)) + 60 * Math.Sin(Tau * 0.3 * t);
+ fl.Run(l, cut, 2.0); fr.Run(r, cut * 1.05, 2.0);
+ double rm = rumble.Process(b.Rng.Next()) * 0.6;
+ b.L[i] = (fl.Lp + rm) * 0.2; b.R[i] = (fr.Lp + rm) * 0.2;
+ }
+ Space(b, 0.45, 0.9, 0.85, 0.6);
+ Edges(b, 0.5, 0.6);
+ }
+
+ private static void Ocean(SourceBuffer b)
+ {
+ var fl = new Svf(); var fr = new Svf();
+ double pinkL = 0, pinkR = 0;
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ double wave = 0.5 + 0.5 * Math.Sin(Tau * t / 3.6 - 1.2) * (0.7 + 0.3 * Math.Sin(Tau * t / 2.3));
+ wave = wave * wave;
+ pinkL = pinkL * 0.97 + b.Rng.Next() * 0.03 * 6; pinkR = pinkR * 0.97 + b.Rng.Next() * 0.03 * 6;
+ double cut = 350 + 3200 * wave;
+ fl.Run(pinkL + b.Rng.Next() * 0.15, cut, 0.8); fr.Run(pinkR + b.Rng.Next() * 0.15, cut * 1.1, 0.8);
+ double g = 0.15 + 0.85 * wave;
+ b.L[i] = fl.Lp * g * 0.8; b.R[i] = fr.Lp * g * 0.8;
+ }
+ Edges(b, 0.5, 0.6);
+ }
+
+ private static void Rain(SourceBuffer b)
+ {
+ var bed = new Svf(); var bedR = new Svf();
+ for (int i = 0; i < b.N; i++)
+ {
+ bed.Run(b.Rng.Next(), 4500, 0.6); bedR.Run(b.Rng.Next(), 4500, 0.6);
+ b.L[i] = bed.Bp * 0.08; b.R[i] = bedR.Bp * 0.08;
+ }
+ // Droplets: tiny pitched pings, then sparse crackle clicks.
+ double t = 0;
+ double end = b.N / (double)SourceBuffer.Rate;
+ while ((t += 0.004 + 0.045 * b.Rng.Next01()) < end)
+ {
+ double f = 1800 + 4200 * b.Rng.Next01(), pan = b.Rng.Next();
+ var m = new Modal(f, 0.02 + 0.04 * b.Rng.Next01(), SourceBuffer.Rate);
+ int s = b.At(t), len = Math.Min(b.N - s, (int)(0.08 * SourceBuffer.Rate));
+ double g = 0.04 + 0.12 * b.Rng.Next01() * b.Rng.Next01();
+ for (int j = 0; j < len; j++)
+ {
+ double y = m.Process(j == 0 ? 1 : 0) * g;
+ b.L[s + j] += y * (1 - pan) * 0.7; b.R[s + j] += y * (1 + pan) * 0.7;
+ }
+ }
+ t = 0;
+ while ((t += 0.02 + 0.2 * b.Rng.Next01()) < end)
+ {
+ int s = b.At(t);
+ double g = 0.04 + 0.1 * b.Rng.Next01(), pan = b.Rng.Next();
+ for (int j = 0; j < 24 && s + j < b.N; j++)
+ {
+ double y = b.Rng.Next() * g * Math.Exp(-j / 5.0);
+ b.L[s + j] += y * (1 - pan); b.R[s + j] += y * (1 + pan);
+ }
+ }
+ Tame(b, 12);
+ Edges(b, 0.2, 0.3);
+ }
+
+ private static void Radio(SourceBuffer b)
+ {
+ var sweep = new Svf(); var voice = new Formants(); var band = new[] { Biquad.HighPass(350, 0.7, SourceBuffer.Rate), Biquad.LowPass(3200, 0.8, SourceBuffer.Rate) };
+ double pw = 0, pv = 0;
+ double end = b.N / (double)SourceBuffer.Rate;
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ // A tuning dial: a heterodyne whistle gliding past stations, noise in between.
+ double whistle = Sine(ref pw, 700 + 900 * (0.5 + 0.5 * Math.Sin(Tau * t / end * 1.3))) * (0.3 + 0.3 * Math.Sin(Tau * 0.8 * t));
+ sweep.Run(b.Rng.Next(), 600 + 2400 * (0.5 + 0.5 * Math.Sin(Tau * 0.45 * t)), 3);
+ double station = Math.Pow(Math.Max(0, Math.Sin(Tau * t / end * 2.5)), 2);
+ double syll = Math.Max(0, Math.Sin(Tau * 3.7 * t)) * Math.Max(0, Math.Sin(Tau * 0.9 * t + 1));
+ double vox = voice.Run(Pulse(ref pv, 118 * Cents(60 * Math.Sin(Tau * 1.7 * t)), 0.2), 2 + 2 * Math.Sin(Tau * 2.9 * t)) * syll;
+ double x = whistle * 0.25 * (1 - station) + sweep.Bp * 0.6 * (1 - 0.6 * station) + vox * 1.4 * station;
+ foreach (var q in band) x = q.Process(x);
+ x = KitDsp.Saturate(x * 0.8, 0.4, 0.2);
+ b.L[i] = x; b.R[i] = x;
+ }
+ Space(b, 0.12, 0.3, 0.3);
+ Edges(b, 0.1, 0.2);
+ }
+
+ // ======================================================================
+ // Rhythm and sequences
+ // ======================================================================
+
+ private static void Drums(SourceBuffer b)
+ {
+ // A two-bar break played on the Kompakt kit's own voices.
+ var kit = KitCatalog.ById("kompakt") ?? KitCatalog.All[0];
+ KitPad? Pad(int note) { foreach (var p in kit.Pads) if (p.Note == note) return p; return null; }
+ var cache = new Dictionary<int, RenderedSample>();
+ const double step = 60.0 / 120 / 4; // 16ths at 120 BPM
+ (int Step, int Note, double Vel)[] hits =
+ {
+ (0, 36, 1), (3, 36, 0.6), (10, 36, 0.9), (16, 36, 1), (19, 36, 0.55), (24, 36, 0.8), (26, 36, 0.9),
+ (4, 38, 1), (7, 38, 0.35), (12, 38, 1), (15, 38, 0.4), (20, 38, 1), (23, 38, 0.35), (28, 38, 1), (30, 38, 0.5), (31, 38, 0.4),
+ (14, 46, 0.6), (30, 46, 0.5),
+ };
+ var all = new List<(int Step, int Note, double Vel)>(hits);
+ for (int s = 0; s < 32; s += 2) if (s != 14 && s != 30) all.Add((s, 42, s % 4 == 0 ? 0.7 : 0.45));
+ foreach (var (st, note, vel) in all)
+ {
+ var pad = Pad(note);
+ if (pad is null) continue;
+ if (!cache.TryGetValue(note, out var smp)) cache[note] = smp = KitRenderer.Render(pad, (uint)(0x51 + note));
+ int at = b.At(st * step);
+ double pan = note is 42 or 46 ? 0.25 : 0;
+ for (int j = 0; j < smp.Frames && at + j < b.N; j++)
+ {
+ double x = smp.Channels == 2 ? (smp.Interleaved[2 * j] + smp.Interleaved[2 * j + 1]) * 0.5 : smp.Interleaved[j];
+ b.L[at + j] += x * vel * (1 - pan) * 0.5; b.R[at + j] += x * vel * (1 + pan) * 0.5;
+ }
+ }
+ Space(b, 0.15, 0.35, 0.25);
+ }
+
+ private static void Arp(SourceBuffer b)
+ {
+ int[] seq = { 48, 51, 55, 60, 63, 60, 55, 51, 48, 51, 55, 60, 63, 67, 63, 60,
+ 44, 48, 51, 56, 60, 56, 51, 48, 46, 50, 53, 58, 62, 58, 53, 50 };
+ const double step = 60.0 / 120 / 4;
+ var f = new Svf();
+ double ph = 0;
+ for (int i = 0; i < b.N; i++)
+ {
+ double t = SourceBuffer.T(i);
+ int k = Math.Min(seq.Length - 1, (int)(t / step));
+ double local = t - k * step;
+ double env = Math.Exp(-local / 0.09) * Smooth(local, 0.002);
+ double x = Pulse(ref ph, Hz(seq[k]), 0.32);
+ f.Run(x, 400 + 3200 * Math.Exp(-local / 0.06), 2.2);
+ double v = f.Lp * env * 0.3;
+ b.L[i] = v; b.R[i] = v;
+ }
+ PingPong(b, step * 3, 0.45, 0.45);
+ Edges(b, 0, 0.05);
+ }
+
+ private static void Bits(SourceBuffer b)
+ {
+ int[] pent = { 0, 3, 5, 7, 10 };
+ const double step = 60.0 / 120 / 4;
+ int steps = (int)(b.N / (step * SourceBuffer.Rate));
+ for (int k = 0; k < steps; k++)
+ {
+ if (b.Rng.Next01() < 0.22) continue;
+ int s = b.At(k * step);
+ int len = (int)((0.02 + 0.09 * b.Rng.Next01()) * SourceBuffer.Rate);
+ bool noise = b.Rng.Next01() < 0.2;
+ double f = Hz(60 + pent[(int)(b.Rng.Next01() * 5) % 5] + 12 * ((int)(b.Rng.Next01() * 3) - 1));
+ double pan = b.Rng.Next() * 0.8, ph = 0, g = 0.25 + 0.2 * b.Rng.Next01();
+ double glide = b.Rng.Next01() < 0.3 ? (b.Rng.Next01() < 0.5 ? 2 : 0.5) : 1;
+ for (int j = 0; j < len && s + j < b.N; j++)
+ {
+ double u = j / (double)len;
+ double x = noise ? (b.Rng.Next() > 0 ? 1 : -1) : (Pulse(ref ph, f * Math.Pow(glide, u), 0.5));
+ double y = x * g * (1 - u);
+ b.L[s + j] += y * (1 - pan); b.R[s + j] += y * (1 + pan);
+ }
+ }
+ // 5-bit, 8 kHz: the grit is the point.
+ foreach (var ch in new[] { b.L, b.R }) KitDsp.Crush(ch, 5, 6);
+ var lp = Biquad.LowPass(9000, 0.7, SourceBuffer.Rate); var lpR = Biquad.LowPass(9000, 0.7, SourceBuffer.Rate);
+ for (int i = 0; i < b.N; i++) { b.L[i] = lp.Process(b.L[i]) * 0.6; b.R[i] = lpR.Process(b.R[i]) * 0.6; }
+ }
+}
diff --git a/src/managed/Nota.Infrastructure/Interop/NativeMethods.Audition.cs b/src/managed/Nota.Infrastructure/Interop/NativeMethods.Audition.cs
index 598b313e..acce0134 100644
--- a/src/managed/Nota.Infrastructure/Interop/NativeMethods.Audition.cs
+++ b/src/managed/Nota.Infrastructure/Interop/NativeMethods.Audition.cs
@@ -43,7 +43,7 @@ internal static partial class NativeMethods
internal static partial int AuditionMidiParam(IntPtr rig, int index, string name, float value);
[LibraryImport(Lib, EntryPoint = "nota_audition_set_sampler_sample", StringMarshalling = StringMarshalling.Utf8)]
- internal static partial int AuditionSetSamplerSample(IntPtr rig, string? path);
+ internal static partial int AuditionSetSamplerSample(IntPtr rig, string? path, int rootNote);
[LibraryImport(Lib, EntryPoint = "nota_audition_add_source_from")]
internal static partial int AuditionAddSourceFrom(IntPtr rig, IntPtr part, float gain);
diff --git a/src/managed/Nota.Infrastructure/Interop/NotaEngine.Audition.cs b/src/managed/Nota.Infrastructure/Interop/NotaEngine.Audition.cs
index a0e4938e..d99f94b5 100644
--- a/src/managed/Nota.Infrastructure/Interop/NotaEngine.Audition.cs
+++ b/src/managed/Nota.Infrastructure/Interop/NotaEngine.Audition.cs
@@ -45,7 +45,7 @@ public sealed partial class NotaEngine
public bool DeviceParam(int index, string name, float value) => NativeMethods.AuditionDeviceParam(Handle, index, name, value) != 0;
public int AddMidiEffect(int kind) => NativeMethods.AuditionAddMidiEffect(Handle, kind);
public bool MidiParam(int index, string name, float value) => NativeMethods.AuditionMidiParam(Handle, index, name, value) != 0;
- public bool SetSamplerSample(string? path) => NativeMethods.AuditionSetSamplerSample(Handle, path) != 0;
+ public bool SetSamplerSample(string? path, int rootNote = 60) => NativeMethods.AuditionSetSamplerSample(Handle, path, rootNote) != 0;
public bool AddSourceFrom(IAuditionRig part, float gain)
=> part is AuditionRig p && NativeMethods.AuditionAddSourceFrom(Handle, p.Handle, gain) != 0;
public bool UseCachedSource(string key) => NativeMethods.AuditionUseCachedSource(Handle, key) != 0;
diff --git a/src/managed/Nota.Infrastructure/Persistence/FactoryPresetCatalog.cs b/src/managed/Nota.Infrastructure/Persistence/FactoryPresetCatalog.cs
index 4a1d10d6..9fc3a330 100644
--- a/src/managed/Nota.Infrastructure/Persistence/FactoryPresetCatalog.cs
+++ b/src/managed/Nota.Infrastructure/Persistence/FactoryPresetCatalog.cs
@@ -710,42 +710,76 @@ public sealed class FactoryPresetCatalog : IFactoryPresets
("dcoarse", 0.0667f), ("dlevel", 1f), ("datk", 0.14f), ("dsus", 0.85f), ("drel", 0.3f),
("veltofm", 0.85f), ("filfreq", 0.7f), ("volume", 0.8f));
- // ---- Nota Grain (kind 10) — granular, 25 presets. Scan Mode 0 Scan · .5 Freeze · 1 Key;
- // Scan 0.5 = still (±4×, 0.625 = 1×); Coarse 0.5 = 0 st (±24, 0.75 = +12); window
- // 0 Hann · 1/3 Gauss · 2/3 Tukey · 1 Tri; filter 0 LP · .5 HP · 1 BP; Dry/Wet 1 = the
- // cloud alone, 0 = the sample played straight. Presets shape whatever sample is
- // loaded (the built-in pad until you drop your own).
+ // ---- Nota Grain (kind 10) — granular, 50 presets. Every preset outside Basics brings its
+ // own source (GrainSources: rendered once, then loaded with the preset); Basics shape
+ // whatever sample is loaded. Scan Mode 0 Scan · .5 Freeze · 1 Key (C4 reads Position,
+ // an octave moves a third of the file, unpitched); Scan .5 = still (±4×, .625 = 1×);
+ // Size 4 ms·100^v (.5 ≈ 40 ms, .7 ≈ 100 ms, .75 ≈ 125 ms, .9 ≈ 250 ms); Density =
+ // overlap 1 + 7v; Spray / Pos Rand ±25 % of the file at 1; Coarse .5 = 0 st (±24,
+ // .25 = −12, .75 = +12); window 0 Hann · 1/3 Gauss · 2/3 Tukey · 1 Tri; filter 0 LP ·
+ // .5 HP · 1 BP, freq 60 Hz·300^v (.5 ≈ 1 k, .7 ≈ 3.2 k, .9 ≈ 10 k); attack 1 ms·3000^v,
+ // release 3 ms·2000^v; Dry/Wet 1 = the cloud alone, 0 = the sample played straight.
Section("Basics");
Inst("grain", 10, "Half Frozen", ("scanmode", 0.5f), ("grainsize", 0.7f), ("density", 0.8f), ("spread", 0.6f), ("posrand", 0.1f), ("drywet", 0.5f), ("attack", 0.5f), ("release", 0.7f), ("volume", 0.8f));
Inst("grain", 10, "Sample Plus", ("scanmode", 0.5f), ("grainsize", 0.45f), ("density", 0.7f), ("spread", 0.8f), ("panrand", 0.6f), ("pitchrand", 0.02f), ("drywet", 0.25f), ("attack", 0.1f), ("release", 0.5f), ("volume", 0.8f));
Inst("grain", 10, "Plain Sampler",("drywet", 0f), ("filfreq", 1f), ("attack", 0.05f), ("release", 0.45f), ("volume", 0.8f));
+ Inst("grain", 10, "Slow Scan", ("scanmode", 0f), ("scan", 0.56f), ("position", 0f), ("grainsize", 0.6f), ("density", 0.8f), ("spread", 0.6f), ("posrand", 0.05f), ("attack", 0.3f), ("release", 0.6f), ("volume", 0.8f));
Section("Pads");
- Inst("grain", 10, "Frozen Choir", ("scanmode", 0.5f), ("position", 0.3f), ("spray", 0.2f), ("grainsize", 0.5f), ("density", 0.8f), ("spread", 0.6f), ("posrand", 0.15f), ("attack", 0.3f), ("release", 0.6f), ("filfreq", 0.9f), ("volume", 0.8f));
- Inst("grain", 10, "Drift Cloud", ("scanmode", 0f), ("scan", 0.6f), ("grainsize", 0.6f), ("density", 0.7f), ("spread", 0.7f), ("posrand", 0.3f), ("panrand", 0.5f), ("attack", 0.2f), ("release", 0.7f), ("volume", 0.78f));
- Inst("grain", 10, "Shimmer", ("scanmode", 0.5f), ("coarse", 0.75f), ("grainsize", 0.5f), ("density", 0.8f), ("pitchrand", 0.1f), ("spread", 0.6f), ("filfreq", 1f), ("release", 0.6f), ("volume", 0.76f));
- Inst("grain", 10, "Slow Bloom", ("scanmode", 0.5f), ("grainshape", 1f / 3f), ("grainsize", 0.8f), ("density", 0.9f), ("spread", 0.7f), ("posrand", 0.2f), ("attack", 0.85f), ("release", 0.9f), ("filfreq", 0.75f), ("volume", 0.78f));
- Inst("grain", 10, "Glass Halo", ("scanmode", 0.5f), ("coarse", 0.75f), ("fine", 0.52f), ("grainsize", 0.55f), ("density", 0.85f), ("pitchrand", 0.05f), ("spread", 0.8f), ("panrand", 0.6f), ("filtype", 0.5f), ("filfreq", 0.35f), ("attack", 0.7f), ("release", 0.85f), ("volume", 0.74f));
- Inst("grain", 10, "Dust Cloud", ("scanmode", 0.5f), ("grainshape", 1f), ("grainsize", 0.2f), ("density", 0.95f), ("spray", 0.5f), ("posrand", 0.6f), ("panrand", 0.8f), ("spread", 0.9f), ("filtype", 1f), ("filfreq", 0.42f), ("filreso", 0.35f), ("attack", 0.5f), ("release", 0.75f), ("volume", 0.8f));
- Inst("grain", 10, "Detuned Swarm",("scanmode", 0.5f), ("fine", 0.52f), ("pitchrand", 0.03f), ("grainsize", 0.55f), ("density", 1f), ("spread", 1f), ("panrand", 0.7f), ("attack", 0.6f), ("release", 0.7f), ("volume", 0.76f));
+ Grain("Warm Ensemble", "saw-ensemble", ("scanmode", 0.5f), ("position", 0.4f), ("spray", 0.1f), ("grainsize", 0.7f), ("density", 0.85f), ("spread", 0.7f), ("posrand", 0.1f), ("filfreq", 0.75f), ("attack", 0.55f), ("release", 0.7f), ("volume", 0.9f));
+ Grain("Slow Bloom", "saw-ensemble", ("scanmode", 0f), ("scan", 0.53f), ("grainshape", 1f / 3f), ("grainsize", 0.8f), ("density", 0.9f), ("spread", 0.7f), ("posrand", 0.2f), ("filfreq", 0.6f), ("filreso", 0.2f), ("attack", 0.85f), ("release", 0.9f), ("volume", 1f));
+ Grain("Glass Pad", "glass-pad", ("scanmode", 0.5f), ("position", 0.35f), ("spray", 0.2f), ("grainsize", 0.65f), ("density", 0.9f), ("spread", 0.8f), ("panrand", 0.4f), ("posrand", 0.1f), ("filfreq", 0.85f), ("attack", 0.6f), ("release", 0.8f), ("volume", 0.97f));
+ Grain("Shimmer", "glass-pad", ("scanmode", 0.5f), ("coarse", 0.75f), ("grainsize", 0.55f), ("density", 0.85f), ("pitchrand", 0.03f), ("spread", 1f), ("panrand", 0.6f), ("filtype", 0.5f), ("filfreq", 0.3f), ("attack", 0.7f), ("release", 0.85f), ("volume", 1f));
+ Grain("Tape Memory", "tape-chord", ("scanmode", 0f), ("scan", 0.53f), ("position", 0.1f), ("grainsize", 0.75f), ("density", 0.8f), ("spread", 0.5f), ("posrand", 0.08f), ("filfreq", 0.7f), ("attack", 0.5f), ("release", 0.75f), ("volume", 0.64f));
+ Grain("String Cloud", "strings", ("scanmode", 0.5f), ("position", 0.5f), ("spray", 0.2f), ("grainsize", 0.6f), ("density", 0.9f), ("spread", 0.8f), ("posrand", 0.15f), ("filfreq", 0.8f), ("attack", 0.6f), ("release", 0.75f), ("volume", 0.71f));
+ Grain("Brass Haze", "brass", ("scanmode", 0.5f), ("position", 0.6f), ("spray", 0.1f), ("grainsize", 0.7f), ("density", 0.8f), ("spread", 0.5f), ("posrand", 0.08f), ("filfreq", 0.65f), ("attack", 0.5f), ("release", 0.65f), ("volume", 0.76f));
+ Section("Voices & Choirs");
+ Grain("Frozen Choir", "choir", ("scanmode", 0.5f), ("position", 0.45f), ("spray", 0.2f), ("grainsize", 0.55f), ("density", 0.85f), ("spread", 0.7f), ("posrand", 0.12f), ("filfreq", 0.85f), ("attack", 0.45f), ("release", 0.7f), ("volume", 0.63f));
+ Grain("Angel Octave", "choir", ("scanmode", 0.5f), ("position", 0.5f), ("coarse", 0.75f), ("grainsize", 0.6f), ("density", 0.9f), ("pitchrand", 0.02f), ("spread", 1f), ("panrand", 0.5f), ("filfreq", 0.85f), ("attack", 0.7f), ("release", 0.85f), ("volume", 0.64f));
+ Grain("Vowel Drift", "vowels", ("scanmode", 0f), ("scan", 0.56f), ("position", 0f), ("grainsize", 0.5f), ("density", 0.8f), ("spread", 0.5f), ("posrand", 0.03f), ("attack", 0.3f), ("release", 0.6f), ("volume", 0.58f));
+ Grain("Vowel Slicer", "vowels", ("scanmode", 1f), ("position", 0.5f), ("spray", 0.02f), ("grainsize", 0.55f), ("density", 0.75f), ("spread", 0.4f), ("posrand", 0.02f), ("attack", 0.15f), ("release", 0.5f), ("volume", 0.65f));
+ Grain("Whisper Cloud", "whisper", ("scanmode", 0.5f), ("position", 0.4f), ("spray", 0.3f), ("grainsize", 0.4f), ("density", 0.95f), ("posrand", 0.4f), ("panrand", 0.8f), ("spread", 0.9f), ("filtype", 1f), ("filfreq", 0.6f), ("filreso", 0.3f), ("attack", 0.5f), ("release", 0.75f), ("volume", 1f));
Section("Bass");
- Inst("grain", 10, "Sub Grain", ("scanmode", 0.5f), ("coarse", 0.25f), ("grainsize", 0.6f), ("density", 0.7f), ("filfreq", 0.4f), ("filreso", 0.2f), ("volume", 0.85f));
- Inst("grain", 10, "Grain Bass", ("scanmode", 0.5f), ("coarse", 0.25f), ("grainsize", 0.7f), ("density", 0.65f), ("spread", 0.1f), ("panrand", 0f), ("posrand", 0.02f), ("filfreq", 0.45f), ("filreso", 0.25f), ("attack", 0.1f), ("decay", 0.45f), ("sustain", 0.7f), ("release", 0.35f), ("drywet", 0.4f), ("volume", 0.85f));
+ Grain("Sub Grain", "sub", ("scanmode", 0.5f), ("position", 0.3f), ("spray", 0f), ("grainsize", 0.75f), ("density", 0.7f), ("spread", 0.05f), ("panrand", 0f), ("posrand", 0.02f), ("filfreq", 0.5f), ("attack", 0.05f), ("release", 0.3f), ("drywet", 0.5f), ("volume", 0.86f));
+ Grain("Grain Bass", "saw-bass", ("scanmode", 0.5f), ("position", 0.02f), ("spray", 0f), ("grainsize", 0.65f), ("density", 0.7f), ("spread", 0.15f), ("panrand", 0.05f), ("posrand", 0.01f), ("filfreq", 0.55f), ("filreso", 0.25f), ("attack", 0.03f), ("decay", 0.45f), ("sustain", 0.7f), ("release", 0.3f), ("drywet", 0.4f), ("volume", 0.91f));
+ Grain("Growl Cloud", "fm-growl", ("scanmode", 0f), ("scan", 0.6f), ("grainsize", 0.5f), ("density", 0.75f), ("spray", 0.05f), ("spread", 0.25f), ("panrand", 0.1f), ("posrand", 0.03f), ("filfreq", 0.6f), ("filreso", 0.3f), ("attack", 0.03f), ("release", 0.3f), ("volume", 0.62f));
+ Grain("Pluck Bass", "nylon", ("scanmode", 0.5f), ("position", 0f), ("spray", 0f), ("posrand", 0f), ("coarse", 0.25f), ("grainsize", 0.55f), ("density", 0.6f), ("spread", 0.1f), ("panrand", 0f), ("filfreq", 0.6f), ("attack", 0f), ("decay", 0.62f), ("sustain", 0f), ("release", 0.35f), ("drywet", 0.3f), ("volume", 1f));
+ Grain("Deep Swell", "drone", ("scanmode", 0.5f), ("position", 0.5f), ("spray", 0.1f), ("grainsize", 0.9f), ("density", 0.9f), ("spread", 0.3f), ("posrand", 0.05f), ("filfreq", 0.45f), ("attack", 0.45f), ("release", 0.7f), ("volume", 1f));
Section("Keys & Plucks");
- Inst("grain", 10, "Key Scan", ("scanmode", 1f), ("grainsize", 0.4f), ("density", 0.6f), ("spread", 0.4f), ("attack", 0.05f), ("release", 0.4f), ("volume", 0.82f));
- Inst("grain", 10, "Key Morph", ("scanmode", 1f), ("grainshape", 1f / 3f), ("grainsize", 0.55f), ("density", 0.75f), ("spread", 0.5f), ("posrand", 0.08f), ("attack", 0.4f), ("release", 0.6f), ("volume", 0.8f));
// Dry/Wet puts the sample's own attack under the cloud.
- Inst("grain", 10, "Grain Keys", ("scanmode", 0.5f), ("grainsize", 0.6f), ("density", 0.7f), ("spray", 0.05f), ("posrand", 0.03f), ("attack", 0.2f), ("decay", 0.5f), ("sustain", 0.4f), ("release", 0.55f), ("drywet", 0.55f), ("volume", 0.82f));
- Inst("grain", 10, "Soft Pluck", ("scanmode", 0.5f), ("grainsize", 0.5f), ("density", 0.6f), ("attack", 0.1f), ("decay", 0.45f), ("sustain", 0f), ("release", 0.45f), ("filfreq", 0.7f), ("drywet", 0.5f), ("volume", 0.85f));
- Inst("grain", 10, "Granular Perc",("scanmode", 0.5f), ("grainsize", 0.3f), ("density", 0.6f), ("posrand", 0.2f), ("attack", 0f), ("decay", 0.35f), ("sustain", 0f), ("release", 0.3f), ("filfreq", 0.8f), ("filreso", 0.3f), ("volume", 0.85f));
+ Grain("Grain Keys", "epiano", ("scanmode", 0.5f), ("position", 0.02f), ("spray", 0.03f), ("posrand", 0.02f), ("grainsize", 0.6f), ("density", 0.7f), ("attack", 0.05f), ("decay", 0.68f), ("sustain", 0.35f), ("release", 0.55f), ("drywet", 0.55f), ("volume", 1f));
+ Grain("Soft Pluck", "nylon", ("scanmode", 0.5f), ("position", 0f), ("spray", 0f), ("posrand", 0.01f), ("grainsize", 0.5f), ("density", 0.6f), ("filfreq", 0.7f), ("attack", 0.02f), ("decay", 0.65f), ("sustain", 0f), ("release", 0.45f), ("drywet", 0.5f), ("volume", 1f));
+ Grain("Nylon Halo", "nylon", ("scanmode", 0.5f), ("position", 0.12f), ("spray", 0.1f), ("grainsize", 0.7f), ("density", 0.9f), ("spread", 0.8f), ("pitchrand", 0.015f), ("posrand", 0.1f), ("attack", 0.3f), ("release", 0.75f), ("volume", 0.99f));
+ Grain("Harp Scan", "harp-run", ("scanmode", 0f), ("scan", 0.625f), ("position", 0f), ("spray", 0.01f), ("posrand", 0.01f), ("grainsize", 0.5f), ("density", 0.7f), ("spread", 0.5f), ("attack", 0.02f), ("release", 0.5f), ("volume", 0.46f));
+ Grain("Box Slicer", "music-box", ("scanmode", 1f), ("position", 0.3f), ("spray", 0.01f), ("posrand", 0.01f), ("grainsize", 0.5f), ("density", 0.6f), ("spread", 0.4f), ("attack", 0.02f), ("decay", 0.7f), ("sustain", 0.6f), ("release", 0.45f), ("volume", 1f));
+ Grain("Organ Keys", "organ", ("scanmode", 0.5f), ("position", 0.3f), ("spray", 0.05f), ("grainsize", 0.5f), ("density", 0.7f), ("spread", 0.5f), ("attack", 0.02f), ("decay", 0.5f), ("sustain", 0.8f), ("release", 0.35f), ("drywet", 0.6f), ("volume", 0.83f));
+ Grain("Breath Lead", "flute", ("scanmode", 0.5f), ("position", 0.4f), ("spray", 0.1f), ("grainsize", 0.55f), ("density", 0.85f), ("spread", 0.4f), ("posrand", 0.05f), ("attack", 0.25f), ("release", 0.5f), ("drywet", 1f), ("volume", 1f));
+ Section("Bells & Mallets");
+ Grain("Kalimba Cloud", "kalimba", ("scanmode", 0.5f), ("position", 0f), ("spray", 0f), ("posrand", 0.02f), ("grainsize", 0.45f), ("density", 0.7f), ("spread", 0.5f), ("attack", 0f), ("decay", 0.7f), ("sustain", 0f), ("release", 0.6f), ("drywet", 0.6f), ("volume", 0.41f));
+ Grain("Marimba Grains","marimba", ("scanmode", 0.5f), ("position", 0.01f), ("spray", 0f), ("posrand", 0.05f), ("grainsize", 0.35f), ("density", 0.6f), ("spread", 0.4f), ("attack", 0f), ("decay", 0.62f), ("sustain", 0f), ("release", 0.45f), ("drywet", 0.5f), ("volume", 0.65f));
+ Grain("Frozen Bell", "bell", ("scanmode", 0.5f), ("position", 0.08f), ("spray", 0.1f), ("grainsize", 0.7f), ("density", 0.9f), ("spread", 0.8f), ("posrand", 0.08f), ("attack", 0.5f), ("release", 0.85f), ("volume", 0.38f));
+ Grain("Bell Tower", "bell", ("scanmode", 0f), ("scan", 0.625f), ("position", 0f), ("spray", 0f), ("posrand", 0.01f), ("grainsize", 0.6f), ("density", 0.7f), ("spread", 0.5f), ("attack", 0f), ("decay", 0.8f), ("sustain", 0.2f), ("release", 0.8f), ("drywet", 0.7f), ("volume", 0.42f));
+ Grain("Music Box Drift","music-box", ("scanmode", 0f), ("scan", 0.6f), ("grainsize", 0.5f), ("density", 0.75f), ("spread", 0.7f), ("panrand", 0.4f), ("posrand", 0.03f), ("attack", 0.3f), ("release", 0.7f), ("volume", 0.71f));
+ Grain("Glass Chimes", "chimes", ("scanmode", 0.5f), ("position", 0.5f), ("spray", 0.4f), ("posrand", 0.6f), ("grainsize", 0.6f), ("density", 0.7f), ("spread", 1f), ("panrand", 0.8f), ("attack", 0.1f), ("release", 0.7f), ("volume", 0.78f));
+ Section("Drones & Atmospheres");
+ Grain("Deep Drone", "drone", ("scanmode", 0f), ("scan", 0.52f), ("grainsize", 0.9f), ("density", 0.9f), ("spread", 0.5f), ("posrand", 0.05f), ("filfreq", 0.5f), ("attack", 0.8f), ("release", 0.9f), ("volume", 1f));
+ Grain("Ocean Air", "ocean", ("scanmode", 0f), ("scan", 0.6f), ("grainsize", 0.7f), ("density", 0.9f), ("spread", 1f), ("panrand", 0.6f), ("posrand", 0.1f), ("attack", 0.7f), ("release", 0.85f), ("volume", 1f));
+ Grain("Bowed Metal", "metal", ("scanmode", 0.5f), ("position", 0.4f), ("spray", 0.2f), ("grainsize", 0.6f), ("density", 0.9f), ("spread", 0.8f), ("posrand", 0.2f), ("attack", 0.6f), ("release", 0.85f), ("volume", 0.58f));
+ Grain("Low Choir", "choir", ("scanmode", 0.5f), ("position", 0.5f), ("coarse", 0.25f), ("grainsize", 0.85f), ("density", 1f), ("spread", 0.7f), ("posrand", 0.1f), ("filfreq", 0.55f), ("attack", 0.8f), ("release", 0.9f), ("volume", 0.83f));
+ Grain("Tape Ghosts", "tape-chord", ("scanmode", 0f), ("scan", 0.45f), ("grainsize", 0.6f), ("density", 0.8f), ("spread", 0.8f), ("posrand", 0.2f), ("pitchrand", 0.01f), ("filfreq", 0.55f), ("attack", 0.7f), ("release", 0.85f), ("volume", 0.95f));
+ Grain("Dust Cloud", "rain", ("scanmode", 0.5f), ("grainshape", 1f), ("grainsize", 0.2f), ("density", 0.95f), ("spray", 0.5f), ("posrand", 0.6f), ("panrand", 0.8f), ("spread", 0.9f), ("filtype", 1f), ("filfreq", 0.75f), ("filreso", 0.35f), ("attack", 0.5f), ("release", 0.75f), ("volume", 1f));
+ // The Held-chord preview suits these: a loop or a sequence makes its own rhythm.
+ Section("Loops & Sequences");
+ Grain("Break Stutter", "drums", ("scanmode", 0.5f), ("position", 0.5f), ("grainshape", 2f / 3f), ("grainsize", 0.75f), ("density", 0f), ("spray", 0.1f), ("posrand", 0.1f), ("spread", 0.3f), ("attack", 0.02f), ("release", 0.3f), ("volume", 0.85f));
+ Grain("Break Scan", "drums", ("scanmode", 0f), ("scan", 0.625f), ("position", 0f), ("grainshape", 2f / 3f), ("grainsize", 0.45f), ("density", 0.6f), ("spray", 0f), ("posrand", 0f), ("spread", 0.3f), ("attack", 0.02f), ("release", 0.3f), ("volume", 0.98f));
+ Grain("Arp Smear", "arp", ("scanmode", 0f), ("scan", 0.625f), ("position", 0f), ("grainsize", 0.6f), ("density", 0.85f), ("spread", 0.8f), ("posrand", 0.02f), ("filfreq", 0.75f), ("attack", 0.1f), ("release", 0.5f), ("volume", 0.52f));
+ Grain("Glitch Spray", "bits", ("scanmode", 0.5f), ("grainsize", 0.15f), ("density", 0.9f), ("spray", 0.6f), ("posrand", 0.5f), ("pitchrand", 0.3f), ("attack", 0.02f), ("release", 0.2f), ("volume", 0.61f));
+ Grain("Bit Rain", "bits", ("scanmode", 0.5f), ("grainshape", 1f), ("grainsize", 0.05f), ("density", 0.35f), ("spray", 0.8f), ("posrand", 0.8f), ("pitchrand", 0.5f), ("panrand", 1f), ("filtype", 0.5f), ("filfreq", 0.45f), ("attack", 0.05f), ("release", 0.4f), ("volume", 0.84f));
Section("Textures & FX");
- Inst("grain", 10, "Glitch Spray", ("scanmode", 0.5f), ("grainsize", 0.15f), ("density", 0.9f), ("spray", 0.6f), ("posrand", 0.5f), ("pitchrand", 0.3f), ("attack", 0.02f), ("release", 0.2f), ("volume", 0.8f));
- Inst("grain", 10, "Tape Stretch", ("scanmode", 0f), ("scan", 0.52f), ("grainshape", 2f / 3f), ("grainsize", 0.75f), ("density", 0.8f), ("posrand", 0.05f), ("spread", 0.3f), ("attack", 0.6f), ("release", 0.7f), ("volume", 0.8f));
- Inst("grain", 10, "Reverse Tide", ("scanmode", 0f), ("scan", 0.4f), ("grainsize", 0.6f), ("density", 0.75f), ("spread", 0.6f), ("panrand", 0.4f), ("attack", 0.65f), ("release", 0.8f), ("volume", 0.8f));
- Inst("grain", 10, "Wide Scan", ("scanmode", 0f), ("scan", 0.6f), ("grainsize", 0.5f), ("density", 0.8f), ("spread", 1f), ("panrand", 1f), ("posrand", 0.15f), ("drywet", 0.8f), ("attack", 0.4f), ("release", 0.7f), ("volume", 0.78f));
- Inst("grain", 10, "Stutter", ("scanmode", 0.5f), ("grainshape", 2f / 3f), ("grainsize", 0.35f), ("density", 0f), ("spray", 0.3f), ("posrand", 0.2f), ("attack", 0.02f), ("release", 0.3f), ("volume", 0.82f));
- Inst("grain", 10, "Bit Rain", ("scanmode", 0.5f), ("grainshape", 1f), ("grainsize", 0.05f), ("density", 0.35f), ("spray", 0.8f), ("posrand", 0.8f), ("pitchrand", 0.5f), ("panrand", 1f), ("filtype", 0.5f), ("filfreq", 0.45f), ("attack", 0.05f), ("release", 0.4f), ("volume", 0.78f));
- Inst("grain", 10, "Broken Radio", ("scanmode", 0f), ("scan", 0.7f), ("grainsize", 0.25f), ("density", 0.5f), ("spray", 0.4f), ("pitchrand", 0.15f), ("filtype", 1f), ("filfreq", 0.6f), ("filreso", 0.6f), ("volume", 0.8f));
- Inst("grain", 10, "Deep Drone", ("scanmode", 0f), ("scan", 0.52f), ("coarse", 0.25f), ("grainsize", 0.9f), ("density", 0.9f), ("spread", 0.5f), ("filfreq", 0.5f), ("attack", 0.8f), ("release", 0.9f), ("volume", 0.84f));
+ Grain("Broken Radio", "radio", ("scanmode", 0f), ("scan", 0.7f), ("grainsize", 0.25f), ("density", 0.5f), ("spray", 0.4f), ("pitchrand", 0.15f), ("filtype", 1f), ("filfreq", 0.6f), ("filreso", 0.6f), ("volume", 1f));
+ Grain("Reverse Bells", "bell", ("scanmode", 0f), ("scan", 0.4f), ("position", 0.3f), ("grainsize", 0.6f), ("density", 0.75f), ("spread", 0.6f), ("panrand", 0.4f), ("attack", 0.65f), ("release", 0.8f), ("volume", 0.84f));
+ Grain("Whisper Swarm", "whisper", ("scanmode", 0f), ("scan", 0.7f), ("grainsize", 0.15f), ("density", 1f), ("posrand", 0.5f), ("panrand", 1f), ("pitchrand", 0.2f), ("spread", 1f), ("attack", 0.3f), ("release", 0.6f), ("volume", 0.73f));
+ Grain("Detuned Swarm", "strings", ("scanmode", 0.5f), ("fine", 0.52f), ("pitchrand", 0.03f), ("grainsize", 0.55f), ("density", 1f), ("spread", 1f), ("panrand", 0.7f), ("attack", 0.6f), ("release", 0.7f), ("volume", 0.75f));
+ Grain("Metal Rain", "chimes", ("scanmode", 0.5f), ("grainshape", 1f), ("grainsize", 0.1f), ("density", 0.6f), ("spray", 0.7f), ("posrand", 0.7f), ("pitchrand", 0.25f), ("panrand", 1f), ("spread", 1f), ("attack", 0.02f), ("release", 0.6f), ("volume", 1f));
// ---- Nota Flux (kind 11) — vector-morph synth, 25 presets. Vector (X,Y) blends the
// corners WARM (0,0) · GLASS (1,0) · MOOG (0,1) · GRAIN (1,1); target: 0 Filter,
@@ -2463,6 +2497,13 @@ public sealed class FactoryPresetCatalog : IFactoryPresets
private void Inst(string group, int kind, string name, params (string Id, float Value)[] ps)
=> Add(group, name, "builtin-instrument", kind, isInstrument: true, isMidi: false, ps);
+ // A Nota Grain preset that loads its own factory source (GrainSources id) with its params.
+ private void Grain(string name, string source, params (string Id, float Value)[] ps)
+ {
+ Inst("grain", 10, name, ps);
+ _byId["grain/" + name].GrainSource = source;
+ }
+
private void Fx(string group, int kind, string name, params (string Name, float Value)[] ps)
=> Add(group, name, "builtin-effect", kind, isInstrument: false, isMidi: false, ps);
diff --git a/src/managed/Nota.Infrastructure/Persistence/PresetModel.cs b/src/managed/Nota.Infrastructure/Persistence/PresetModel.cs
index 1aaef14f..6aa1da1e 100644
--- a/src/managed/Nota.Infrastructure/Persistence/PresetModel.cs
+++ b/src/managed/Nota.Infrastructure/Persistence/PresetModel.cs
@@ -33,6 +33,11 @@ public sealed class PresetDocument
/// units; instrument values are normalized 0..1.</summary>
public System.Collections.Generic.Dictionary<string, float>? NamedParams { get; set; }
+ /// <summary>Nota Grain factory presets: the factory source (GrainSources id) the
+ /// preset loads before its params. Null = keep whatever sample is loaded.</summary>
+ [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
+ public string? GrainSource { get; set; }
+
// Plugin: stable identity + base64-encoded state blob.
public string PluginId { get; set; } = "";
public string StateBase64 { get; set; } = "";
diff --git a/src/managed/Nota.Infrastructure/Persistence/PresetService.cs b/src/managed/Nota.Infrastructure/Persistence/PresetService.cs
index c7969595..eabeb97d 100644
--- a/src/managed/Nota.Infrastructure/Persistence/PresetService.cs
+++ b/src/managed/Nota.Infrastructure/Persistence/PresetService.cs
@@ -234,7 +234,7 @@ public static class PresetService
&& doc.NamedParams.TryGetValue(pid, out var v))
engine.PluginParamSet(t, -1, i, v);
}
- return "";
+ return LoadGrainSource(doc, engine, t);
}
case "plugin-effect":
{
@@ -260,6 +260,16 @@ public static class PresetService
}
}
+ // A Nota Grain factory preset plays its own source: render it on first use, then load it.
+ private static string LoadGrainSource(PresetDocument doc, IAudioEngine engine, int trackId)
+ {
+ if (doc.BuiltinKind != 10 || string.IsNullOrEmpty(doc.GrainSource)) return "";
+ var src = Grain.GrainSources.ById(doc.GrainSource);
+ var path = Grain.GrainSourceLibrary.Ensure(doc.GrainSource);
+ if (src is null || path is null) return $"Couldn't prepare the sample \"{doc.GrainSource}\".";
+ return engine.SetTrackGrainSample(trackId, path, src.Root) ? "" : $"Couldn't load the sample \"{src.Name}\".";
+ }
+
/// <summary>Applies a preset to an EXISTING instrument/device in place (no new track/
/// device is created) — for the in-header preset picker. Built-in instruments reset all
/// params to default first so the patch is clean; effects set the preset's named params.</summary>
@@ -286,7 +296,7 @@ public static class PresetService
float v = named ? doc.NamedParams![id] : engine.InstrumentParamDefault(trackId, i);
engine.PluginParamSet(trackId, -1, i, v);
}
- return "";
+ return kind == 10 ? LoadGrainSource(doc, engine, trackId) : "";
}
case "builtin-effect":
{
diff --git a/src/native/nota.engine/include/nota/nota_engine.h b/src/native/nota.engine/include/nota/nota_engine.h
index db92aa4c..8a7f034b 100644
--- a/src/native/nota.engine/include/nota/nota_engine.h
+++ b/src/native/nota.engine/include/nota/nota_engine.h
@@ -197,8 +197,9 @@ NOTA_API int32_t nota_audition_add_device(NotaAudition* rig, int32_t kind);
NOTA_API int32_t nota_audition_device_param(NotaAudition* rig, int32_t index, const char* name, float value);
NOTA_API int32_t nota_audition_add_midi_effect(NotaAudition* rig, int32_t kind);
NOTA_API int32_t nota_audition_midi_param(NotaAudition* rig, int32_t index, const char* name, float value);
-/* The Sampler's sample (NULL / "" = a procedural keys tone at C4). */
-NOTA_API int32_t nota_audition_set_sampler_sample(NotaAudition* rig, const char* path_utf8);
+/* The Sampler's or Nota Grain's sample, played at its own pitch on root_note (NULL / "" =
+ * the Sampler's procedural keys tone at C4; Grain keeps its built-in pad). */
+NOTA_API int32_t nota_audition_set_sampler_sample(NotaAudition* rig, const char* path_utf8, int32_t root_note);
/* Effect source: a file (its first max_seconds), or a demo track mixed from part rigs that
* were rendered first (add_source_from). cache_source normalizes the mix to target_peak and
* keeps it process-wide under key; use_cached_source takes it back (0 = not cached). */
diff --git a/src/native/nota.engine/src/Audition.cpp b/src/native/nota.engine/src/Audition.cpp
index bec30fed..91bcc4bd 100644
--- a/src/native/nota.engine/src/Audition.cpp
+++ b/src/native/nota.engine/src/Audition.cpp
@@ -15,6 +15,7 @@
#include "AudioFile.h"
#include "Device.h"
#include "DeviceFactory.h"
+#include "GrainSynth.h"
#include "Instrument.h"
#include "MidiDevice.h"
#include "Sampler.h"
@@ -123,13 +124,19 @@ bool AuditionRig::midiParam(int32_t index, const std::string& name, float value)
return false;
}
-bool AuditionRig::setSamplerSample(const std::string& path) {
+bool AuditionRig::setSamplerSample(const std::string& path, int32_t rootNote) {
+ if (auto* grain = dynamic_cast<GrainSynth*>(inst_.get())) {
+ auto buf = path.empty() ? nullptr : decodeAudioFile(path);
+ if (!buf || buf->empty()) return false;
+ grain->setSample(buf, rootNote, false);
+ return true;
+ }
auto* sampler = dynamic_cast<Sampler*>(inst_.get());
if (!sampler) return false;
std::shared_ptr<SampleBuffer> buf = path.empty() ? nullptr : decodeAudioFile(path);
if (buf && !buf->empty()) {
buf->buildPeakTable();
- sampler->setSample(buf, 60, false);
+ sampler->setSample(buf, rootNote, false);
return true;
}
sampler->setSample(keysTone(sr_), 60, false);
diff --git a/src/native/nota.engine/src/Audition.h b/src/native/nota.engine/src/Audition.h
index b933c941..9584ab5b 100644
--- a/src/native/nota.engine/src/Audition.h
+++ b/src/native/nota.engine/src/Audition.h
@@ -55,8 +55,9 @@ public:
bool deviceParam(int32_t index, const std::string& name, float value);
int32_t addMidiEffect(int32_t kind);
bool midiParam(int32_t index, const std::string& name, float value);
- // The Sampler's sample: a file, or (empty path) a procedural keys tone rooted at C4.
- bool setSamplerSample(const std::string& path);
+ // The Sampler's or Nota Grain's sample, rooted at rootNote: a file, or (empty path, the
+ // Sampler only) a procedural keys tone rooted at C4.
+ bool setSamplerSample(const std::string& path, int32_t rootNote = 60);
bool setSourceFile(const std::string& path, double maxSeconds);
// Mixes another rig's rendered result into this rig's source (a demo track's part).
bool addSourceFrom(const AuditionRig& part, float gain);
diff --git a/src/native/nota.engine/src/nota_engine_audition.cpp b/src/native/nota.engine/src/nota_engine_audition.cpp
index 934dc694..5c32adf4 100644
--- a/src/native/nota.engine/src/nota_engine_audition.cpp
+++ b/src/native/nota.engine/src/nota_engine_audition.cpp
@@ -34,8 +34,8 @@ int32_t nota_audition_add_midi_effect(NotaAudition* rig, int32_t kind) { return
int32_t nota_audition_midi_param(NotaAudition* rig, int32_t index, const char* name, float value) {
return rig && name && RIG(rig)->midiParam(index, name, value) ? 1 : 0;
}
-int32_t nota_audition_set_sampler_sample(NotaAudition* rig, const char* path) {
- return rig && RIG(rig)->setSamplerSample(path ? std::string(path) : std::string()) ? 1 : 0;
+int32_t nota_audition_set_sampler_sample(NotaAudition* rig, const char* path, int32_t root_note) {
+ return rig && RIG(rig)->setSamplerSample(path ? std::string(path) : std::string(), root_note) ? 1 : 0;
}
int32_t nota_audition_add_source_from(NotaAudition* rig, const NotaAudition* part, float gain) {
return rig && part && RIG(rig)->addSourceFrom(*CRIG(part), gain) ? 1 : 0;
diff --git a/tests/Nota.SmokeTest/Program.cs b/tests/Nota.SmokeTest/Program.cs
index a0098430..636b915e 100644
--- a/tests/Nota.SmokeTest/Program.cs
+++ b/tests/Nota.SmokeTest/Program.cs
@@ -2912,26 +2912,59 @@ Console.WriteLine("-- Nota Grain --");
Check(held > 0.01f && held > alone * 5, $"Grain: an overlapping repeat of a note keeps sounding (RMS {held:F3} vs {alone:F4} released)");
}
- // Factory presets: 25 ship, every named param is a real Grain id, each applies in place
- // and renders audible and finite.
+ // Factory presets: 50 ship in folders, every named param is a real Grain id, every preset
+ // outside Basics names a factory source that renders, each applies in place (loading its
+ // source on its root) and renders audible and finite.
{
var cat = new FactoryPresetCatalog();
var mine = cat.All().Where(p => p.IsInstrument && p.BuiltinKind == 10).ToList();
- Check(mine.Count == 25, $"Nota Grain ships 25 factory presets (got {mine.Count})");
+ Check(mine.Count == 50, $"Nota Grain ships 50 factory presets (got {mine.Count})");
+ var folders = mine.Select(p => p.Category).Distinct().ToList();
+ Check(folders.Count >= 8 && mine.All(p => p.Category.Length > 0), $"Grain presets are filed in folders ({string.Join(", ", folders)})");
var bad = mine.SelectMany(p => cat.Document(p.Id)!.NamedParams!.Keys.Where(k => !gIds.Contains(k)).Select(k => $"{p.DisplayName}:{k}")).ToList();
Check(bad.Count == 0, $"every Grain preset param id exists{(bad.Count > 0 ? " — bad: " + string.Join(", ", bad) : "")}");
+ var srcBad = mine.Where(p => (p.Category == "Basics") != (cat.Document(p.Id)!.GrainSource is null)
+ || cat.Document(p.Id)!.GrainSource is { } sid && Nota.Infrastructure.Grain.GrainSources.ById(sid) is null)
+ .Select(p => p.DisplayName).ToList();
+ Check(srcBad.Count == 0, $"every Grain preset outside Basics names a known source{(srcBad.Count > 0 ? " — bad: " + string.Join(", ", srcBad) : "")}");
+ var used = mine.Select(p => cat.Document(p.Id)!.GrainSource).Where(x => x is not null).Distinct().Count();
+ Check(used == Nota.Infrastructure.Grain.GrainSources.All.Count, $"every Grain source is used by a preset ({used}/{Nota.Infrastructure.Grain.GrainSources.All.Count})");
+
+ // Each source renders finite, non-silent and levelled under the ceiling.
+ var srcOff = new System.Collections.Generic.List<string>();
+ foreach (var src in Nota.Infrastructure.Grain.GrainSources.All)
+ {
+ var d = Nota.Infrastructure.Grain.GrainSourceLibrary.Render(src);
+ double pk = 0, acc = 0; bool fin = true;
+ foreach (var x in d) { if (!float.IsFinite(x)) fin = false; pk = Math.Max(pk, Math.Abs(x)); acc += x * x; }
+ double srms = Math.Sqrt(acc / d.Length);
+ if (!fin || pk > 0.9 || srms < 0.01 || d.Length / 2 != (int)(src.Seconds * 48000)) srcOff.Add($"{src.Id} (peak {pk:F2}, RMS {srms:F3})");
+ }
+ Check(srcOff.Count == 0, $"every Grain source renders finite, audible, under −1 dBFS{(srcOff.Count > 0 ? " — off: " + string.Join(", ", srcOff) : "")}");
+
gx.SetClipNotes(g, 0, new[] { new NotaNote(48, 0.0, 1.5, 0.9f), new NotaNote(60, 0.0, 1.5, 0.9f), new NotaNote(67, 0.5, 1.0, 0.9f) });
var off = new System.Collections.Generic.List<string>();
+ var roots = new System.Collections.Generic.List<string>();
var pbuf = new float[GrainFrames(2.0) * 2];
+ float lo = float.MaxValue, hi = 0; string loN = "", hiN = "";
foreach (var p in mine)
{
- if (cat.ApplyInPlace(gx, p.Id, g, -1).Length != 0) { off.Add($"{p.DisplayName} (apply)"); continue; }
+ if (cat.ApplyInPlace(gx, p.Id, g, -1) is { Length: > 0 } warn) { off.Add($"{p.DisplayName} (apply: {warn})"); continue; }
+ if (cat.Document(p.Id)!.GrainSource is { } sid)
+ {
+ var src = Nota.Infrastructure.Grain.GrainSources.ById(sid)!;
+ if (!gx.TryGetGrainInfo(g, out var gi) || gi.SampleId == 0 || gi.RootNote != src.Root) roots.Add($"{p.DisplayName} (root {gi.RootNote})");
+ }
gx.Seek(0.0); gx.Play(); GrainRender(pbuf); GrainStop();
bool ok = true; foreach (var x in pbuf) if (!float.IsFinite(x) || Math.Abs(x) > 1.01f) { ok = false; break; }
float r = Rms(pbuf, pbuf.Length / 2);
if (!ok || r < 0.005f) off.Add($"{p.DisplayName} (RMS {r:F3})");
+ if (r < lo) { lo = r; loN = p.DisplayName; }
+ if (r > hi) { hi = r; hiN = p.DisplayName; }
}
+ Check(roots.Count == 0, $"every sourced Grain preset loads its sample on the source's root{(roots.Count > 0 ? " — off: " + string.Join(", ", roots) : "")}");
Check(off.Count == 0, $"every Grain preset is audible and finite{(off.Count > 0 ? " — off: " + string.Join(", ", off) : "")}");
+ Console.WriteLine($" (Grain preset RMS {lo:F3} {loN} … {hi:F3} {hiN})");
}
}