Skip to content

Adding loop functionality to Sampler (#1310) #1350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

Caden-Hornyak
Copy link

This PR adds looping functionality to the Sampler consistent with Tone.Player. Looping is useful for Sampler instruments that need permanent continuous sound through crossfading such as the Violin.

An earlier PR by maximelebreton's from 2023 introduced basic looping but it lacked Unit testing as well as consistent behavior with Tone.Player such as getter's/setter's.

Copy link

codecov bot commented Jun 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.97%. Comparing base (2626e0a) to head (2631c37).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1350      +/-   ##
==========================================
+ Coverage   98.95%   98.97%   +0.01%     
==========================================
  Files         203      203              
  Lines       22725    22829     +104     
  Branches      996     1005       +9     
==========================================
+ Hits        22487    22593     +106     
+ Misses        237      236       -1     
+ Partials        1        0       -1     

☔ 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.

Copy link
Contributor

@tambien tambien left a comment

Choose a reason for hiding this comment

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

thank you so much for adding this functionality, it is very often requested!

a couple mostly very small nits in the PR and otherwise it looks good!

// get the current sources
this._activeSources.forEach((sourceList) => {
sourceList.forEach((source) => {
source.loopStart = loopEnd;
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like you accidentally set loopStart instead of loopEnd here.

@@ -197,6 +226,7 @@ export class Sampler extends Instrument<SamplerOptions> {
if (!Array.isArray(notes)) {
notes = [notes];
}
const offset = defaultArg(0, this._loopStart);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you have this reversed, should be

const offset = defaultArg(this._loopStart, 0);

@@ -210,16 +240,22 @@ export class Sampler extends Instrument<SamplerOptions> {
const playbackRate = intervalToFrequencyRatio(
difference + remainder
);
let duration = this._loop
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: if duration isn't redefined, could be const instead of let

* @param loopStart The loop start time
* @param loopEnd The loop end time
* @example
* const sampler = new Tone.Sampler("https://tonejs.github.io/audio/berklee/guitar_chord4.mp3").toDestination();
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this @example isn't a valid use of the API. i think it'd need to look like:

const sampler = new Tone.Sampler({
	urls: {
		A1: "https://tonejs.github.io/audio/berklee/guitar_chord4.mp3",
	},
}).toDestination();
// loop between the given points
sampler.setLoopPoints(0.2, 0.3);
sampler.loop = true;

@Caden-Hornyak
Copy link
Author

I've pushed fixes for the nits and linting errors. Let me know if anything else needs adjusting!

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.

2 participants