Offset on sounds api

This commit is contained in:
2026-11-22 08:35:54 -03:00
parent d18fe19027
commit 5d374091cd
5 changed files with 58 additions and 4 deletions
+6 -1
View File
@@ -134,6 +134,11 @@ class WebVoice implements Voice {
this.ended = true;
return;
}
// played with an offset past its end, it would have finished already
if (!settings.loop && settings.offset >= buffer.duration) {
this.stop();
return;
}
const source = new AudioBufferSourceNode(context, {
buffer,
loop: settings.loop,
@@ -144,7 +149,7 @@ class WebVoice implements Voice {
this.ended = true;
this.#disconnect();
};
source.start(0, settings.loop ? settings.offset % buffer.duration : 0);
source.start(0, settings.offset % buffer.duration);
this.#source = source;
});
}
+7 -2
View File
@@ -40,7 +40,8 @@ export interface VoiceSettings {
volume: number;
pitch: number;
loop: boolean;
// seconds into the file it starts at, for a loop coming back partway. past the file's length it goes around
// seconds into the file it starts at, for one played with an offset or a loop coming back partway. past the file's
// length a loop goes around, and anything else has finished
offset: number;
}
@@ -157,12 +158,16 @@ export class Sounds {
play(sound: string, options: ClientPlayOptions = {}): PlayingSound {
const json = sound_to_play(sound, options);
const offset = options.offset ?? 0;
if (typeof offset !== "number" || !(offset >= 0) || !Number.isFinite(offset)) {
throw new Error("a sound's offset must be 0 or more seconds");
}
const path = json.files[Math.floor(Math.random() * json.files.length)];
const file = this.#files.get(`${sound.split(":")[0]}:${path}`);
if (!file) throw new Error(`sound ${sound} has no file ${path}`);
const at = options.follow !== undefined ? this.#entity_position(options.follow) : options.at;
const playing = new Playing(json, file, { ...options }, this.clock(), at && { ...at });
const playing = new Playing(json, file, { ...options }, this.clock() - offset, at && { ...at });
this.#playing.push(playing);
// an entity this client doesn't have yet, like one whose animator is being made, has until the next frame to
// turn up
+4
View File
@@ -146,6 +146,10 @@ export interface ClientPlayOptions extends PlayOptions {
// plays again from the start until it's stopped. one out of the player's range waits silently, and comes back in
// as far into the file as it would have got
loop?: boolean;
// plays as if it had started this many seconds ago, for a sound that goes with something that's been going on a
// while, like a jester that was already winding when the player came. a sound that would have finished by now
// doesn't play. 0 when not given
offset?: number;
}
// a sound ctx.sounds.play started
+20 -1
View File
@@ -138,6 +138,7 @@ With nothing but its id it's heard as it is wherever the player is, like a click
| `loop` | Plays again from the start until stopped. |
| `volume` | Multiplies the sound's own. |
| `pitch` | Multiplies the sound's own. 2 is an octave up, and twice as fast. |
| `offset` | Plays as if it had started this many seconds ago. One that would have finished by now doesn't play. |
`ctx.sounds.exists(id)` checks a sound is there before playing it. `play` returns the sound playing, which can be
stopped or changed while it plays:
@@ -185,4 +186,22 @@ ctx.render.register_entity_animator("copper_tools:walker", (walker) => {
});
```
A player who comes near after it started hears it too, since the animator gets the data when it's made.
A player who comes near after it started hears it too, since the animator gets the data when it's made. It starts from
the beginning for them though, which is fine for a hum but not for music counting down to something. For that, the
server puts when it started in the client data too, and the animator plays it with an `offset`:
```ts
// server, when it gets angry
mob.set_client_data("angry_since", ctx.world.time);
// client, in on_data. world time is in ticks
const since = walker.get_client_data<number>("angry_since") ?? ctx.world.time;
theme ??= ctx.sounds.play("copper_tools:walker/theme", {
follow: walker.id,
loop: true,
offset: (ctx.world.time - since) / 20,
});
```
Play it as a loop even when it's longer than what it goes with: a sound that isn't a loop and is played out of range is
dropped, while a loop waits for the player to come near and comes in where it would be by then.
+21
View File
@@ -175,6 +175,27 @@ Deno.test("a loop out of range waits, and comes back as far in as it would have
assertEquals(output.playing[0].settings.offset, 10);
});
Deno.test("a sound played with an offset starts as if it had started that long ago", () => {
const { client, output, time } = client_sounds([{ id: "noisy:theme", range: 10 }]);
client.frame(at(0), () => undefined, VOLUMES);
time.now = 100;
client.play("noisy:theme", { offset: 12 });
// its file at twice the speed is twice as far in
client.play("noisy:theme", { offset: 12, pitch: 2 });
assertEquals(output.voices.map((voice) => voice.settings.offset), [12, 24]);
// a loop coming back in range keeps counting from when it would have started
output.voices.length = 0;
client.play("noisy:theme", { at: { x: 0, y: 0, z: 0 }, loop: true, offset: 30 });
time.now = 103;
client.frame(at(50), () => undefined, VOLUMES);
time.now = 105;
client.frame(at(1), () => undefined, VOLUMES);
assertEquals(output.playing.at(-1)!.settings.offset, 35);
assertThrows(() => client.play("noisy:theme", { offset: -1 }), Error, "offset");
});
Deno.test("a loop played before anything can play starts once it can, and a one-off is dropped", () => {
const { client, output } = client_sounds([{ id: "noisy:hum" }, { id: "noisy:thud" }]);
output.ready = false;