Player

Plays a song on a chip, in time. It hands each note to the chip a little before the note sounds, and can pause, move to another moment of the song and leave the notes of chosen keys out.

Import

import { Player } from '@halleyfante/chiptune';

Playing a song

Constructor

new Player({ chip: Chip, lookahead?: number })
OptionDescription
lookaheadSeconds before it sounds that a note is handed to the chip. The longer, the less the timing depends on the page staying responsive, and the longer a change takes to be heard. Defaults to 0.3.

Properties

NameTypeDescription
playingboolean
pausedboolean
positionnumberSeconds of the song already played, which keep growing while it repeats and stand still while paused. Zero while stopped. Setting it moves the song there: what is sounding ends at once and the notes from that moment on come next. In a song that loops, a moment past the end of the loop falls inside one of its repetitions. Setting it while stopped does nothing.
volumenumberThe master volume of the chip, from 0 to 1. Changing it is heard at once.

Methods

MethodDescription
play(song: Song): voidPlays each track on the channel of the chip in the same position, from the first note. The notes of a track have to be written in the order they start. Does nothing while a song is playing or paused. A track with a loop goes back to its loop start each time it reaches its loop end, for as long as the song plays, so the notes before the loop start sound only once. Each track repeats on its own loop, and all of them keep counting from the same first tick.
silence(keys: readonly number[]): voidLeaves the notes of these keys out of what plays from now on, and lets every other key sound again. A note that was already handed to the chip, which happens up to `lookahead` seconds before it sounds, still plays.
keys: Counted as in MIDI, where 60 is middle C.
pause(): voidEnds the sound at once and keeps the place in the song. Does nothing unless playing.
resume(): voidGoes on from where the song paused. A note that was sounding then is sounded again for the time it had left. Does nothing unless paused.
stop(): voidEnds the sound at once and goes back to the first note. Does nothing while stopped.