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