Skip to content

EZ Web Audio / Sequence

Class: Sequence

Defined in: packages/core/src/sequence.ts:70

Sequence schedules arbitrary callbacks at musical time positions tied to a Transport.

Events are stored as beat positions (not seconds) so that live BPM changes automatically affect subsequent event scheduling without re-scheduling. Sequences follow the Transport lifecycle: they play when the Transport plays, pause when it pauses, and reset when it stops.

Example

typescript
import { createTransport, createSequence, createSound } from 'ez-web-audio'

const transport = await createTransport({ bpm: 120, timeSignature: [4, 4] })
const kick = await createSound('kick.mp3')

const seq = createSequence(transport, { length: '4m' })

// Schedule kick on beat 1 of each bar
seq.at('1:1:0', (time) => kick.playIn(time - ctx.currentTime))
seq.at('2:1:0', (time) => kick.playIn(time - ctx.currentTime))
seq.at('3:1:0', (time) => kick.playIn(time - ctx.currentTime))
seq.at('4:1:0', (time) => kick.playIn(time - ctx.currentTime))

transport.start() // Events fire at correct musical positions

// Change BPM — events automatically adjust timing
transport.bpm = 140

Extends

Constructors

Constructor

new Sequence(transport, options): Sequence

Defined in: packages/core/src/sequence.ts:99

Parameters

transport

Transport

options

SequenceOptions

Returns

Sequence

Overrides

TypedEventEmitter.constructor

Accessors

length

Get Signature

get length(): number

Defined in: packages/core/src/sequence.ts:135

Length of the sequence in beats.

Returns

number


loop

Get Signature

get loop(): boolean

Defined in: packages/core/src/sequence.ts:130

Whether this sequence loops.

Returns

boolean

Methods

_clearListeners()

protected _clearListeners(): void

Defined in: packages/core/src/events/typed-event-emitter.ts:192

Remove every listener registered through this emitter (via addEventListener(), on(), or once()), regardless of how many or what event type they're bound to.

