嵌套的setIntervals具有不同的时间或在LCD上计算setIntveral

时间:2013-11-04 05:27:22

标签: javascript

我正在尝试创建几种不同的乐器,每种乐器都有一个或多个测量,所有测量都包含一个或多个节拍。

乐器有自己的速度,单位为BPM(每分钟节拍)。因此,如果乐器播放其“歌曲”,则以numberOfMeasuresInInstrument*beatsInSingleMeasure/instrumentTempo*60.0*1000.0计算以ms为单位的长度/持续时间 beatsInSingleMeasure也可以称为signature,因为音乐是以4/4等单位定义的。

我正在尝试循环播放'歌曲'。因此,如果我有多个具有不同持续时间的乐器,我希望它们全部同时开始,并播放到他们自己的“乐谱”结束,然后等到所有乐器完成(即最长的乐器持续时间),然后重新启动(这实际上是使每个“得分表”的持续时间相等,并添加适当数量和速度的休止符以等于最长持续时间的工具)。因此,所有乐器应该同时开始,并以节奏的间隔播放节拍,我认为这应该由另一个setInterval()管理。

Here是一个小提琴来说明相对于彼此的持续时间。 假设第一个乐器播放持续时间为2秒,第二个乐器播放持续1.6秒,第三个播放持续1.5秒。这将使得第一乐器以每66秒的间隔播放,第二乐器的节奏每隔0.4秒发挥一次,第三乐器的节奏每0.25秒发挥一次。

我该如何做到这一点?从概念上讲,嵌套间隔是有意义的,说每隔2秒开始一次,并以适当的间隔播放你的节拍数量(第一次:每次.66秒三次;第二次:每次.4次四次跳动;每次.25次三次跳动)。这似乎比试图找出节拍持续时间的LCD并以这种方式播放它更容易,因为我需要跟踪什么是节拍或休息,以及什么是填充物以确保它在LCD单元中,在这种情况下,它是.25,.4和.66 =>的LCD。 .01666 s(也是60Hz或1/60秒)

目前我有以下内容:

//Which beat are we on?  first ones are in the negative to allow the 'song'to buffer once through the 'song', required by HTML5 Audio buffer
var beatCounter = 0-(signature*measuresCount-1);
// How many measures?
var measureCounter = 0;
// Are we waiting on another instrument to play, because we haven't reached the maximum play duration
var waitCounter = false;
function calculateWaiting(){
  if ( beatCounter*dur == currentInstrumentDuration && currentInstrumentDuration < maxDuration ){
    waitCounter = true;
  } else {
    waitCounter = false;
  }
};

//This is the instrument interval
this.animationIntervalID = setInterval((function(self) {
  return function() {
    //this is the beat interval                   
    this.beatAnimationIntervalID = setInterval((function(self) {
      return function() {
        //if we have not yet played all the beats and are waiting on the 
        if (waitCounter == false){            
          // As long as we are still have beats to play, keep playing                                                                
          if (beatCounter >= 0 && beatCounter < totalNumberOfBeats) {
            //Get all the beats
            var beats = $('#measure-rep-'+self.measureRepModel.cid).find('.audio-beat');
            // If the beat is selected, ie not a rest....
            var selected = self.parentMeasureModel.get('beats').models[beatCounter].get('selected');
            //animate it
            self.audioAnimate(beats.eq(beatCounter)[0], dur, selected);
          }
          //Increase the beatCounter until all of the beats have played, then reset the beatCounter
          if (beatCounter < (totalNumberOfBeats-1)) {
            beatCounter ++;
          } else {
            beatCounter = 0;
          }
          calculateWaiting();
        } else {
          // Waiting until the longest measure is complete
          console.log('waiting until the longer instrument is done playing');
        }
      };
    })(this), dur); //dur is the cycle at which every beat should be animating/lasting
  };
})(this), maxDuration); //maxDuration is the cycle at which every instrument should be waiting for to repeat

所以这是较短的版本:

//This is the instrument interval
this.animationIntervalID = setInterval((function(self) {
  return function() {
    //this is the beat interval                   
    this.beatAnimationIntervalID = setInterval((function(self) {
      return function() { ... };
    })(this), dur); //dur is the cycle at which every beat should be animating/lasting
  };
})(this), maxDuration); //maxDuration is the cycle at which every instrument should be waiting for to repeat

我想要完成的线性视觉:

//Scroll this window to see it linearly with time
// Let's assume that the first instrument plays for a duration of 2 seconds, the second
// plays for 1.6 s, and the third plays for 1.5 s. That would make the first instruments 
// beats play at an interval of every .66s, the second instrument's beats play every
// .4 s, and the third instrument's beats play every .25s.
// 
// The LCD of all instrument's beats is .01666s (60 Hz or 1/60th of a second), so it
// should look something like this: P meaning Play the beat, p, meaning the beat still 
// being sustained, I meaning the LCD interval, and W meaning waiting 
//
//    0s                            .5s                           1s                            1.5s                          2s
//    ^                             ^                             ^                             ^                             ^
//    IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
// 1: PpppppppppppppppppppppppppppppppppppppppPpppppppppppppppppppppppppppppppppppppppPppppppppppppppppppppppppppppppppppppppp
// 2: PpppppppppppppppppppppppPpppppppppppppppppppppppPpppppppppppppppppppppppPpppppppppppppppppppppppWWWWWWWWWWWWWWWWWWWWWWWW
// 3: PppppppppppppppPppppppppppppppPppppppppppppppPppppppppppppppPppppppppppppppPppppppppppppppWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

0 个答案:

没有答案