Audio
Volver a la referencia

Audio

Sound, music in layers, zones and the listener.

52 símbolos

renderSfxfunctionnacatamalon

renderSfx(options: TSfxOptions, sampleRate: number) => Float32Array

Makes a sound effect from a preset, the way the 8-bit consoles made theirs: a coin, a jump, a hit, an explosion; and for a game with people in it, a crowd, a cheer with clapping, a referee's whistle, a wooden bat on a ball. A seed is one take on the preset, always the same one; another seed, another take. crowd and cheer are long, for under a scene: length makes them as long as needed.

Nothing of the browser is used, so the same options give the same samples anywhere.

Parámetros

optionsTSfxOptions

Which preset, and how it differs from it.

sampleRatenumber

Samples per second. Default 44100.

Ejemplo

const coin = renderSfx({ preset: 'coin', seed: 3 });
const lowJump = renderSfx({ preset: 'jump', pitch: 0.5 });
const stands = renderSfx({ preset: 'crowd', length: 8 });
Ver el código · audio/synth/render_sfx.ts:228

renderSongfunctionnacatamalon

renderSong(doc: TSongDoc, sampleRate: number, options: { transpose, samples }) => TRenderedSong

Turns a song into sound: one track per voice, of samples between -1 and 1, all the same length so they play together as layers and can be mixed into one.

It needs nothing of the browser, so the same song sounds the same in a game, in the editor that wrote it, and in a test that measures it. Given the same song it gives back the same samples, every time.

Whatever in the text could not be read is in errors, with where it was, and the rest is played: a wrong note costs that note, not the song. What could not be read in its instruments is in instrumentErrors.

An instrument is the song's own when it has one of that name, and otherwise the engine's (BUILTIN.MUSIC.INSTRUMENTS); nacatamalon: in front of a name always means the engine's.

Parámetros

docTSongDoc

The song.

sampleRatenumber

Samples per second. Default 44100.

options{ transpose, samples }

transpose: semitones up (or down, below zero) for every voice but the noise and the samples, which play drums and have no key. samples: the sound files its instruments' sample name, already read, by that name; the engine's own (nacatamalon:kick) need not be given.

Ejemplo

const song = renderSong({
    format: DOCUMENTS.SONG.FORMAT,
    tempo: 120,
    meter: [3, 4],
    voices: { pulse1: { notes: "d'4 g8 a b c' | d'4 g g |" } },
});
song.errors; // [] when every bar adds up
Ver el código · audio/synth/render_song.ts:852

soundOffunctionnacatamalon

soundOf(box: TBox, name: string) => THandle | null

The handle of a sound a box carries, found by its name: the component's id, or its file (crowd, /sfx/crowd.wav, or the key it was loaded under). For music, any of its layers' files; for a song, the .song. null when the box carries none by that name.

What lets a behaviour play a sound laid out in the scene, where a person can choose its file, its volume and whether it loops, instead of naming the file in code:

registerScript('title-screen', (self) => {
    useUpdate(() => {
        if (pressed) soundOf<TSongHandle>(self, 'title_theme')?.stop();
    });
});

The scene builds a box's components in order, so a sound written after the behaviour is not there yet while the behaviour starts: ask for it when it is played.

Parámetros

boxTBox

The box that carries the sound.

namestring

The component's id, or the file it plays.

Ver el código · audio/attachment_players.ts:74

useAudiofunctionhooknacatamalon

useAudio() => TAudioHandle

The game's volumes: the general one and one per channel, plus a way to silence everything.

A channel is a group of sounds that share a volume. 'sfx' and 'music' are there from the start, and any other name works the moment a sound uses it. This is what the sliders of an options screen move, and the numbers a game saves in its store so they are still there next time.

Volumes go from 0 (silent) to 1 (as recorded); above 1 is allowed and can distort.

Ejemplo

declare const settings: TGameStore<{ music: number }>;
declare const quieter: TText;

export const Options: TSceneFn = () => {
    const audio = useAudio();
    audio.setVolume(settings.state.music, 'music');

    listen(quieter, { onClick: () => audio.setVolume(audio.getVolume('music') - 0.1, 'music') });
    return createScene();
};
Ver el código · hooks/audio/use_audio.ts:35

useAudioListenerfunctionhooknacatamalon

useAudioListener(options: { enabled }) => TAudioListener

Puts the game's ears on this object: every sound placed in the world is heard from where it is, and in a 3D scene, facing the way it faces.

Without it the game is heard from the camera, which is right until the camera is not where the player is. A game seen from behind its character wants the ears on the character: a bird to the character's left is then heard on the left, wherever the camera has swung round to.

One per object. With several switched on the first one is used, and that is said once; switch one off with enabled to hand over to the next, and in the end back to the camera.

Parámetros

options{ enabled }

Whether it starts on. Default on.

Ejemplo

const Player = () => {
    useTransform({ x: 0, y: 0.5, z: 0 });
    createMesh({ geometry: useCubeGeometry() });
    useAudioListener();
};
Ver el código · hooks/audio/use_audio_listener.ts:34

useMusicfunctionhooknacatamalon

useMusic(options: TMusicOptions) => TMusicHandle

Plays one piece of music recorded in several files (its layers, or stems) that sound together, and hands back the controls for it.

