Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | 72x 72x 72x 72x 72x 72x 72x 72x 12x 12x 68x 63x 63x 63x 63x 51x 51x 51x 51x 51x 51x 51x 51x 51x 63x 63x 2x 2x 63x 52x 63x 63x 12x 12x 12x 12x 12x 12x 3x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 1x 1x | import { loadData } from "./loader.js";
/**
* Base class for audio components that load and play back sample buffers
* (e.g. disc drive noise, cassette relay clicks).
*
* Provides: gain node setup, sample loading, one-shot playback, and
* gain-based mute/unmute. Subclasses add domain-specific behaviour.
*/
export class SamplePlayer {
constructor(context, destination, volume) {
this.context = context;
this.volume = volume;
this.sounds = {};
this.gain = context.createGain();
this.gain.gain.value = volume;
this.gain.connect(destination);
// Prevent older Safari from GC-ing in-flight AudioBufferSourceNodes.
this.playing = [];
this.fades = new WeakMap();
}
/**
* Load a map of {name: path} into decoded AudioBuffers stored in this.sounds.
*/
async loadSounds(pathMap) {
const entries = Object.entries(pathMap);
const decoded = await Promise.all(
entries.map(async ([, path]) => {
const data = await loadData(path);
// Safari doesn't support the promise form of decodeAudioData.
return new Promise((resolve, reject) => {
this.context.decodeAudioData(
data.buffer,
(buf) => resolve(buf),
(err) => reject(err),
);
});
}),
);
for (let i = 0; i < entries.length; i++) {
this.sounds[entries[i][0]] = decoded[i];
}
}
/**
* Fire-and-forget: play a buffer once, return its duration.
*/
oneShot(sound) {
this.startSound(sound);
return sound.duration;
}
/**
* Start `sound` at `when` on the audio clock (now if 0), `offset` seconds in, for
* `duration` seconds (the rest of it if undefined), with `fadeSeconds` of fade at each
* end so a cut into the middle of it does not click. Returns the source, or null when
* the context is not running.
*/
startSound(sound, { when = 0, offset = 0, duration, fadeSeconds = 0 } = {}) {
if (this.context.state !== "running") return null;
const source = this.context.createBufferSource();
source.buffer = sound;
let into = this.gain;
if (fadeSeconds > 0 && duration !== undefined) {
const fade = this.context.createGain();
const start = when || this.context.currentTime;
fade.gain.setValueAtTime(0, start);
fade.gain.linearRampToValueAtTime(1, start + fadeSeconds);
fade.gain.setValueAtTime(1, start + duration - fadeSeconds);
fade.gain.linearRampToValueAtTime(0, start + duration);
fade.connect(this.gain);
this.fades.set(source, fade);
into = fade;
}
source.connect(into);
source.onended = () => {
this.playing = this.playing.filter((s) => s !== source);
if (into !== this.gain) into.disconnect();
};
if (duration === undefined) source.start(when, offset);
else source.start(when, offset, duration);
this.playing.push(source);
return source;
}
/**
* Stop a sound started with a fade at `at`, fading it out over the `fadeSeconds` before, so
* that cutting it short does not click either. A time already gone fades from now.
*/
stopSound(source, at, fadeSeconds) {
const from = Math.max(at - fadeSeconds, this.context.currentTime);
const fade = this.fades.get(source);
fade.gain.cancelScheduledValues(from);
fade.gain.setValueAtTime(1, from);
fade.gain.linearRampToValueAtTime(0, from + fadeSeconds);
source.stop(from + fadeSeconds);
}
/**
* Play a buffer, optionally looping. Returns a Promise that resolves
* with the source node (if looping) or when playback ends (if not).
*/
play(sound, loop) {
if (this.context.state !== "running") return Promise.reject();
return new Promise((resolve) => {
const source = this.context.createBufferSource();
source.loop = !!loop;
source.buffer = sound;
source.connect(this.gain);
source.onended = () => {
this.playing = this.playing.filter((s) => s !== source);
Eif (!source.loop) resolve();
};
source.start();
this.playing.push(source);
if (source.loop) resolve(source);
});
}
mute() {
this.gain.gain.value = 0;
}
unmute() {
this.gain.gain.value = this.volume;
}
}
|