Connect with an application
Your application already knows what is happening: a player is in danger, a scene changes, a value crosses a threshold. GAS lets the music respond. You load the score once, then change tempo, flavor, or the active tracks as that state changes.
This page extends the application from Run locally. A Danger nearby checkbox makes the piano faster and more tense, and clearing it returns the music to what the score says.
Add the danger control
Add a checkbox beside the buttons in index.html:
<label><input id="danger" type="checkbox" /> Danger nearby</label>Then add this controller to src/main.ts, before the open function:
import type { GasSession } from '@luna-estelar/gas';
import type { Command } from '@luna-estelar/gas/protocol';
function connectDanger(input: HTMLInputElement, report: (text: string) => void) {
let session: GasSession | undefined;
let queue = Promise.resolve();
function apply(): Promise<void> {
const target = session;
if (!target) return queue;
const commands: Command[] = input.checked
? [
{ kind: 'setTempo', bpm: 108 },
{ kind: 'setTrackFlavor', trackId: 'track.piano', value: 'tense, short phrases' }
]
: [{ kind: 'clearTempo' }, { kind: 'clearTrackFlavor', trackId: 'track.piano' }];
queue = queue
.then(() => target.apply(commands))
.then(
() => undefined,
() => report('Could not apply the danger change.')
);
return queue;
}
input.addEventListener('change', () => {
void apply();
});
return {
attach(next: GasSession): Promise<void> {
session = next;
return apply();
}
};
}
const danger = connectDanger(document.querySelector<HTMLInputElement>('#danger')!, (text) => {
document.querySelector<HTMLParagraphElement>('#status')!.textContent = text;
});When danger starts, the controller sets two app values in one apply call: a tempo and a flavor for the piano. apply sets both or neither. The JavaScript API lists every command, its setter, and its matching clear. Commands take track IDs, so the score’s piano track is track.piano. When danger ends, clearTempo and clearTrackFlavor hand the values back, and the music returns to whatever the score says at that point. Restoring an old value by hand instead would miss any change the score makes in the meantime. App values and the cascade explains how app values layer over the score.
The controller also keeps the changes in order. It applies one reaction at a time, so turning danger on and off quickly can’t apply the two reactions out of order. A failed change is reported in the status rather than stopping later changes.
Connect it to playback
The controller replaces the Make it busier button. Make these edits:
-
Delete the
#changebutton fromindex.html. Insrc/main.ts, delete itsconst changedeclaration, its click listener, and thechange.disabledline in thestatelistener. -
In
open, after the line that shows the load warnings, add:await danger.attach(session);
App values last until your application clears them. Stop, the end of a piece, and a failed connection all keep them, so the controller applies each change once, when the checkbox changes. It works while the music is stopped too, and the next Play starts with the values in place.
Try it: press Play and toggle Danger nearby. Then press Stop, change the checkbox, and press Play again. The music starts in the state the checkbox shows.
Use the same pattern in a game
Way Back Home loads one looping score and changes it as the player moves through the rooms. Finding a suit brings in a hopeful layer, and reaching the beach changes the texture.
There are two ways to send a change, and both reach the same session. A fixed musical change, like a room’s mood, can be a live command written in GAS:
piano.flavor "warm evening air, unhurried"tempo 84
A value your application calculates, like distance from danger, is better sent with a setter or apply. The choice is about where the musical decision lives: in GAS text you can read and edit, or in your application’s code.
The JavaScript API covers the rest of the session: loading, playback, events, and errors. If something doesn’t work, start with Troubleshooting.
Next: Architecture — how GAS is built, from score to audio.