Skip to content

Commit c7e2da4

Browse files
authored
Merge pull request #34 from hynding/claude/m5.1-audio
M5.1: audio
2 parents 9a3c360 + 6e2fee2 commit c7e2da4

33 files changed

Lines changed: 1113 additions & 14 deletions

‎CLAUDE.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ The game is a static site, deployed to GitHub Pages from `main` by [.github/work
8585

8686
The **API** lives in [server/](server/) (its README lists settings and routes): Fastify, SQLite via `node:sqlite` (loaded with `createRequire`, since Vite can't bundle it), bundles and covers stored by SHA-256 ([server/blobs.ts](server/blobs.ts)). It imports the game's own `readBundle` / `buildRegistry` / `defaultPack`, so a published pack passes exactly the checks the game runs; keep `src/content` free of browser-only code. Sessions are an opaque token in an HttpOnly cookie (hash stored); state-changing requests need the `x-fight-forge: 1` header. `npm run server` runs it (`DEV_LOGIN=1` allows sign-in by name for development); its tests (`server/app.test.ts`, `fastify.inject`, in-memory store) run with the rest. The client is [src/net/api.ts](src/net/api.ts): with no `VITE_API_URL`, `apiEnabled()` is false and online UI stays hidden. The Forge's pack page has [PublishTools](src/components/forge/PublishTools.tsx): sign in, choose visibility, publish the current version (a published version must be newer; it offers the next patch version). The start menu's BROWSE PACKS opens the **gallery** (`gameScreen: 'gallery'`, [src/components/Gallery.tsx](src/components/Gallery.tsx)): search, sort, a creator filter, and a pack page with Play (`fetchBundle`, cached per version in the Cache API since bundles are immutable, then `installBundle` and `startRun` at the pack's first own level) and a share link. `?pack=<id>` (`linkedPack()`) opens straight to that pack's page. **Remix in Forge** (`remixBundle` in workspace.ts) copies a published pack into the workspace under a new id (`<id>-remix`), version 1.0.0, with `meta.remixOf` crediting the original; the server links a published remix to its original (`remix_of`), and pack pages show "Remix of" and the list of remixes. Likes are one per user (`POST /packs/:id/like`, sort `likes`). The gallery marks packs updated since this browser last played them ([src/net/played.ts](src/net/played.ts), localStorage). **Moderation**: anyone signed in can ⚑ Report a pack; moderators (`ADMINS` setting, `provider:name`) get a Reports panel ([Moderation.tsx](src/components/Moderation.tsx)) to take a pack down, dismiss reports or ban its owner, each logged in `moderation_actions`, and Take down / Put back on pack pages. A taken-down pack, or any pack of a banned user, is visible only to its owner (marked in PublishTools) and admins. Owners can remove their pack from the server. Limits are server-side: publishes and reports per user per hour, and writes per IP. Bundles check models with `glbProblem` (a whole glTF 2.0 file with no outside URIs) and audio with `audioType`; tests build models with `tinyGlb` from [src/test/glb.ts](src/test/glb.ts).
8787

88+
### Audio
89+
Sound is content. The content's `sounds` (`SoundDef`, [src/audio/types.ts](src/audio/types.ts)) are `synth` (wave, pitch sweep, duration: no file needed) or `file` (an `audio` asset or a site URL); `music` is a looping `pattern` (tempo and tracks of space-separated notes like `A2 . C3 -`, parsed by [src/audio/notes.ts](src/audio/notes.ts)) or a `file`. `settings.audio` names the sound for each event (`hit`, `hurt`, `block`, `ko`, `pickup`, `clear`); a move can add `sound` (played as its active frames begin) and `hitSound`; a level can name `music`. [src/audio/engine.ts](src/audio/engine.ts) is the WebAudio engine: `playSound(id)` (from CombatSystem, Pickups, LevelDirector), `playMusic` / `stopMusic` (GameScene, per level), `duckMusic` while frozen, and `previewSound` / `previewMusic` for the Forge's [SoundTools](src/components/forge/SoundTools.tsx) (▶ Play, and "Use an audio file…" via `addAudioAsset`). Audio starts on the first click or key (`installAudioUnlock` in main.tsx). Master / music / effects volumes are a per-browser preference ([src/audio/volume.ts](src/audio/volume.ts)) set from [VolumeControls](src/components/VolumeControls.tsx) on the start menu (SOUND) and the pause menu. `audioLog` keeps the last sounds asked for, for headless tests.
90+
8891
### Fighters and controllers
8992
Player and enemies are both **fighters**. [src/components/FighterBody.tsx](src/components/FighterBody.tsx) is the one shared body: rigid body and collider, a `fighter:<name>` sim system (order `SYSTEM_ORDER.fighters`, before attacks and combat) that steps the hit reaction, asks a controller for an `Intent`, moves (or slides with knockback while reacting), jumps, updates facing and the fighter state machine, plus the model, attack tint, hurt flash and hitbox overlay. [Player.tsx](src/components/Player.tsx) and [Enemy.tsx](src/components/Enemy.tsx) are thin wrappers that supply the controller and start moves (the player's combo; an enemy's attack when it takes the slot). Both use `forwardRef<RapierRigidBody, …>` so `GameScene` holds refs to their bodies.
9093

‎content/default/levels.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"id": "level-1",
44
"name": "Back Alley",
55
"theme": "default",
6+
"music": "alley",
67
"segments": [
78
{
89
"length": 45,
@@ -252,6 +253,7 @@
252253
"id": "level-2",
253254
"name": "The Docks",
254255
"theme": "docks",
256+
"music": "docks",
255257
"segments": [
256258
{
257259
"length": 40,

‎content/default/moves.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"jab": {
3+
"sound": "whoosh",
34
"startup": 0.05,
45
"active": 0.08,
56
"recovery": 0.17,
@@ -25,6 +26,7 @@
2526
"next": "jab2"
2627
},
2728
"jab2": {
29+
"sound": "whoosh",
2830
"startup": 0.05,
2931
"active": 0.08,
3032
"recovery": 0.17,
@@ -50,6 +52,7 @@
5052
"next": "hook"
5153
},
5254
"hook": {
55+
"sound": "whoosh",
5356
"startup": 0.08,
5457
"active": 0.1,
5558
"recovery": 0.22,
@@ -75,6 +78,8 @@
7578
"next": "uppercut"
7679
},
7780
"uppercut": {
81+
"sound": "whoosh",
82+
"hitSound": "heavy",
7883
"startup": 0.12,
7984
"active": 0.1,
8085
"recovery": 0.35,
@@ -100,6 +105,8 @@
100105
}
101106
},
102107
"jumpKick": {
108+
"sound": "whoosh",
109+
"hitSound": "heavy",
103110
"startup": 0.06,
104111
"active": 0.25,
105112
"recovery": 0.1,
@@ -125,6 +132,8 @@
125132
"air": true
126133
},
127134
"dashPunch": {
135+
"sound": "whoosh",
136+
"hitSound": "heavy",
128137
"startup": 0.1,
129138
"active": 0.15,
130139
"recovery": 0.35,
@@ -151,6 +160,8 @@
151160
"lunge": 7
152161
},
153162
"rushPunch": {
163+
"sound": "whoosh",
164+
"hitSound": "heavy",
154165
"startup": 0.2,
155166
"active": 0.15,
156167
"recovery": 0.5,
@@ -176,6 +187,7 @@
176187
"lunge": 9
177188
},
178189
"enemyPunch": {
190+
"sound": "whoosh",
179191
"startup": 0.4,
180192
"active": 0.1,
181193
"recovery": 0.4,

‎content/default/music.json‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"alley": {
3+
"kind": "pattern",
4+
"tempo": 112,
5+
"volume": 0.35,
6+
"tracks": [
7+
{
8+
"wave": "square",
9+
"volume": 0.35,
10+
"notes": "A2 . A2 . C3 . A2 . G2 . G2 . E2 . G2 . A2 . A2 . C3 . D3 . E3 - D3 . C3 . G2 ."
11+
},
12+
{
13+
"wave": "triangle",
14+
"volume": 0.5,
15+
"notes": "A4 - - - . . E4 . G4 - - - . . D4 . A4 - - - . . C5 . B4 - A4 - G4 - E4 -"
16+
}
17+
]
18+
},
19+
"docks": {
20+
"kind": "pattern",
21+
"tempo": 96,
22+
"volume": 0.35,
23+
"tracks": [
24+
{
25+
"wave": "sawtooth",
26+
"volume": 0.25,
27+
"notes": "D2 . D2 D2 . . F2 . G2 . G2 G2 . . A2 . D2 . D2 D2 . . C3 . A2 . G2 . F2 . E2 ."
28+
},
29+
{
30+
"wave": "sine",
31+
"volume": 0.5,
32+
"notes": "D4 - - - F4 - - - A4 - - - G4 - F4 - E4 - - - C4 - - - D4 - - - - - - -"
33+
}
34+
]
35+
}
36+
}

‎content/default/settings.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,13 @@
131131
"pause": ["escape"],
132132
"hitboxes": ["h"]
133133
}
134+
},
135+
"audio": {
136+
"hit": "punch",
137+
"hurt": "hurt",
138+
"block": "block",
139+
"ko": "ko",
140+
"pickup": "pickup",
141+
"clear": "clear"
134142
}
135143
}

‎content/default/sounds.json‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"whoosh": {
3+
"kind": "synth",
4+
"wave": "noise",
5+
"freq": 1800,
6+
"freqEnd": 600,
7+
"duration": 0.09,
8+
"volume": 0.25
9+
},
10+
"punch": {
11+
"kind": "synth",
12+
"wave": "square",
13+
"freq": 180,
14+
"freqEnd": 55,
15+
"duration": 0.11,
16+
"volume": 0.5
17+
},
18+
"heavy": {
19+
"kind": "synth",
20+
"wave": "sawtooth",
21+
"freq": 140,
22+
"freqEnd": 35,
23+
"duration": 0.2,
24+
"volume": 0.6
25+
},
26+
"hurt": {
27+
"kind": "synth",
28+
"wave": "sawtooth",
29+
"freq": 320,
30+
"freqEnd": 120,
31+
"duration": 0.16,
32+
"volume": 0.45
33+
},
34+
"block": {
35+
"kind": "synth",
36+
"wave": "triangle",
37+
"freq": 1100,
38+
"freqEnd": 800,
39+
"duration": 0.07,
40+
"volume": 0.4
41+
},
42+
"ko": {
43+
"kind": "synth",
44+
"wave": "square",
45+
"freq": 400,
46+
"freqEnd": 40,
47+
"duration": 0.45,
48+
"volume": 0.5
49+
},
50+
"pickup": {
51+
"kind": "synth",
52+
"wave": "sine",
53+
"freq": 660,
54+
"freqEnd": 1320,
55+
"duration": 0.14,
56+
"volume": 0.45
57+
},
58+
"clear": {
59+
"kind": "synth",
60+
"wave": "triangle",
61+
"freq": 523,
62+
"freqEnd": 1046,
63+
"duration": 0.6,
64+
"volume": 0.5
65+
}
66+
}

‎src/audio/audio.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { noteFreq, parseNotes, patternSteps, stepSeconds } from './notes'
3+
import { DEFAULT_VOLUMES, parseVolumes } from './volume'
4+
5+
describe('music notes', () => {
6+
it('names notes by pitch', () => {
7+
expect(noteFreq('A4')).toBe(440)
8+
expect(noteFreq('A3')).toBe(220)
9+
expect(noteFreq('C4')).toBeCloseTo(261.63, 2)
10+
expect(noteFreq('C#4')).toBeCloseTo(noteFreq('Db4')!, 6)
11+
expect(noteFreq('H2')).toBeNull()
12+
expect(noteFreq('.')).toBeNull()
13+
})
14+
15+
it('reads rests and holds', () => {
16+
const { notes, steps } = parseNotes('A4 - - . C5 . - E5')
17+
expect(steps).toBe(8)
18+
expect(notes.map((n) => [n.step, n.steps])).toEqual([
19+
[0, 3],
20+
[4, 1],
21+
[7, 1],
22+
])
23+
})
24+
25+
it('times steps as sixteenth notes and loops on the longest track', () => {
26+
expect(stepSeconds(120)).toBe(0.125)
27+
expect(patternSteps([{ wave: 'sine', volume: 1, notes: 'A4 .' }, { wave: 'square', volume: 1, notes: 'A2 . . .' }])).toBe(4)
28+
})
29+
})
30+
31+
describe('volume settings', () => {
32+
it('fills in and clamps stored values', () => {
33+
expect(parseVolumes(null)).toEqual(DEFAULT_VOLUMES)
34+
expect(parseVolumes({ master: 2, music: -1, sfx: 'loud' })).toEqual({ master: 1, music: 0, sfx: DEFAULT_VOLUMES.sfx })
35+
})
36+
})

0 commit comments

Comments
 (0)