What it is for: music that changes with the game without starting over: the drums come in for a fight, the tune goes muffled under water. Not for: a single recording (that is useSound), or a tune written as notes in a .song (that is useSong, which can also drop a voice, change key and speed up).

The layers are the same tune played by different instruments, one file each and all the same length. They always play together and in time; what the game changes is how loud each one is. That is how music follows the place without ever starting over: walk into the water and the tune goes muffled, start a fight and the drums come in, and the melody never loses its place.

It belongs to the channel 'music' unless it says otherwise, so an options screen moves it with the rest. Like every sound, it stops with the scene or the object that asked for it.

With loopStart it has an intro that plays once, and with bpm the game can follow its beat.

Music laid out in a scene document (a music component) is made by this same hook; a behaviour gets its controls with soundOf(box, name). In a scene shown rather than played (an editor's viewport), autoplay waits for the game to play.

Parámetros

optionsTMusicOptions

The layers by name, each with the volume it starts at, and the piece's volume, channel and whether it starts on its own.

Ejemplo

declare const player: TSprite;

const Level = () => {
    const music = useMusic({
        layers: {
            land: { src: '/audio/theme_land.ogg' },
            water: { src: '/audio/theme_water.ogg', volume: 0 },
        },
        autoplay: true,
    });

    useUpdate(() => {
        const under = player.transform.y < 0;
        music.setLayer('land', under ? 0 : 1, 0.5);
        music.setLayer('water', under ? 1 : 0, 0.5);
    });
    return createScene();
};
Ver el código · hooks/audio/use_music.ts:65

useSongfunctionhooknacatamalon

useSong(song: TSongDoc | TSongFile | TSongAccompaniment, options: TSongOptions) => TSongHandle

Plays a song written as notes (a .song file, or one written in code) with the engine's 8-bit synthesizer, and hands back the controls.

What it is for: chiptune music the game can change while it plays: faster for the last inning, in another key, with a voice dropped. Not for: a recorded file (that is useSound, or useMusic for one recorded in layers).

It does not load the file: useLoadSong({ src }) does, and its result is given here, useSong(useLoadSong({ src: '/music/title.song' })).

The song is turned into sound here, in the game, as it plays: a moment ahead of what is heard, one voice of an 8-bit console at a time, the way the console played it. So a whole tune weighs a few kilobytes instead of megabytes, starts at once and holds almost nothing while it plays, and what a recording cannot do, this can: it can speed up for the last inning, change key to build tension and come back, and drop a voice without a file per voice. Each change is heard at once, carrying on from the beat it was on.

The file says where the loop goes back to, so a song can have an intro that plays once. It is in the channel 'music' unless it says otherwise, and it stops with the scene or the object that asked for it.

A song can be sung over: accompany(vocal, song) from nacatamalon/speech hands it here with a vocal part, which is made with it as it plays, in a voice of its own (vocal), at its tempo and in its key.

A song laid out in a scene document (a song component, chosen in an editor) is made by this same hook; a behaviour gets its controls with soundOf(box, name). In a scene shown rather than played (an editor's viewport), autoplay waits for the game to play.

Parámetros

songTSongDoc | TSongFile | TSongAccompaniment

A song from useLoadSong, a TSongDoc written in code, or either with a vocal part over it, from accompany.

optionsTSongOptions

Its volume, channel, each voice's volume, tempo and key, and whether it starts on its own.

Ejemplo

declare const crowd: TSprite;
declare const lastInning: TGameSignal;

const Ballpark = () => {
    const song = useSong(useLoadSong({ src: '/music/minuet.song' }), { autoplay: true });

    song.onBeat((beat) => {
        crowd.transform.y = beat === 1 ? 118 : 120;
    });
    useSignal(lastInning, () => song.setTempo(song.tempo * 1.2));
    return createScene();
};
Ver el código · hooks/audio/use_song.ts:78

useSoundfunctionhooknacatamalon

useSound(source: TSoundSource, options: TSoundOptions) => TSoundHandle

Plays one audio file (.wav, .mp3, .ogg), or an effect made from a preset, and hands back the controls for it.

What it is for: sound effects (a jump, a coin, a bat hitting the ball), an ambience that loops (rain, a crowd, an engine), and music that is a single recording. Not for: music in layers that come in and out (that is useMusic), or a tune written as notes in a .song (that is useSong).

It loads the file itself when given { src }, so one line is enough: useSound({ src: '/sfx/jump.wav' }).

Called in the body of a scene, like every hook; the controls are used whenever, from an update, a click, a signal. When the scene stops or the object that asked for it is destroyed, its sound stops: a level's music does not survive the level.

A one-off (the default) can sound on top of itself, which is what effects need: two coins picked up at once are heard twice. A looping sound is a single thing that pause, resume and stop act on: music, rain, an engine.

With spatial it is heard from where the object that asked for it is, and follows it; with zone it fills an area around the object instead, the way birds fill a meadow. Starting and stopping can fade, and so can the volume.

Pausing the game or the scene does not stop a sound, because a menu over a frozen level often wants the music to carry on. To silence something on pause, listen with useScene().onPaused or useGame().onPaused and decide for each sound.

A sound laid out in a scene document (a sound component, chosen in an editor) is made by this same hook; a behaviour gets its controls with soundOf(box, name). In a scene shown rather than played (an editor's viewport), autoplay waits for the game to play.

Parámetros

sourceTSoundSource

A clip from useLoadAudio, { src } to load it here, { sfx } to make an effect from a preset, or { samples, sampleRate } for a sound worked out in code.

optionsTSoundOptions

Volume, looping, speed, channel, and placing it in the world: at a point, facing a way, or filling an area.

Ejemplo

export const Level: TSceneFn = () => {
    const music = useSound({ src: '/audio/theme.mp3' }, { channel: 'music', loop: true, autoplay: true });
    const jump = useSound({ src: '/audio/jump.mp3' }, { volume: 0.6 });
    const keys = useKeyboard();

    useUpdate(() => {
        if (keys.justPressed('Space')) jump.play();
    });

    useScene().onPaused(() => music.setVolume(0.2));
    return createScene();
};
Ver el código · hooks/audio/use_sound.ts:121

TAudioHandletypenacatamalon

The volumes of a game, and the way to silence it. Returned by useAudio.

A channel is a group of sounds that share a volume: 'sfx' and 'music' are there from the start, and any other name works the moment a sound uses it. This is what an options screen with a slider per kind of sound is made of, and what a game saves in its store.

Propiedades

setVolume
getVolume
stopAll
setOffset
getOffset
Ver el código · audio/types/t_audio.ts:190

TAudioListenertypenacatamalon

Where the game is heard from, put on an object: its ears.

Without one the ears are in the camera, which is right for most games. A game seen from behind its character wants them on the character instead, or a bird on the left of the character is heard on whichever side of the camera it happens to be.

Propiedades

type'audio-listener'
idstring
enabledboolean

Off, the ears go back to the next in line, and in the end to the camera.

Ver el código · audio/types/t_audio_listener.ts:12

TAudioMeasuretypenacatamalon

What a piece of sound measures: what tells you, without hearing it, whether it is too loud, too quiet, or broken.

Propiedades

durationnumber

In seconds.

peaknumber

The loudest sample, 0 to 1, and the same in decibels below full scale.

peakDbnumber
rmsnumber

How loud it is on average, 0 to 1, and in decibels.

rmsDbnumber
clippednumber

How many samples reached full scale, where a speaker can go no further and the sound distorts.

Ver el código · audio/synth/types/t_song.ts:452

TBeatListenertypenacatamalon

TBeatListener: (beat: number, bar: number) => void

What a beat is heard as: which beat of the bar (from 1) and which bar of the tune (from 1).

Ver el código · audio/beat_clock.ts:10

TFmOperatortypenacatamalon

One operator of an FM voice: a sine wave with its own envelope, which either is heard or bends the operators after it, as its voice's algorithm says.

Propiedades

multipleopcionalnumber

Its frequency as a multiple of the note's: 1 the note, 2 an octave up, 0.5 one down. Default 1.

detuneopcionalnumber

A hair off its multiple, in cents, for a richer, beating sound. Default 0.

levelopcionalnumber

How strong it is, 0 to 1: how loud when it is heard, how much it bends when it bends another. Default 1.

attackopcionalnumber

Seconds to come in, seconds to fall to sustain (0 to 1), and seconds to die away once the note ends. Default 0, 0, 1, 0.05.

decayopcionalnumber
sustainopcionalnumber
releaseopcionalnumber
Ver el código · audio/synth/types/t_song.ts:38

TFmPatchtypenacatamalon

An FM voice of the Genesis: four operators joined one of eight ways.

Propiedades

algorithmnumber

How the four operators are joined, 0 to 7, as the console numbers them: 0 each bends the next and only the fourth is heard (the most metallic), 7 all four heard (an organ). 4 is two pairs, each the first bending the second.

feedbackopcionalnumber

How much the first operator bends itself, 0 to 7: grit, up to a buzz. Default 0.

Ver el código · audio/synth/types/t_song.ts:70

TMusicAttachmenttypenacatamalon

A piece of music in layers attached to one object: which files, and how loud each is meant to be. What it keeps is the intention, never how far into the tune it had got.

Propiedades

type'music'
idstring
layersTMusicLayer[]
volumenumber
channelstring
autoplayboolean
fadeInnumber

Seconds, 0 for none: see the options.

fadeOutnumber
loopStartnumber

0 for both is the whole tune.

loopEndnumber
bpmnumber | null

null when nobody said, and then it has no beats to count.

beatsPerBarnumber
firstBeatnumber
Ver el código · audio/types/t_music.ts:112

TMusicHandletypenacatamalon

Controls a piece of music made with useMusic.

Propiedades

play
stop
pause
resume
setVolume
setLayer
getLayer
onBeat
positionnumber | null

How far into the music the player is hearing now, in seconds, or null while it is stopped: by the audio clock, with the time the sound takes to reach the speaker and the player's offset (useAudio().setOffset) taken off. It goes back to loopStart when the music loops. The same as a song's: see TSongHandle.position.

beatnumber | null

How far into the music the player is hearing now, in beats from its first (firstBeat), or null while it is stopped or when it has no bpm: 3.5 is half way through the fourth beat, and before the first it is below 0. What a thing moving with the music is placed by: dancer.frame = beat - Math.floor(beat) < 0.2 ? 1 : 0. Not the beat an onBeat listener is given, which is the beat of the bar.

latencynumber

How long a sound started now takes to be heard, in seconds: the device's delay and the player's offset. What position takes off.

positionAt
playingboolean
Ver el código · audio/types/t_music.ts:144

TMusicLayerOptionstypenacatamalon

TMusicLayerOptions: { src, key } | { clip } & { volume }

One layer of a piece of music: a file, or a clip already loaded, and how loud it starts.

Propiedades

volumeopcionalnumber

0 silent, 1 as recorded. Default 1.

Ver el código · audio/types/t_music.ts:12

TMusicOptionstypenacatamalon

How a piece of music in layers plays.

Propiedades

idopcionalstring

The same name the saved scene keeps. Made up when nobody says.

layersRecord<string, TMusicLayerOptions>

The layers, by name: the same tune played by different instruments, each in its own file and all the same length. They always play together and in time, and what changes is how loud each one is.

volumeopcionalnumber

The volume of the whole piece. Default 1.

channelopcionalstring

Default 'music'.

autoplayopcionalboolean

Starts as soon as every layer is ready, with no play(). Default false. In a scene coming in with a transition it waits for the swap, when the screen is covered, rather than sounding under the cover over the music of the scene still on screen.

fadeInopcionalnumber

Seconds that autoplay takes to come up from silence. Default 0, at full volume at once. With the fadeOut of the music it replaces, two scenes cross their music: the one leaving goes down as this one comes up.

fadeOutopcionalnumber

Seconds it takes to go down to silence when what asked for it goes away: its scene stops, its object is destroyed. It goes on being heard for that long after. Default 0, cut at once.

loopStartopcionalnumber

Where the loop goes back to, in seconds into the tune. Everything before it is an intro that plays once. Default 0, the whole tune.

loopEndopcionalnumber

Where the loop turns back, in seconds into the tune. Default 0, the end of the files.

bpmopcionalnumber

The tune's tempo in beats per minute, which is what lets onBeat count it. A file does not say its own tempo, so without this there are no beats to count.

beatsPerBaropcionalnumber

How many beats a bar has. Default 4.

firstBeatopcionalnumber

Seconds into the file where the first beat falls, for a file with silence or a pickup before it. Default 0.

Ver el código · audio/types/t_music.ts:26

TRenderedSongtypenacatamalon

A song turned into sound: one track per voice, all the same length so they can play together.

Propiedades

sampleRatenumber
lengthnumber

In samples, every voice's track this long. Rounded up to whole bars, so a loop lands on one.

durationnumber

In seconds.

loopSamplenumber

Where the loop goes back to, in samples and in seconds: what loopStart wants.

loopStartnumber
voicesPartial<Record<TSongVoiceName, Float32Array>>
echoopcionalFloat32Array

The SNES's echo, a track of its own beside the voices, for a song that has it.

notesTSongNote[]

Every note, in the order the voices were read, for whoever wants to draw them.

errorsTSongError[]

Whatever could not be read. What could is still played.

instrumentErrorsTSongInstrumentError[]

Whatever in its instruments could not be read. The rest of each instrument is still used.

Ver el código · audio/synth/types/t_song.ts:372

TSampledSoundtypenacatamalon

A sound made in code: one channel of samples from -1 to 1, and how many of them make a second.

For what no preset makes and no file holds: an engine whose note is worked out from the revolutions, a tone built from the level's seed. The samples are copied when the sound is made, so changing the array afterwards changes nothing; make another sound for another tone. Any rate works, the audio engine converts it, and the era's own were low: 22050 or 11025 is plenty for a buzz.

Nothing is written to a saved scene for it, since there is no file to name, the same as { sfx }.

Propiedades

samplesFloat32Array
sampleRatenumber
keyopcionalstring

What it is called where sounds are listed (an editor, a log of what the game played). Default 'samples'.

Ejemplo

const Level = () => {
    // a second of a square wave at 110 Hz, the low hum of a motor
    const rate = 22050;
    const samples = new Float32Array(rate);
    for (let i = 0; i < samples.length; i++) {
        samples[i] = Math.sin((i / rate) * 110 * Math.PI * 2) > 0 ? 0.3 : -0.3;
    }
    const motor = useSound({ samples, sampleRate: rate }, { loop: true, autoplay: true });

    return createScene();
};
Ver el código · audio/types/t_sound.ts:45

TSfxOptionstypenacatamalon

What sound effect to make, and how it differs from the preset as it comes.

Propiedades

seedopcionalnumber

Each seed is a slightly different take on the same preset: the same seed, the same sound. Default 1.

pitchopcionalnumber

Higher or lower than the preset, as a multiple: 2 is an octave up. Default 1.

lengthopcionalnumber

How long it lasts, in seconds. Default, what the preset lasts.

volumeopcionalnumber

How loud it peaks, 0 to 1. Default 0.8.

Ver el código · audio/synth/types/t_song.ts:423

TSfxPresettypenacatamalon

TSfxPreset: 'coin' | 'jump' | 'hit' | 'explosion' | 'crowd' | 'cheer' | 'whistle' | 'bat'

The kinds of sound effect renderSfx makes: coin, jump, hit, explosion; crowd (people murmuring), cheer (an ovation with clapping), whistle (a referee's) and bat (wood on a ball).

Ver el código · audio/synth/types/t_song.ts:414

TSongAccompanimenttypenacatamalon

A song with something sung or played over it, made into sound with it as it plays: what accompany from nacatamalon/speech hands to useSong. The song is played as it always is, and what goes over it comes in a voice of its own, at the song's tempo and in its key, following them when they change.

Propiedades

type'song-accompaniment'
songTSongFile | TSongDoc

The song played under it.

Ver el código · audio/types/t_song_player.ts:18

TSongAttachmenttypenacatamalon

A song attached to one object: which file, and how it was asked to play.

Propiedades

type'song'
idstring
volumenumber
channelstring
autoplayboolean
fadeInnumber

Seconds, 0 for none: see the options.

fadeOutnumber
voicesPartial<Record<TSongVoiceName, number>>
panPartial<Record<TSongVoiceName, number>>

Where each voice is heard, left (-1) to right (1), only those changed from the file's.

temponumber | null

null for what the file says.

transposenumber
Ver el código · audio/types/t_song_player.ts:101

TSongBuiltinInfotypenacatamalon

One song the engine has: what it is, who wrote it, which console it is arranged for, and what a game might play it for.

Propiedades

keystring

What a game asks for it by, after nacatamalon:: useLoadSong({ src: 'nacatamalon:canon' }).

labelstring
composerstring
yearnumber

When it was written, near enough.

descriptionstring
usesstring[]
Ver el código · audio/synth/builtin/song_library.ts:11

TSongChiptypenacatamalon

TSongChip: 'nes' | 'snes' | 'genesis'

Which console a song sounds like, and so which voices it has:

  • 'nes', the default: pulse1, pulse2, triangle, noise and dpcm.
  • 'snes': eight voices, s1 to s8, each playing a sample (an instrument's sample), with the console's grain, its envelopes and its echo.
  • 'genesis': six FM voices, fm1 to fm6 (an instrument's fm), and the three squares and noise of its second chip, psg1 to psg3 and psgNoise.
Ver el código · audio/synth/types/t_song.ts:28

TSongDoctypenacatamalon

A song as the text of a .song file: the tempo, the bar, where it loops, and what each voice plays.

Small enough to write by hand and to read back, and played the same in the editor, in a browser and on the desktop runtime, because it is turned into sound by the same function everywhere.

Propiedades

formatnumber
temponumber

Beats per minute, a beat being the note the bar counts in (a quarter in 3/4).

meter[number, number]

How many beats a bar has, and which note is a beat: [3, 4] is a minuet's three quarters.

loopopcional{ bar }

The bar the song goes back to when it reaches the end, counted from 1. Everything before it is an intro that plays once. Absent, it loops from the start.

voicesPartial<Record<TSongVoiceName, TSongVoice>>
instrumentsopcionalRecord<string, TSongInstrument>

The instruments its voices play with, by name.

chipopcionalTSongChip

Which console it sounds like, and so which voices it has. Default 'nes'.

echoopcional{ delay, feedback, volume, filter }

The SNES's echo, which the instruments with echo go into: how far behind it comes (seconds, up to 0.24), how much of it comes round again (-1 to 1), how loud it is (0 to 1), and the eight steps it is filtered by, each -1 to 1. Default 0.12, 0.4, 0.25, and a filter that leaves it as it is.

Ver el código · audio/synth/types/t_song.ts:262

TSongHandletypenacatamalon

Controls a song made with useSong.

Propiedades

play
stop
pause
resume
setVolume
setVoice
getVoice
setPan
panRecord<TSongVoiceName, number>

Where each voice is heard, left (-1) to right (1). Written, a voice moves there at once, so a tween can carry it across: tween({ target: song.pan, to: { pulse2: 1 }, duration: 2 }).play().

setTempo
setTranspose
temponumber

The beats per minute it plays at now.

positionnumber | null

How far into the song the player is hearing now, in seconds, or null while it is stopped.

Read from the sound card's clock, not the game's, and with the time the sound takes to reach the speaker taken off: what a rhythm game times its notes against, so a press is judged against the beat that was heard. It goes back to the loop's start when the song loops.

beatnumber | null

How far into the song the player is hearing now, in beats from its start, or null while it is stopped: position counted in beats, with what is left of the beat after the point (3.5 is half way through the fourth). Right at any tempo, setTempo included. It goes back to the loop's start when the song loops.

Not the beat an onBeat listener is given, which is the beat of the bar (1 to 4 in 4/4) and starts again every bar: this one counts on, and a whole song's notes are written by it, so it is what a note falling towards the line a player hits is placed by: note.y = LINE - (note.start - song.beat) * SPEED, a note of notes.

latencynumber

How long a sound started now takes to be heard, in seconds: the delay the device reports on its way to the speaker, plus the player's offset (useAudio().setOffset). What position takes off. A sound meant to land on a beat is started this much before it: the line a character sings over the song. 0 under the frame clock.

positionAt
onBeat
errorstypeOperator

What in the notes could not be read. Empty until the song is ready, and when it is right.

notestypeOperator

The notes it plays, every voice's, in the order they start: each with its voice, its start and length (beats) in beats from the start of the song, its pitch as a MIDI number (a drum hit is 72), and its instrument and effects when it has them. Silences are not notes. Empty until the song is ready; there before it plays, to lay out a level from.

One pass of the song: when it goes round its loop, beat goes back to the loop's start and the same notes come again. Counted in beats, a note stays where it is when setTempo changes the tempo; its pitch moves with setTranspose, as what is heard does, but a drum's.

What a rhythm game is built from, without writing the song a second time:

for (const note of song.notes) {
    if (note.voice === 'noise') place(note.start);
}
playingboolean
Ver el código · audio/types/t_song_player.ts:132

TSongInstrumenttypenacatamalon

How a note sounds over its life, the way an 8-bit tracker describes it: lists of numbers read one a frame (a sixtieth of a second), from the moment the note starts.

Each list is numbers separated by spaces. Past its end it keeps its last number, unless a | marks where it starts again: '15 12 | 9 8' falls to 9 and then goes 8 9 8 9…, '| 0 4 7' repeats from the start for as long as the note lasts.

Propiedades

volumeopcionalstring

How loud, 0 to 15 a frame: '15 12 9 6 3 0' is a pluck, '15 14 13 12 | 11' a note that settles.

dutyopcionalstring

The colour of a square wave a frame: 0 12.5 %, 1 25 %, 2 50 %, 3 75 %. Square voices only.

arpeggioopcionalstring

Semitones added a frame, for the chords an 8-bit console plays one note at a time: '| 0 4 7' is a major chord, '| 0 3 7' a minor one.

pitchopcionalstring

Semitones added a frame, fractions allowed, for a note that bends as it starts: a kick that drops, a laser that falls.

vibratoopcional{ speed, depth, delay }

A wobble in pitch: how many times a second, how far in semitones, and how long the note is held before it starts, in seconds.

slideopcionalnumber

How long, in seconds, each note takes to glide from the note before it in its voice.

adsropcional{ attack, decay, sustain, release }

How a sample or an FM voice comes in and goes out: seconds to full, seconds to fall to sustain (0 to 1), and seconds to die away once the note ends. SNES voices only.

echoopcionalboolean

Whether its notes go into the song's echo. SNES voices only.

rootopcionalnumber

The note a sample of the game sounds as it was recorded, on a SNES voice: 60 is middle C, which is what it is when not said. The engine's samples know their own.

loopopcionalnumber

Where, in seconds, a sample of the game starts again once it reaches its end, on a SNES voice, so a note can be held. Absent, it plays once. The engine's samples know their own.

fmopcionalTFmPatch

The four operators of a Genesis FM voice.

sampleopcionalstring

The recording the dpcm voice plays: a sample the engine has ('nacatamalon:kick': see BUILTIN.MUSIC.SAMPLES) or a sound file of the game ('/samples/hit.wav'). It is played at the console's grain, one bit at a time, and the note's pitch chooses how fast: c' as it was recorded, lower notes slower and deeper.

Ver el código · audio/synth/types/t_song.ts:146

TSongInstrumentErrortypenacatamalon

Something wrong in an instrument, and where: which instrument, which of its fields, and for a list, which of its words, counted from 1.

Propiedades

instrumentstring
fieldTSongInstrumentList | 'vibrato' | 'slide' | 'sample' | 'adsr' | 'fm' | 'root' | 'loop'
positionopcionalnumber

The word of the list, from 1. Absent for a field that is not a list.

messagestring
Ver el código · audio/synth/types/t_song.ts:355

TSongInstrumentInfotypenacatamalon

One instrument the engine has: what it is called, what it is for, which voices it is meant for, and the instrument itself.

Propiedades

keystring

What the notes call it: @lead, or @nacatamalon:lead to say it is the engine's.

labelstring
descriptionstring
voicestypeOperator

The voices it sounds right on. Any voice can play it.

instrumentTSongInstrument
Ver el código · audio/synth/builtin/song_instruments.ts:11

TSongNotetypenacatamalon

One note a voice plays, worked out from its text: when, for how long, and which.

Propiedades

startnumber

Counted in beats from the start of the song.

beatsnumber
instrumentopcionalstring

The instrument it is played with, by name, when it has one.

effectsopcionalTSongNoteEffects

What it does on its own, written after it in brackets.

pitchnumber

As a MIDI number: 60 is middle C, 69 the A of 440 Hz. A drum hit is 72.

Ver el código · audio/synth/types/t_song.ts:302

TSongNoteEffectstypenacatamalon

What one note does on its own, written after it in brackets.

Propiedades

slideopcionalnumber

Seconds it takes to glide in from the note before it in its voice.

vibratoopcional{ speed, depth, delay }
arpeggioopcionalnumber[]

Semitones played one a frame, round and round.

volumeopcionalnumber

How loud, 0 to 15, on top of what its instrument says.

Ver el código · audio/synth/types/t_song.ts:215

TSongOptionstypenacatamalon

How a song written as notes plays.

Propiedades

idopcionalstring

The same name the saved scene keeps. Made up when nobody says.

volumeopcionalnumber

The volume of the whole song. Default 1.

channelopcionalstring

Default 'music'.

autoplayopcionalboolean

Starts as soon as the song is ready, with no play(). Default false. In a scene coming in with a transition it waits for the swap, when the screen is covered, rather than sounding under the cover over the music of the scene still on screen.

fadeInopcionalnumber

Seconds that autoplay takes to come up from silence. Default 0, at full volume at once. With the fadeOut of the music it replaces, two scenes cross their music: the one leaving goes down as this one comes up.

fadeOutopcionalnumber

Seconds it takes to go down to silence when what asked for it goes away: its scene stops, its object is destroyed. It goes on being heard for that long after. Default 0, cut at once.

voicesopcionalPartial<Record<TSongVoiceName, number>>

How loud each voice starts, 0 to 1. A voice not named starts at 1.

panopcionalPartial<Record<TSongVoiceName, number>>

Where between left (-1) and right (1) each voice is heard, only those not where the file puts them (the middle, unless it says).

tempoopcionalnumber

Beats per minute, instead of what the file says.

transposeopcionalnumber

Semitones up, or down below zero, for every voice but the drums. Default 0.

Ver el código · audio/types/t_song_player.ts:45

TSongSampletypenacatamalon

A recording, as its samples, for the dpcm voice.

Propiedades

samplesFloat32Array
sampleRatenumber
rootopcionalnumber

The note it sounds as it is, on a SNES voice. Default 60, middle C.

loopStartopcionalnumber

Where, in samples, it starts again once it reaches its end, so a note can be held. Absent, it plays once.

Ver el código · audio/synth/types/t_song.ts:238

TSongVoicetypenacatamalon

One voice of a song: what it plays, written as text, and how.

Propiedades

notesstring

The notes, separated by spaces, read left to right:

  • c d e f g a b, then # or b to raise or lower it, then ' (an octave up) or , (an octave down) as many times as needed. c alone is middle C, so a is the A of 440 Hz.
  • Then how long: 1 a whole note, 2 a half, 4 a quarter, 8, 16, 32. A . after it adds half as much again. With no number, it lasts what the note before it lasted.
  • ~ at the end holds it into the next note, which must be the same one.
  • r is a rest, and in the noise voice x is a drum hit.
  • | marks where a bar ends, and it is checked: a bar that does not add up is an error.
  • @name plays the notes after it with that instrument of the song's instruments, or one the engine has (@kick, @lead: see BUILTIN.MUSIC.INSTRUMENTS), and @- with none.
  • After a note, in brackets with no spaces, what only that note does: [slide] glides in from the note before ([slide:0.1] in that many seconds), [vib] wobbles ([vib:6,0.3]: times a second and semitones, [vib:6,0.3,0.2] after a delay), [arp:0,4,7] plays a chord one note a frame, [vol:8] is quieter (0 to 15). Several go together: c'2[vib][vol:10].

In the noise voice a note's pitch chooses how the noise sounds: higher, more of a hiss.

volumeopcionalnumber

0 silent, 1 as loud as this voice goes. Default 1. The four voices are mixed so that all of them at full volume never go past what a speaker takes.

dutyopcional0.125 | 0.25 | 0.5 | 0.75

How much of each cycle a square wave spends up, which is its colour: 0.125 thin and nasal, 0.5 round and hollow. Square voices only. Default 0.5.

panopcionalnumber

Where it is heard, from left (-1) to right (1). Default 0, the middle. The Genesis put each of its voices left, right or in the middle, and the SNES anywhere between.

instrumentopcionalstring

The instrument its notes start with, by its name in the song's instruments. @name in the notes changes it from there on.

Ver el código · audio/synth/types/t_song.ts:91

TSongVoiceNametypenacatamalon

TSongVoiceName: 'pulse1' | 'pulse2' | 'triangle' | 'noise' | 'dpcm' | 's1' | 's2' | 's3' | 's4' | 's5' | 's6' | 's7' | 's8' | 'fm1' | 'fm2' | 'fm3' | 'fm4' | 'fm5' | 'fm6' | 'psg1' | 'psg2' | 'psg3' | 'psgNoise'

The voices of an 8-bit console: two square waves for the tune and the harmony, a triangle for the bass, noise for the drums, and dpcm, which plays short recorded samples (a drum kit, a hit) at the console's own grain.

Ver el código · audio/synth/types/t_song.ts:10

TSoundAttachmenttypenacatamalon

One sound attached to one object: which clip, and how it was asked to play.

What it holds is the intention, never what is sounding. A scene saved while the music was halfway through comes back ready to start, not halfway through: where a voice had got to is a fact about this run, and a level is not.

It carries the clip itself rather than its name, the way a sprite carries its texture, so that whoever writes the scene down can name the file and ask for it to be fetched again in one step.

Propiedades

type'sound'
idstring
volumenumber
loopboolean
loopStartnumber

0 for both is the whole file.

loopEndnumber
ratenumber
pannumber

Where between left (-1) and right (1) it is heard.

channelstring
autoplayboolean
spatialboolean
refDistancenumber | null

null until somebody chooses one: then it is what the scene measures in.

maxDistancenumber | null
coneTSoundCone | null
zoneTSoundZone | null
duckTSoundDuck | null
Ver el código · audio/types/t_sound.ts:259

TSoundConetypenacatamalon

Where a sound is heard loudest: a speaker, a siren, a radio on a shelf. Straight ahead of its object it plays at full volume, and further round it drops towards outerVolume.

Ahead is the way the object faces: its -Z in a 3D scene, the same way a camera and a lamp face, and along its turn in a flat one (to the right when it is not turned).

Propiedades

innernumber

How wide the loud part is, in degrees, from edge to edge.

outernumber

Past this width, in degrees, it is at its quietest. Between the two it fades.

outerVolumenumber

How loud it is behind, from 0 (silent) to 1 (the same as ahead).

Ver el código · audio/types/t_sound.ts:66

TSoundDucktypenacatamalon

Another channel made quieter while a sound plays: the music going down under a cheer, a voice, an alarm, and coming back up on its own once it is over.

It moves a volume of its own behind the channel, never the channel's: the one an options screen set is still there when the music comes back.

Propiedades

channelstring

The channel that goes down, usually 'music'.

tonumber

How loud it is kept while this sounds, from 0 (silent) to 1 (untouched). When several sounds hold the same channel down, the quietest one wins.

fadeopcionalnumber

How long it takes to go down, in seconds. Default 0.15.

releaseopcionalnumber

How long it takes to come back up once nothing holds it down. Default, the same as fade.

Ver el código · audio/types/t_sound.ts:122

TSoundHandletypenacatamalon

Controls a sound made with useSound.

Propiedades

play
stop
pause
resume
setVolume
setLoop
setRate
setPan
pannumber

Where between left (-1) and right (1) it is heard: written, it moves there at once, so a tween can carry it across, tween({ target: sound, to: { pan: 1 }, duration: 2 }).play().

playingboolean

Whether something of this sound is sounding right now.

Ver el código · audio/types/t_sound.ts:295

TSoundOptionstypenacatamalon

How a sound plays.

Propiedades

idopcionalstring

The name this sound keeps in a saved scene, so that opening one and saving it again gives the same file back. Made up when nobody says, which is right for a sound written in code and wrong for one that came out of a document.

volumeopcionalnumber

0 silent, 1 as recorded. Default 1. This sound only: the channel and the general volume still apply.

loopopcionalboolean

Repeats until it is stopped. Default false.

It also decides what the sound is: a one-off can be played on top of itself (two coins at once are heard twice), while a looping one is a single sound that pause, resume and stop act on: music, rain, an engine.

loopStartopcionalnumber

Where the loop goes back to, in seconds into the file. Everything before it is an intro that plays once. Looping sounds only. Default 0, the whole file.

loopEndopcionalnumber

Where the loop turns back, in seconds into the file. Default 0, the end of the file.

rateopcionalnumber

How fast it plays, which also changes the pitch: 2 is twice as fast, an octave up. Default 1.

panopcionalnumber

Where between left (-1) and right (1) it is heard. Default 0, the middle. A sound placed by its object (spatial) is put there by where the object is, and this moves it further.

channelopcionalstring

Which volume group it belongs to: 'sfx' (the default) or 'music' are there from the start, and any other name works straight away. An options screen moves these with useAudio.

autoplayopcionalboolean

Starts as soon as the file is ready, with no play(). Default false.

spatialopcionalboolean

Places the sound in the world, where the object that asked for it is: it comes from a side and gets quieter with distance. It follows the object wherever it goes, including when what carries it is what moves.

Heard from the camera, or from whatever useAudioListener put the ears on. In a 3D scene it can also be told apart in front and behind, best with headphones. Default false, which is the same volume wherever the listener is.

refDistanceopcionalnumber

How far it can be heard at full volume. Placed sounds only. Measured the way the scene measures: 100 pixels in a flat scene and under an orthographic camera, 1 unit under a perspective one.

maxDistanceopcionalnumber

Past this distance it is not heard at all: between refDistance and this it fades in a straight line. Placed sounds only. 2000 pixels, or 20 units under a perspective camera.

coneopcionalTSoundCone

Louder ahead of the object than behind it. Placed sounds only.

zoneopcionalTSoundZone

Fills an area instead of coming from a point: full volume inside, fading outside, and never from one side, because an ambience is all around. Wins over spatial.

duckopcionalTSoundDuck

Makes another channel quieter while this sounds: the music under a cheer.

Ver el código · audio/types/t_sound.ts:149

TSoundSourcetypenacatamalon

TSoundSource: TAudioClip | { src, key } | { sfx } | TSampledSound

What to play: a clip from useLoadAudio, just where the file is and let useSound load it, an effect made on the spot from a preset ({ sfx: { preset: 'coin' } }, see renderSfx), or a sound the game worked out itself, sample by sample ({ samples, sampleRate }).

Ver el código · audio/types/t_sound.ts:14

TSoundZonetypenacatamalon

An area a sound fills, the way a meadow is full of birds or a cave of dripping: inside, it plays at full volume wherever you stand, and outside it fades as you walk away.

Propiedades

shapeTSoundZoneShape

Measured on the object, so it moves, turns and grows with it.

fadenumber

How far outside the shape it takes to fade to silence. 0 stops dead at the edge.

Ver el código · audio/types/t_sound.ts:100

nacatamalon/files

parseSongDocfunctionnacatamalon/files

parseSongDoc(raw: unknown, src: string) => TSongDoc

Reads the contents of a .song file, already parsed from JSON, into a song.

It refuses what it could not play (no tempo, a bar that is not two whole numbers, a voice the console does not have) with an error that names the file. The notes themselves are not checked here: renderSong reads them and says where each mistake is, without refusing the song.

Parámetros

rawunknown

The file's JSON.

srcstring

Where it came from, for the errors.

Ver el código · audio/synth/parse_song_doc.ts:48