Skip to content

Using maxiTimestretch on a sample

Kevin Lewis edited this page Feb 12, 2017 · 1 revision

Step 0 - Set up your sample

var audio = new maximJs.maxiAudio();

function setup() {
  audio.play = playLoop;
  audio.init();
  sound = new maximJs.maxiSample();
  audio.loadSample("assets/piano.mp3", sound);
}

function playLoop() {
  if(sound.isReady()) {
    this.output = sound.play();
  } else {
    this.output = 0;
  }
}

Step 1 - Create a Timestretch object as a global variable

var timeStretcher = new maximJs.maxiTimestretch();

Step 2 - Create a conditional which only happens once, in the play loop

In this conditional, we will set the sample of our timeStretcher object. We will use boolean logic to ensure this is only triggered once.

// GLOBAL
var samplePlayerSet = false;

function playLoop() {
  if(!samplePlayerSet) {
    timeStretcher.setSample(sound);
    samplePlayerSet = true;
  }

  if(sound.isReady()) {
    this.output = sound.play();  
  } else {
    this.output = 0;
  }
}

Step 3 - Play the timeStretcher object

function playLoop() {
  if(!samplePlayerSet) {
    timeStretcher.setSample(sound);
    samplePlayerSet = true;
  }

  if(sound.isReady()) {
    this.output = timeStretcher.play(0.5, 0.1, 2, 0);  
  } else {
    this.output = 0;
  }
}

The value underlined is the only one which matters for a basic time stretching. It sets the speed relative to the usual speed of the sample, where 1 is normal, 0.5 is half and 2 is double.

Clone this wiki locally