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) Support of MPE recording

Egor Khindikaynen3e1c8d266865
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0a77469d..c3ad7162 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -40,7 +40,11 @@ Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.
Operator's FM depth. Settings ▸ MIDI ▸ MPE switches it and sets the bend range (±48 by
default; a controller that announces its own range wins). An ordinary keyboard on channel 1
plays as before — and its pitch wheel and aftertouch now reach Nota's synths and hosted
- plug-ins too. Expression is played live; it isn't recorded into clips yet.
+ plug-ins too. Recording keeps each note's bend, pressure and slide with it: the take plays
+ back exactly as performed, the expression moves, copies and quantizes with its note and is
+ saved with the project, and the piano roll draws it over the note — bend as the pitch line
+ you played, pressure as a shade. (The pitch wheel, channel pressure and sustain pedal
+ aren't recorded.)
- **Nota Remote: Expressive keys.** On a track that takes MPE, the Keys screen's Keyboard and
Scale modes get an **Expressive** switch: a finger keeps its note, sliding sideways bends it,
the height on the key changes its timbre, and a phone that senses touch force sends pressure.
diff --git a/FEATURES.md b/FEATURES.md
index 5c8dc18a..6cb8a321 100644
--- a/FEATURES.md
+++ b/FEATURES.md
@@ -209,7 +209,10 @@ the mixer opens as a separate floating window (**View → Mixer**, ⌘M).
index, Physical: how long the body rings, Keys: drives the tine into the pickup / shifts its
symmetry). Instrument Racks pass it to their chains; hosted plug-ins get the wheel, channel
pressure, polyphonic aftertouch and the sustain pedal (CC64), which also holds Nota Keys'
- notes. Expression is played live — it is not recorded into clips.
+ notes. **Recording** keeps each note's bend, pressure and slide with the note: it plays back
+ from the clip, moves, copies and quantizes with the note, is saved with the project, and the
+ piano roll draws it over the note (bend as the pitch it played, pressure as a shade). The
+ pitch wheel, channel pressure and sustain pedal aren't recorded.
- **Nota Remote Keys — Expressive**: on a track whose instrument takes MPE, the phone's
Keyboard and Scale modes can keep each finger on the note it struck: sliding sideways bends
it (a key's width is its pitch step), the height on the key is its slide, and a screen that
diff --git a/src/managed/Nota.App/MainWindow.Detail.cs b/src/managed/Nota.App/MainWindow.Detail.cs
index de288bb1..9356abf5 100644
--- a/src/managed/Nota.App/MainWindow.Detail.cs
+++ b/src/managed/Nota.App/MainWindow.Detail.cs
@@ -421,6 +421,7 @@ public partial class MainWindow
},
// Light up the key being played (computer keyboard S–K / MIDI), regardless of routing.
PollHeldNotes = buf => Engine.LiveHeldNotes(buf),
+ ExpressionOf = id => Engine.GetNoteExpression(id),
};
double length = Engine.TryGetClipInfo(trackId, clipIndex, out var ci) && ci.LengthBeats > 0 ? ci.LengthBeats : 4;
double start = ci.StartBeat;
@@ -600,6 +601,7 @@ public partial class MainWindow
_patternView?.Reload();
},
PollHeldNotes = buf => Engine.LiveHeldNotes(buf),
+ ExpressionOf = id => Engine.GetNoteExpression(id),
};
double slotLen = Engine.SessionSlotLength(trackId, scene);
roll.SetTrackColor(TrackBrush(trackId));
diff --git a/src/managed/Nota.App/PianoRollView.cs b/src/managed/Nota.App/PianoRollView.cs
index e0a33713..7b6abf9a 100644
--- a/src/managed/Nota.App/PianoRollView.cs
+++ b/src/managed/Nota.App/PianoRollView.cs
@@ -34,8 +34,19 @@ public sealed class PianoRollView : UserControl
/// <summary>Polls the engine for currently-pressed live-input pitches (keyboard + MIDI) into
/// the buffer, returning the count — used to light up the played key. Set by the host.</summary>
public Func<int[], int>? PollHeldNotes { get; set; }
+ /// <summary>A note's recorded MPE by <see cref="NotaNote.ExprId"/>, drawn over the note.
+ /// Curves never change under an id, so they are fetched once. Set by the host.</summary>
+ public Func<int, NotaExprPoint[]>? ExpressionOf { get; set; }
public event Action? Changed;
+ private readonly Dictionary<int, NotaExprPoint[]> _exprCache = new();
+ private NotaExprPoint[] ExprOf(int id)
+ {
+ if (id <= 0 || ExpressionOf is null) return Array.Empty<NotaExprPoint>();
+ if (!_exprCache.TryGetValue(id, out var pts)) _exprCache[id] = pts = ExpressionOf(id);
+ return pts;
+ }
+
// Currently-pressed keys (from PollHeldNotes), for the played-key highlight.
private readonly HashSet<int> _held = new();
private readonly int[] _heldBuf = new int[16];
@@ -215,6 +226,11 @@ public sealed class PianoRollView : UserControl
private static readonly IBrush KeyLabelHot = NotaPalette.TextPrimary;
private static readonly IBrush KeyLabelOnAccent = NotaPalette.TextOnAccent;
private static readonly IBrush Divider = NotaPalette.BorderDefault;
+ // Recorded MPE over a note: the bend as the pitch line it played, pressure as a shade
+ // rising from the note's floor.
+ private static readonly IPen BendPen = new Pen(NotaPalette.TextPrimary, 1.25, lineCap: PenLineCap.Round);
+ private static readonly IPen BendPenOnSel = new Pen(NotaPalette.TextOnAccent, 1.25, lineCap: PenLineCap.Round);
+ private static readonly IBrush PressureShade = NotaPalette.Wash(NotaPalette.SurfaceAbyss, 0x66);
private static readonly Typeface Mono = NotaFonts.Mono;
private static double RangeH => (MaxPitch - MinPitch + 1) * RowH;
@@ -990,6 +1006,7 @@ public sealed class PianoRollView : UserControl
if (!_o.InScale(n.Pitch)) a *= 0.4; // out-of-scale notes read faint
ctx.FillRectangle(new SolidColorBrush(Color.FromArgb((byte)(255 * a), c.R, c.G, c.B)), r, 2);
}
+ if (n.ExprId > 0) DrawExpression(ctx, n, r, sel, ppb);
// Resize-edge affordance: a bright bar on the edge a drag would trim.
bool edgeHi = ((_drag == Mode.Resize || _drag == Mode.ResizeLeft) && sel) || (_drag == Mode.None && _hoverNote == i);
if (edgeHi)
@@ -1012,6 +1029,54 @@ public sealed class PianoRollView : UserControl
}
}
+ // The note's recorded MPE: pressure shades up from its floor, the bend is drawn as the
+ // pitch it played (a semitone per row), both held flat before the first point and after
+ // the last, and both kept within the note's length.
+ private void DrawExpression(DrawingContext ctx, NotaNote n, Rect r, bool sel, double ppb)
+ {
+ var pts = _o.ExprOf(n.ExprId);
+ if (pts.Length == 0) return;
+ double x0 = r.X, x1 = r.Right, mid = r.Y + RowH * 0.5;
+ List<Point> Curve(NoteExpressionDim d, Func<float, double> y)
+ {
+ var line = new List<Point>();
+ foreach (var p in pts)
+ {
+ if (p.Dim != d) continue;
+ double x = Math.Clamp(x0 + p.Beat * ppb, x0, x1);
+ if (line.Count == 0 && x > x0) line.Add(new Point(x0, y(p.Value)));
+ line.Add(new Point(x, y(p.Value)));
+ }
+ if (line.Count > 0 && line[^1].X < x1) line.Add(new Point(x1, line[^1].Y));
+ return line;
+ }
+ var pressure = Curve(NoteExpressionDim.Pressure, v => r.Bottom - Math.Clamp(v, 0, 1) * (r.Height - 1));
+ if (pressure.Count > 1)
+ {
+ var g = new StreamGeometry();
+ using (var gc = g.Open())
+ {
+ gc.BeginFigure(new Point(pressure[0].X, r.Bottom), true);
+ foreach (var p in pressure) gc.LineTo(p);
+ gc.LineTo(new Point(pressure[^1].X, r.Bottom));
+ gc.EndFigure(true);
+ }
+ using (ctx.PushClip(r)) ctx.DrawGeometry(PressureShade, null, g);
+ }
+ var bend = Curve(NoteExpressionDim.Bend, v => mid - v * RowH);
+ if (bend.Count > 1)
+ {
+ var g = new StreamGeometry();
+ using (var gc = g.Open())
+ {
+ gc.BeginFigure(bend[0], false);
+ for (int i = 1; i < bend.Count; i++) gc.LineTo(bend[i]);
+ gc.EndFigure(false);
+ }
+ ctx.DrawGeometry(null, sel ? BendPenOnSel : BendPen, g);
+ }
+ }
+
private static Rect RectOf(Point a, Point b)
=> new(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Abs(a.X - b.X), Math.Abs(a.Y - b.Y));
diff --git a/src/managed/Nota.Application/Engine/EngineContracts.cs b/src/managed/Nota.Application/Engine/EngineContracts.cs
index ed1fdac8..107e9d7b 100644
--- a/src/managed/Nota.Application/Engine/EngineContracts.cs
+++ b/src/managed/Nota.Application/Engine/EngineContracts.cs
@@ -20,6 +20,19 @@ public struct NotaNote(int pitch, double startBeat, double lengthBeats, float ve
public double StartBeat = startBeat;
public double LengthBeats = lengthBeats;
public float Velocity = velocity;
+ /// <summary>The note's recorded MPE in the engine's expression store (0 = none). Copy the
+ /// note (don't rebuild it from its fields) and the expression travels with it.</summary>
+ public int ExprId;
+}
+
+/// <summary>One breakpoint of a note's recorded expression (MPE). Matches native NotaExprPoint:
+/// Beat is the offset from the note's start; Value is semitones for Bend, 0..1 otherwise.</summary>
+[StructLayout(LayoutKind.Sequential)]
+public struct NotaExprPoint(NoteExpressionDim dim, float beat, float value)
+{
+ public NoteExpressionDim Dim = dim;
+ public float Beat = beat;
+ public float Value = value;
}
/// <summary>Track summary for the Arrangement View. Matches native NotaTrackInfo.</summary>
diff --git a/src/managed/Nota.Application/Engine/IAudioEngine.cs b/src/managed/Nota.Application/Engine/IAudioEngine.cs
index 8a3ff379..2d49aede 100644
--- a/src/managed/Nota.Application/Engine/IAudioEngine.cs
+++ b/src/managed/Nota.Application/Engine/IAudioEngine.cs
@@ -756,6 +756,11 @@ public interface IAudioEngine : IDisposable
/// <summary>Live note update with no undo checkpoint (piano-roll drag pushing every frame).</summary>
void SetClipNotesLive(int trackId, int clipIndex, NotaNote[] notes);
NotaNote[] GetClipNotes(int trackId, int clipIndex);
+ /// <summary>Stores a note expression curve (recorded MPE) and returns its id for
+ /// <see cref="NotaNote.ExprId"/> (0 when there are no points). Ids live as long as the engine.</summary>
+ int CreateNoteExpression(NotaExprPoint[] points);
+ /// <summary>The curve behind an expression id (empty for 0 / unknown), per dim sorted by beat.</summary>
+ NotaExprPoint[] GetNoteExpression(int exprId);
// --- Live MIDI, arming & recording -------------------------------------
void SetTrackArmed(int trackId, bool armed);
diff --git a/src/managed/Nota.Application/Midi/MidiToolUtil.cs b/src/managed/Nota.Application/Midi/MidiToolUtil.cs
index 50c16a70..2d621e12 100644
--- a/src/managed/Nota.Application/Midi/MidiToolUtil.cs
+++ b/src/managed/Nota.Application/Midi/MidiToolUtil.cs
@@ -40,7 +40,9 @@ public static class MidiToolUtil
double start = Math.Max(0, n.StartBeat);
if (start >= lengthBeats - 1e-9) continue;
double len = Math.Min(Math.Max(MinLength, n.LengthBeats), lengthBeats - start);
- outp.Add(Note(n.Pitch, start, len, n.Velocity));
+ var m = Note(n.Pitch, start, len, n.Velocity);
+ m.ExprId = n.ExprId; // a moved / trimmed note keeps its recorded MPE
+ outp.Add(m);
}
return Sorted(outp);
}
diff --git a/src/managed/Nota.Infrastructure/Interop/NativeMethods.Tracks.cs b/src/managed/Nota.Infrastructure/Interop/NativeMethods.Tracks.cs
index b4836cf6..c5aae483 100644
--- a/src/managed/Nota.Infrastructure/Interop/NativeMethods.Tracks.cs
+++ b/src/managed/Nota.Infrastructure/Interop/NativeMethods.Tracks.cs
@@ -520,6 +520,12 @@ internal static partial class NativeMethods
internal static partial int ClipGetNotes(IntPtr engine, int trackId, int clipIndex,
[Out] NotaNote[] outNotes, int maxNotes);
+ [LibraryImport(Lib, EntryPoint = "nota_note_expr_create")]
+ internal static partial int NoteExprCreate(IntPtr engine, [In] NotaExprPoint[] points, int count);
+
+ [LibraryImport(Lib, EntryPoint = "nota_note_expr_points")]
+ internal static partial int NoteExprPoints(IntPtr engine, int exprId, [Out] NotaExprPoint[]? outPoints, int maxPoints);
+
[LibraryImport(Lib, EntryPoint = "nota_clip_note_count")]
internal static partial int ClipNoteCount(IntPtr engine, int trackId, int clipIndex);
diff --git a/src/managed/Nota.Infrastructure/Interop/NotaEngine.Tracks.cs b/src/managed/Nota.Infrastructure/Interop/NotaEngine.Tracks.cs
index ab047960..e0374f32 100644
--- a/src/managed/Nota.Infrastructure/Interop/NotaEngine.Tracks.cs
+++ b/src/managed/Nota.Infrastructure/Interop/NotaEngine.Tracks.cs
@@ -696,6 +696,23 @@ public sealed partial class NotaEngine
return written == count ? buf : buf[..written];
}
+ public int CreateNoteExpression(NotaExprPoint[] points)
+ {
+ ThrowIfDisposed();
+ return points.Length == 0 ? 0 : NativeMethods.NoteExprCreate(_handle, points, points.Length);
+ }
+
+ public NotaExprPoint[] GetNoteExpression(int exprId)
+ {
+ ThrowIfDisposed();
+ if (exprId <= 0) return Array.Empty<NotaExprPoint>();
+ int count = NativeMethods.NoteExprPoints(_handle, exprId, null, 0);
+ if (count <= 0) return Array.Empty<NotaExprPoint>();
+ var buf = new NotaExprPoint[count];
+ int written = NativeMethods.NoteExprPoints(_handle, exprId, buf, count);
+ return written == count ? buf : buf[..written];
+ }
+
// --- Audio persistence (M7-6b) -----------------------------------------
/// <summary>Full geometry of an audio clip. False if the clip at that index isn't audio.</summary>
diff --git a/src/managed/Nota.Infrastructure/Persistence/ProjectModel.cs b/src/managed/Nota.Infrastructure/Persistence/ProjectModel.cs
index 4cdddb13..176526d4 100644
--- a/src/managed/Nota.Infrastructure/Persistence/ProjectModel.cs
+++ b/src/managed/Nota.Infrastructure/Persistence/ProjectModel.cs
@@ -378,7 +378,47 @@ public sealed class NoteDto
public double Length { get; set; }
public float Velocity { get; set; } = 1.0f;
+ /// <summary>Recorded MPE, when the note has any (omitted otherwise).</summary>
+ public NoteMpeDto? Mpe { get; set; }
+
public NoteDto() { }
public NoteDto(NotaNote n) { Pitch = n.Pitch; Start = n.StartBeat; Length = n.LengthBeats; Velocity = n.Velocity; }
public NotaNote ToNote() => new(Pitch, Start, Length, Velocity);
}
+
+/// <summary>A note's recorded expression: per dimension a flat [beat, value, beat, value…] list,
+/// beats from the note's start (Bend in semitones, Pressure / Slide 0..1).</summary>
+public sealed class NoteMpeDto
+{
+ public float[]? Bend { get; set; }
+ public float[]? Pressure { get; set; }
+ public float[]? Slide { get; set; }
+
+ [JsonExtensionData] public Dictionary<string, JsonElement>? Extra { get; set; }
+
+ public static NoteMpeDto? From(NotaExprPoint[] points)
+ {
+ if (points.Length == 0) return null;
+ float[]? Of(NoteExpressionDim d)
+ {
+ var list = new List<float>();
+ foreach (var p in points) if (p.Dim == d) { list.Add(p.Beat); list.Add(p.Value); }
+ return list.Count > 0 ? list.ToArray() : null;
+ }
+ return new NoteMpeDto { Bend = Of(NoteExpressionDim.Bend), Pressure = Of(NoteExpressionDim.Pressure), Slide = Of(NoteExpressionDim.Slide) };
+ }
+
+ public NotaExprPoint[] ToPoints()
+ {
+ var list = new List<NotaExprPoint>();
+ void Add(NoteExpressionDim d, float[]? flat)
+ {
+ if (flat is null) return;
+ for (int i = 0; i + 1 < flat.Length; i += 2) list.Add(new NotaExprPoint(d, flat[i], flat[i + 1]));
+ }
+ Add(NoteExpressionDim.Bend, Bend);
+ Add(NoteExpressionDim.Pressure, Pressure);
+ Add(NoteExpressionDim.Slide, Slide);
+ return list.ToArray();
+ }
+}
diff --git a/src/managed/Nota.Infrastructure/Persistence/ProjectService.cs b/src/managed/Nota.Infrastructure/Persistence/ProjectService.cs
index e411ba6a..86ca0429 100644
--- a/src/managed/Nota.Infrastructure/Persistence/ProjectService.cs
+++ b/src/managed/Nota.Infrastructure/Persistence/ProjectService.cs
@@ -191,7 +191,7 @@ public sealed class ProjectService
Active = ci.IsActive,
StartBeat = ci.StartBeat,
LengthBeats = ci.LengthBeats,
- Notes = ToDtos(engine.GetClipNotes(ti.Id, c)),
+ Notes = ToDtos(engine, engine.GetClipNotes(ti.Id, c)),
VelocityEnvelope = vel.Length > 0 ? System.Array.ConvertAll(vel, p => new AutomationPointDto(p)) : null,
VolumeEnvelope = vol.Length > 0 ? System.Array.ConvertAll(vol, p => new AutomationPointDto(p)) : null,
});
@@ -225,7 +225,7 @@ public sealed class ProjectService
{
Scene = s,
LengthBeats = engine.SessionSlotLength(ti.Id, s),
- Notes = ToDtos(engine.GetSessionNotes(ti.Id, s)),
+ Notes = ToDtos(engine, engine.GetSessionNotes(ti.Id, s)),
}));
}
else if (ti.Type == 0 && engine.TryGetSessionAudioSlot(ti.Id, s, out var sa))
@@ -616,7 +616,7 @@ public sealed class ProjectService
if (ci < 0) continue;
if (c.Name is { Length: > 0 } mcn) engine.SetClipName(id, ci, mcn);
if (!c.Active) engine.SetClipActive(id, ci, false); // clip deactivate (v17)
- if (c.Notes.Length > 0) engine.SetClipNotes(id, ci, ToNotes(c.Notes));
+ if (c.Notes.Length > 0) engine.SetClipNotes(id, ci, ToNotes(engine, c.Notes));
if (c.VelocityEnvelope is { Length: > 0 } ve) // MIDI clip envelopes (v10)
engine.SetMidiClipEnvelope(id, ci, MidiClipEnvelope.Velocity, System.Array.ConvertAll(ve, p => p.ToPoint()));
if (c.VolumeEnvelope is { Length: > 0 } vo)
@@ -657,7 +657,7 @@ public sealed class ProjectService
else
{
engine.AddSessionMidiClip(id, sl.Scene, sl.LengthBeats);
- if (sl.Notes.Length > 0) engine.SetSessionNotes(id, sl.Scene, ToNotes(sl.Notes));
+ if (sl.Notes.Length > 0) engine.SetSessionNotes(id, sl.Scene, ToNotes(engine, sl.Notes));
}
ApplyClipProps(engine, id, sl);
}
@@ -941,17 +941,34 @@ public sealed class ProjectService
return false;
}
- private static NoteDto[] ToDtos(NotaNote[] notes)
+ // A note's recorded MPE goes inline with it (notes sharing one curve, like a duplicate,
+ // each carry a copy).
+ private static NoteDto[] ToDtos(IAudioEngine engine, NotaNote[] notes)
{
var arr = new NoteDto[notes.Length];
- for (int i = 0; i < notes.Length; i++) arr[i] = new NoteDto(notes[i]);
+ var cache = new Dictionary<int, NoteMpeDto?>();
+ for (int i = 0; i < notes.Length; i++)
+ {
+ arr[i] = new NoteDto(notes[i]);
+ int id = notes[i].ExprId;
+ if (id <= 0) continue;
+ if (!cache.TryGetValue(id, out var mpe)) cache[id] = mpe = NoteMpeDto.From(engine.GetNoteExpression(id));
+ arr[i].Mpe = mpe;
+ }
return arr;
}
- private static NotaNote[] ToNotes(NoteDto[] dtos)
+ private static NotaNote[] ToNotes(IAudioEngine engine, NoteDto[] dtos)
{
var arr = new NotaNote[dtos.Length];
- for (int i = 0; i < dtos.Length; i++) arr[i] = dtos[i].ToNote();
+ var ids = new Dictionary<NoteMpeDto, int>(ReferenceEqualityComparer.Instance);
+ for (int i = 0; i < dtos.Length; i++)
+ {
+ arr[i] = dtos[i].ToNote();
+ if (dtos[i].Mpe is not { } mpe) continue;
+ if (!ids.TryGetValue(mpe, out int id)) ids[mpe] = id = engine.CreateNoteExpression(mpe.ToPoints());
+ arr[i].ExprId = id;
+ }
return arr;
}
diff --git a/src/native/nota.engine/include/nota/nota_engine.h b/src/native/nota.engine/include/nota/nota_engine.h
index 6e798313..ac4af1ea 100644
--- a/src/native/nota.engine/include/nota/nota_engine.h
+++ b/src/native/nota.engine/include/nota/nota_engine.h
@@ -46,8 +46,18 @@ typedef struct NotaNoteData {
double start_beat; /* relative to clip start */
double length_beats;
float velocity; /* 0..1 */
+ int32_t expr_id; /* recorded MPE (nota_note_expr_*), 0 = none. Keep it when you move,
+ * copy or resize the note — the expression travels with it. */
} NotaNoteData;
+/* One breakpoint of a note's recorded expression (MPE). dim: 0 bend (semitones), 1 pressure
+ * 0..1, 2 slide 0..1; beat is the offset from the note's start. */
+typedef struct NotaExprPoint {
+ int32_t dim;
+ float beat;
+ float value;
+} NotaExprPoint;
+
/* A parameter-automation breakpoint (M9). `value` is in the target's native
* units (volume ~0..2, pan -1..1, device param in its min..max). Sorted by beat.
* `curve` (M9-D) shapes the segment to the next point: 0 linear, [-1,1]. */
@@ -675,6 +685,12 @@ NOTA_API int32_t nota_engine_live_held_notes(const NotaEngine* engine, int32_t*
NOTA_API int32_t nota_clip_get_notes(const NotaEngine* engine, int32_t track_id, int32_t clip_index,
NotaNoteData* out, int32_t max_notes);
NOTA_API int32_t nota_clip_note_count(const NotaEngine* engine, int32_t track_id, int32_t clip_index);
+/* Recorded MPE lives in an engine-wide store of immutable curves; a note refers to one by
+ * NotaNoteData.expr_id. create stores `count` points (any order; per dim sorted by beat)
+ * and returns the new id (0 when there are no points). points copies a curve out
+ * (out = NULL → just the count). Ids stay valid for the life of the engine. */
+NOTA_API int32_t nota_note_expr_create(NotaEngine* engine, const NotaExprPoint* points, int32_t count);
+NOTA_API int32_t nota_note_expr_points(const NotaEngine* engine, int32_t expr_id, NotaExprPoint* out, int32_t max_points);
/* ---- Live MIDI input, arming & recording (M2) ---------------------------- */
NOTA_API NotaResult nota_track_set_armed(NotaEngine* engine, int32_t track_id, int32_t armed);
diff --git a/src/native/nota.engine/src/Engine.h b/src/native/nota.engine/src/Engine.h
index c9a27299..be844408 100644
--- a/src/native/nota.engine/src/Engine.h
+++ b/src/native/nota.engine/src/Engine.h
@@ -30,7 +30,9 @@
#include <functional>
#include <list>
#include <memory>
+#include <map>
#include <mutex>
+#include <unordered_map>
#include <string>
#include <vector>
@@ -50,6 +52,12 @@ using MidiQueue = SpscRingBuffer<MidiEvent, 1024>;
// Recorded note (audio -> message), lock-free up-queue. startBeat is absolute.
struct RecordedNote { int32_t pitch; double startBeat; double lengthBeats; float velocity; };
using RecordedQueue = SpscRingBuffer<RecordedNote, 1024>;
+// A recorded note's expression sample (audio -> message): the note is identified by its pitch
+// and start (as its RecordedNote will carry them), `beat` is the offset from that start.
+struct RecordedExpr { int32_t pitch; double startBeat; int32_t dim; float beat; float value; };
+using RecordedExprQueue = SpscRingBuffer<RecordedExpr, 1u << 14>;
+// A clip note's expression at a sample offset of the block (the render pass replays these).
+struct ExprEv { int32_t off; int32_t pitch; int32_t dim; float value; };
// Captured input frame (input thread -> message), lock-free up-queue (M4-3).
struct InputFrame { float l; float r; };
@@ -900,6 +908,14 @@ public:
bool setClipNotesLive(int32_t trackId, int32_t clipIndex, const NotaNoteData* notes, int32_t count);
int32_t getClipNotes(int32_t trackId, int32_t clipIndex, NotaNoteData* out, int32_t maxNotes) const;
int32_t clipNoteCount(int32_t trackId, int32_t clipIndex) const;
+ // Recorded MPE store (MidiClip.h NoteExpr): immutable curves under stable ids, shared by
+ // every note that refers to them. Message thread (guarded, so any thread may read).
+ int32_t createNoteExpr(const NotaExprPoint* points, int32_t count);
+ int32_t storeNoteExpr(std::shared_ptr<const NoteExpr> e);
+ int32_t noteExprPoints(int32_t id, NotaExprPoint* out, int32_t maxPoints) const;
+ // NotaNoteData <-> Note, resolving / reporting the expression id.
+ Note noteFromData(const NotaNoteData& d) const;
+ static void noteToData(const Note& n, NotaNoteData& d);
// --- live MIDI, arming, recording (M2) ---
void setTrackArmed(int32_t trackId, bool armed);
@@ -995,10 +1011,12 @@ private:
// auditioned) to evs; returns the new count. Shared by a track's own render and by MIDI
// routing (a destination gathers its sources' notes the same way).
int gatherInstrumentNotes(Track& t, MidiEv* evs, int n, int32_t frames,
- double blockStart, double spb, bool playing);
+ double blockStart, double spb, bool playing,
+ ExprEv* xev = nullptr, int* xn = nullptr);
// A track's own post-MIDI-FX events (gather + drum swing + its MIDI FX chain), run once.
int computeInstrumentMidi(Track& t, MidiEv* evs, int32_t frames,
- double blockStart, double spb, bool playing, bool arrangementActive);
+ double blockStart, double spb, bool playing, bool arrangementActive,
+ ExprEv* xev = nullptr, int* xn = nullptr);
// Pre-pass: fill blockMidi_/blockMidiN_ for every non-session instrument track.
void computeBlockMidi(Graph* g, int32_t frames, double blockStart, double spb, bool playing, bool arrangementActive);
// Hand a MIDI-keyed device its source track's notes for this block (from blockMidi_).
@@ -1099,6 +1117,14 @@ private:
MidiQueue remoteMidi_; // track-addressed notes (any thread → audio)
std::mutex remoteMidiMx_; // producer side only
RecordedQueue recorded_;
+ RecordedExprQueue recordedExpr_;
+ mutable std::mutex exprMx_; // the expression store
+ std::unordered_map<int32_t, std::shared_ptr<const NoteExpr>> exprStore_;
+ int32_t nextExprId_ = 1;
+ // Expression of notes still being recorded, keyed by (pitch, start) — message thread.
+ std::map<std::pair<int32_t, double>, NoteExpr> pendingExpr_;
+ void collectRecordedExpr();
+ void attachRecordedExpr(Note& n, int32_t pitch, double startBeat);
ControlEventQueue midiControl_; // incoming CC/note-on for MIDI-learn (AR-6)
Transport transport_;
@@ -1337,6 +1363,18 @@ private:
double recRoutedStart_[128] = {};
float recRoutedVel_[128] = {};
void captureRoutedNotes(const MidiEv* evs, int n, double blockStart, double spb);
+ void captureLiveExpr(double nowBeat, double slotLen); // recorded MPE (drainLiveMidi)
+
+ // Clip-note expression replayed per block (computeBlockMidi fills, the render pass merges),
+ // per track position like blockMidi_.
+ static constexpr int kMaxBlockExpr = 1024;
+ std::vector<std::vector<ExprEv>> blockExpr_;
+ std::vector<int> blockExprN_;
+ // Appends a clip note's recorded expression for the block: the note sounds over
+ // [onS, offS) in samples relative to the block start; one value per dim at the note's
+ // start (or the block's) and then every kExprStep samples while its curve moves.
+ static void noteExprEvents(const Note& note, double onS, double offS, int32_t frames, double spb,
+ ExprEv* out, int& n, int cap);
// Live input monitoring: the input thread also feeds this ring while any audio track
// monitors the hardware input; the audio thread pops one segment's worth per mixGraph
diff --git a/src/native/nota.engine/src/Engine_Recording.cpp b/src/native/nota.engine/src/Engine_Recording.cpp
index fa68dd60..a1b7a893 100644
--- a/src/native/nota.engine/src/Engine_Recording.cpp
+++ b/src/native/nota.engine/src/Engine_Recording.cpp
@@ -213,6 +213,7 @@ void Engine::setRecording(bool on) {
// when no track is armed for a take — if the UI then pops the button back up
// it calls us again with false, which clears it.
setAutomationRecord(on);
+ if (on) { collectRecordedExpr(); pendingExpr_.clear(); } // a new take starts with no stale curves
if (!on) {
recording_.store(false, std::memory_order_relaxed);
stopAudioRecording(); // materialise if an audio take was rolling
diff --git a/src/native/nota.engine/src/Engine_Render.cpp b/src/native/nota.engine/src/Engine_Render.cpp
index bbbff9f3..cafbc2b0 100644
--- a/src/native/nota.engine/src/Engine_Render.cpp
+++ b/src/native/nota.engine/src/Engine_Render.cpp
@@ -57,6 +57,62 @@ constexpr float kMetroGain = 0.5f;
inline bool midiEvLess(const MidiEv& a, const MidiEv& b) {
return a.off < b.off || (a.off == b.off && !a.on && b.on);
}
+
+// Drive an instrument through one block: note events and clip-note expression merged by
+// sample offset (notes first at a shared offset, so a note's opening expression lands on it),
+// rendering the stretches in between. `live` runs once, before the first sample is rendered —
+// after the offset-0 notes, where live input sits — to apply the block's live expression.
+template <class Live>
+void playEvents(Instrument& inst, float* dst, int32_t frames, const MidiEv* evs, int n,
+ const ExprEv* xev, int xn, Live&& live) {
+ int32_t cursor = 0;
+ bool liveDone = false;
+ auto renderTo = [&](int32_t to) {
+ if (to <= cursor) return;
+ if (!liveDone) { live(); liveDone = true; }
+ inst.render(&dst[cursor * 2], to - cursor);
+ cursor = to;
+ };
+ int k = 0, x = 0;
+ while (k < n || x < xn) {
+ const int32_t nOff = k < n ? std::clamp(evs[k].off, 0, frames) : INT32_MAX;
+ const int32_t xOff = x < xn ? std::clamp(xev[x].off, 0, frames) : INT32_MAX;
+ if (nOff <= xOff) {
+ renderTo(nOff);
+ if (evs[k].on) inst.noteOn(evs[k].pitch, evs[k].vel);
+ else inst.noteOff(evs[k].pitch);
+ ++k;
+ } else {
+ renderTo(xOff);
+ inst.noteExpression(xev[x].pitch, xev[x].dim, xev[x].value);
+ ++x;
+ }
+ }
+ if (!liveDone) { live(); liveDone = true; }
+ renderTo(frames);
+}
+}
+
+void Engine::noteExprEvents(const Note& note, double onS, double offS, int32_t frames, double spb,
+ ExprEv* out, int& n, int cap) {
+ static constexpr int32_t kExprStep = 64; // ~1.3 ms at 48 kHz; the voices smooth between
+ if (!note.expr || spb <= 0.0 || offS <= 0.0 || onS >= frames || offS <= onS) return;
+ const int32_t first = static_cast<int32_t>(std::max(0.0, onS)); // the note-on's own offset
+ const int32_t end = static_cast<int32_t>(std::min<double>(frames, offS));
+ if (first >= frames) return;
+ const NoteExpr& e = *note.expr;
+ for (int32_t d = 0; d < kExprDims; ++d) {
+ const auto& c = e.dim[d];
+ if (c.empty()) continue;
+ auto valueAt = [&](int32_t off) { return e.valueAt(d, (off - onS) / spb); };
+ if (n < cap) out[n++] = ExprEv{ first, note.pitch, d, valueAt(first) };
+ if (c.size() < 2) continue;
+ // While the curve moves (one step past its last point, to land on it).
+ const double moveFrom = onS + c.front().beat * spb - kExprStep;
+ const double moveTo = onS + c.back().beat * spb + kExprStep;
+ for (int32_t off = (first / kExprStep + 1) * kExprStep; off < end && n < cap; off += kExprStep)
+ if (off >= moveFrom && off <= moveTo) out[n++] = ExprEv{ off, note.pitch, d, valueAt(off) };
+ }
}
// Runs the block's sorted note events through the track's MIDI FX chain (bypass =
@@ -137,6 +193,8 @@ void Engine::poll() {
RecordedNote rn;
std::vector<RecordedNote> batch;
while (recorded_.pop(rn)) batch.push_back(rn);
+ // Expression after the notes: a note's points were all queued before its note-off was.
+ collectRecordedExpr();
if (batch.empty()) return;
// Session-slot recording (M5-4): notes are already loop-local; append into the slot.
@@ -151,6 +209,7 @@ void Engine::poll() {
n.startBeat = std::fmod(r.startBeat < 0 ? 0 : r.startBeat, L);
n.lengthBeats = r.lengthBeats > 0.0 ? r.lengthBeats : 0.1;
n.velocity = r.velocity;
+ attachRecordedExpr(n, r.pitch, r.startBeat);
s.midi.notes.push_back(n);
}
republishWithTrackRaw(recordSessionTrackId_, nt); // grows the live take, not a new undo step
@@ -167,6 +226,7 @@ void Engine::poll() {
n.startBeat = r.startBeat - clip.startBeat;
n.lengthBeats = r.lengthBeats > 0.0 ? r.lengthBeats : 0.1;
n.velocity = r.velocity;
+ attachRecordedExpr(n, r.pitch, r.startBeat);
clip.notes.push_back(n);
// Grow the clip so freshly recorded notes fall inside it (gating in
// renderInstrumentRaw drops notes past lengthBeats).
@@ -176,6 +236,35 @@ void Engine::poll() {
republishWithTrackRaw(recordTrackId_, nt); // grows the live take, not a new undo step
}
+void Engine::collectRecordedExpr() {
+ RecordedExpr x;
+ while (recordedExpr_.pop(x)) {
+ auto& curve = pendingExpr_[{ x.pitch, x.startBeat }].dim[x.dim];
+ if (!curve.empty() && x.beat <= curve.back().beat) curve.back().value = x.value; // same instant
+ else curve.push_back({ x.beat, x.value });
+ }
+}
+
+// A finished recorded note takes the curve gathered for it, thinned to the points that shape
+// it (a block-rate stream is far denser than the gesture needs) — or nothing when every
+// dimension stayed neutral (an MPE controller announces a note's resting state too).
+void Engine::attachRecordedExpr(Note& n, int32_t pitch, double startBeat) {
+ auto it = pendingExpr_.find({ pitch, startBeat });
+ if (it == pendingExpr_.end()) return;
+ auto e = std::make_shared<NoteExpr>();
+ static constexpr float kTol[kExprDims] = { 0.03f, 0.01f, 0.01f }; // semitones / units
+ for (int32_t d = 0; d < kExprDims; ++d) {
+ const auto& src = it->second.dim[d];
+ bool moved = false;
+ for (const auto& p : src) if (std::fabs(p.value - kExprNeutral[d]) > kTol[d]) { moved = true; break; }
+ if (moved) e->dim[d] = thinExprCurve(src, kTol[d]);
+ }
+ pendingExpr_.erase(it);
+ if (e->empty()) return;
+ n.expr = e;
+ n.exprId = storeNoteExpr(e);
+}
+
// --- audio thread ----------------------------------------------------------
void Engine::render(float* out, int32_t numFrames) {
@@ -313,6 +402,23 @@ void Engine::drainLiveMidi(double blockStartBeat, bool playing) {
}
}
}
+ if (rec && playing && liveExprCount_ > 0) captureLiveExpr(nowBeat, slot ? slotLen : 0.0);
+}
+
+// Recorded MPE: the block's (coalesced) live expression for a note being recorded becomes a
+// point on that note's curve, timed from its start. Notes still held keep growing theirs; the
+// message thread pairs them with the RecordedNote at note-off (poll). Audio thread.
+void Engine::captureLiveExpr(double nowBeat, double slotLen) {
+ const int32_t recTrack = recordTrackRt_.load(std::memory_order_relaxed);
+ for (int i = 0; i < liveExprCount_; ++i) {
+ const MidiEvent& e = liveExpr_[i];
+ if (e.pitch < 0 || e.pitch > 127 || e.expr < 0 || e.expr >= kExprDims) continue; // instrument-wide: not per note
+ if (!pendingActive_[e.pitch] || (e.trackId >= 0 && e.trackId != recTrack)) continue;
+ double off = nowBeat - pendingStart_[e.pitch];
+ if (slotLen > 0.0 && off < 0.0) off += slotLen; // a Session take wrapped past its loop end
+ recordedExpr_.push(RecordedExpr{ e.pitch, pendingStart_[e.pitch], e.expr,
+ static_cast<float>(std::max(0.0, off)), e.velocity });
+ }
}
// Print routed-in MIDI (a source's post-FX note events) into the record track's take, pairing
@@ -336,7 +442,7 @@ void Engine::captureRoutedNotes(const MidiEv* evs, int n, double blockStart, dou
// Append a track's own note events (clips while playing + live input when armed/auditioned).
int Engine::gatherInstrumentNotes(Track& t, MidiEv* evs, int n, int32_t frames,
- double blockStart, double spb, bool playing) {
+ double blockStart, double spb, bool playing, ExprEv* xev, int* xn) {
if (playing) {
for (const MidiClip& clip : t.midiClips) {
// Deactivated clip (key 0): emit NO new note-ons, but still emit note-offs — so a
@@ -364,6 +470,9 @@ int Engine::gatherInstrumentNotes(Track& t, MidiEv* evs, int n, int32_t frames,
evs[n++] = {0, true, note.pitch, vel};
if (offS >= blockStart && offS < blockStart + frames && n < 1024)
evs[n++] = {static_cast<int32_t>(offS - blockStart), false, note.pitch, 0.0f};
+ // Recorded MPE: the note's curve while it sounds in this block.
+ if (xev && note.expr && clipActive)
+ noteExprEvents(note, onS - blockStart, offS - blockStart, frames, spb, xev, *xn, kMaxBlockExpr);
}
}
}
@@ -401,10 +510,12 @@ void Engine::applyLiveExpression(Graph* g, const Track& t, Instrument& inst) con
// its own MIDI FX chain. This is what feeds the track's instrument and, via routing, any
// destination it forwards to — so the FX (arp etc.) run exactly once per block.
int Engine::computeInstrumentMidi(Track& t, MidiEv* evs, int32_t frames,
- double blockStart, double spb, bool playing, bool arrangementActive) {
+ double blockStart, double spb, bool playing, bool arrangementActive,
+ ExprEv* xev, int* xn) {
// Arrangement clips gather only when the Arrangement is active; live input (armed/audition)
// is independent, so a session-only jam still lets you play instruments live.
- int n = gatherInstrumentNotes(t, evs, 0, frames, blockStart, spb, playing && arrangementActive);
+ if (xn) *xn = 0;
+ int n = gatherInstrumentNotes(t, evs, 0, frames, blockStart, spb, playing && arrangementActive, xev, xn);
std::sort(evs, evs + n, midiEvLess);
// Drum Rack swing + humanize: nudge note-on sample offsets. Off-beat 1/16 hits
@@ -455,15 +566,21 @@ void Engine::computeBlockMidi(Graph* g, int32_t frames, double blockStart, doubl
const size_t nt = g ? g->tracks.size() : 0;
if (blockMidi_.size() < nt) blockMidi_.resize(nt);
if (blockMidiN_.size() < nt) blockMidiN_.resize(nt);
+ if (blockExpr_.size() < nt) blockExpr_.resize(nt);
+ if (blockExprN_.size() < nt) blockExprN_.resize(nt);
for (size_t i = 0; i < nt; ++i) {
blockMidiN_[i] = 0;
+ blockExprN_[i] = 0;
Track& t = *g->tracks[i];
// Session-active tracks feed via renderSessionSlotRaw (their own clock), so skip here.
const bool sessionActive = t.sessionPlayer && t.sessionPlayer->playing.load(std::memory_order_relaxed) >= 0;
if (t.type() != TrackType::Instrument || !t.instrument || sessionActive) continue;
auto& buf = blockMidi_[i];
if (buf.size() < 1024) buf.resize(1024);
- blockMidiN_[i] = computeInstrumentMidi(t, buf.data(), frames, blockStart, spb, playing, arrangementActive);
+ auto& xbuf = blockExpr_[i];
+ if (xbuf.size() < static_cast<size_t>(kMaxBlockExpr)) xbuf.resize(kMaxBlockExpr);
+ blockMidiN_[i] = computeInstrumentMidi(t, buf.data(), frames, blockStart, spb, playing, arrangementActive,
+ xbuf.data(), &blockExprN_[i]);
}
}
@@ -489,6 +606,8 @@ void Engine::renderInstrumentRaw(Graph* g, Track& t, float* dst, int32_t frames,
MidiEv evs[1024];
int n = 0;
+ ExprEv xev[kMaxBlockExpr];
+ int xn = 0;
if (g) {
const int32_t srcId = t.midiFromTrackId(); // "MIDI In": the track we receive from (-1 = none)
// Recording the destination prints the routed MIDI into its take (arm the destination).
@@ -506,30 +625,19 @@ void Engine::renderInstrumentRaw(Graph* g, Track& t, float* dst, int32_t frames,
const int m = blockMidiN_[j];
const MidiEv* srcEvs = blockMidi_[j].data();
for (int k = 0; k < m && n < 1024; ++k) evs[n++] = srcEvs[k];
+ if (j < blockExprN_.size())
+ for (int k = 0; k < blockExprN_[j] && xn < kMaxBlockExpr; ++k) xev[xn++] = blockExpr_[j][k];
if (routed && captureRouted) captureRoutedNotes(srcEvs, m, blockStart, spb);
}
} else {
n = computeInstrumentMidi(t, evs, frames, blockStart, spb, playing,
- arrangementActive_.load(std::memory_order_relaxed)); // fallback: no graph
+ arrangementActive_.load(std::memory_order_relaxed), xev, &xn); // fallback: no graph
}
std::sort(evs, evs + n, midiEvLess);
-
- // Live expression lands after the offset-0 note events (live notes all sit at 0), so a
- // note's initial MPE values follow the note-on that starts it.
- bool exprDone = liveExprCount_ == 0;
- int32_t cursor = 0;
- for (int k = 0; k < n; ++k) {
- const int32_t segEnd = std::clamp(evs[k].off, 0, frames);
- if (segEnd > cursor) {
- if (!exprDone) { applyLiveExpression(g, t, *inst); exprDone = true; }
- inst->render(&dst[cursor * 2], segEnd - cursor); cursor = segEnd;
- }
- if (evs[k].on) inst->noteOn(evs[k].pitch, evs[k].vel);
- else inst->noteOff(evs[k].pitch);
- }
- if (!exprDone) applyLiveExpression(g, t, *inst);
- if (cursor < frames) inst->render(&dst[cursor * 2], frames - cursor);
+ std::stable_sort(xev, xev + xn, [](const ExprEv& a, const ExprEv& b) { return a.off < b.off; });
+ playEvents(*inst, dst, frames, evs, n, xev, xn,
+ [&] { if (liveExprCount_ > 0) applyLiveExpression(g, t, *inst); });
// MIDI clip volume envelope (M9 follow-up): scale the instrument output per
// sample within each clip's time window (0..1 curve in clip-local beats).
@@ -579,6 +687,20 @@ void Engine::renderSessionSlotRaw(Track& t, float* dst, int32_t frames, double s
evs[n++] = {static_cast<int32_t>((offAbs - p) * spb), false, note.pitch, 0.0f};
}
}
+ // Recorded MPE of the slot's notes sounding in this block (a note may have started a pass
+ // earlier, so look one iteration back).
+ ExprEv xev[kMaxBlockExpr];
+ int xn = 0;
+ for (long it = std::max(0L, i0 - 1); it <= i0 + 1; ++it) {
+ if (!s.loop && it > 0) break;
+ for (const Note& note : s.midi.notes) {
+ if (!note.expr || note.startBeat < 0.0 || note.startBeat >= L) continue;
+ const double onAbs = it * L + note.startBeat;
+ noteExprEvents(note, (onAbs - p) * spb, (onAbs + note.lengthBeats - p) * spb, frames, spb, xev, xn, kMaxBlockExpr);
+ }
+ }
+ std::stable_sort(xev, xev + xn, [](const ExprEv& a, const ExprEv& b) { return a.off < b.off; });
+
// Live monitoring while overdub-recording into this slot (M5-4).
const bool monitoring = recordSlotPlayer_.load(std::memory_order_acquire) == &sp;
if (monitoring)
@@ -597,24 +719,12 @@ void Engine::renderSessionSlotRaw(Track& t, float* dst, int32_t frames, double s
}
// The monitored live expression, after the offset-0 notes (as in renderInstrumentRaw).
- auto applyExpr = [&] {
+ playEvents(*inst, dst, frames, evs, n, xev, xn, [&] {
+ if (!monitoring) return;
for (int i = 0; i < liveExprCount_; ++i)
if (liveExpr_[i].trackId < 0 || liveExpr_[i].trackId == t.id())
inst->noteExpression(liveExpr_[i].pitch, liveExpr_[i].expr, liveExpr_[i].velocity);
- };
- bool exprDone = !monitoring || liveExprCount_ == 0;
- int32_t cursor = 0;
- for (int k = 0; k < n; ++k) {
- const int32_t segEnd = std::clamp(evs[k].off, 0, frames);
- if (segEnd > cursor) {
- if (!exprDone) { applyExpr(); exprDone = true; }
- inst->render(&dst[cursor * 2], segEnd - cursor); cursor = segEnd;
- }
- if (evs[k].on) inst->noteOn(evs[k].pitch, evs[k].vel);
- else inst->noteOff(evs[k].pitch);
- }
- if (!exprDone) applyExpr();
- if (cursor < frames) inst->render(&dst[cursor * 2], frames - cursor);
+ });
sp.localBeats = p + db;
}
diff --git a/src/native/nota.engine/src/Engine_Session.cpp b/src/native/nota.engine/src/Engine_Session.cpp
index 35702e74..2fe27b86 100644
--- a/src/native/nota.engine/src/Engine_Session.cpp
+++ b/src/native/nota.engine/src/Engine_Session.cpp
@@ -522,9 +522,7 @@ bool Engine::setSessionNotes(int32_t trackId, int32_t scene, const NotaNoteData*
clip.notes.clear();
clip.notes.reserve(count);
for (int32_t i = 0; i < count; ++i) {
- Note n; n.pitch = notes[i].pitch; n.startBeat = notes[i].start_beat;
- n.lengthBeats = notes[i].length_beats; n.velocity = notes[i].velocity;
- clip.notes.push_back(n);
+ clip.notes.push_back(noteFromData(notes[i]));
}
republishWithTrack(trackId, nt);
return true;
@@ -535,12 +533,7 @@ int32_t Engine::getSessionNotes(int32_t trackId, int32_t scene, NotaNoteData* ou
if (!t || scene < 0 || scene >= static_cast<int32_t>(t->sessionSlots.size())) return 0;
const MidiClip& clip = t->sessionSlots[scene].midi;
const int32_t n = std::min<int32_t>(maxNotes, static_cast<int32_t>(clip.notes.size()));
- for (int32_t i = 0; i < n; ++i) {
- out[i].pitch = clip.notes[i].pitch;
- out[i].start_beat = clip.notes[i].startBeat;
- out[i].length_beats = clip.notes[i].lengthBeats;
- out[i].velocity = clip.notes[i].velocity;
- }
+ for (int32_t i = 0; i < n; ++i) noteToData(clip.notes[i], out[i]);
return n;
}
diff --git a/src/native/nota.engine/src/Engine_Tracks.cpp b/src/native/nota.engine/src/Engine_Tracks.cpp
index b3a4bd38..51b05304 100644
--- a/src/native/nota.engine/src/Engine_Tracks.cpp
+++ b/src/native/nota.engine/src/Engine_Tracks.cpp
@@ -2855,9 +2855,7 @@ bool Engine::setClipNotes(int32_t trackId, int32_t clipIndex, const NotaNoteData
clip.notes.clear();
clip.notes.reserve(count);
for (int32_t i = 0; i < count; ++i) {
- Note n; n.pitch = notes[i].pitch; n.startBeat = notes[i].start_beat;
- n.lengthBeats = notes[i].length_beats; n.velocity = notes[i].velocity;
- clip.notes.push_back(n);
+ clip.notes.push_back(noteFromData(notes[i]));
}
republishWithTrack(trackId, nt);
return true;
@@ -2871,9 +2869,7 @@ bool Engine::setClipNotesLive(int32_t trackId, int32_t clipIndex, const NotaNote
clip.notes.clear();
clip.notes.reserve(count);
for (int32_t i = 0; i < count; ++i) {
- Note n; n.pitch = notes[i].pitch; n.startBeat = notes[i].start_beat;
- n.lengthBeats = notes[i].length_beats; n.velocity = notes[i].velocity;
- clip.notes.push_back(n);
+ clip.notes.push_back(noteFromData(notes[i]));
}
republishWithTrackRaw(trackId, nt); // no undo checkpoint — the UI seeds one per gesture
return true;
@@ -2884,15 +2880,67 @@ int32_t Engine::getClipNotes(int32_t trackId, int32_t clipIndex, NotaNoteData* o
if (!t || clipIndex < 0 || clipIndex >= static_cast<int32_t>(t->midiClips.size())) return 0;
const MidiClip& clip = t->midiClips[clipIndex];
const int32_t n = std::min<int32_t>(maxNotes, static_cast<int32_t>(clip.notes.size()));
- for (int32_t i = 0; i < n; ++i) {
- out[i].pitch = clip.notes[i].pitch;
- out[i].start_beat = clip.notes[i].startBeat;
- out[i].length_beats = clip.notes[i].lengthBeats;
- out[i].velocity = clip.notes[i].velocity;
+ for (int32_t i = 0; i < n; ++i) noteToData(clip.notes[i], out[i]);
+ return n;
+}
+
+// ---- recorded MPE store ----------------------------------------------------------
+
+int32_t Engine::storeNoteExpr(std::shared_ptr<const NoteExpr> e) {
+ if (!e || e->empty()) return 0;
+ std::lock_guard<std::mutex> lk(exprMx_);
+ const int32_t id = nextExprId_++;
+ exprStore_.emplace(id, std::move(e));
+ return id;
+}
+
+int32_t Engine::createNoteExpr(const NotaExprPoint* points, int32_t count) {
+ if (!points || count <= 0) return 0;
+ auto e = std::make_shared<NoteExpr>();
+ for (int32_t i = 0; i < count; ++i) {
+ const auto& p = points[i];
+ if (p.dim < 0 || p.dim >= kExprDims || !std::isfinite(p.beat) || !std::isfinite(p.value)) continue;
+ const float v = p.dim == ExprBend ? std::clamp(p.value, -96.0f, 96.0f) : std::clamp(p.value, 0.0f, 1.0f);
+ e->dim[p.dim].push_back({ std::max(0.0f, p.beat), v });
+ }
+ for (auto& d : e->dim)
+ std::stable_sort(d.begin(), d.end(), [](const ExprPoint& a, const ExprPoint& b) { return a.beat < b.beat; });
+ return storeNoteExpr(std::move(e));
+}
+
+int32_t Engine::noteExprPoints(int32_t id, NotaExprPoint* out, int32_t maxPoints) const {
+ std::shared_ptr<const NoteExpr> e;
+ {
+ std::lock_guard<std::mutex> lk(exprMx_);
+ auto it = exprStore_.find(id);
+ if (it == exprStore_.end()) return 0;
+ e = it->second;
+ }
+ int32_t n = 0;
+ for (int32_t d = 0; d < kExprDims; ++d)
+ for (const auto& p : e->dim[d]) {
+ if (out && n < maxPoints) out[n] = NotaExprPoint{ d, p.beat, p.value };
+ ++n;
+ }
+ return out ? std::min(n, maxPoints) : n;
+}
+
+Note Engine::noteFromData(const NotaNoteData& d) const {
+ Note n; n.pitch = d.pitch; n.startBeat = d.start_beat;
+ n.lengthBeats = d.length_beats; n.velocity = d.velocity;
+ if (d.expr_id > 0) {
+ std::lock_guard<std::mutex> lk(exprMx_);
+ auto it = exprStore_.find(d.expr_id);
+ if (it != exprStore_.end()) { n.exprId = d.expr_id; n.expr = it->second; }
}
return n;
}
+void Engine::noteToData(const Note& n, NotaNoteData& d) {
+ d.pitch = n.pitch; d.start_beat = n.startBeat; d.length_beats = n.lengthBeats;
+ d.velocity = n.velocity; d.expr_id = n.expr ? n.exprId : 0;
+}
+
int32_t Engine::clipNoteCount(int32_t trackId, int32_t clipIndex) const {
auto t = findTrackAuthoring(trackId);
if (!t || clipIndex < 0 || clipIndex >= static_cast<int32_t>(t->midiClips.size())) return 0;
diff --git a/src/native/nota.engine/src/MidiClip.h b/src/native/nota.engine/src/MidiClip.h
index c1843c9c..b950ce32 100644
--- a/src/native/nota.engine/src/MidiClip.h
+++ b/src/native/nota.engine/src/MidiClip.h
@@ -8,17 +8,71 @@
#pragma once
#include "Automation.h"
+#include "NoteExpression.h"
+#include <algorithm>
+#include <cmath>
#include <cstdint>
+#include <memory>
#include <string>
#include <vector>
namespace nota {
+// A note's recorded MPE (per-note bend / pressure / slide): one breakpoint curve per
+// dimension, times in beats from the note's start, values as Instrument::noteExpression
+// takes them (bend in semitones). Immutable once built — notes share it by pointer, so
+// moving, copying or undoing a note carries its expression for free. The engine keeps every
+// curve in a store under a stable id (NotaNoteData.expr_id) so the UI can round-trip it.
+struct ExprPoint { float beat; float value; };
+
+struct NoteExpr {
+ std::vector<ExprPoint> dim[kExprDims]; // each sorted by beat; empty = not recorded
+
+ bool empty() const { for (const auto& d : dim) if (!d.empty()) return false; return true; }
+ // Linear between points, held before the first and after the last.
+ float valueAt(int32_t d, double beat) const {
+ const auto& p = dim[d];
+ if (p.empty()) return kExprNeutral[d];
+ if (beat <= p.front().beat) return p.front().value;
+ if (beat >= p.back().beat) return p.back().value;
+ auto it = std::upper_bound(p.begin(), p.end(), beat, [](double b, const ExprPoint& e) { return b < e.beat; });
+ const ExprPoint& b = *it; const ExprPoint& a = *(it - 1);
+ const double t = b.beat > a.beat ? (beat - a.beat) / (b.beat - a.beat) : 1.0;
+ return static_cast<float>(a.value + (b.value - a.value) * t);
+ }
+};
+
+// Ramer–Douglas–Peucker: the fewest points that stay within `tol` of the curve (first and
+// last always kept). Turns a block-rate recording into an editable shape.
+inline std::vector<ExprPoint> thinExprCurve(const std::vector<ExprPoint>& p, float tol) {
+ if (p.size() <= 2) return p;
+ std::vector<char> keep(p.size(), 0);
+ keep.front() = keep.back() = 1;
+ std::vector<std::pair<size_t, size_t>> stack{ { 0, p.size() - 1 } };
+ while (!stack.empty()) {
+ auto [a, b] = stack.back(); stack.pop_back();
+ if (b <= a + 1) continue;
+ float worst = -1.0f; size_t at = a;
+ for (size_t i = a + 1; i < b; ++i) {
+ const float span = p[b].beat - p[a].beat;
+ const float t = span > 0.0f ? (p[i].beat - p[a].beat) / span : 0.0f;
+ const float d = std::fabs(p[i].value - (p[a].value + (p[b].value - p[a].value) * t));
+ if (d > worst) { worst = d; at = i; }
+ }
+ if (worst > tol) { keep[at] = 1; stack.push_back({ a, at }); stack.push_back({ at, b }); }
+ }
+ std::vector<ExprPoint> out;
+ for (size_t i = 0; i < p.size(); ++i) if (keep[i]) out.push_back(p[i]);
+ return out;
+}
+
struct Note {
int32_t pitch = 60; // MIDI note number 0..127
double startBeat = 0.0; // relative to the clip start
double lengthBeats = 1.0;
float velocity = 0.8f; // 0..1
+ int32_t exprId = 0; // the engine's expression store id (0 = none)
+ std::shared_ptr<const NoteExpr> expr; // resolved from exprId; null = none
};
struct MidiClip {
diff --git a/src/native/nota.engine/src/nota_engine_track.cpp b/src/native/nota.engine/src/nota_engine_track.cpp
index 43a44ef7..72c4533d 100644
--- a/src/native/nota.engine/src/nota_engine_track.cpp
+++ b/src/native/nota.engine/src/nota_engine_track.cpp
@@ -626,6 +626,12 @@ int32_t nota_clip_get_notes(const NotaEngine* e, int32_t track_id, int32_t clip_
int32_t nota_clip_note_count(const NotaEngine* e, int32_t track_id, int32_t clip_index) {
return e ? CENG(e)->clipNoteCount(track_id, clip_index) : 0;
}
+int32_t nota_note_expr_create(NotaEngine* e, const NotaExprPoint* points, int32_t count) {
+ return (e && points && count > 0) ? ENG(e)->createNoteExpr(points, count) : 0;
+}
+int32_t nota_note_expr_points(const NotaEngine* e, int32_t expr_id, NotaExprPoint* out, int32_t max_points) {
+ return e ? CENG(e)->noteExprPoints(expr_id, out, max_points) : 0;
+}
NotaResult nota_track_set_armed(NotaEngine* e, int32_t track_id, int32_t armed) {
if (!e) return NOTA_ERR_INVALID_ARG; ENG(e)->setTrackArmed(track_id, armed != 0); return NOTA_OK;
diff --git a/tests/Nota.SmokeTest/MpeTests.cs b/tests/Nota.SmokeTest/MpeTests.cs
index 150f4f6b..11ab8acc 100644
--- a/tests/Nota.SmokeTest/MpeTests.cs
+++ b/tests/Nota.SmokeTest/MpeTests.cs
@@ -159,6 +159,8 @@ internal static class MpeTests
yield return (At(x, 220) > At(x, 440), "track-addressed expression stays on its track");
}
+ foreach (var r in Recording()) yield return r;
+
// The MPE preference round-trips through the staged MIDI config.
{
using var e = new NotaEngine();
@@ -169,4 +171,108 @@ internal static class MpeTests
yield return (!on1 && r1 == 24, $"the MPE setting stages (default {(on0 ? "on" : "off")} ±{r0})");
}
}
+
+ // ---- recorded MPE: into the take, back out of the clip, through edits and save/load ----
+
+ private static float[] PlayFrom(NotaEngine e, int skipBlocks = 2, int blocks = 12)
+ {
+ e.StopTransport(); e.Seek(0); e.Play();
+ Render(e, skipBlocks);
+ return Render(e, blocks);
+ }
+
+ private static IEnumerable<(bool, string)> Recording()
+ {
+ // The store: a curve goes in, the same curve comes out, sorted per dimension.
+ using (var e = new NotaEngine())
+ {
+ int id = e.CreateNoteExpression(new[]
+ {
+ new NotaExprPoint(NoteExpressionDim.Bend, 0.5f, 2f), new NotaExprPoint(NoteExpressionDim.Bend, 0f, 0f),
+ new NotaExprPoint(NoteExpressionDim.Pressure, 0f, 0.7f),
+ });
+ var back = e.GetNoteExpression(id);
+ yield return (id > 0 && back.Length == 3 && back[0].Dim == NoteExpressionDim.Bend && back[0].Beat == 0f && back[1].Value == 2f
+ && back[2].Dim == NoteExpressionDim.Pressure && e.GetNoteExpression(0).Length == 0,
+ "the expression store returns a curve as given, sorted per dimension");
+ }
+
+ // Record: a note bent smoothly up an octave and pressed, on the armed track.
+ using var r = new NotaEngine();
+ r.SetBpm(120);
+ int t = r.AddInstrumentTrack();
+ r.SetTrackBuiltinInstrument(t, 0);
+ r.SetTrackArmed(t, true);
+ r.SetRecording(true);
+ Render(r, 4);
+ r.TrackNoteOn(t, 57, 0.8f);
+ r.TrackNoteExpression(t, 57, NoteExpressionDim.Bend, 0f);
+ r.TrackNoteExpression(t, 57, NoteExpressionDim.Slide, 0.5f); // resting: not kept
+ for (int k = 1; k <= 40; k++)
+ {
+ r.TrackNoteExpression(t, 57, NoteExpressionDim.Bend, Math.Min(12f, k * 0.6f));
+ r.TrackNoteExpression(t, 57, NoteExpressionDim.Pressure, 0.8f);
+ Render(r, 1);
+ }
+ Render(r, 20); // hold the octave
+ r.TrackNoteOff(t, 57);
+ Render(r, 4);
+ r.SetRecording(false);
+ r.Poll();
+ NotaNote? rec = null; int recClip = -1;
+ if (r.TryGetTrackInfo(0, out var ti))
+ for (int c = 0; c < ti.ClipCount && rec is null; c++)
+ foreach (var n in r.GetClipNotes(t, c)) if (n.Pitch == 57) { rec = n; recClip = c; }
+ var curve = rec is { } rn ? r.GetNoteExpression(rn.ExprId) : Array.Empty<NotaExprPoint>();
+ var bend = curve.Where(p => p.Dim == NoteExpressionDim.Bend).ToArray();
+ yield return (rec is { ExprId: > 0 } && bend.Length >= 2 && Math.Abs(bend[^1].Value - 12f) < 0.1f && bend[0].Value < 1f,
+ $"a recorded note keeps its bend (points {bend.Length}, {(bend.Length > 0 ? bend[0].Value : -1):F1} → {(bend.Length > 0 ? bend[^1].Value : -1):F1} st)");
+ yield return (bend.Length < 12, $"the recorded curve is thinned to its shape (a 40-step ramp → {bend.Length} points)");
+ yield return (curve.Any(p => p.Dim == NoteExpressionDim.Pressure && Math.Abs(p.Value - 0.8f) < 0.01f) && !curve.Any(p => p.Dim == NoteExpressionDim.Slide),
+ "pressure is kept; a dimension that stayed at rest is not");
+ if (rec is null) yield break;
+
+ // Play the take back: the note ends an octave up (its last stretch is held at +12).
+ var outp = PlayFrom(r, skipBlocks: 2 + 4 + 40, blocks: 12);
+ double a220 = At(outp, 220), a440 = At(outp, 440);
+ yield return (a440 > a220 * 4, $"playback replays the recorded bend (220/440 Hz {a220:E1}/{a440:E1})");
+
+ // A piano-roll edit (move the note, push the whole set back) keeps the curve.
+ var notes = r.GetClipNotes(t, recClip);
+ for (int i = 0; i < notes.Length; i++) notes[i].StartBeat += 0.0; // same position, round-tripped
+ r.SetClipNotes(t, recClip, notes);
+ var again = r.GetClipNotes(t, recClip).First(n => n.Pitch == 57);
+ yield return (again.ExprId == rec.Value.ExprId, "pushing the notes back from the editor keeps their expression");
+
+ // A note given an expression plays it from the clip: +12 st from its start.
+ using (var e = new NotaEngine())
+ {
+ e.SetBpm(120);
+ int u = e.AddInstrumentTrack();
+ e.SetTrackBuiltinInstrument(u, 0);
+ int c = e.AddMidiClip(u, 0, 4);
+ var n = new NotaNote(57, 0, 4, 0.8f) { ExprId = e.CreateNoteExpression(new[] { new NotaExprPoint(NoteExpressionDim.Bend, 0f, 12f) }) };
+ var m = new NotaNote(64, 0, 4, 0.8f);
+ e.SetClipNotes(u, c, new[] { n, m });
+ var x = PlayFrom(e);
+ double b220 = At(x, 220), b440 = At(x, 440), e330 = At(x, 329.63);
+ yield return (b440 > b220 * 4 && e330 > b220 * 4, $"a clip note plays its curve; its neighbour stays put (220/440/330 Hz {b220:E1}/{b440:E1}/{e330:E1})");
+
+ // Save and load: the curve is in the project file and comes back on the note.
+ e.StopTransport();
+ var doc = ProjectService.Capture(e, new TransportState(120.0, 1.0, false, false), new List<string>());
+ string bundle = Path.Combine(Path.GetTempPath(), "nota-mpe-" + Guid.NewGuid().ToString("N") + ".nota");
+ ProjectService.Save(doc, bundle, e);
+ using var e2 = new NotaEngine();
+ ProjectService.Apply(ProjectService.Load(bundle), e2, bundle);
+ NotaNote? loaded = null;
+ for (int i = 0; i < e2.TrackCount; i++)
+ if (e2.TryGetTrackInfo(i, out var lti) && lti.IsInstrument && lti.ClipCount > 0)
+ foreach (var ln in e2.GetClipNotes(lti.Id, 0)) if (ln.Pitch == 57) loaded = ln;
+ var lc = loaded is { } l ? e2.GetNoteExpression(l.ExprId) : Array.Empty<NotaExprPoint>();
+ yield return (lc.Length == 1 && lc[0].Dim == NoteExpressionDim.Bend && lc[0].Value == 12f,
+ "recorded MPE survives save and load");
+ try { Directory.Delete(bundle, true); } catch { /* temp */ }
+ }
+ }
}