Skip to content

Resample clips moved to a track with a different sample rate - #12436

Merged
saintmatthieu merged 4 commits into
audacity:masterfrom
dozzzzer:fix-clip-move-sample-rate
Oct 28, 2026
Merged

saintmatthieu merged 4 commits into
audacity:masterfrom
dozzzzer:fix-clip-move-sample-rate

Conversation

@dozzzzer

@dozzzzer dozzzzer commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Resolves: #11930
Resolves: #12512
Resolves: #12513

Do a resampling when moving or copying clips between projects/tracks of different sample rate

QA checklist, oh boy, there we go

Moving

given:

  • Track A1: 44.1 khz
  • Track A2: 44.1 khz
  • Track B1: 48 khz
  • Track B2: 48 khz
  • Track C1: 96 khz
  • Track C2: 96 khz
  • Clip T: on track A1 5min tone
  • Clip X: on track A1 1-min tone
  • Clip Y: on track B1 1-min tone
  • Clip Z: on track C1 1-min tone

One clip move:

  • Drag T from A1 to B1:
    • the resampling dialog shows up,
    • operation finished successully
    • clip is resampled
    • during resampling nothing unexpected happens (for example clip doesn't jump back to A)
  • Drag T back from B1 to A1, all the above is true
  • Move T using keyboard from A1 to C1, the same
  • Drag T from A1 to A2
    • Clip was moved, no resampling dialog, no resampling happened
  • Change pitch and speed of T, drag to resample, observe that pitch and speed doesn't change
  • Move T to C1, then move to an empty track
    • No resample should happen
    • The empty track sample rate should be set to 96khz
  • Move T to C1, then move to a new track
    • No resample should happen
    • The new track sample rate should be set to 96khz
  • Repeat steps that cause the resampling and cancel it
    • Clip is returned to the original position
    • No history record created
  • Repeat steps that cause the resampling and press undo and redo
    • Clip moves correctly
    • Application behaves

Multiple clip move

given:
same as above plus a Clip T2 on A1

  • Repeat all steps from "One clip move", there should be no change in behavior
  • Group T1 and T2, repeat previous step, there should be no change in behavior, the group should stay

Cross project move

given:
Same as above in two separate project P1 and P2

  • Repeat previous steps with single and multiple clips, instead of moving clips:
    • Copy a single clip, multiple clips, and grouped clips from P1 to P2
    • Cut a single clip, multiple clips, and grouped clips from P2 to P1

Import (yes, unfortunately import is also might be affected)

  • Drag 44.1 khz file onto an empty 48khz track
    • No resampling
    • Track set to 48khz
  • Drag 44.1khz file onto a 48khz track that has audio
    • Clip resampled
    • Track stays at 48khz
  • Enable tempo detection, drag a 44.1 loop onto a 48khz track that has audio, choose "Stretch to project tempo"
    • Clip is stretched
    • Clip is resampled
  • Same as above, instead of dragging, copy a file in the file manager
  • Same as above, drag a multichannel file onto the tracks of mixed sample rate, verify that the behavior is sane

and the original issues:

  • 12512 is fixed
  • 12513 is fixed
  • I signed CLA
  • This PR includes AI assistance
  • If the PR is AI-assisted, I have read, understood and complied with the guidelines for AI-assisted contributions
  • The title of the pull request describes an issue it addresses
  • If changes are extensive, then there is a sequence of easily reviewable commits
  • Each commit's message describes its purpose and effects
  • There are no behavior changes unnecessary for the stated purpose of the PR

Recommended:

  • Each commit compiles and runs on my machine without known undesirable changes of behavior

@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Walkthrough

Clip moves and paste operations now resample clips when destination track rates differ. The resampling utility reports progress and returns errors, including on cancellation or when a non-placeholder clip becomes empty. Vertical moves propagate resampling errors and notify track changes when sample rate or channel count changes. Paste operations update pasted track rates after successful resampling. Wave-track clipboard copies use the current project’s WaveTrackFactory. The move controller keeps the preview active until the move operation returns.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  actor User
  participant Au3ClipsInteraction
  participant resampleClips
  participant IInteractive
  User->>Au3ClipsInteraction: Move clips vertically
  Au3ClipsInteraction->>resampleClips: Resample clips for destination rates
  resampleClips->>IInteractive: Report progress
  IInteractive-->>resampleClips: Continue or cancel
  resampleClips-->>Au3ClipsInteraction: Return result
  Au3ClipsInteraction-->>User: Return move result
Loading
Merge Risk: Low · up to 1ad89

Pasting into an empty track can unnecessarily change the audio’s sample rate. This is a bounded issue to fix or accept before merging.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: resampling clips when they move to tracks with a different sample rate.
Linked Issues check Passed The changes address all three active linked issues. For [#11930], vertical moves resample clips to the destination track rate and abort on resampling failure. The shared utility updates clip rates aft…
Out of Scope Changes check Passed The paste and clipboard changes are within scope. [#12512] directly requires reliable multi-clip copy and paste between projects. [#12513] covers data integrity for clips with different sample rates. …
Description check Passed The pull request description includes issue links, a clear summary and motivation, a detailed QA checklist, and all required template checkboxes. The description is complete and relevant.
Full details: Linked Issues check

Explanation

The changes address all three active linked issues. For [#11930], vertical moves resample clips to the destination track rate and abort on resampling failure. The shared utility updates clip rates after conversion, which preserves pitch and duration. For [#12512], trackDataCopy creates copied wave tracks through the current project's WaveTrackFactory, including copied clips. For [#12513], move and paste paths resample before track replacement or project modification, and failed or canceled resampling stops the operation. The supplied summary provides no evidence of automated regression tests, but the linked issues do not specify a test implementation requirement.

Full details: Out of Scope Changes check

Explanation

The paste and clipboard changes are within scope. [#12512] directly requires reliable multi-clip copy and paste between projects. [#12513] covers data integrity for clips with different sample rates. Move-preview handling, cancellation propagation, empty-track rate assignment, and shared resampling support the stated move, copy, and import behavior. No unrelated change is demonstrated by the supplied whole-PR summary.

  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Linked repositories: Public OSS repositories can only analyze public repositories installed in this organization. No linked repositories were analyzed; skipped musescore/musescore.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@dozzzzer
dozzzzer requested a review from kryksyh October 9, 2026 09:01

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟡 Minor · Defer the horizontal mutation until vertical resampling… · au3clipsinteraction.cpp:957-974

src/trackedit/internal/au3/au3clipsinteraction.cpp:957-974
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Defer the horizontal mutation until vertical resampling succeeds.

When both offsets are nonzero, moveClips changes the original clip start time before moveSelectedClipsUpOrDown begins resampling. Cancellation returns failure before the temporary track list replaces the original, but no rollback restores the start time. This violates the Cancel-leaves-clip-untouched contract.

Apply the horizontal change after the successful vertical move, using the updated clip keys.

Suggested fix
-    changeClipsStartTime(clipKeyList, timePositionOffset, false);
-
     muse::Ret moveRet = muse::make_ok();
     const bool needsDownmixing = trackPositionOffset != 0
                                  && moveSelectedClipsUpOrDown(newClipKeyList, trackPositionOffset, moveRet) == NeedsDownmixing::Yes;
     if (!moveRet) {
         return muse::RetVal<ClipKeyList>::make_ret(moveRet);
     }
 
+    changeClipsStartTime(newClipKeyList, timePositionOffset, false);
+
     const muse::Ret makeRoomRet = makeRoomForClips(newClipKeyList);

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 99e098c3-880c-48ad-9213-564a17df6e4c
📥 Commits

Reviewing files that changed from the base of the PR and between bc5eed4 and 87edeec.

📒 Files selected for processing: 3
  • src/projectscene/view/tracksitemsview/trackitemsmovecontroller.cpp
  • src/trackedit/internal/au3/au3tracksinteraction.cpp
  • src/trackedit/tests/au3tracksinteraction_tests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@kryksyh
kryksyh force-pushed the fix-clip-move-sample-rate branch from 87edeec to d3a6bc3 Compare October 21, 2026 11:14
@kryksyh kryksyh changed the title 11930 Resample clips moved to a track with a different sample rate Resample clips moved to a track with a different sample rate Oct 21, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 35d54ec6-07c2-49ef-92f9-42dbfddd246a
📥 Commits

Reviewing files that changed from the base of the PR and between d3a6bc3 and 1ad89a5.

📒 Files selected for processing: 4
  • src/trackedit/internal/au3/au3clipsinteraction.cpp
  • src/trackedit/internal/au3/au3interactionutils.cpp
  • src/trackedit/internal/au3/au3interactionutils.h
  • src/trackedit/internal/au3/au3tracksinteraction.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Comment on lines +293 to +305
// A destination with clips keeps its rate: convert the pasted data before the project is changed
std::vector<std::pair<Au3WaveTrack*, int> > tracksToResample;
std::vector<std::pair<std::shared_ptr<Au3WaveClip>, int> > clipsToResample;
for (size_t i = 0; i < dstTracksIds.size(); ++i) {
const Au3WaveTrack* dstWaveTrack = DomAccessor::findWaveTrack(projectRef(), Au3TrackId(dstTracksIds[i]));
const auto trackToPaste = std::static_pointer_cast<Au3WaveTrack>(copiedData.at(i)->track());
if (dstWaveTrack && !dstWaveTrack->IsEmpty() && trackToPaste->GetRate() != dstWaveTrack->GetRate()) {
const int rate = static_cast<int>(dstWaveTrack->GetRate());
tracksToResample.emplace_back(trackToPaste.get(), rate);
for (const auto& clip : trackToPaste->Intervals()) {
clipsToResample.emplace_back(clip, rate);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

set -o pipefail
printf '%s\n' '--- pasteClips and insertBlankSpace references ---'
rg -n -F --glob '*.cpp' --glob '*.h' \
  -e 'pasteClips' -e 'insertBlankSpace' -e 'IsPlaceholder' -e 'SetIsPlaceholder' \
  -e 'GetRate()' -e 'SetRate(' \
  src/trackedit/internal/au3/au3tracksinteraction.cpp \
  au3/libraries/au3-wave-track/WaveTrack.cpp \
  au3/libraries/au3-wave-track/WaveTrack.h \
  src/trackedit/tests/au3tracksinteraction_tests.cpp || test "$?" -eq 1

printf '%s\n' '--- pasteClips block ---'
sed -n '250,370p' src/trackedit/internal/au3/au3tracksinteraction.cpp

printf '%s\n' '--- paste caller and insertBlankSpace block ---'
sed -n '180,235p' src/trackedit/internal/au3/au3tracksinteraction.cpp

printf '%s\n' '--- WaveTrack emptiness and placeholder-related definitions ---'
rg -n -F --glob '*.cpp' --glob '*.h' \
  -e 'bool WaveTrack::IsEmpty' -e 'IsEmpty() const' -e 'IsPlaceholder' -e 'SetIsPlaceholder' \
  au3/libraries/au3-wave-track src/trackedit | head -120

printf '%s\n' '--- WaveTrack implementation excerpts ---'
sed -n '930,975p' au3/libraries/au3-wave-track/WaveTrack.cpp
sed -n '1760,1910p' au3/libraries/au3-wave-track/WaveTrack.cpp

printf '%s\n' '--- nearby tests and rate-related assertions ---'
rg -n -F --glob '*test*' \
  -e 'moveAllTracks' -e 'paste' -e 'GetRate' -e 'SetRate' -e 'sample rate' -e 'sampleRate' \
  src/trackedit/tests src/trackedit || test "$?" -eq 1

Repository: audacity/audacity

Length of output: 35227


Treat placeholder-only destinations as empty in both rate checks.

moveAllTracks inserts a placeholder before pasteClips(). WaveTrack::IsEmpty() counts that placeholder, so an originally empty destination enters the resampling branch. The later empty-track branch also remains skipped. WaveTrack::Paste() then resamples the new clip to the destination’s existing rate.

Use non-placeholder occupancy for both checks. This lets the destination adopt the pasted track’s rate while preserving the existing behavior for destinations with real clips.

Suggested fix
     if (!ret) {
         return ret;
     }

+    const auto hasRealClips = [](const Au3WaveTrack* track) {
+        if (!track) {
+            return false;
+        }
+        for (const auto& clip : track->Intervals()) {
+            if (!clip->GetIsPlaceholder()) {
+                return true;
+            }
+        }
+        return false;
+    };
+
     // A destination with clips keeps its rate: convert the pasted data before the project is changed
     std::vector<std::pair<Au3WaveTrack*, int> > tracksToResample;
     std::vector<std::pair<std::shared_ptr<Au3WaveClip>, int> > clipsToResample;
     for (size_t i = 0; i < dstTracksIds.size(); ++i) {
         const Au3WaveTrack* dstWaveTrack = DomAccessor::findWaveTrack(projectRef(), Au3TrackId(dstTracksIds[i]));
         const auto trackToPaste = std::static_pointer_cast<Au3WaveTrack>(copiedData.at(i)->track());
-        if (dstWaveTrack && !dstWaveTrack->IsEmpty() && trackToPaste->GetRate() != dstWaveTrack->GetRate()) {
+        if (hasRealClips(dstWaveTrack) && trackToPaste->GetRate() != dstWaveTrack->GetRate()) {
             const int rate = static_cast<int>(dstWaveTrack->GetRate());
             tracksToResample.emplace_back(trackToPaste.get(), rate);
             for (const auto& clip : trackToPaste->Intervals()) {
                 clipsToResample.emplace_back(clip, rate);
             }
@@
-        if (dstWaveTrack->IsEmpty()) {
+        if (!hasRealClips(dstWaveTrack)) {

kryksyh and others added 4 commits October 28, 2026 13:03
Co-Authored-By: [email protected]
Assisted-by: Claude Code (Claude Opus 5.5)
A multi-clip paste into a track with clips at another rate inserted the
clips without resampling. As a result they played at the wrong pitch,
and might be removed on the next load (audacity#12513)

Co-Authored-By: [email protected]
Assisted-by: Claude Code (Claude Opus 5.5)
Assisted-by: Claude Code (Claude Opus 5.5)
@saintmatthieu
saintmatthieu force-pushed the fix-clip-move-sample-rate branch from 1ad89a5 to 10d1f7e Compare October 28, 2026 12:03
@saintmatthieu
saintmatthieu merged commit 7321d83 into audacity:master Oct 28, 2026
3 of 10 checks passed
saintmatthieu added a commit that referenced this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants