# `tecs.audio.sequencer`
## Overview
::: api-overview
**functions**
- [`checkOptions`](#tecs.audio.sequencer.checkOptions)
- [`newPlayer`](#tecs.audio.sequencer.newPlayer)
**types**
- [`Driver`](#tecs.audio.sequencer.Driver)
- [`Player`](#tecs.audio.sequencer.Player)
- [`PlayOptions`](#tecs.audio.sequencer.PlayOptions)
- [`State`](#tecs.audio.sequencer.State)
:::
::: api-group
## Functions
::: api-item tecs.audio.sequencer.checkOptions
### `checkOptions` _function_
```nupp
function(name: string, options: PlayOptions?)
```
Raises unless play options are in range.
#### Arguments
`name` - `string`
:
`options` - [`PlayOptions`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.PlayOptions)`?`
:
#### Raises
| Type | Condition |
| --- | --- |
| `string` | when `tempo`, `lookahead` or `start` is out of range |
:::
::: api-item tecs.audio.sequencer.newPlayer
### `newPlayer` _function_
```nupp
function(
mixer: audio.Audio,
driver: Driver,
handle: assets.Handle?,
options: PlayOptions?
): Player
```
Starts a player on a driver and registers it with the mixer's update.
What the format modules' `play` functions end in, and what a game with a
format of its own calls with a driver it wrote.
#### Arguments
`mixer` - [`audio.Audio`](../../../../modules/tecs/audio/index.html#tecs.audio.Audio)
: the mixer whose `update` advances the player
`driver` - [`Driver`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Driver)
: the song and its voices, which the player now owns
`handle` - [`assets.Handle`](../../../../modules/tecs/assets/index.html#tecs.assets.Handle)`?`
: the song's handle, which the player retains until it stops and watches for a reload, or nil for a song held some other way
`options` - [`PlayOptions`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.PlayOptions)`?`
: the loop, tempo, lookahead and start, or nil for the defaults. `group` and `gain` are the driver's to apply.
#### Returns
[`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
: the playing player
#### Raises
| Type | Condition |
| --- | --- |
| `string` | when an option is out of range |
:::
:::
::: api-group
## Types
::: api-item tecs.audio.sequencer.Driver
### `Driver` _record_
```nupp
record Driver
```
What a format module supplies to a [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player):
the voices a song plays on and the song's events, behind the operations a
player needs.
Song time is seconds from the start of the song at a tempo of one. Clock
time is seconds on the voices' own clock, which `now` reads. A player maps
one to the other with an anchor: song time `song` is clock time `clock`,
and `tempo` song seconds pass for each clock second.
#### Members
::: api-overview
**constructors**
- [`constructor`](#tecs.audio.sequencer.Driver.constructor)
**static methods**
- [`jump`](#tecs.audio.sequencer.Driver.jump)
- [`now`](#tecs.audio.sequencer.Driver.now)
- [`pause`](#tecs.audio.sequencer.Driver.pause)
- [`pending`](#tecs.audio.sequencer.Driver.pending)
- [`reload`](#tecs.audio.sequencer.Driver.reload)
- [`resume`](#tecs.audio.sequencer.Driver.resume)
- [`schedule`](#tecs.audio.sequencer.Driver.schedule)
- [`seek`](#tecs.audio.sequencer.Driver.seek)
- [`setGain`](#tecs.audio.sequencer.Driver.setGain)
- [`setMuted`](#tecs.audio.sequencer.Driver.setMuted)
- [`stop`](#tecs.audio.sequencer.Driver.stop)
**fields**
- [`duration`](#tecs.audio.sequencer.Driver.duration)
- [`loopEnd`](#tecs.audio.sequencer.Driver.loopEnd)
- [`loopStart`](#tecs.audio.sequencer.Driver.loopStart)
- [`tail`](#tecs.audio.sequencer.Driver.tail)
:::
::: api-item tecs.audio.sequencer.Driver.constructor
#### `constructor` _constructor_
```nupp
function(
duration: number,
loopStart: number?,
loopEnd: number?,
tail: number,
now: function(): number,
schedule: function(to: number, song: number, clock: number, tempo: number): number,
pending: function(): number?,
jump: function(target: number, at: number): nil,
seek: function(target: number, at: number): nil,
reload: function(payload: any): (boolean, string?),
pause: function(): nil,
resume: function(): nil,
setGain: function(gain: number): nil,
setMuted: function(index: integer, muted: boolean): nil,
stop: function(fadeOut: number?): nil
): tecs.audio.sequencer.Driver
```
Constructs an instance from named fields. Optional fields may be omitted.
##### Arguments
`duration` - `number`
: Required. Read-only. Reports the song's length in seconds.
`loopStart` - `number?`
: Optional. Defaults to `nil`. Read-only. Reports where the song loops back to, in seconds, or nil for
a song without a loop.
`loopEnd` - `number?`
: Optional. Defaults to `nil`. Read-only. Reports where a loop ends and the song goes back to
`loopStart`, in seconds, or nil for the end of the song.
`tail` - `number`
: Required. Read-only. Reports the seconds a voice keeps sounding after the last
event, for releases and reverb, before a finished song frees it.
`now` - `function(): number`
: Required. Read-only. Reports the mixer's clock in seconds, which every `at` the
driver sends is on.
`schedule` - `function(to: number, song: number, clock: number, tempo: number): number`
: Required. Read-only. Sends every event due at or after the driver's cursor and
before song time `to`, each at clock time
`clock + (time - song) / tempo`, and returns the song time it reached,
`to`. An event a voice refuses is skipped rather than retried.
`pending` - `function(): number?`
: Required. Read-only. Reports the song time of the first event the cursor has not
sent, or nil when every event has gone.
`jump` - `function(target: number, at: number): nil`
: Required. Read-only. Moves the cursor back to song time `target` for a loop,
after ending what still sounds at clock time `at`.
`seek` - `function(target: number, at: number): nil`
: Required. Read-only. Moves the cursor to song time `target` from anywhere,
putting the voices in the state the song had there, at clock time `at`.
`reload` - `function(payload: any): (boolean, string?)`
: Required. Read-only. Replaces the song with the payload of a reloaded handle,
and returns false and a reason when it cannot.
`pause` - `function(): nil`
: Required. Read-only. Pauses every voice, which stops its clock.
`resume` - `function(): nil`
: Required. Read-only. Resumes every voice.
`setGain` - `function(gain: number): nil`
: Required. Read-only. Sets the gain of every voice.
`setMuted` - `function(index: integer, muted: boolean): nil`
: Required. Read-only. Mutes or unmutes one part of the song: a MIDI channel or a
chip, numbered from zero.
`stop` - `function(fadeOut: number?): nil`
: Required. Read-only. Stops and frees every voice, over `fadeOut` seconds when
that is given.
##### Returns
[`tecs.audio.sequencer.Driver`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Driver)
: the constructed instance
##### Examples
Using values of the argument types above:
```nupp
local value = require("tecs.audio.sequencer").Driver(
duration = duration,
tail = tail,
now = now,
schedule = schedule,
pending = pending,
jump = jump,
seek = seek,
reload = reload,
pause = pause,
resume = resume,
setGain = setGain,
setMuted = setMuted,
stop = stop
)
```
:::
::: api-item tecs.audio.sequencer.Driver.duration
#### `duration` _field_
```nupp
duration: number
```
Read-only. Reports the song's length in seconds.
:::
::: api-item tecs.audio.sequencer.Driver.loopStart
#### `loopStart` _field_
```nupp
loopStart: number?
```
Read-only. Reports where the song loops back to, in seconds, or nil for
a song without a loop.
:::
::: api-item tecs.audio.sequencer.Driver.loopEnd
#### `loopEnd` _field_
```nupp
loopEnd: number?
```
Read-only. Reports where a loop ends and the song goes back to
`loopStart`, in seconds, or nil for the end of the song.
:::
::: api-item tecs.audio.sequencer.Driver.tail
#### `tail` _field_
```nupp
tail: number
```
Read-only. Reports the seconds a voice keeps sounding after the last
event, for releases and reverb, before a finished song frees it.
:::
::: api-item tecs.audio.sequencer.Driver.now
#### `now` _method_
```nupp
function(): number
```
Read-only. Reports the mixer's clock in seconds, which every `at` the
driver sends is on.
##### Returns
`number`
:
:::
::: api-item tecs.audio.sequencer.Driver.schedule
#### `schedule` _method_
```nupp
function(to: number, song: number, clock: number, tempo: number): number
```
Read-only. Sends every event due at or after the driver's cursor and
before song time `to`, each at clock time
`clock + (time - song) / tempo`, and returns the song time it reached,
`to`. An event a voice refuses is skipped rather than retried.
##### Arguments
`to` - `number`
:
`song` - `number`
:
`clock` - `number`
:
`tempo` - `number`
:
##### Returns
`number`
:
:::
::: api-item tecs.audio.sequencer.Driver.pending
#### `pending` _method_
```nupp
function(): number?
```
Read-only. Reports the song time of the first event the cursor has not
sent, or nil when every event has gone.
##### Returns
`number?`
:
:::
::: api-item tecs.audio.sequencer.Driver.jump
#### `jump` _method_
```nupp
function(target: number, at: number)
```
Read-only. Moves the cursor back to song time `target` for a loop,
after ending what still sounds at clock time `at`.
##### Arguments
`target` - `number`
:
`at` - `number`
:
:::
::: api-item tecs.audio.sequencer.Driver.seek
#### `seek` _method_
```nupp
function(target: number, at: number)
```
Read-only. Moves the cursor to song time `target` from anywhere,
putting the voices in the state the song had there, at clock time `at`.
##### Arguments
`target` - `number`
:
`at` - `number`
:
:::
::: api-item tecs.audio.sequencer.Driver.reload
#### `reload` _method_
```nupp
function(payload: any): (boolean, string?)
```
Read-only. Replaces the song with the payload of a reloaded handle,
and returns false and a reason when it cannot.
##### Arguments
`payload` - `any`
:
##### Returns
`boolean`
:
`string?`
:
:::
::: api-item tecs.audio.sequencer.Driver.pause
#### `pause` _method_
```nupp
function()
```
Read-only. Pauses every voice, which stops its clock.
:::
::: api-item tecs.audio.sequencer.Driver.resume
#### `resume` _method_
```nupp
function()
```
Read-only. Resumes every voice.
:::
::: api-item tecs.audio.sequencer.Driver.setGain
#### `setGain` _method_
```nupp
function(gain: number)
```
Read-only. Sets the gain of every voice.
##### Arguments
`gain` - `number`
:
:::
::: api-item tecs.audio.sequencer.Driver.setMuted
#### `setMuted` _method_
```nupp
function(index: integer, muted: boolean)
```
Read-only. Mutes or unmutes one part of the song: a MIDI channel or a
chip, numbered from zero.
##### Arguments
`index` - `integer`
:
`muted` - `boolean`
:
:::
::: api-item tecs.audio.sequencer.Driver.stop
#### `stop` _method_
```nupp
function(fadeOut: number?)
```
Read-only. Stops and frees every voice, over `fadeOut` seconds when
that is given.
##### Arguments
`fadeOut` - `number?`
:
:::
:::
::: api-item tecs.audio.sequencer.Player
### `Player` _record_
```nupp
record Player
```
A song playing on voices of its own.
Made by `tecs.gm.midi.play`, `tecs.ymfm.music.play` or
`newPlayer`, advanced by the mixer's `update`, and finished with `stop`.
Nothing here raises for a player that has stopped: every call on one
does nothing.
#### Members
::: api-overview
**instance methods**
- [`duration`](#tecs.audio.sequencer.Player.duration)
- [`finished`](#tecs.audio.sequencer.Player.finished)
- [`looping`](#tecs.audio.sequencer.Player.looping)
- [`loops`](#tecs.audio.sequencer.Player.loops)
- [`pause`](#tecs.audio.sequencer.Player.pause)
- [`paused`](#tecs.audio.sequencer.Player.paused)
- [`playing`](#tecs.audio.sequencer.Player.playing)
- [`position`](#tecs.audio.sequencer.Player.position)
- [`reloadFailure`](#tecs.audio.sequencer.Player.reloadFailure)
- [`resume`](#tecs.audio.sequencer.Player.resume)
- [`seek`](#tecs.audio.sequencer.Player.seek)
- [`setGain`](#tecs.audio.sequencer.Player.setGain)
- [`setLoop`](#tecs.audio.sequencer.Player.setLoop)
- [`setMuted`](#tecs.audio.sequencer.Player.setMuted)
- [`setTempo`](#tecs.audio.sequencer.Player.setTempo)
- [`status`](#tecs.audio.sequencer.Player.status)
- [`stop`](#tecs.audio.sequencer.Player.stop)
- [`tempo`](#tecs.audio.sequencer.Player.tempo)
- [`update`](#tecs.audio.sequencer.Player.update)
:::
::: api-item tecs.audio.sequencer.Player.stop
#### `stop` _method_
```nupp
function(exclusive self: Player, fadeOut: number?)
```
##### Arguments
`exclusive self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
`fadeOut` - `number?`
:
:::
::: api-item tecs.audio.sequencer.Player.pause
#### `pause` _method_
```nupp
function(exclusive self: Player)
```
##### Arguments
`exclusive self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
:::
::: api-item tecs.audio.sequencer.Player.resume
#### `resume` _method_
```nupp
function(exclusive self: Player)
```
##### Arguments
`exclusive self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
:::
::: api-item tecs.audio.sequencer.Player.status
#### `status` _method_
```nupp
function(borrows self: Player): State
```
##### Arguments
`borrows self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
[`State`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.State)
:
:::
::: api-item tecs.audio.sequencer.Player.playing
#### `playing` _method_
```nupp
function(borrows self: Player): boolean
```
##### Arguments
`borrows self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
`boolean`
:
:::
::: api-item tecs.audio.sequencer.Player.paused
#### `paused` _method_
```nupp
function(borrows self: Player): boolean
```
##### Arguments
`borrows self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
`boolean`
:
:::
::: api-item tecs.audio.sequencer.Player.finished
#### `finished` _method_
```nupp
function(borrows self: Player): boolean
```
##### Arguments
`borrows self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
`boolean`
:
:::
::: api-item tecs.audio.sequencer.Player.position
#### `position` _method_
```nupp
function(borrows self: Player): number
```
##### Arguments
`borrows self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
`number`
:
:::
::: api-item tecs.audio.sequencer.Player.duration
#### `duration` _method_
```nupp
function(borrows self: Player): number
```
##### Arguments
`borrows self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
`number`
:
:::
::: api-item tecs.audio.sequencer.Player.seek
#### `seek` _method_
```nupp
function(exclusive self: Player, seconds: number)
```
##### Arguments
`exclusive self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
`seconds` - `number`
:
:::
::: api-item tecs.audio.sequencer.Player.setLoop
#### `setLoop` _method_
```nupp
function(exclusive self: Player, loop: boolean)
```
##### Arguments
`exclusive self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
`loop` - `boolean`
:
:::
::: api-item tecs.audio.sequencer.Player.looping
#### `looping` _method_
```nupp
function(borrows self: Player): boolean
```
##### Arguments
`borrows self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
`boolean`
:
:::
::: api-item tecs.audio.sequencer.Player.loops
#### `loops` _method_
```nupp
function(borrows self: Player): integer
```
##### Arguments
`borrows self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
`integer`
:
:::
::: api-item tecs.audio.sequencer.Player.setTempo
#### `setTempo` _method_
```nupp
function(exclusive self: Player, scale: number)
```
##### Arguments
`exclusive self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
`scale` - `number`
:
:::
::: api-item tecs.audio.sequencer.Player.tempo
#### `tempo` _method_
```nupp
function(borrows self: Player): number
```
##### Arguments
`borrows self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
`number`
:
:::
::: api-item tecs.audio.sequencer.Player.setGain
#### `setGain` _method_
```nupp
function(exclusive self: Player, gain: number)
```
##### Arguments
`exclusive self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
`gain` - `number`
:
:::
::: api-item tecs.audio.sequencer.Player.setMuted
#### `setMuted` _method_
```nupp
function(exclusive self: Player, index: integer, muted: boolean)
```
##### Arguments
`exclusive self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
`index` - `integer`
:
`muted` - `boolean`
:
:::
::: api-item tecs.audio.sequencer.Player.reloadFailure
#### `reloadFailure` _method_
```nupp
function(borrows self: Player): string?
```
##### Arguments
`borrows self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
`string?`
:
:::
::: api-item tecs.audio.sequencer.Player.update
#### `update` _method_
```nupp
function(exclusive self: Player): boolean
```
##### Arguments
`exclusive self` - [`Player`](../../../../modules/tecs/audio/sequencer/index.html#tecs.audio.sequencer.Player)
:
##### Returns
`boolean`
:
:::
:::
::: api-item tecs.audio.sequencer.PlayOptions
### `PlayOptions` _type_
```nupp
type PlayOptions = {
group: string?,
gain: number?,
loop: boolean?,
tempo: number?,
lookahead: number?,
start: number?,
track: tecs.audio.Track?,
timeline: tecs.audio.timeline.Timeline?
}
```
Configures a player. The format modules' `play` functions take it too.
#### Members
::: api-overview
**fields**
- [`gain`](#tecs.audio.sequencer.PlayOptions.gain)
- [`group`](#tecs.audio.sequencer.PlayOptions.group)
- [`lookahead`](#tecs.audio.sequencer.PlayOptions.lookahead)
- [`loop`](#tecs.audio.sequencer.PlayOptions.loop)
- [`start`](#tecs.audio.sequencer.PlayOptions.start)
- [`tempo`](#tecs.audio.sequencer.PlayOptions.tempo)
- [`timeline`](#tecs.audio.sequencer.PlayOptions.timeline)
- [`track`](#tecs.audio.sequencer.PlayOptions.track)
:::
::: api-item tecs.audio.sequencer.PlayOptions.group
#### `group` _field_
```nupp
group: string?
```
Caller-writable. Selects the group the player's voices join, so the
group's gain, mute and pause apply to them. Defaults to none.
:::
::: api-item tecs.audio.sequencer.PlayOptions.gain
#### `gain` _field_
```nupp
gain: number?
```
Caller-writable. Sets the linear gain of the player's voices, before
group and master gain, and defaults to one.
:::
::: api-item tecs.audio.sequencer.PlayOptions.loop
#### `loop` _field_
```nupp
loop: boolean?
```
Caller-writable. Repeats the song from its loop point. Defaults to true
when the song has one, and is ignored when it has none.
:::
::: api-item tecs.audio.sequencer.PlayOptions.tempo
#### `tempo` _field_
```nupp
tempo: number?
```
Caller-writable. Scales the speed, from 0.01 to 100, and defaults to
one.
:::
::: api-item tecs.audio.sequencer.PlayOptions.lookahead
#### `lookahead` _field_
```nupp
lookahead: number?
```
Caller-writable. Sets the seconds the player schedules ahead, from
0.005 to 2, and defaults to 0.05.
:::
::: api-item tecs.audio.sequencer.PlayOptions.start
#### `start` _field_
```nupp
start: number?
```
Caller-writable. Sets where in the song playback starts, in seconds,
and defaults to zero.
:::
::: api-item tecs.audio.sequencer.PlayOptions.track
#### `track` _field_
`track:` [`tecs.audio.Track`](../../../../modules/tecs/audio/index.html#tecs.audio.Track)`?`
Caller-writable. The Track the player's voices sound on, which a
driver passes on. Omitted, their group's.
:::
::: api-item tecs.audio.sequencer.PlayOptions.timeline
#### `timeline` _field_
`timeline:` [`tecs.audio.timeline.Timeline`](../../../../modules/tecs/audio/timeline/index.html#tecs.audio.timeline.Timeline)`?`
Caller-writable. A Timeline the song follows: it pauses while the
Timeline is paused, and its speed is its own `tempo` times the
Timeline's tempo over 60, so a song on a scene's Timeline stops with
the scene and slows with it.
:::
:::
::: api-item tecs.audio.sequencer.State
### `State` _type_
```nupp
type State = "playing" | "paused" | "finished" | "stopped"
```
Where a player stands.
:::
:::