Skip to content
jshanley edited this page Nov 1, 2014 · 10 revisions

# blip.loop()

Creates and returns a new loop instance.

# loop.tempo([value])

If value is specified, sets the tempo of the loop. The tempo is in ticks-per-minute. Setting the tempo also updates the value of the loop's .tickInterval accordingly.

If value is not specified, returns the current tempo.

# loop.tickInterval([value]) added in v0.2.0

If value is specified, sets the loop's tick interval, which is the length of time between ticks (in seconds). Setting the tick interval also updates the value of the loop's .tempo accordingly.

If value is not specified, returns the current tick interval.

# loop.tick([function])

If function is specified, defines a block of code to be run on each tick of the loop. The time between ticks is determined by the current tempo. The function accepts up to 3 arguments: (time, datum, index) which represent the time of the tick (in seconds since the AudioContext was created), the current datum from the data array passed to the loop, and the current index of the data array passed to the loop.

Within the specified function, the this context is a reference to the loop, so you may also adjust the loop's settings while it is running. For example:

// a loop that speeds up a little on each tick
var loop = blip.loop()
  .tempo(110)
  .tick(function(t) {
    clip.play(t);
    this.tempo(this.tempo() + 2);
  })

If function is not specified, returns the current "tick" function. This may be useful for reusing the same tick function in multiple loops. For example:

// loopB reusing the tick function from loopA
loopB.tick(loopA.tick());

# loop.each([function]) added in v0.2.0

If function is specified, defines a block of code to be run on the first tick of each iteration of the loop. The function accepts up to two arguments: (time, iteration) which represent the time of the tick (in seconds since the blip AudioContext was created), and the iteration number, which is zero-based, meaning it represents the number of iterations that have already occurred by the time of the current iteration.

If function is not specified, returns the current "each" function.

# loop.data([array])

If array is specified, assigns the array to the loop. The loop will then... loop through this data in a circular fashion, passing one element at a time to the tick function.

If array is not specified, returns the current data array.

# loop.limit([number])

If number is specified, sets the number of iterations a loop will perform before stopping. The loop is then reset, so calling .start() on it will play the loop from the beginning, stopping again when the limit is reached.

If number is not specified, returns the current limit.

# loop.start([time])

Starts the loop. If time is specified, the loop starts at the specified time, otherwise it starts immediately.

# loop.stop()

Stops the loop. This does not reset the loop to the beginning, it simply stops scheduling new ticks. This means that calling .start() again will resume the loop from where it left off.

# loop.reset()

Resets the loop back to its initial state. If the loop is running, this does not stop the loop. Ticks will continue to be scheduled at the correct times, but starting from the first tick.

Clone this wiki locally