Skip to content
jshanley edited this page Oct 31, 2014 · 8 revisions

Clips are wrappers for individual sound samples. They allow a sound to be played multiple times without having to manually create and wire up a buffer source each time.

# blip.clip()

Creates and returns a new clip instance.

# clip.sample([name])

If name is specified, assigns a sample to the clip. The name must match the name of a sample loaded using blip.sampleLoader. Only one sample can be assigned to a clip at once. Assigning a new sample to a clip is allowed, but in most cases it is easier to just create a new clip for each separate sample.

If name is not specified, returns the name of the sample currently assigned to the clip.

# clip.chain([chain])

If chain is specified, connects the clip to the specified chain. In the current version of blip, chain must be an instance of blip.chain. Future versions may allow an array of nodes to be passed instead, generating the blip chain from the array.

If chain is not specified, returns the current chain.

# clip.play([time[, params]])

Plays the clip. If time is specified, the clip will be played at the specified time, which represents the time (in seconds) since the blip context was created. If time is not specified, or if time is less than the current blip time, the clip is played immediately. See the Time page for more info about how time is handled in blip.

In addition to the playback time, an optional params object may also be passed. This is used to set parameters on the clip at the time it is played. Normally in blip, a node's AudioParams may be set directly, however the clip is unique because it must internally create an AudioBufferSourceNode each time the clip is played. This means that all AudioParams attached to the source node are unavailable until play-time. Currently, the valid parameters are "rate" and "gain", which affect the playbackRate parameter of the AudioBufferSourceNode which is created at play-time, and the gain parameter of the clip's output GainNode, respectively. Passing a numeric value as a parameter sets the value at the time the clip is played, using AudioParam.setValueAtTime under the hood. For example:

// create a clip
var myClip = blip.clip().sample('someSample');
// create a reference to a time 2 seconds from now
var t = blip.now() + 2;

// play the clip at the specified time, setting the playback rate to 0.5 at play-time
myClip.play(t, { rate: 0.5 });

For even greater control, you can pass a function as the value of a parameter. This will allow you to take advantage of the native methods of the underlying AudioParam. For example:

// ramps the clip's gain to 0.2 by time t+1
myClip.play(t, { 
  gain: function() {
    // `this` is the AudioParam (in this case the `gain` param of a GainNode)
    this.linearRampToValueAtTime(0.2, t+1)
  }
})

See the documentation for node.param for more info on manipulating AudioParams with blip.

# clip.rate([value]) added in v0.2.0

If value is specified, sets the default playback rate of the clip. This is the rate that will be used if no 'rate' parameter is passed to the clip's .play function.

If value is not specified, gets the default playback rate of the clip. Defaults to 1.0 if it has not previously been set.

# clip.gain([value]) added in v0.2.0

If value is specified, sets the default value of the gain parameter of the clip's output GainNode. This is the gain value that will be used if no 'gain' parameter is passed to the clip's .play function.

If value is not specified, gets the default gain value of the clip's output GainNode. This defaults to 1.0 if it has not previously been set.

Clone this wiki locally