This class represents a single "beat" for a rhythmic instrument. An instance of this class can be set to active or not to facilitate the way that most drum machines work (when a beat is not active, the time that it occupies still exists, but it does not cause audio to play, effectively resulting in a "rest"). It provides properties that track when it is played, and when a "rest" is played in it's place.

This class does not have the ability to create audio on it's own and is expected be a "child" of one of the Sound classes. See it's implementation in BeatTrack for an example.

// Cannot play audio on it's own.
// Must pass in parentPlay and/or parentPlayIn from a parent class.
Beat.create({
  _parentPlayIn: this.playIn.bind(this),
  _parentPlay: this.play.bind(this),
});

Property Summary

Public Properties
public

If active is true, all methods of play will cause this instance to play. If active is false, the playIfActive() and ifActivePlayIn() methods will treat this instance as a rest (a timed period of silence).

public

Whether a Beat instance is currently playing, considering both active and inactive beats (rests). When switched to true, is automatically returned to false after the time specified by the duration property.

public

If specified, Determines length of time, in milliseconds, before isPlaying and currentTimeIsPlaying are automatically switched back to false after having been switched to true. 100ms is used by default.

public

Whether a Beat instance is currently playing, considering only active beats. When switched to true, is automatically returned to false after the time specified by the duration property.

Private Properties
private

On Beat instance instantiation, this property should be set to the parent's audioBuffer.duration.

Method Summary

Public Methods
public
ifActivePlayIn(offset)

If the beat is marked active, calls it's parent's playIn() method directly to play the beat in ${offset} seconds.

public
play( )

Calls it's parent's play() method directly to play the beat immediately.

public

If active, calls it's parent's play() method directly to play the beat immediately.

public
playIn(offset)

Calls it's parent's playIn() method directly to play the beat in ${offset} seconds.

Private Methods
private

Sets currentTimeIsPlaying to true and sets up a timer that sets currentTimeIsPlaying back to false after duration has elapsed.

private

Sets isPlaying to true and sets up a timer that sets isPlaying back to false after duration has elapsed.

Public Properties

If active is true, all methods of play will cause this instance to play. If active is false, the playIfActive() and ifActivePlayIn() methods will treat this instance as a rest (a timed period of silence).

addon/classes/beat.js:52

public currentTimeIsPlaying: Boolean

Whether a Beat instance is currently playing, considering both active and inactive beats (rests). When switched to true, is automatically returned to false after the time specified by the duration property.

addon/classes/beat.js:86

public duration: Number

If specified, Determines length of time, in milliseconds, before isPlaying and currentTimeIsPlaying are automatically switched back to false after having been switched to true. 100ms is used by default.

addon/classes/beat.js:64

public isPlaying: Boolean

Whether a Beat instance is currently playing, considering only active beats. When switched to true, is automatically returned to false after the time specified by the duration property.

Private Properties

addon/classes/beat.js:76

private _audioBufferDuration: Number | Null

On Beat instance instantiation, this property should be set to the parent's audioBuffer.duration.

Public Methods

addon/classes/beat.js:120

public ifActivePlayIn(offset)

If the beat is marked active, calls it's parent's playIn() method directly to play the beat in ${offset} seconds.

If active, isPlaying is marked true after the provided offset has elapsed.

currentTimeIsPlaying is marked true after the provided offset has elapsed, even if beat is not active.

Parameters:

Name Type Attribute Description
offset Number

Number of seconds from "now" that the audio should play.

addon/classes/beat.js:146

public play( )

Calls it's parent's play() method directly to play the beat immediately.

isPlaying and currentTimeIsPlaying are both immediately marked true.

addon/classes/beat.js:160

public playIfActive( )

If active, calls it's parent's play() method directly to play the beat immediately.

If active, isPlaying is immediately marked true.

currentTimeIsPlaying is immediately marked true, even if beat is not active.

addon/classes/beat.js:98

public playIn(offset)

Calls it's parent's playIn() method directly to play the beat in ${offset} seconds.

isPlaying and currentTimeIsPlaying are both marked true after the provided offset has elapsed.

Parameters:

Name Type Attribute Description
offset Number

Number of seconds from "now" that the audio should play.

Private Methods

addon/classes/beat.js:192

private _markCurrentTimePlaying( )

Sets currentTimeIsPlaying to true and sets up a timer that sets currentTimeIsPlaying back to false after duration has elapsed.

addon/classes/beat.js:180

private _markPlaying( )

Sets isPlaying to true and sets up a timer that sets isPlaying back to false after duration has elapsed.