Sounds on the client
This commit is contained in:
@@ -32,6 +32,8 @@ mods/
|
||||
schematics/*.cbsm # schematics, never sent to players. see misc/schematics.md
|
||||
textures/*.png
|
||||
textures/*.json # animations, see resources/textures.md
|
||||
sounds/**/*.ogg # sound files, see resources/sounds.md
|
||||
sounds.json # the sounds they make up
|
||||
scripts/
|
||||
main.ts # the mod's script, its exports are its phases. sent to players, see concepts/sides.md
|
||||
*.ts # whatever else it imports
|
||||
@@ -116,10 +118,11 @@ deno task build # packs every mod in mods/ into server_mo
|
||||
```
|
||||
copper_tools.cubi
|
||||
manifest.json # with main and credits pointing into the zip
|
||||
data.json # every block, model, item, entity, recipe, ore, tag, loot table and schematic, and the config
|
||||
data.json # every block, model, item, entity, recipe, ore, tag, loot table, sound and schematic, and the config
|
||||
script.js # the script bundled into one module
|
||||
textures/<name>.png
|
||||
textures/<name>.json # for animated textures
|
||||
sounds/<path>.ogg
|
||||
credits.md
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
# Sounds
|
||||
|
||||
A mod's sounds are `.ogg` files in its `sounds/` folder, put together into sounds by its `sounds.json`. Client phases
|
||||
play them with `ctx.sounds`.
|
||||
|
||||
## Sound Files
|
||||
|
||||
Any `.ogg` (Vorbis or Opus) in `sounds/`, in folders if you like. Its path without `.ogg` is what `sounds.json` names it
|
||||
by, so `sounds/walker/step_1.ogg` is `walker/step_1`. Paths are a-z, 0-9 and `_`.
|
||||
|
||||
Mono and stereo files both work, from a point in the world or not.
|
||||
|
||||
## `sounds.json`
|
||||
|
||||
```json
|
||||
{
|
||||
"format_version": 1,
|
||||
"sounds": {
|
||||
"walker/step": {
|
||||
"files": ["walker/step_1", "walker/step_2", "walker/step_3"],
|
||||
"category": "entities"
|
||||
},
|
||||
"walker/theme": { "files": ["walker/theme"], "category": "entities", "range": 32 },
|
||||
"click": { "files": ["click"], "category": "ui", "volume": 0.5 }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Each key is a sound, whose id is `<mod id>:<key>`, so `walker/step` in `copper_tools` is `copper_tools:walker/step`. Any
|
||||
mod can play any sound by its id.
|
||||
|
||||
| Field | What it does |
|
||||
| ---------- | ----------------------------------------------------------------------------------------------------------- |
|
||||
| `files` | The files it plays, one picked at random each time. A file listed twice comes up twice as often. |
|
||||
| `volume` | 0 to 1, 1 if not given. Every play multiplies it. |
|
||||
| `pitch` | 1 if not given. Every play multiplies it. |
|
||||
| `range` | How far away it's heard at volume 1, in blocks. 16 if not given. |
|
||||
| `category` | Which volume the player's settings play it at: `music`, `ambient`, `blocks`, `entities`, `players` or `ui`. |
|
||||
|
||||
A sound without a category only follows the player's master volume.
|
||||
|
||||
`deno task check-mods` reports a file that isn't there, and warns about an `.ogg` no sound plays.
|
||||
|
||||
## Playing Sounds
|
||||
|
||||
In a client phase, `ctx.sounds.play` plays a sound on that player's client only:
|
||||
|
||||
```ts
|
||||
export function client(ctx: ClientContext) {
|
||||
ctx.ui.add_inventory_overlay({
|
||||
on_render(g) {
|
||||
if (g.button("Ring", 4, 4, 40, 18)) ctx.sounds.play("copper_tools:click");
|
||||
},
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
With nothing but its id it's heard as it is wherever the player is, like a click in a screen. The options:
|
||||
|
||||
| Option | What it does |
|
||||
| -------- | --------------------------------------------------------------------------------------------------- |
|
||||
| `at` | A point it comes from. It fades to nothing at its range, and isn't played at all from further away. |
|
||||
| `follow` | An entity's id. It comes from where the entity is drawn, and stops when the entity goes. |
|
||||
| `loop` | Plays again from the start until stopped. |
|
||||
| `volume` | Multiplies the sound's own. Over 1 it's heard further away, not louder: at 2, twice as far. |
|
||||
| `pitch` | Multiplies the sound's own. 2 is an octave up, and twice as fast. |
|
||||
|
||||
`play` throws for a sound no mod defines, and `ctx.sounds.exists(id)` checks first. It returns the sound playing, which
|
||||
can be stopped or changed while it plays:
|
||||
|
||||
```ts
|
||||
const hum = ctx.sounds.play("copper_tools:machine/hum", { at: { x: 10.5, y: 64.5, z: 3.5 }, loop: true });
|
||||
hum.pitch = 1.5; // the machine sped up
|
||||
if (!hum.playing) ctx.log("it's done");
|
||||
hum.stop();
|
||||
```
|
||||
|
||||
A looping sound out of range waits silently, and when the player comes back in range it plays on from as far into the
|
||||
file as it would have got, as if it never stopped.
|
||||
|
||||
### What Doesn't Play
|
||||
|
||||
- **Before the player's first click or key press.** Browsers keep sound off until then. A sound played earlier is
|
||||
dropped, and a loop starts then.
|
||||
- **More than 64 at once.** A new sound past that is dropped, and a loop waits for room.
|
||||
|
||||
Either way `playing` is false for a sound that never played. A file plays a moment late the first time, since it's only
|
||||
decoded once something plays it.
|
||||
|
||||
## Lasting Sounds
|
||||
|
||||
A sound that lasts as long as something does, like a machine's hum while it runs or a monster's music while it's in a
|
||||
state, goes with what the client already knows. Start it when that begins and stop it when it ends, like in an entity's
|
||||
animator when its [client data](../entities/index.md) changes:
|
||||
|
||||
```ts
|
||||
ctx.render.register_entity_animator("copper_tools:walker", (walker) => {
|
||||
let theme: PlayingSound | undefined;
|
||||
return {
|
||||
on_data(field) {
|
||||
if (field !== "angry") return;
|
||||
if (walker.get_client_data<boolean>("angry")) {
|
||||
theme ??= ctx.sounds.play("copper_tools:walker/theme", { follow: walker.id, loop: true });
|
||||
} else {
|
||||
theme?.stop();
|
||||
theme = undefined;
|
||||
}
|
||||
},
|
||||
pose(pose, time) {/* ... */},
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
A player who comes near after it started hears it too, since the animator gets the data when it's made.
|
||||
Reference in New Issue
Block a user