Skip to content

Playing multiple samples controlled with the playhead

Kevin Lewis edited this page Feb 5, 2017 · 1 revision
var audio = new maximJs.maxiAudio();

var timer = new maximJs.maxiOsc();
var currentCount;
var isTriggered;
var playhead = 0;

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

  kick = new maximJs.maxiSample();
  audio.loadSample("assets/kick.mp3", kick);
}

function playLoop(){
  currentCount = timer.phasor(8);

  if(currentCount < 0.5 && !isTriggered && click.isReady()) {
    playhead = (playhead+1)%16;
    isTriggered = true;

    if(playhead % 2 == 0) {
      click.trigger();
    }

    if(playhead % 4 == 0) {
      kick.trigger();
    }

  } else if (currentCount > 0.5) {
    isTriggered = false;
  }

  this.output = (click.playOnce() + kick.playOnce()) / 2;
}
  1. Create a new maximJs.maxiSample() object.
  2. Load a sample into the object.
  3. Use the playhead position to trigger the new sample.
  4. Don't forget to divide your this.output by the total number of samples.
Clone this wiki locally