@vibechain/os 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +15 -6
- package/README.md +35 -10
- package/SECURITY.md +3 -1
- package/dist/app-client.js +20 -4
- package/dist/app-host.d.ts +12 -2
- package/dist/app-host.js +12 -2
- package/dist/app-protocol.d.ts +21 -0
- package/dist/app-protocol.js +9 -0
- package/dist/audio.d.ts +65 -0
- package/dist/audio.js +162 -0
- package/dist/browser.d.ts +1 -0
- package/dist/browser.js +1 -0
- package/dist/bundle.d.ts +2 -0
- package/dist/bundle.js +2 -0
- package/dist/cli.js +3 -1
- package/dist/commands.d.ts +6 -0
- package/dist/commands.js +17 -0
- package/dist/index.d.ts +2 -2
- package/dist/links.d.ts +6 -0
- package/dist/links.js +2 -0
- package/dist/os.d.ts +20 -2
- package/dist/os.js +30 -5
- package/dist/preview-parent.js +15 -2
- package/dist/preview-ui.js +1 -1
- package/dist/preview.js +1 -1
- package/dist/templates/plain/game.js +3 -1
- package/dist/testing.d.ts +9 -1
- package/dist/testing.js +27 -6
- package/dist/vibemarket-os.js +1 -1
- package/examples/paw-beat/README.md +28 -9
- package/examples/paw-beat/game.ts +280 -123
- package/examples/paw-beat/index.html +18 -4
- package/examples/paw-beat/main.ts +101 -44
- package/examples/paw-beat/music.ts +72 -337
- package/examples/paw-beat/stage.ts +23 -9
- package/examples/paw-beat/store/screenshot-1.webp +0 -0
- package/examples/paw-beat/store/screenshot-2.webp +0 -0
- package/examples/paw-beat/synth.ts +477 -0
- package/examples/paw-beat/tracks.ts +286 -0
- package/examples/paw-beat/vibemarket.json +2 -2
- package/examples/vibexp-app/plain/game.ts +6 -2
- package/package.json +7 -1
- package/skill.md +399 -0
|
@@ -1,371 +1,106 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { beatSeconds, songBeats } from "./game";
|
|
2
|
+
import { createSynth, type PlayableTrack, type Synth } from "./synth";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
|
-
* The song
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* hear and what you hit stay together.
|
|
8
|
-
*
|
|
9
|
-
* The arrangement follows the sections in game.ts: a count-in, a gentle intro that hints at
|
|
10
|
-
* the tune, a build with a rising arpeggio and snare roll, a drop that carries the tune, a
|
|
11
|
-
* breakdown on pads and bell, a fuller finale and a cadence that comes home to A minor.
|
|
5
|
+
* The live song. The music itself (the tracks and the instruments that play them) is in
|
|
6
|
+
* tracks.ts and synth.ts; this file connects it to the speakers through the SDK's audio helper
|
|
7
|
+
* and keeps it in step with the game: it schedules each beat a little ahead of the audio clock,
|
|
8
|
+
* and that same clock times the steps, so what you hear and what you hit stay together.
|
|
12
9
|
*/
|
|
13
10
|
export interface Music {
|
|
14
|
-
/** Start
|
|
15
|
-
start(seed: number): void;
|
|
11
|
+
/** Start `track` from the top, with this round's seed. Call from a user gesture. */
|
|
12
|
+
start(track: PlayableTrack, seed: number): void;
|
|
16
13
|
/** Seconds into the song by the audio clock, or null when there is no audio. */
|
|
17
14
|
time(): number | null;
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Schedule the next few beats from `songTime`, the game's position. Call every frame while the song plays. If the audio
|
|
17
|
+
* clock started late (the browser held the context until a later click or key), the music joins at the current beat
|
|
18
|
+
* instead of starting the song over behind the steps.
|
|
19
|
+
*/
|
|
20
|
+
pump(songTime: number): void;
|
|
24
21
|
stop(): void;
|
|
25
22
|
dispose(): void;
|
|
26
23
|
}
|
|
27
24
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const MINOR = [0, 2, 3, 5, 7, 8, 10];
|
|
32
|
-
const degreeHz = (degree: number) => hz(MINOR[((degree % 7) + 7) % 7]! + 12 * Math.floor(degree / 7));
|
|
33
|
-
|
|
34
|
-
interface Chord { readonly bass: number; readonly pad: readonly number[]; readonly arp: readonly number[] }
|
|
35
|
-
// A minor, F, C, G: one chord per bar. Pad voicings stay close together around middle C.
|
|
36
|
-
const AM: Chord = { bass: hz(-36), pad: [hz(-12), hz(-9), hz(-5)], arp: [hz(-12), hz(-9), hz(-5), hz(0)] };
|
|
37
|
-
const F: Chord = { bass: hz(-40), pad: [hz(-16), hz(-12), hz(-9)], arp: [hz(-16), hz(-12), hz(-9), hz(-4)] };
|
|
38
|
-
const C: Chord = { bass: hz(-33), pad: [hz(-14), hz(-9), hz(-5)], arp: [hz(-9), hz(-5), hz(-2), hz(3)] };
|
|
39
|
-
const G: Chord = { bass: hz(-38), pad: [hz(-14), hz(-10), hz(-7)], arp: [hz(-14), hz(-10), hz(-7), hz(-2)] };
|
|
40
|
-
const PROGRESSION = [AM, F, C, G];
|
|
41
|
-
|
|
42
|
-
/** The chord under `beat`: the progression restarts with each section; the outro is G then home to A minor. */
|
|
43
|
-
function chordAt(beat: number): Chord {
|
|
44
|
-
const section = sectionAt(beat), bar = Math.floor((beat - section.start) / BEATS_PER_BAR);
|
|
45
|
-
if (section.name === "outro") return bar === 0 ? G : AM;
|
|
46
|
-
return PROGRESSION[bar % PROGRESSION.length]!;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface Synth {
|
|
50
|
-
/** The mixed, limited output. Connect it to a destination. */
|
|
51
|
-
readonly output: AudioNode;
|
|
52
|
-
/** Schedule everything that starts during song beat `index`, which begins at context time `at`. */
|
|
53
|
-
beat(index: number, at: number): void;
|
|
54
|
-
}
|
|
25
|
+
const LOOKAHEAD = 0.25;
|
|
26
|
+
/** How far the audio clock may trail the game before the music jumps to the current beat. */
|
|
27
|
+
const DRIFT = 0.1;
|
|
55
28
|
|
|
56
29
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
30
|
+
* What the sound button says. The game's own toggle is an extra mute; it never claims sound vmOS has turned off, so with the
|
|
31
|
+
* desktop muted (or at volume 0) it says so and points at the taskbar.
|
|
59
32
|
*/
|
|
60
|
-
export function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// (A DynamicsCompressorNode would also work, but it delays everything by a fixed 6 ms look-ahead,
|
|
65
|
-
// which would put the music behind the clock that judges the steps.)
|
|
66
|
-
const bus = context.createGain(), clipper = context.createWaveShaper();
|
|
67
|
-
bus.gain.value = 0.52;
|
|
68
|
-
const curve = new Float32Array(2049);
|
|
69
|
-
for (let i = 0; i < curve.length; i++) {
|
|
70
|
-
const x = i / 1024 - 1, size = Math.abs(x);
|
|
71
|
-
curve[i] = Math.sign(x) * (size < 0.6 ? size : 0.6 + 0.35 * Math.tanh((size - 0.6) / 0.35));
|
|
72
|
-
}
|
|
73
|
-
clipper.curve = curve;
|
|
74
|
-
bus.connect(clipper);
|
|
75
|
-
const drums = context.createGain(), music = context.createGain();
|
|
76
|
-
drums.connect(bus);
|
|
77
|
-
// Everything but the drums ducks a little on each kick, which makes the groove pump.
|
|
78
|
-
music.connect(bus);
|
|
79
|
-
|
|
80
|
-
// One second of seeded white noise for the drums and risers.
|
|
81
|
-
const noise = context.createBuffer(1, context.sampleRate, context.sampleRate), samples = noise.getChannelData(0);
|
|
82
|
-
let state = 0x9e3779b9;
|
|
83
|
-
for (let i = 0; i < samples.length; i++) {
|
|
84
|
-
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
|
|
85
|
-
samples[i] = state / 2147483648 - 1;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// ---- Building blocks ----
|
|
89
|
-
/** A gain envelope: quick attack to `peak`, hold, then an exponential fall to silence by `end`. */
|
|
90
|
-
const envelope = (at: number, peak: number, attack: number, holdUntil: number, end: number, to: AudioNode) => {
|
|
91
|
-
const gain = context.createGain();
|
|
92
|
-
gain.gain.setValueAtTime(0, at);
|
|
93
|
-
gain.gain.linearRampToValueAtTime(peak, at + attack);
|
|
94
|
-
gain.gain.setValueAtTime(peak, Math.max(at + attack, holdUntil));
|
|
95
|
-
gain.gain.exponentialRampToValueAtTime(0.0001, end);
|
|
96
|
-
gain.connect(to);
|
|
97
|
-
return gain;
|
|
98
|
-
};
|
|
99
|
-
const oscillator = (type: OscillatorType, frequency: number, at: number, end: number, to: AudioNode, detune = 0) => {
|
|
100
|
-
const voice = context.createOscillator();
|
|
101
|
-
voice.type = type;
|
|
102
|
-
voice.frequency.setValueAtTime(frequency, at);
|
|
103
|
-
voice.detune.value = detune;
|
|
104
|
-
voice.connect(to);
|
|
105
|
-
voice.start(at);
|
|
106
|
-
voice.stop(end + 0.02);
|
|
107
|
-
return voice;
|
|
108
|
-
};
|
|
109
|
-
const filter = (type: BiquadFilterType, frequency: number, q: number, to: AudioNode) => {
|
|
110
|
-
const shape = context.createBiquadFilter();
|
|
111
|
-
shape.type = type;
|
|
112
|
-
shape.frequency.value = frequency;
|
|
113
|
-
shape.Q.value = q;
|
|
114
|
-
shape.connect(to);
|
|
115
|
-
return shape;
|
|
116
|
-
};
|
|
117
|
-
const hiss = (at: number, length: number, to: AudioNode) => {
|
|
118
|
-
const source = context.createBufferSource();
|
|
119
|
-
source.buffer = noise;
|
|
120
|
-
source.loop = true;
|
|
121
|
-
source.connect(to);
|
|
122
|
-
source.start(at, (at * 7.31) % 0.9);
|
|
123
|
-
source.stop(at + length + 0.02);
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
// ---- Drums ----
|
|
127
|
-
const kick = (at: number, level: number) => {
|
|
128
|
-
const body = envelope(at, level, 0.002, at + 0.02, at + 0.38, drums);
|
|
129
|
-
oscillator("sine", 155, at, at + 0.38, body).frequency.exponentialRampToValueAtTime(46, at + 0.13);
|
|
130
|
-
hiss(at, 0.012, filter("highpass", 2500, 0.7, envelope(at, level * 0.22, 0.001, at, at + 0.012, drums)));
|
|
131
|
-
// The duck: the music dips under the kick and swells back before the off-beat.
|
|
132
|
-
music.gain.setValueAtTime(0.5, at);
|
|
133
|
-
music.gain.linearRampToValueAtTime(1, at + 0.22);
|
|
134
|
-
};
|
|
135
|
-
const snare = (at: number, level: number) => {
|
|
136
|
-
hiss(at, 0.2, filter("bandpass", 1800, 0.9, envelope(at, level, 0.002, at, at + 0.2, drums)));
|
|
137
|
-
oscillator("triangle", 190, at, at + 0.1, envelope(at, level * 0.6, 0.002, at, at + 0.1, drums));
|
|
138
|
-
};
|
|
139
|
-
const clap = (at: number, level: number) => {
|
|
140
|
-
// Three quick bursts and a tail, like hands that do not quite agree.
|
|
141
|
-
for (const [offset, length] of [[0, 0.02], [0.012, 0.02], [0.024, 0.16]] as const) {
|
|
142
|
-
hiss(at + offset, length, filter("bandpass", 1300, 1.4, envelope(at + offset, level, 0.001, at + offset, at + offset + length, drums)));
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
const hat = (at: number, open: boolean, level: number) => {
|
|
146
|
-
const length = open ? 0.22 : 0.045;
|
|
147
|
-
hiss(at, length, filter("highpass", 7500, 0.8, envelope(at, level, 0.001, at, at + length, drums)));
|
|
148
|
-
};
|
|
149
|
-
const crash = (at: number, level: number) => hiss(at, 1.6, filter("highpass", 4200, 0.5, envelope(at, level, 0.003, at, at + 1.6, drums)));
|
|
150
|
-
/** Filtered noise sweeping up into the next section. */
|
|
151
|
-
const riser = (at: number, length: number, level: number) => {
|
|
152
|
-
const sweep = filter("bandpass", 300, 2, envelope(at, level, length * 0.9, at + length * 0.95, at + length, drums));
|
|
153
|
-
sweep.frequency.exponentialRampToValueAtTime(7000, at + length);
|
|
154
|
-
hiss(at, length, sweep);
|
|
155
|
-
};
|
|
156
|
-
const tick = (at: number, accent: boolean) => {
|
|
157
|
-
oscillator("sine", accent ? 1760 : 1320, at, at + 0.07, envelope(at, 0.5, 0.001, at, at + 0.07, drums));
|
|
158
|
-
oscillator("triangle", accent ? 880 : 660, at, at + 0.05, envelope(at, 0.25, 0.001, at, at + 0.05, drums));
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
// ---- Pitched instruments ----
|
|
162
|
-
/** A saw bass through a resonant low-pass whose cutoff snaps shut, for a round, plucky note. */
|
|
163
|
-
const bass = (at: number, frequency: number, length: number, level: number, bright: number) => {
|
|
164
|
-
const end = at + length;
|
|
165
|
-
const tone = filter("lowpass", 180 + bright * 1400, 5, envelope(at, level, 0.004, at + length * 0.6, end, music));
|
|
166
|
-
tone.frequency.setTargetAtTime(160 + bright * 200, at + 0.01, 0.06);
|
|
167
|
-
oscillator("sawtooth", frequency, at, end, tone);
|
|
168
|
-
oscillator("sine", frequency, at, end, envelope(at, level * 0.8, 0.004, at + length * 0.6, end, music));
|
|
169
|
-
};
|
|
170
|
-
/** Three chord tones, each two detuned saws, softened by a low-pass: a warm bed. */
|
|
171
|
-
const pad = (at: number, chord: Chord, length: number, level: number, cutoff: number) => {
|
|
172
|
-
const end = at + length, warm = filter("lowpass", cutoff, 0.7, envelope(at, level, 0.35, end - 0.4, end, music));
|
|
173
|
-
for (const frequency of chord.pad) {
|
|
174
|
-
oscillator("sawtooth", frequency, at, end, warm, -8);
|
|
175
|
-
oscillator("sawtooth", frequency, at, end, warm, 8);
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
/** The lead: square and saw an octave apart, with a little vibrato on longer notes. */
|
|
179
|
-
const lead = (at: number, frequency: number, length: number, level: number) => {
|
|
180
|
-
const end = at + length, tone = filter("lowpass", 2600, 1.5, envelope(at, level, 0.008, end - 0.06, end + 0.08, music));
|
|
181
|
-
const square = oscillator("square", frequency, at, end + 0.08, tone), saw = oscillator("sawtooth", frequency / 2, at, end + 0.08, tone, 4);
|
|
182
|
-
if (length >= BEAT_SECONDS) {
|
|
183
|
-
const wobble = context.createOscillator(), depth = context.createGain();
|
|
184
|
-
wobble.frequency.value = 5.5;
|
|
185
|
-
depth.gain.setValueAtTime(0, at);
|
|
186
|
-
depth.gain.linearRampToValueAtTime(14, at + 0.35);
|
|
187
|
-
wobble.connect(depth);
|
|
188
|
-
depth.connect(square.detune);
|
|
189
|
-
depth.connect(saw.detune);
|
|
190
|
-
wobble.start(at);
|
|
191
|
-
wobble.stop(end + 0.1);
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
const pluck = (at: number, frequency: number, level: number, brightness: number) => {
|
|
195
|
-
const tone = filter("lowpass", 900 + brightness * 3000, 2, envelope(at, level, 0.003, at, at + 0.3, music));
|
|
196
|
-
oscillator("triangle", frequency, at, at + 0.3, tone);
|
|
197
|
-
oscillator("square", frequency, at, at + 0.3, envelope(at, level * 0.25, 0.003, at, at + 0.12, tone));
|
|
198
|
-
};
|
|
199
|
-
/** A soft bell: a sine and an inharmonic partial that dies sooner. */
|
|
200
|
-
const bell = (at: number, frequency: number, length: number, level: number) => {
|
|
201
|
-
oscillator("sine", frequency, at, at + length + 0.8, envelope(at, level, 0.004, at, at + length + 0.8, music));
|
|
202
|
-
oscillator("sine", frequency * 2.76, at, at + 0.5, envelope(at, level * 0.35, 0.002, at, at + 0.5, music));
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
// ---- The arrangement ----
|
|
206
|
-
const tune: TuneNote[][] = Array.from({ length: SONG_BEATS }, () => []);
|
|
207
|
-
for (const note of createTune(seed)) tune[Math.floor(note.beat)]?.push(note);
|
|
208
|
-
const playTune = (index: number, at: number, harmony: boolean) => {
|
|
209
|
-
for (const note of tune[index] ?? []) {
|
|
210
|
-
const when = at + (note.beat - index) * BEAT_SECONDS, length = note.length * BEAT_SECONDS, pitch = degreeHz(note.degree);
|
|
211
|
-
if (note.voice === "lead") {
|
|
212
|
-
lead(when, pitch, length, 0.16);
|
|
213
|
-
if (harmony) lead(when, degreeHz(note.degree - 2), length, 0.07); // a third below, in the finale
|
|
214
|
-
} else if (note.voice === "pluck") pluck(when, pitch, 0.22, 0.4);
|
|
215
|
-
else bell(when, pitch, length, 0.16);
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
return {
|
|
220
|
-
output: clipper,
|
|
221
|
-
beat(index, at) {
|
|
222
|
-
if (index < 0 || index >= SONG_BEATS) return;
|
|
223
|
-
const half = BEAT_SECONDS / 2, quarter = BEAT_SECONDS / 4;
|
|
224
|
-
if (index < COUNT_IN_BEATS) {
|
|
225
|
-
tick(at, index === COUNT_IN_BEATS - 1);
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
const section = sectionAt(index), inBar = index % BEATS_PER_BAR, barStart = inBar === 0;
|
|
229
|
-
const inSection = index - section.start, progress = inSection / section.beats, chord = chordAt(index);
|
|
230
|
-
switch (section.name) {
|
|
231
|
-
case "intro": {
|
|
232
|
-
// Half-time kick for two bars, then every beat; a pad and soft hats; the tune hinted on a pluck.
|
|
233
|
-
if (inSection >= 8 || inBar % 2 === 0) kick(at, 0.6);
|
|
234
|
-
hat(at + half, false, 0.08);
|
|
235
|
-
if (barStart) pad(at, chord, BEATS_PER_BAR * BEAT_SECONDS, 0.06, 700 + progress * 900);
|
|
236
|
-
if (inBar % 2 === 0) bass(at, chord.bass, BEAT_SECONDS * 1.8, 0.22, 0.2);
|
|
237
|
-
playTune(index, at, false);
|
|
238
|
-
break;
|
|
239
|
-
}
|
|
240
|
-
case "build": {
|
|
241
|
-
const roll = inSection >= section.beats - 4;
|
|
242
|
-
kick(at, 0.8);
|
|
243
|
-
if (inBar === 1 || inBar === 3) clap(at, 0.4);
|
|
244
|
-
for (let step = 0; step < 4; step++) {
|
|
245
|
-
// A sixteenth-note arpeggio that climbs and brightens across the build.
|
|
246
|
-
pluck(at + step * quarter, chord.arp[(inSection * 4 + step) % 4]! * (progress > 0.5 ? 2 : 1), 0.1 + progress * 0.06, progress);
|
|
247
|
-
if (roll) snare(at + step * quarter, 0.12 + (inSection - section.beats + 4 + step / 4) * 0.07);
|
|
248
|
-
}
|
|
249
|
-
bass(at, chord.bass, half * 0.9, 0.3, 0.3 + progress * 0.4);
|
|
250
|
-
bass(at + half, chord.bass, half * 0.9, 0.26, 0.3 + progress * 0.4);
|
|
251
|
-
hat(at + half, false, 0.12);
|
|
252
|
-
if (inSection === section.beats - 8) riser(at, 8 * BEAT_SECONDS, 0.22);
|
|
253
|
-
break;
|
|
254
|
-
}
|
|
255
|
-
case "drop":
|
|
256
|
-
case "finale": {
|
|
257
|
-
const finale = section.name === "finale";
|
|
258
|
-
if (inSection === 0 || (inSection === 16 && !finale)) crash(at, 0.3);
|
|
259
|
-
kick(at, 0.95);
|
|
260
|
-
if (inBar === 1 || inBar === 3) { snare(at, 0.36); clap(at, 0.24); }
|
|
261
|
-
hat(at, false, 0.08);
|
|
262
|
-
hat(at + half, true, 0.11);
|
|
263
|
-
if (finale) { hat(at + quarter, false, 0.05); hat(at + 3 * quarter, false, 0.05); }
|
|
264
|
-
// Pumping bass: root on the beat, octave on the off-beat.
|
|
265
|
-
bass(at, chord.bass, half * 0.85, 0.36, 0.55);
|
|
266
|
-
bass(at + half, chord.bass * 2, half * 0.85, 0.3, 0.75);
|
|
267
|
-
if (barStart) pad(at, chord, BEATS_PER_BAR * BEAT_SECONDS, finale ? 0.07 : 0.05, 1600);
|
|
268
|
-
playTune(index, at, finale);
|
|
269
|
-
break;
|
|
270
|
-
}
|
|
271
|
-
case "breakdown": {
|
|
272
|
-
const lastBar = inSection >= section.beats - 4;
|
|
273
|
-
if (barStart) {
|
|
274
|
-
pad(at, chord, BEATS_PER_BAR * BEAT_SECONDS, 0.08, 1100);
|
|
275
|
-
bass(at, chord.bass, BEATS_PER_BAR * BEAT_SECONDS * 0.9, 0.2, 0.05);
|
|
276
|
-
}
|
|
277
|
-
// A soft half-time kick under the half-note steps.
|
|
278
|
-
if (!lastBar && inBar % 2 === 0) kick(at, 0.4);
|
|
279
|
-
if (lastBar) {
|
|
280
|
-
kick(at, 0.75);
|
|
281
|
-
for (let step = 0; step < 4; step++) snare(at + step * quarter, 0.1 + (inSection - section.beats + 4 + step / 4) * 0.08);
|
|
282
|
-
}
|
|
283
|
-
if (inSection === section.beats - 4) riser(at, 4 * BEAT_SECONDS, 0.2);
|
|
284
|
-
hat(at + half, false, 0.06);
|
|
285
|
-
playTune(index, at, false);
|
|
286
|
-
break;
|
|
287
|
-
}
|
|
288
|
-
case "outro": {
|
|
289
|
-
// G for a bar under the cadence, then one last A minor that rings out.
|
|
290
|
-
if (inSection < 4) {
|
|
291
|
-
kick(at, 0.85);
|
|
292
|
-
if (inBar === 1 || inBar === 3) snare(at, 0.32);
|
|
293
|
-
hat(at + half, true, 0.1);
|
|
294
|
-
bass(at, chord.bass, half * 0.85, 0.34, 0.5);
|
|
295
|
-
bass(at + half, chord.bass * 2, half * 0.85, 0.28, 0.7);
|
|
296
|
-
if (inSection === 0) pad(at, chord, BEATS_PER_BAR * BEAT_SECONDS, 0.07, 1600);
|
|
297
|
-
} else if (inSection === 4) {
|
|
298
|
-
kick(at, 1);
|
|
299
|
-
crash(at, 0.34);
|
|
300
|
-
pad(at, chord, 4 * BEAT_SECONDS - 0.1, 0.12, 1400);
|
|
301
|
-
bass(at, chord.bass, 4 * BEAT_SECONDS - 0.1, 0.36, 0.3);
|
|
302
|
-
}
|
|
303
|
-
playTune(index, at, true);
|
|
304
|
-
break;
|
|
305
|
-
}
|
|
306
|
-
default:
|
|
307
|
-
break;
|
|
308
|
-
}
|
|
309
|
-
},
|
|
310
|
-
};
|
|
33
|
+
export function soundLabel(on: boolean, desktop: Readonly<{ muted: boolean; volume: number }>): { text: string; pressed: boolean; title: string } {
|
|
34
|
+
const desktopOn = !desktop.muted && desktop.volume > 0;
|
|
35
|
+
if (!on) return { text: "♪ Sound off", pressed: false, title: "" };
|
|
36
|
+
return desktopOn ? { text: "♪ Sound on", pressed: true, title: "" } : { text: "♪ Sound off in vmOS", pressed: true, title: "Turn the sound on with the speaker in the vmOS taskbar." };
|
|
311
37
|
}
|
|
312
38
|
|
|
313
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Where the music plays: the SDK's `createAudio(app)` gives both, already following the desktop's sound. Both are read on
|
|
41
|
+
* every pump, because the helper replaces a context the browser closed or kept stopped.
|
|
42
|
+
*/
|
|
43
|
+
export interface MusicOutput { readonly context: BaseAudioContext; readonly master: AudioNode }
|
|
314
44
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
45
|
+
/**
|
|
46
|
+
* The live song. `output` is asked for on the first `start()` (inside the Start click, so the browser lets the audio
|
|
47
|
+
* start); it returns null where there is no Web Audio, and the game then keeps its own clock and plays silent.
|
|
48
|
+
*/
|
|
49
|
+
export function createMusic(output: () => MusicOutput | null): Music {
|
|
50
|
+
let out: MusicOutput | null = null, synth: Synth | undefined, made: BaseAudioContext | undefined;
|
|
51
|
+
let startAt = 0, nextBeat = 0, playing = false, seed = 1, track: PlayableTrack | undefined;
|
|
52
|
+
const latency = (context: BaseAudioContext) => {
|
|
53
|
+
// What is heard now left the context a little earlier: subtract the output latency.
|
|
54
|
+
const live = context as Partial<AudioContext>;
|
|
55
|
+
return Math.min(0.2, Math.max(0, live.outputLatency || live.baseLatency || 0));
|
|
56
|
+
};
|
|
57
|
+
const time = () => out && playing && out.context.state === "running" ? out.context.currentTime - latency(out.context) - startAt : null;
|
|
318
58
|
|
|
319
59
|
return {
|
|
320
|
-
start(
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
} catch {
|
|
324
|
-
return; // No audio: the game falls back to its own clock and plays silent.
|
|
325
|
-
}
|
|
326
|
-
void context.resume().catch(() => {});
|
|
327
|
-
if (!volume) {
|
|
328
|
-
volume = context.createGain();
|
|
329
|
-
volume.gain.value = muted ? 0 : 1;
|
|
330
|
-
volume.connect(context.destination);
|
|
331
|
-
}
|
|
60
|
+
start(next, round) {
|
|
61
|
+
out ??= output();
|
|
62
|
+
if (!out) return;
|
|
332
63
|
// A fresh mix per round: the old one's nodes finish on their own and are collected.
|
|
64
|
+
track = next;
|
|
65
|
+
seed = round;
|
|
333
66
|
synth?.output.disconnect();
|
|
334
|
-
synth = createSynth(context, seed);
|
|
335
|
-
synth.output.connect(
|
|
336
|
-
startAt = context.currentTime + 0.15;
|
|
67
|
+
synth = createSynth(made = out.context, track, seed);
|
|
68
|
+
synth.output.connect(out.master);
|
|
69
|
+
startAt = out.context.currentTime + 0.15;
|
|
337
70
|
nextBeat = 0;
|
|
338
71
|
playing = true;
|
|
339
72
|
},
|
|
340
|
-
time
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
73
|
+
time,
|
|
74
|
+
pump(songTime) {
|
|
75
|
+
if (!out || !synth || !track || !playing || out.context.state !== "running") return;
|
|
76
|
+
const context = out.context, beat = beatSeconds(track), beats = songBeats(track);
|
|
77
|
+
if (context !== made) {
|
|
78
|
+
// a new context (the old one was closed or stuck): a new mix on it, joining at the current beat
|
|
79
|
+
synth.output.disconnect();
|
|
80
|
+
synth = createSynth(made = context, track, seed);
|
|
81
|
+
synth.output.connect(out.master);
|
|
82
|
+
nextBeat = 0;
|
|
83
|
+
startAt = Infinity;
|
|
84
|
+
}
|
|
85
|
+
const heard = time()!;
|
|
86
|
+
// (at the very start songTime holds at 0 while the clock counts up through its lead-in: that is not late)
|
|
87
|
+
if (songTime > 0 && heard < songTime - DRIFT) {
|
|
88
|
+
// The audio started late: put the song where the steps are and skip the beats already gone.
|
|
89
|
+
startAt = context.currentTime - latency(context) - songTime;
|
|
90
|
+
nextBeat = Math.max(nextBeat, Math.ceil(songTime / beat - 1e-9));
|
|
91
|
+
}
|
|
92
|
+
while (nextBeat < beats && startAt + nextBeat * beat < context.currentTime + LOOKAHEAD) {
|
|
93
|
+
synth.beat(nextBeat, startAt + nextBeat * beat);
|
|
350
94
|
nextBeat++;
|
|
351
95
|
}
|
|
352
96
|
},
|
|
353
|
-
setActive(active) {
|
|
354
|
-
if (!context) return;
|
|
355
|
-
void (active ? context.resume() : context.suspend()).catch(() => {});
|
|
356
|
-
},
|
|
357
|
-
setMuted(next) {
|
|
358
|
-
muted = next;
|
|
359
|
-
if (context && volume) volume.gain.setTargetAtTime(muted ? 0 : 1, context.currentTime, 0.02);
|
|
360
|
-
},
|
|
361
97
|
stop() {
|
|
362
98
|
playing = false;
|
|
363
99
|
},
|
|
364
100
|
dispose() {
|
|
365
101
|
playing = false;
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
volume = undefined;
|
|
102
|
+
synth?.output.disconnect();
|
|
103
|
+
out = null;
|
|
369
104
|
synth = undefined;
|
|
370
105
|
},
|
|
371
106
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as THREE from "three";
|
|
2
|
-
import { LANES, type Judgement, type Lane, type Note, type
|
|
2
|
+
import { LANES, type Judgement, type Lane, type Note, type SectionRole } from "./game";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* The stage around Biscuit: a tiled dance floor and a backdrop that pulse with the beat, two
|
|
@@ -19,17 +19,19 @@ const NOTE_Y = 0.42, TILT = 0.8;
|
|
|
19
19
|
/** In-plane turn of the arrow (which points up when unturned) for each lane. */
|
|
20
20
|
const LANE_TURN: Readonly<Record<Lane, number>> = { up: 0, left: Math.PI / 2, down: Math.PI, right: -Math.PI / 2 };
|
|
21
21
|
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Each track brings two stage colours; each section role tints them toward a calm night indigo by
|
|
24
|
+
* this much (the drops glow in the track's own colours) and the break swaps them. The stage fades
|
|
25
|
+
* from one section's pair to the next as the song moves on.
|
|
26
|
+
*/
|
|
27
|
+
const ROLE_CALM: Readonly<Record<SectionRole, number>> = { "count-in": 0.6, intro: 0.5, build: 0.3, drop: 0, break: 0.45, finale: 0, outro: 0.2 };
|
|
28
|
+
const NIGHT = "#6b4cff";
|
|
27
29
|
|
|
28
30
|
export type Framing = "title" | "play";
|
|
29
31
|
export interface StageFrame {
|
|
30
32
|
/** Song position in beats (it keeps counting on the title screen, slowly, so the stage breathes). */
|
|
31
33
|
beat: number;
|
|
32
|
-
section:
|
|
34
|
+
section: SectionRole;
|
|
33
35
|
/** 0 to 1: how much the stage moves. */
|
|
34
36
|
energy: number;
|
|
35
37
|
reducedMotion: boolean;
|
|
@@ -56,6 +58,8 @@ export interface Stage {
|
|
|
56
58
|
/** Screen position (CSS pixels) of a world point. */
|
|
57
59
|
project(point: THREE.Vector3, out: THREE.Vector2): THREE.Vector2;
|
|
58
60
|
update(seconds: number, frame: StageFrame): void;
|
|
61
|
+
/** The playing (or chosen) track's two colours. */
|
|
62
|
+
setColours(colours: readonly [string, string]): void;
|
|
59
63
|
/** The stage's current main colour, for the cat's rim light. */
|
|
60
64
|
readonly accent: THREE.Color;
|
|
61
65
|
dispose(): void;
|
|
@@ -247,8 +251,17 @@ export function createStage(): Stage {
|
|
|
247
251
|
const scene = new THREE.Scene(), camera = new THREE.PerspectiveCamera(30, 1, 0.1, 90);
|
|
248
252
|
const disposables: { dispose(): void }[] = [];
|
|
249
253
|
const keep = <T extends { dispose(): void }>(item: T) => (disposables.push(item), item);
|
|
250
|
-
const accent = new THREE.Color(
|
|
251
|
-
const sectionColors = Object.fromEntries(Object.
|
|
254
|
+
const accent = new THREE.Color(NIGHT), second = new THREE.Color("#3fa9d6");
|
|
255
|
+
const sectionColors = Object.fromEntries((Object.keys(ROLE_CALM) as SectionRole[]).map(role => [role, [new THREE.Color(), new THREE.Color()]])) as Record<SectionRole, [THREE.Color, THREE.Color]>;
|
|
256
|
+
const night = new THREE.Color(NIGHT);
|
|
257
|
+
const setColours = (colours: readonly [string, string]) => {
|
|
258
|
+
for (const role of Object.keys(ROLE_CALM) as SectionRole[]) {
|
|
259
|
+
const [a, b] = sectionColors[role], swap = role === "break";
|
|
260
|
+
a.set(colours[swap ? 1 : 0]).lerp(night, ROLE_CALM[role]);
|
|
261
|
+
b.set(colours[swap ? 0 : 1]).lerp(night, ROLE_CALM[role] * 0.5);
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
setColours(["#7b5cff", "#3fa9d6"]);
|
|
252
265
|
const shared = { uA: { value: accent }, uB: { value: second }, uBeat: { value: 0 }, uEnergy: { value: 0.5 } };
|
|
253
266
|
const horizon = new THREE.Color("#1c1233"), top = new THREE.Color("#07050f");
|
|
254
267
|
|
|
@@ -425,6 +438,7 @@ export function createStage(): Stage {
|
|
|
425
438
|
scene,
|
|
426
439
|
camera,
|
|
427
440
|
accent,
|
|
441
|
+
setColours,
|
|
428
442
|
resize(nextWidth, nextHeight, nextInset) {
|
|
429
443
|
width = nextWidth;
|
|
430
444
|
height = nextHeight;
|
|
Binary file
|
|
Binary file
|