Sample slicing in Tone.js, Autotel


Sample slicing in Tone.js

Date: 2016.05, Context: Aalto, Context: For-the-sake-of-doing


Today I needed to make a sample that would play a portion of a sample instead of playing the whole sample, from a nexus ui waveshape element. There are three problems that disallowed this:


So, I made a fork of the Tone.js library, that allows the sampler to have start and end points. They are inserted when triggering attack, which is out of the Tone logic; but it allowed me to solve the problem fast, to continue my collaborative MsComposer95 project.

line 18504; replacement for Tone.Sampler.prototype.triggerAttack:

 *  Start the sample and simultaneously trigger the envelopes.
 *  @param {string=} sample The name of the sample to trigger, defaults to
 *                          the last sample used.
 *  @param {Time} [time=now] The time when the sample should start
 *  @param {number} [velocity=1] The velocity of the note
 *  @param {range} [] Define start and end points within the sample player, if you don't want to play it entirely
 *  @returns {Tone.Sampler} this
 *  @example
 * sampler.triggerAttack("B.1");
  Tone.Sampler.prototype.triggerAttack = function (name, time, velocity, range) {
      time = this.toSeconds(time);
      if (name) {
          this.sample = name;
			if( (range||{}).hasOwnProperty("start")&&(range||{}).hasOwnProperty("end") ){
				console.log("no range");

      this.envelope.triggerAttack(time, velocity);
      return this;

So, you would make it work by calling it this way:

//where sampler is a Tone Sampler, and loopStart & loopEnd are defined

Future needs

My future push will consist of using the sampler’s loopStart and loopEnd points instead of injecting the range object. You can grab the tweaked version in my Tone.js fork.

You can download a working example here.


Leave a Reply