Native EventTarget has no removeAllListeners(). This works around that by aborting a shared AbortSignal threaded through every addEventListener() call this class makes, then swapping in a fresh AbortController so the instance can keep accepting new listeners afterward (e.g. if it's reused before being garbage collected).

Subclasses call this from their dispose() alongside neutering dispatchEvent — the neuter stops future emits, this stops stale listener closures from being retained/invoked at all.

Returns

void

Inherited from

TypedEventEmitter._clearListeners


addEventListener()

Call Signature

addEventListener<K>(type, listener, options?): void

Defined in: packages/core/src/events/typed-event-emitter.ts:44

Add a typed event listener for known event types. Overloaded to provide type safety for known event types while remaining compatible with the native EventTarget API.

Type Parameters
K

K extends "event" | "loop"

Parameters
type

K

The event type (key of TMap)

listener

(event) => void

Typed event handler

options?

Standard addEventListener options

boolean | AddEventListenerOptions

Returns

void

Inherited from

TypedEventEmitter.addEventListener

Call Signature

addEventListener(type, listener, options?): void

Defined in: packages/core/src/events/typed-event-emitter.ts:49

Add a typed event listener for known event types. Overloaded to provide type safety for known event types while remaining compatible with the native EventTarget API.

Parameters
type

string

The event type (key of TMap)

listener

Typed event handler

EventListenerOrEventListenerObject | null

options?

Standard addEventListener options

boolean | AddEventListenerOptions

Returns

void

Inherited from

TypedEventEmitter.addEventListener


at()

at(time, callback): string

Defined in: packages/core/src/sequence.ts:153

Schedule a callback at a musical time position.

Parameters

time

MusicalTimeNotation

Musical time notation (e.g., '4n', '2:1:0') or beats

callback

SequenceCallback

Function to call when the event fires

Returns

string

Event ID for later removal via .remove()

Example

typescript
const id = seq.at('4n', (time, pos) => {
  console.log(`Event at bar ${pos.bar}, beat ${pos.beat}`)
})

clear()

clear(): void

Defined in: packages/core/src/sequence.ts:196

Remove all scheduled events.

Returns

void


dispose()

dispose(): void

Defined in: packages/core/src/sequence.ts:427

Dispose the Sequence, unregistering from Transport and releasing resources. After disposal, the Sequence should not be used.

Returns

void


emit()

protected emit<K>(type, detail): void

Defined in: packages/core/src/events/typed-event-emitter.ts:95

Emit a typed event with the given detail.

Creates a CustomEvent with the provided detail and dispatches it on this target. Subclasses call this internally to fire lifecycle events.

Type Parameters

K

K extends "event" | "loop"

Parameters

type

K

The event type to emit (key of TMap)

detail

SequenceEventMap[K]["detail"]

The event detail object (typed by TMap)

Returns

void

Inherited from

TypedEventEmitter.emit


off()

off<K>(type, listener): this

Defined in: packages/core/src/events/typed-event-emitter.ts:167

Unsubscribe from an event.

Note: Due to native EventTarget limitations, you must provide the same listener function reference that was used when subscribing.

Type Parameters

K

K extends "event" | "loop"

Parameters

type

K

The event type to unsubscribe from

listener

(event) => void

The event handler function to remove

Returns

this

this for chaining

Example

typescript
const handler = (e) => console.log(e.detail)
emitter.on('play', handler)
// later...
emitter.off('play', handler)

Inherited from

TypedEventEmitter.off


on()

on<K>(type, listener): this

Defined in: packages/core/src/events/typed-event-emitter.ts:116

Subscribe to one or more events. Supports chaining.

Type Parameters

K

K extends "event" | "loop"

Parameters

type

The event type(s) to subscribe to (key or array of keys of TMap)

K | K[]

listener

(event) => void

The event handler function

Returns

this

this for chaining

Example

typescript
emitter.on('play', handlePlay).on('stop', handleStop)
emitter.on(['play', 'stop'], handleBoth)

Inherited from

TypedEventEmitter.on


once()

once<K>(type, listener): this

Defined in: packages/core/src/events/typed-event-emitter.ts:141

Subscribe to an event once. Handler is removed after first invocation.

Type Parameters

K

K extends "event" | "loop"

Parameters

type

K

The event type to subscribe to

listener

(event) => void

The event handler function

Returns

this

this for chaining

Example

typescript
emitter.once('end', () => console.log('Finished'))

Inherited from

TypedEventEmitter.once


remove()

remove(id): boolean

Defined in: packages/core/src/sequence.ts:185

Remove a previously scheduled event by its ID.

Parameters

id

string

Event ID returned by .at()

Returns

boolean

true if the event was found and removed


removeEventListener()

Call Signature

removeEventListener<K>(type, listener, options?): void

Defined in: packages/core/src/events/typed-event-emitter.ts:70

Remove a typed event listener for known event types. Overloaded to provide type safety for known event types while remaining compatible with the native EventTarget API.

Type Parameters
K

K extends "event" | "loop"

Parameters
type

K

The event type (key of TMap)

listener

(event) => void

Typed event handler to remove

options?

Standard removeEventListener options

boolean | EventListenerOptions

Returns

void

Inherited from

TypedEventEmitter.removeEventListener

Call Signature

removeEventListener(type, listener, options?): void

Defined in: packages/core/src/events/typed-event-emitter.ts:75

Remove a typed event listener for known event types. Overloaded to provide type safety for known event types while remaining compatible with the native EventTarget API.

Parameters
type

string

The event type (key of TMap)

listener

Typed event handler to remove

EventListenerOrEventListenerObject | null

options?

Standard removeEventListener options

boolean | EventListenerOptions

Returns

void

Inherited from

TypedEventEmitter.removeEventListener