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) Audio clips into instruments

Egor Khindikaynenc8e4ec55a436
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4783fe38..d2d2e044 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,13 @@ Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.
## [Unreleased]
### Added
+- **Audio clips into instruments:** drag an audio clip from the Arrangement onto the row of a
+ track with a Sampler, Nota Grain, Drum Rack or Instrument Rack and its audio loads there (a
+ Drum Rack takes it on the next free pad). Pull a clip out of the Arrangement and it becomes a
+ sample you can drop anywhere a sample goes — a Drum Rack pad, a Grain, a Rhythm voice, a
+ Session slot. Rest over an instrument track while dragging and its devices open, so a
+ specific pad is one move away. The clip stays where it was; the sample is the part of the
+ clip you hear.
- **Nota Shutter · Pattern:** a fourth tab turns the gate into a rhythmic, tempo-synced
gate. Draw the steps (Draw, Line, Erase, Flip) on an 8, 16 or 32-step grid, flip through
pattern shapes or roll a new one with the dice and Complexity, and shape the gain with Smooth
diff --git a/src/managed/Nota.App/ArrangementView.ClipDrag.cs b/src/managed/Nota.App/ArrangementView.ClipDrag.cs
new file mode 100644
index 00000000..0e69f256
--- /dev/null
+++ b/src/managed/Nota.App/ArrangementView.ClipDrag.cs
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.
+//
+// An audio clip dragged to an instrument. Moving a clip onto the row of a track whose
+// instrument plays samples (Sampler, Grain, Drum Rack, Instrument Rack) loads the clip's audio
+// there instead of moving it; pulling it out of the arrangement turns the move into a sample
+// drag that every sample drop target takes (a Drum Rack pad, a Grain chamber…). Resting over
+// such a row opens its devices, so a specific pad is a drag away. The clip itself stays put.
+
+using System;
+using Avalonia.Input;
+using Avalonia.Threading;
+using Nota.Presentation;
+
+namespace Nota.App;
+
+public sealed partial class ArrangementView
+{
+ /// <summary>A clip's audio (as a sample item) dropped on an instrument track: item, track id.</summary>
+ public event Action<BrowserItem, int>? SampleToInstrument;
+
+ /// <summary>A clip drag rested over an instrument track: show its devices (track id).</summary>
+ public event Action<int>? DevicesPeekRequested;
+
+ // Instruments that take a dropped sample: Sampler, Instrument Rack, Drum Rack, Grain.
+ internal bool TakesSample(int row)
+ {
+ if (_engine is null || row < 0 || row >= _tracks.Count) return false;
+ var t = _tracks[row];
+ if (!t.IsInstrument || t.IsGroup || t.IsReturn) return false;
+ return _engine.TrackInstrumentKind(t.Id) is 1 or 3 or 4 or 10;
+ }
+
+ // Spring-loaded devices: the row a clip drag rests over opens in the Devices panel.
+ private DispatcherTimer? _peekTimer;
+ private int _peekTrackId = -1;
+
+ internal void DwellOverTrack(int trackId)
+ {
+ if (trackId == _peekTrackId) return;
+ _peekTrackId = trackId;
+ _peekTimer?.Stop();
+ if (trackId <= 0) return;
+ _peekTimer ??= new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(500) };
+ _peekTimer.Tick -= OnPeekTick;
+ _peekTimer.Tick += OnPeekTick;
+ _peekTimer.Start();
+ }
+
+ private void OnPeekTick(object? sender, EventArgs e)
+ {
+ _peekTimer?.Stop();
+ if (_peekTrackId > 0) DevicesPeekRequested?.Invoke(_peekTrackId);
+ }
+
+ internal void LoadClipIntoTrack(BrowserItem item, int trackId) => SampleToInstrument?.Invoke(item, trackId);
+
+ // A clip pulled out and dragged back over the lanes: only sample-taking instrument rows
+ // accept it (the clip already lives here, a drop elsewhere would duplicate it).
+ private void OnClipDragOverLanes(DragEventArgs e)
+ {
+ int row = RowAtY(e.GetPosition(_lanes).Y);
+ bool ok = TakesSample(row);
+ e.DragEffects = ok ? DragDropEffects.Copy : DragDropEffects.None;
+ SetDropTrack(ok ? row : -1);
+ DwellOverTrack(ok ? _tracks[row].Id : -1);
+ }
+
+ private void OnClipDropOnLanes(DragEventArgs e)
+ {
+ SetDropTrack(-1);
+ DwellOverTrack(-1);
+ int row = RowAtY(e.GetPosition(_lanes).Y);
+ if (!TakesSample(row) || BrowserView.CurrentDrag is not { } item) return;
+ LoadClipIntoTrack(item, _tracks[row].Id);
+ e.Handled = true;
+ }
+}
diff --git a/src/managed/Nota.App/ArrangementView.LaneControl.cs b/src/managed/Nota.App/ArrangementView.LaneControl.cs
index 0913970c..bb906d4d 100644
--- a/src/managed/Nota.App/ArrangementView.LaneControl.cs
+++ b/src/managed/Nota.App/ArrangementView.LaneControl.cs
@@ -103,6 +103,15 @@ public sealed partial class ArrangementView
private double _grabOrigStart; // grabbed clip's original start (delta reference)
private int _grabRow; // grabbed clip's row (vertical delta reference)
private int _moveRowDelta; // rows to shift the group (instrument→instrument only)
+
+ // Audio clip → instrument (see ArrangementView.ClipDrag): the grabbed clip, the
+ // sample-taking instrument row it's held over (-1 none: a plain move), and the press
+ // a drag out of the arrangement starts its sample drag from.
+ private ClipVM? _grabClip;
+ private int _grabTrackId;
+ private int _loadRow = -1;
+ private PointerPressedEventArgs? _pressArgs;
+ private bool _dragOutTried;
private List<double> _snapTargets = new(); // neighbour clip edges + markers (magnetic snap, 2.4)
// Rubber-band on empty lane space. Plain drag = clip marquee (select whole clips);
@@ -279,6 +288,8 @@ public sealed partial class ArrangementView
_pendingClipIndex = hh.clip.ClipIndex;
_pendingGrabBeat = beat;
_pendingWasSelected = _o.IsSelected(hh.track.Id, hh.clip.ClipIndex);
+ _pressArgs = e;
+ _dragOutTried = false;
e.Pointer.Capture(this);
InvalidateVisual();
}
@@ -383,6 +394,7 @@ public sealed partial class ArrangementView
// Grabbing an already-selected clip moves the whole group; otherwise the
// clip becomes the sole selection first (req 2.1/2.2).
if (!_pendingWasSelected) _o.Select(_pendingTrackId, _pendingClipIndex);
+ _grabClip = _pendingClip; _grabTrackId = _pendingTrackId;
BeginGroupMove(_pendingTrackId, _pendingClip!.StartBeat, _pendingGrabBeat);
}
else
@@ -398,6 +410,13 @@ public sealed partial class ArrangementView
}
if (_drag == Drag.None) { if (!UpdateAdsrHover(pos)) UpdateEdgeHover(pos); return; }
+ // An audio clip pulled out of the arrangement becomes a sample drag.
+ if (_drag == Drag.Move && _groupMove is not null && !_dragOutTried && _grabClip is { IsMidi: false }
+ && e.GetPosition(_o) is var op && (op.X < 0 || op.Y < 0 || op.X > _o.Bounds.Width || op.Y > _o.Bounds.Height))
+ {
+ _ = DragOutAsync();
+ return;
+ }
EnsureAutoScroll();
UpdateActiveDrag(pos, e.KeyModifiers);
}
@@ -418,6 +437,15 @@ public sealed partial class ArrangementView
// Vertical: how many rows to shift (instrument→instrument only).
int targetRow = Math.Max(0, _o.RowAtYClamped(pos.Y));
_moveRowDelta = ClampRowDelta(targetRow - _grabRow);
+ // Over a sample-taking instrument row an audio clip loads there on release
+ // instead of moving, so the group rests at home meanwhile.
+ int hoverRow = _o.RowAtY(pos.Y);
+ SetLoadRow(_grabClip is { IsMidi: false } && hoverRow != _grabRow && _o.TakesSample(hoverRow) ? hoverRow : -1);
+ if (_loadRow >= 0)
+ {
+ foreach (var m in _groupMove) m.vm.StartBeat = m.origStart;
+ _moveRowDelta = 0;
+ }
InvalidateVisual();
return;
}
@@ -528,6 +556,19 @@ public sealed partial class ArrangementView
return;
}
+ if (_drag == Drag.Move && _groupMove is not null && _loadRow >= 0)
+ {
+ int trackId = _o._tracks[_loadRow].Id;
+ var grabbed = _grabClip;
+ SetLoadRow(-1);
+ _groupMove = null; _drag = Drag.None; _moveRowDelta = 0;
+ e.Pointer.Capture(null);
+ InvalidateVisual();
+ if (eng is not null && grabbed is not null && ClipSample(eng, grabbed) is { } item)
+ _o.LoadClipIntoTrack(item, trackId);
+ return;
+ }
+
if (_drag == Drag.Move && _groupMove is not null)
{
int rowDelta = _moveRowDelta;
@@ -606,12 +647,48 @@ public sealed partial class ArrangementView
_autoDrag = null; _autoDragTrack = null; _bendLeft = _bendRight = null; _bendTrack = null;
_rangeTrack = null;
_adsrDrag = AdsrHandle.None; _adsrClip = null;
+ SetLoadRow(-1);
_autoScroll?.Stop();
SetResizeCursor(false);
_o.Refresh(); // reload authoritative clip/automation positions, discarding the preview
return true;
}
+ private void SetLoadRow(int row)
+ {
+ if (row == _loadRow) return;
+ _loadRow = row;
+ _o.SetDropTrack(row);
+ _o.DwellOverTrack(row >= 0 ? _o._tracks[row].Id : -1);
+ if (row >= 0) _o.StatusMessage?.Invoke($"Release to load the clip into {_o._tracks[row].Name}");
+ }
+
+ // The grabbed clip's audio as a sample file (null + a status line when it can't be).
+ private BrowserItem? ClipSample(IAudioEngine eng, ClipVM clip)
+ {
+ string name = clip.Name.Length > 0 ? clip.Name : _o._tracks.Find(t => t.Id == _grabTrackId)?.Name ?? "";
+ try { return ClipAudioExport.AsSample(eng, _grabTrackId, clip.ClipIndex, name); }
+ catch (Exception ex) { _o.StatusMessage?.Invoke($"Couldn't take the clip's audio: {ex.Message}"); return null; }
+ }
+
+ // Hands the move over to a sample drag: the clip snaps back home and its audio rides
+ // the browser payload, so any sample drop target takes it.
+ private async System.Threading.Tasks.Task DragOutAsync()
+ {
+ _dragOutTried = true;
+ if (_pressArgs is not { } press || _grabClip is not { } clip || _o._engine is not { } eng) return;
+ if (ClipSample(eng, clip) is not { } item) return;
+ CancelGesture();
+ press.Pointer.Capture(null);
+ try { await BrowserView.DragItemAsync(press, item, fromClip: true); }
+ finally
+ {
+ _pressArgs = null;
+ _o.SetDropTrack(-1);
+ _o.DwellOverTrack(-1);
+ }
+ }
+
// NB: we deliberately do NOT cancel on OnPointerCaptureLost. On macOS that event fires
// *before* OnPointerReleased on a normal button-up, so cancelling there would abort every
// drag/resize/marquee right before it commits. External loss (alt-tab) is handled via the
diff --git a/src/managed/Nota.App/ArrangementView.cs b/src/managed/Nota.App/ArrangementView.cs
index 0bbf9140..e6d9abf9 100644
--- a/src/managed/Nota.App/ArrangementView.cs
+++ b/src/managed/Nota.App/ArrangementView.cs
@@ -327,7 +327,7 @@ public sealed partial class ArrangementView : UserControl
// below the tracks) so instruments/samples can be dropped anywhere.
DragDrop.SetAllowDrop(scroller, true);
DragDrop.AddDragOverHandler(scroller, OnLaneDragOver);
- DragDrop.AddDragLeaveHandler(scroller, (_, _) => SetDropTrack(-1));
+ DragDrop.AddDragLeaveHandler(scroller, (_, _) => { SetDropTrack(-1); DwellOverTrack(-1); });
DragDrop.AddDropHandler(scroller, OnLaneDrop);
// Right-click the empty area below the tracks → paste a copied track there (the
// header cards / lanes are top-anchored, so clicks below them land on the scroller).
@@ -1101,6 +1101,7 @@ public sealed partial class ArrangementView : UserControl
// --- browser drag & drop (M7-5) ---
private void OnLaneDragOver(object? sender, DragEventArgs e)
{
+ if (BrowserView.IsClipDrag) { OnClipDragOverLanes(e); return; }
bool ok = BrowserView.IsAcceptableDrag(e);
e.DragEffects = ok ? DragDropEffects.Copy : DragDropEffects.None;
SetDropTrack(ok ? RowAtY(e.GetPosition(_lanes).Y) : -1);
@@ -1163,6 +1164,7 @@ public sealed partial class ArrangementView : UserControl
private void OnLaneDrop(object? sender, DragEventArgs e)
{
+ if (BrowserView.IsClipDrag) { OnClipDropOnLanes(e); return; }
SetDropTrack(-1);
var items = BrowserView.DroppedItems(e);
if (items.Count == 0) return;
diff --git a/src/managed/Nota.App/BrowserView.cs b/src/managed/Nota.App/BrowserView.cs
index 5a75e81b..c2c8291b 100644
--- a/src/managed/Nota.App/BrowserView.cs
+++ b/src/managed/Nota.App/BrowserView.cs
@@ -16,6 +16,7 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
+using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
@@ -47,6 +48,24 @@ public sealed class BrowserView : UserControl
/// <summary>True while a browser row is being dragged (used by drop targets).</summary>
public static bool IsBrowserDrag => CurrentDrag is not null;
+ /// <summary>True while the drag is an arrangement audio clip pulled out as a sample (it
+ /// rides the browser payload, so every sample drop target takes it as-is).</summary>
+ public static bool IsClipDrag { get; private set; }
+
+ /// <summary>Drags <paramref name="item"/> as the browser payload from a press elsewhere in
+ /// the app. Completes when it's dropped (or cancelled).</summary>
+ internal static async Task DragItemAsync(PointerPressedEventArgs press, BrowserItem item, bool fromClip = false)
+ {
+ // Single native pasteboard item (one format → one drag image → one
+ // pasteboard item: macOS is satisfied). The payload rides CurrentDrag.
+ var data = new DataTransfer();
+ data.Add(DataTransferItem.Create(DataFormat.Text, DragSentinel));
+ CurrentDrag = item;
+ IsClipDrag = fromClip;
+ try { await DragDrop.DoDragDropAsync(press, data, DragDropEffects.Copy); }
+ finally { CurrentDrag = null; IsClipDrag = false; }
+ }
+
// --- external file drops (from Finder / other programs) -----------------
// Audio extensions Nota can import as a clip / sample; MIDI files (MidiFileReader)
// import as MIDI clips. Anything else in an external drag is ignored.
@@ -1172,14 +1191,7 @@ public sealed class BrowserView : UserControl
var args = _pressArgs;
_pressedItem = null; _pressArgs = null; // start the drag once
DismissPreview();
-
- // Single native pasteboard item (one format → one drag image → one
- // pasteboard item: macOS is satisfied). The payload rides CurrentDrag.
- var data = new DataTransfer();
- data.Add(DataTransferItem.Create(DataFormat.Text, DragSentinel));
- CurrentDrag = item;
- try { await DragDrop.DoDragDropAsync(args, data, DragDropEffects.Copy); }
- finally { CurrentDrag = null; }
+ await DragItemAsync(args, item);
}
private static bool InButton(StyledElement? el)
diff --git a/src/managed/Nota.App/ClipAudioExport.cs b/src/managed/Nota.App/ClipAudioExport.cs
new file mode 100644
index 00000000..fa58263c
--- /dev/null
+++ b/src/managed/Nota.App/ClipAudioExport.cs
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.
+//
+// An arrangement audio clip as a sample file, so it can be dragged onto anything that takes a
+// dropped sample (a Grain, a Drum Rack pad, a Sampler…). Clip samples live in engine memory —
+// a recording has no file at all, and an imported one is usually trimmed — so the clip's
+// played region is written to a WAV of its own. Warped clips take the region under their
+// play window (proportionally across the warp); a reversed clip is written reversed. Gain,
+// ADSR and envelopes are mix-time shaping and are left out: the sample is the raw material.
+
+using System;
+using System.IO;
+using System.Linq;
+using Nota.Application;
+using Nota.Infrastructure;
+using Nota.Presentation;
+
+namespace Nota.App;
+
+internal static class ClipAudioExport
+{
+ /// <summary>The clip's played region as a Sample browser item backed by a fresh WAV, or
+ /// null when the clip has no audio.</summary>
+ public static BrowserItem? AsSample(IAudioEngine engine, int trackId, int clipIndex, string name)
+ {
+ if (!engine.TryGetAudioClipInfo(trackId, clipIndex, out var ci) || ci.SampleId == 0) return null;
+ if (!engine.TryGetSampleInfo(ci.SampleId, out var si) || si.Channels <= 0 || si.Frames <= 0) return null;
+
+ long off = Math.Clamp((long)ci.SourceOffsetFrames, 0, si.Frames);
+ long len = si.Frames - off;
+ if (ci.LengthFrames > 0) len = Math.Min(len, ci.LengthFrames);
+ if (ci.WarpEnabled != 0 && ci.WarpBeats > 0)
+ {
+ double a = Math.Clamp(ci.WarpPlayStart / ci.WarpBeats, 0, 1);
+ double b = ci.WarpPlayEnd > 0 ? Math.Clamp(ci.WarpPlayEnd / ci.WarpBeats, a, 1) : 1;
+ long from = (long)Math.Round(a * len);
+ off += from;
+ len = Math.Max(0, (long)Math.Round(b * len) - from);
+ }
+ if (len <= 0) return null;
+
+ var data = engine.ReadSample(ci.SampleId);
+ int ch = si.Channels;
+ if (data.Length < (off + len) * ch) return null;
+ var region = new float[len * ch];
+ Array.Copy(data, off * ch, region, 0, region.Length);
+ if (ci.Reversed != 0)
+ for (long i = 0, j = len - 1; i < j; i++, j--)
+ for (int k = 0; k < ch; k++)
+ (region[i * ch + k], region[j * ch + k]) = (region[j * ch + k], region[i * ch + k]);
+
+ // A folder per drag: the file is named after the clip (instruments show it), and two
+ // clips with the same name must never overwrite a file an instrument already loaded.
+ string clean = new string((name.Length > 0 ? name : "Clip")
+ .Select(c => Path.GetInvalidFileNameChars().Contains(c) ? '_' : c).ToArray()).Trim();
+ if (clean.Length == 0) clean = "Clip";
+ string dir = Path.Combine(Path.GetTempPath(), "Nota", "Clip Samples", Guid.NewGuid().ToString("N")[..8]);
+ Directory.CreateDirectory(dir);
+ string path = Path.Combine(dir, clean + ".wav");
+ using (var w = new WavWriter(path, si.SampleRate > 0 ? (int)Math.Round(si.SampleRate) : 44100, ch, WavBitDepth.Float32))
+ w.WriteFrames(region, (int)len);
+ return new BrowserItem { Name = clean, Kind = BrowserItemKind.Sample, Path = path };
+ }
+}
diff --git a/src/managed/Nota.App/MainWindow.axaml.cs b/src/managed/Nota.App/MainWindow.axaml.cs
index c94c3c32..8453bc1c 100644
--- a/src/managed/Nota.App/MainWindow.axaml.cs
+++ b/src/managed/Nota.App/MainWindow.axaml.cs
@@ -180,6 +180,8 @@ public partial class MainWindow : Window
Timeline.MidiClipActivated += OpenClipEditor;
Timeline.AudioClipActivated += OpenAudioClipEditor;
Timeline.ItemDropped += OnArrangementDrop; // browser drag & drop (M7-5)
+ Timeline.SampleToInstrument += (item, t) => DropBrowserItem(item, t); // audio clip → Grain / Drum Rack / Sampler…
+ Timeline.DevicesPeekRequested += t => { if (_deviceChain?.TrackId != t) ShowDevices(t); };
Timeline.PasteBouncedRequested += (track, beat) => _ = PasteBouncedAsync(track, beat);
Timeline.ConvertClipRequested += OnConvertClip; // audio clip → MIDI (Convert / Slice)
Timeline.SeparateStemsRequested += OnSeparateStems; // audio clip → stem tracks (AI model)
diff --git a/src/managed/Nota.App/PreferencesWindow.cs b/src/managed/Nota.App/PreferencesWindow.cs
index 6f171bf4..7cc529f9 100644
--- a/src/managed/Nota.App/PreferencesWindow.cs
+++ b/src/managed/Nota.App/PreferencesWindow.cs
@@ -1055,6 +1055,8 @@ public sealed partial class PreferencesWindow : NotaWindow
("Drag a device header", "Reorder devices (or right-click the header → Move left / right)"),
("Right-click a device header", "A / B compare, move, copy, delete, presets, save preset"),
("Delete (device selected)", "Remove the selected device — the header no longer carries a close button"),
+ ("Drag an audio clip onto an instrument track", "Load its audio into the Sampler, Grain, Drum Rack (next free pad) or Instrument Rack · rest there to open its devices"),
+ ("Drag an audio clip out of the Arrangement", "Take it as a sample: drop on a pad, a Grain, a Rhythm voice, a Session slot — the clip stays put"),
("Drag clip + ⌥", "Position freely, ignoring the grid for this drag (the magnet in the transport latches the same thing)"),
("Drag knob", "Change a device value · hold ⌘ or ⇧ for fine steps"),
("Double-click knob", "Reset the value to its default"),