Skip to content

feat: Start/End and Reverse Parameters for Tone.Sampler#1397

Open
LoopBraker wants to merge 4 commits intoTonejs:devfrom
LoopBraker:dev
Open

feat: Start/End and Reverse Parameters for Tone.Sampler#1397
LoopBraker wants to merge 4 commits intoTonejs:devfrom
LoopBraker:dev

Conversation

@LoopBraker
Copy link
Copy Markdown

Summary

This PR extends Tone.Sampler with two new features that enable more flexible sample playback, inspired by Autotel:

1. Start/End Parameters for triggerAttack

Added optional start and end parameters to triggerAttack() allowing playback of specific portions of a sample without requiring loop mode.

// Play from 0.5s to 2.0s of the sample
sampler.triggerAttack("C4", Tone.now(), 1, 0.5, 2.0);

Signature change:

triggerAttack(
  notes: Frequency | Frequency[],
  time?: Time,
  velocity?: NormalRange,
  start?: Time,    // NEW: optional start offset in seconds
  end?: Time       // NEW: optional end offset in seconds
): this

2. Reverse Playback Support

Added a reverse property that plays samples backwards. When combined with start/end parameters, the offsets are automatically recalculated to maintain intuitive behavior.

sampler.reverse = true;
sampler.triggerAttack("C4"); // Plays the sample in reverse

Implementation Details

  • Uses ToneBufferSource's existing start and playbackRate capabilities
  • Reverse mode temporarily reverses the buffer, plays it, then restores it
  • All existing Sampler functionality remains unchanged (100% backward compatible)
  • Attack/release envelope still applies normally

New Example

Added samplerRange.html demonstrating:

  • File upload for custom samples
  • Interactive waveform visualization with zoom
  • Start/end range selection with visual overlay
  • Reverse toggle
  • Envelope visualization (attack/release curves)
  • Auto-fit selection toggle

Allows playing a portion of a sample without requiring loop mode.
This enables smooth playback from arbitrary positions with the
envelope's fadeIn applied to prevent clicking artifacts.

Usage: sampler.triggerAttack("C4", time, velocity, startTime, endTime)
Adds a `reverse` property to Sampler that enables reversed sample playback.
When combined with start/end parameters in triggerAttack, allows playing
portions of samples backwards while still benefiting from the envelope's
fadeIn/fadeOut to prevent clicking artifacts.

Usage:
  sampler.reverse = true;
  sampler.triggerAttack("A4", time, velocity, 0.5, 1.0); // plays 1.0s->0.5s reversed
@tambien tambien changed the title Start/End and Reverse Parameters for Tone.Sampler feat: Start/End and Reverse Parameters for Tone.Sampler Jan 10, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 10, 2026

Codecov Report

❌ Patch coverage is 56.71642% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.85%. Comparing base (d27e6c1) to head (e38bc2c).
⚠️ Report is 21 commits behind head on dev.

Files with missing lines Patch % Lines
Tone/instrument/Sampler.ts 56.72% 29 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1397      +/-   ##
==========================================
- Coverage   98.98%   98.85%   -0.13%     
==========================================
  Files         204      204              
  Lines       23138    23211      +73     
  Branches     1017     1027      +10     
==========================================
+ Hits        22901    22943      +42     
- Misses        237      268      +31     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant