Issue with Javascript, M4L and jump()

Charles Turner's icon

Hi all-

Encountering some unintended behavior that I can't solve, and hoping someone can enlighten me about what's going on. First:

The project has an audio effect that has two variants of the Javascript:

1) Jump: should position the play position marker at the cue point "start"
2) Play: should do the above, and then play until it reaches the "end" cue point

Here's my trouble: Jump works without problems AFAICT. With Play, if the play position is either before or after the "start" cue point, but before the "end" cue point, it works fine. If the play position is after the "end" cue point, then the play position jumps to the song beginning, and plays without stopping at the "end" cue point. Urgh.

Any pointers greatly appreciated!

Charles

Charles Turner's icon

OK, solved. Had something to do with the order in which I setup my callback. Also helps to have a separate LiveAPI for the callback:

function play() {
  var play_end, cue_count;
  var i;
  function listener(args) {
    if(args[0] === 'current_song_time') {
      if(args[1] >= play_end) {
        song_pos.call('stop_playing');
        song_pos.property = '';
      }
    }
  }
  var song_pos = new LiveAPI(this.patcher, listener, 'live_set');
  song_pos.property = 'current_song_time';

  var api = new LiveAPI(this.patcher, listener, 'live_set');
  cue_count = api.getcount('cue_points');
  for(i = 0; i < cue_count; i++) {
    api.path = 'live_set cue_points ' + i;
    if(api.getstring('name') === 'start') {
      api.call('jump');
    }
    if(api.getstring('name') === 'end') {
      play_end = parseFloat(api.getstring('time'));
    }
  }
  song_pos.call('start_playing');
}

Always appreciative of any suggestions for improvement.

Charles