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.

(fix) clip duplication behaviour

Egor Khindikaynen0c4d40e0f860
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2c3502eb..9a0ff011 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -58,6 +58,9 @@ Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.
bare R and M no longer flip them by accident while you play. The Mixer moves to **⌘⇧M**.
### Fixed
+- Duplicating a clip (⌘D) now places the copy right after it, even when other clips follow.
+ Before, the copy jumped past the last clip on the track. Like dragging, it replaces the
+ part of any clip it lands on.
- A MIDI note that is already held at the playhead now sounds when you press Play in the
middle of it, drag the playhead onto it during playback, or loop back into it. Before,
only the next note in the clip played. Dragging the playhead during playback also
diff --git a/src/native/nota.engine/include/nota/nota_engine.h b/src/native/nota.engine/include/nota/nota_engine.h
index 82add03c..c871f20e 100644
--- a/src/native/nota.engine/include/nota/nota_engine.h
+++ b/src/native/nota.engine/include/nota/nota_engine.h
@@ -735,7 +735,7 @@ NOTA_API NotaResult nota_midi_clip_env_set(NotaEngine* engine, int32_t track_id,
NOTA_API NotaResult nota_clip_set_warp_markers(NotaEngine* engine, int32_t track_id, int32_t clip_index, const double* src_frames, const double* beats, int32_t count);
NOTA_API int32_t nota_clip_get_warp_markers(const NotaEngine* engine, int32_t track_id, int32_t clip_index, double* out_src, double* out_beat, int32_t max_count);
NOTA_API int32_t nota_clip_split(NotaEngine* engine, int32_t track_id, int32_t clip_index, double at_beat); /* -> new clip index, or -1 */
-NOTA_API int32_t nota_clip_duplicate(NotaEngine* engine, int32_t track_id, int32_t clip_index); /* -> new clip index, or -1 */
+NOTA_API int32_t nota_clip_duplicate(NotaEngine* engine, int32_t track_id, int32_t clip_index); /* right after itself, overwriting what it covers -> new clip index, or -1 */
NOTA_API NotaResult nota_clip_delete(NotaEngine* engine, int32_t track_id, int32_t clip_index);
/* Time-range clip ops for the arrangement time-selection: split at the range boundaries
* so only covered content is affected, in one undo step, no ripple. delete_range carves
@@ -769,8 +769,9 @@ NOTA_API int32_t nota_clip_clipboard_kind(NotaEngine* engine);
/* Block clip clipboard (multi-selection). A block = a set of clips (track_ids[i],
* clip_indices[i]) captured with their track ids, beats relative to the block start, and
* each clip's automation. block_paste re-lands the block onto the same tracks at at_beat;
- * block_duplicate places the copy right after the block. Overlap is avoided by shifting the
- * WHOLE block by one shared delta, so relative geometry is preserved. Each op is a single
+ * block_duplicate places the copy right after the block, overwriting (carving) whatever it
+ * lands on, like a drag. block_paste avoids overlap by shifting the WHOLE block by one shared
+ * delta, so relative geometry is preserved. Each op is a single
* undo step. block_duplicate returns the block length (>0), or -1. last_placed writes the
* (track_id, clip_index) of every clip the last paste/duplicate produced (up to cap pairs)
* and returns the total count, so the UI can re-select them. */
diff --git a/src/native/nota.engine/src/Engine.h b/src/native/nota.engine/src/Engine.h
index 969f6ade..5d84dbcf 100644
--- a/src/native/nota.engine/src/Engine.h
+++ b/src/native/nota.engine/src/Engine.h
@@ -1005,10 +1005,11 @@ private:
// BlockClip entries rebased to the block start, returning the block's total length.
// placeBlock re-lands a captured block at placedStart onto the same tracks, shifting the
// WHOLE block right by a single shared delta to clear existing clips, and publishes all
- // touched tracks in one undo step; it records the placed clips in lastPlaced_.
+ // touched tracks in one undo step; it records the placed clips in lastPlaced_. With
+ // overwrite it never shifts: the block lands at placedStart and carves what it covers.
std::vector<BlockClip> captureBlock(const std::vector<std::pair<int32_t,int32_t>>& sel,
double& blockLen) const;
- void placeBlock(const std::vector<BlockClip>& items, double placedStart);
+ void placeBlock(const std::vector<BlockClip>& items, double placedStart, bool overwrite = false);
// Build a fully independent copy of a track (fresh id + cloned instrument/devices).
std::shared_ptr<Track> deepCloneTrack(const Track& src, int32_t newId);
diff --git a/src/native/nota.engine/src/Engine_Tracks.cpp b/src/native/nota.engine/src/Engine_Tracks.cpp
index ded36145..5a3256ad 100644
--- a/src/native/nota.engine/src/Engine_Tracks.cpp
+++ b/src/native/nota.engine/src/Engine_Tracks.cpp
@@ -1663,14 +1663,17 @@ int32_t Engine::duplicateClip(int32_t trackId, int32_t clipIndex) {
if (!old) return -1;
const double spb = transport_.samplesPerBeat(), devSR = transport_.sampleRate();
auto nt = cloneTrack(*old);
+ int32_t newIdx;
if (nt->type() == TrackType::Instrument) {
if (clipIndex < 0 || clipIndex >= static_cast<int32_t>(nt->midiClips.size())) return -1;
MidiClip d = nt->midiClips[clipIndex];
const double srcStart = d.startBeat, len = d.lengthBeats;
auto snips = captureClipAutomation(*nt, srcStart, srcStart + len);
- // Place right after the source, then shift past any further clip so it never overlaps.
- d.startBeat = placeMidiNonOverlap(*nt, d.startBeat + d.lengthBeats, d.lengthBeats);
- nt->midiClips.insert(nt->midiClips.begin() + clipIndex + 1, d);
+ // Land right after the source, overwriting whatever sits there (like a drag).
+ d.startBeat = srcStart + len;
+ midiOverwriteRange(nt->midiClips, d.startBeat, d.startBeat + len);
+ nt->midiClips.push_back(d);
+ newIdx = static_cast<int32_t>(nt->midiClips.size()) - 1;
applyClipAutomation(*nt, trackId, snips, d.startBeat, len, /*sameTrack*/ true);
} else {
if (clipIndex < 0 || clipIndex >= static_cast<int32_t>(nt->clips.size())) return -1;
@@ -1678,13 +1681,15 @@ int32_t Engine::duplicateClip(int32_t trackId, int32_t clipIndex) {
const double len = audioDisplayLenBeats(d, spb, devSR);
const double srcStart = d.startBeat;
auto snips = captureClipAutomation(*nt, srcStart, srcStart + len);
- d.startBeat = placeAudioNonOverlap(*nt, d.startBeat + len, len, spb, devSR);
+ d.startBeat = srcStart + len;
+ audioOverwriteRange(nt->clips, d.startBeat, d.startBeat + len, spb, devSR);
if (d.warpEnabled) configureClipWarp(d, spb, devSR); // the copy needs its own stretcher
- nt->clips.insert(nt->clips.begin() + clipIndex + 1, d);
+ nt->clips.push_back(d);
+ newIdx = static_cast<int32_t>(nt->clips.size()) - 1;
applyClipAutomation(*nt, trackId, snips, d.startBeat, len, /*sameTrack*/ true);
}
republishWithTrack(trackId, nt);
- return clipIndex + 1;
+ return newIdx;
}
// --- clip clipboard (copy/cut/paste, incl. cross-track) ---------------------
@@ -1904,7 +1909,7 @@ std::vector<Engine::BlockClip> Engine::captureBlock(
// Re-land a captured block at placedStart onto the same tracks, one shared non-overlap
// shift for the whole block (so relative geometry is preserved), publishing every touched
// track in a single undo step. Records the placed clips in lastPlaced_.
-void Engine::placeBlock(const std::vector<BlockClip>& items, double placedStart) {
+void Engine::placeBlock(const std::vector<BlockClip>& items, double placedStart, bool overwrite) {
lastPlaced_.clear();
if (items.empty() || !authoring_) return;
const double spb = transport_.samplesPerBeat(), devSR = transport_.sampleRate();
@@ -1921,8 +1926,19 @@ void Engine::placeBlock(const std::vector<BlockClip>& items, double placedStart)
// Find the minimal shared shift d >= 0 so no clip in the block overlaps an existing
// clip on its track. All clips move together by d, so relative positions never change.
+ // In overwrite mode the block lands exactly at placedStart and carves what it covers
+ // instead (all carves first, so they never cut a copy placed by an earlier item).
double d = 0.0;
- bool moved = true;
+ bool moved = !overwrite;
+ if (overwrite) {
+ for (const auto& b : items) {
+ auto it = clones.find(b.trackId);
+ if (it == clones.end()) continue;
+ const double s = placedStart + b.relStart, e = s + b.len;
+ if (b.kind == 1) midiOverwriteRange(it->second->midiClips, s, e);
+ else audioOverwriteRange(it->second->clips, s, e, spb, devSR);
+ }
+ }
while (moved) {
moved = false;
for (const auto& b : items) {
@@ -2096,7 +2112,7 @@ double Engine::duplicateClipBlock(const std::vector<std::pair<int32_t,int32_t>>&
}
}
if (absStart == std::numeric_limits<double>::max()) return -1.0;
- placeBlock(items, absStart + len);
+ placeBlock(items, absStart + len, /*overwrite*/ true); // right after itself, like a drag
return len;
}
diff --git a/tests/Nota.SmokeTest/Program.cs b/tests/Nota.SmokeTest/Program.cs
index bb4caded..591f7b92 100644
--- a/tests/Nota.SmokeTest/Program.cs
+++ b/tests/Nota.SmokeTest/Program.cs
@@ -10504,15 +10504,47 @@ Console.WriteLine("-- block clip ops (multi-selection) --");
Check(e.PasteClipBlock(20.0, -1) == 1 && e.TryGetClipInfo(a, 0, out var pc) && Math.Abs(pc.StartBeat - 20.0) < eps, "cut block pastes back at beat 20");
}
- // Shared non-overlap: a blocker at [4,6) forces the WHOLE block right by one shift, so
- // the copies' internal geometry (a 2-beat gap) is preserved instead of splitting apart.
+ // Duplicate lands right after the source even when the next slots are taken: with four
+ // clips back to back, duplicating the first overwrites the second instead of jumping past
+ // the fourth, and a partial overlap only carves the covered part.
+ {
+ using var e = new NotaEngine();
+ int a = e.AddInstrumentTrack();
+ int c0 = e.AddMidiClip(a, 0.0, 4.0);
+ e.AddMidiClip(a, 4.0, 4.0);
+ e.AddMidiClip(a, 8.0, 4.0);
+ e.AddMidiClip(a, 12.0, 4.0);
+ e.DuplicateClipBlock(new[] { (a, c0) });
+ var placed = e.LastPlacedClips();
+ Check(placed.Length == 1 && e.TryGetClipInfo(a, placed[0].clipIndex, out var d0) && Math.Abs(d0.StartBeat - 4.0) < eps,
+ "duplicate of the first clip lands right after it (beat 4)");
+ var starts = new List<(double s, double l)>();
+ for (int i = 0; e.TryGetClipInfo(a, i, out var ci); i++) starts.Add((ci.StartBeat, ci.LengthBeats));
+ Check(starts.Count == 4, $"covered clip replaced, others kept (got {starts.Count} clips)");
+ Check(starts.Any(x => Math.Abs(x.s - 12.0) < eps), "fourth clip untouched (no copy past the end)");
+ Check(!starts.Any(x => x.s >= 16.0 - eps), "nothing placed after the last clip");
+
+ using var f = new NotaEngine();
+ int b = f.AddInstrumentTrack();
+ int g0 = f.AddMidiClip(b, 0.0, 4.0);
+ f.AddMidiClip(b, 6.0, 4.0); // copy [4,8) half-covers [6,10)
+ Check(f.DuplicateClip(b, g0) >= 0, "single duplicate over a partial overlap");
+ var rest = new List<(double s, double l)>();
+ for (int i = 0; f.TryGetClipInfo(b, i, out var ci); i++) rest.Add((ci.StartBeat, ci.LengthBeats));
+ Check(rest.Any(x => Math.Abs(x.s - 4.0) < eps && Math.Abs(x.l - 4.0) < eps), "copy at [4,8)");
+ Check(rest.Any(x => Math.Abs(x.s - 8.0) < eps && Math.Abs(x.l - 2.0) < eps), "overlapped clip carved to [8,10)");
+ }
+
+ // Shared non-overlap on paste: a blocker at [4,6) forces the WHOLE block right by one shift,
+ // so the copies' internal geometry (a 2-beat gap) is preserved instead of splitting apart.
{
using var e = new NotaEngine();
int a = e.AddInstrumentTrack();
int ca = e.AddMidiClip(a, 0.0, 2.0);
int cb = e.AddMidiClip(a, 2.0, 2.0);
e.AddMidiClip(a, 4.0, 2.0); // blocker at [4,6)
- e.DuplicateClipBlock(new[] { (a, ca), (a, cb) });
+ e.CopyClipBlock(new[] { (a, ca), (a, cb) });
+ e.PasteClipBlock(4.0, -1);
var placed = e.LastPlacedClips();
Check(placed.Length == 2, "two copies placed past the blocker");
e.TryGetClipInfo(placed[0].trackId, placed[0].clipIndex, out var q0);