Add timed ambience and calm prayer-flag motion to Tibetan Monastery
- Extend `AmbientLoop` with `from`/`fadeIn` so an ambient track can stay silent until a given second, then fade in from the start of the file; used for `wind.mp3` over the final 90 s of Monastery Above the Clouds. - Add a smoothed `breeze` and continuous `flutter` phase to the frame, and drive prayer flags off them so they sway gently instead of jittering with gusts. - Tuck monks one row higher on the back path (`BACK_Y`) so the building walls fully hide them as they turn. - Update README ambience docs for the new `from`/`fadeIn` options.
This commit is contained in:
parent
ccc2a2dc5d
commit
d494e43f82
|
|
@ -50,8 +50,9 @@ that is scaled up crisply, so all art is generated in code.
|
||||||
Audio lives in `public/audio/` and is configured per meditation in its `index.ts`:
|
Audio lives in `public/audio/` and is configured per meditation in its `index.ts`:
|
||||||
|
|
||||||
- `music: 'audio/<file>.mp3'` — background track; fades in at the start and out over the last 10 seconds.
|
- `music: 'audio/<file>.mp3'` — background track; fades in at the start and out over the last 10 seconds.
|
||||||
- `ambience: { src: 'audio/<file>.mp3', volume }` — a seamless loop (Web Audio) locked to the scene clock,
|
- `ambience: { src: 'audio/<file>.mp3', volume, from, fadeIn }` — a seamless loop (Web Audio) locked to the scene clock,
|
||||||
so visuals keyed to it stay in sync, including after pause and skip. Mediterranean Coast uses
|
so visuals keyed to it stay in sync, including after pause and skip. With `from` it stays silent until that
|
||||||
|
second and fades in over `fadeIn` (Monastery Above the Clouds uses this for `wind.mp3` over its last 90 s). Mediterranean Coast uses
|
||||||
`fx-waves.mp3`; its wave crashes are timed to the peaks listed in `mediterranean-coast/waves.ts`
|
`fx-waves.mp3`; its wave crashes are timed to the peaks listed in `mediterranean-coast/waves.ts`
|
||||||
(re-measure them if the file changes).
|
(re-measure them if the file changes).
|
||||||
|
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
|
|
@ -48,7 +48,8 @@ export class Music {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A gapless looping ambient sound (Web Audio), locked to the meditation clock: at time t it plays
|
* A gapless looping ambient sound (Web Audio), locked to the meditation clock: at time t it plays
|
||||||
* from t mod loopLength, so visuals keyed to the loop (e.g. waves) stay in sync with what is heard.
|
* from (t - from) mod loopLength, so visuals keyed to the loop (e.g. waves) stay in sync with what
|
||||||
|
* is heard. Before `from` it is silent.
|
||||||
*/
|
*/
|
||||||
export class AmbientLoop {
|
export class AmbientLoop {
|
||||||
private ctx?: AudioContext;
|
private ctx?: AudioContext;
|
||||||
|
|
@ -64,8 +65,14 @@ export class AmbientLoop {
|
||||||
private readonly volume = 0.55,
|
private readonly volume = 0.55,
|
||||||
private readonly fadeIn = 4,
|
private readonly fadeIn = 4,
|
||||||
private readonly fadeOut = 10,
|
private readonly fadeOut = 10,
|
||||||
|
private readonly from = 0,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
/** Position in the file at meditation time t. */
|
||||||
|
private offsetAt(t: number, len: number): number {
|
||||||
|
return (((t - this.from) % len) + len) % len;
|
||||||
|
}
|
||||||
|
|
||||||
/** Must first be called from a user gesture so the AudioContext may start. */
|
/** Must first be called from a user gesture so the AudioContext may start. */
|
||||||
start(t: number): void {
|
start(t: number): void {
|
||||||
if (!this.src) return;
|
if (!this.src) return;
|
||||||
|
|
@ -96,12 +103,12 @@ export class AmbientLoop {
|
||||||
update(t: number, duration: number): void {
|
update(t: number, duration: number): void {
|
||||||
this.lastT = t;
|
this.lastT = t;
|
||||||
if (!this.ctx || !this.master) return;
|
if (!this.ctx || !this.master) return;
|
||||||
const k = Math.max(0, Math.min(1, t / this.fadeIn, (duration - t) / this.fadeOut));
|
const k = Math.max(0, Math.min(1, (t - this.from) / this.fadeIn, (duration - t) / this.fadeOut));
|
||||||
this.master.gain.setTargetAtTime(k * this.volume, this.ctx.currentTime, 0.1);
|
this.master.gain.setTargetAtTime(k * this.volume, this.ctx.currentTime, 0.1);
|
||||||
if (!this.playing || !this.source || !this.buffer) return;
|
if (!this.playing || !this.source || !this.buffer) return;
|
||||||
const len = this.buffer.duration;
|
const len = this.buffer.duration;
|
||||||
const heard = (this.source.offset + this.ctx.currentTime - this.source.startedAt) % len;
|
const heard = (this.source.offset + this.ctx.currentTime - this.source.startedAt) % len;
|
||||||
let drift = Math.abs(heard - (t % len));
|
let drift = Math.abs(heard - this.offsetAt(t, len));
|
||||||
drift = Math.min(drift, len - drift);
|
drift = Math.min(drift, len - drift);
|
||||||
if (drift > 0.08) this.restart(t);
|
if (drift > 0.08) this.restart(t);
|
||||||
}
|
}
|
||||||
|
|
@ -138,7 +145,7 @@ export class AmbientLoop {
|
||||||
node.buffer = this.buffer;
|
node.buffer = this.buffer;
|
||||||
node.loop = true;
|
node.loop = true;
|
||||||
node.connect(gain);
|
node.connect(gain);
|
||||||
const offset = t % this.buffer.duration;
|
const offset = this.offsetAt(t, this.buffer.duration);
|
||||||
node.start(now, offset);
|
node.start(now, offset);
|
||||||
this.source = { node, gain, offset, startedAt: now };
|
this.source = { node, gain, offset, startedAt: now };
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,15 +11,15 @@ const POLE_WOOD = hex('#5a4030');
|
||||||
const POLE_DARK = hex('#3a281e');
|
const POLE_DARK = hex('#3a281e');
|
||||||
const GOLD = hex('#e0b040');
|
const GOLD = hex('#e0b040');
|
||||||
|
|
||||||
/** How far flags stream out downwind (east → west, i.e. to the right), 0 hanging to 1 flying flat. */
|
/** How far flags stream out downwind (to the right): mostly hanging, lifting a little as the breeze builds. */
|
||||||
function liftOf(f: Frame): number {
|
function liftOf(f: Frame): number {
|
||||||
return Math.min(1, 0.12 + f.wind * 0.72);
|
return 0.15 + f.breeze * 0.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
function linePoint(l: FlagLine, f: Frame, li: number, u: number): [number, number] {
|
function linePoint(l: FlagLine, f: Frame, li: number, u: number): [number, number] {
|
||||||
const bow = 4 * u * (1 - u);
|
const bow = 4 * u * (1 - u);
|
||||||
// Lines bow and bounce a little in the gusts.
|
// Lines bow and sway slowly in the breeze.
|
||||||
const sway = f.wind * (0.6 + 0.8 * Math.sin(f.t * (0.9 + li * 0.13) + li * 2.1)) * bow * 1.6;
|
const sway = f.breeze * (0.5 + 0.5 * Math.sin(f.t * (0.35 + li * 0.05) + li * 2.1)) * bow * 1.2;
|
||||||
return [l.ax + (l.bx - l.ax) * u, l.ay + (l.by - l.ay) * u + l.sag * bow + sway];
|
return [l.ax + (l.bx - l.ax) * u, l.ay + (l.by - l.ay) * u + l.sag * bow + sway];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,8 +32,7 @@ function drawLine(s: Surface, f: Frame, l: FlagLine, li: number): void {
|
||||||
s.blend(x, y, string, l.near ? 1 : 0.8);
|
s.blend(x, y, string, l.near ? 1 : 0.8);
|
||||||
}
|
}
|
||||||
const lift = liftOf(f);
|
const lift = liftOf(f);
|
||||||
const freq = 3 + f.wind * 7;
|
const amp = 0.3 + f.breeze * 0.8;
|
||||||
const amp = 0.3 + f.wind * 1.4;
|
|
||||||
const shade = l.near ? 0.78 : 1;
|
const shade = l.near ? 0.78 : 1;
|
||||||
const n = Math.floor(len / l.gap);
|
const n = Math.floor(len / l.gap);
|
||||||
for (let j = 1; j < n; j++) {
|
for (let j = 1; j < n; j++) {
|
||||||
|
|
@ -47,7 +46,7 @@ function drawLine(s: Surface, f: Frame, l: FlagLine, li: number): void {
|
||||||
const phase = j * 1.37 + li * 0.7;
|
const phase = j * 1.37 + li * 0.7;
|
||||||
for (let r = 0; r < rows; r++) {
|
for (let r = 0; r < rows; r++) {
|
||||||
const k = (r + 1) / rows;
|
const k = (r + 1) / rows;
|
||||||
const wave = Math.sin(f.t * freq + phase - r * 0.9);
|
const wave = Math.sin(f.flutter + phase - r * 0.7);
|
||||||
const off = k * (lift * l.fh * 0.95 + amp * wave);
|
const off = k * (lift * l.fh * 0.95 + amp * wave);
|
||||||
const fx = Math.round(x + off);
|
const fx = Math.round(x + off);
|
||||||
const fy = Math.round(y) + 1 + r;
|
const fy = Math.round(y) + 1 + r;
|
||||||
|
|
@ -82,12 +81,11 @@ function drawPole(s: Surface, f: Frame): void {
|
||||||
const len = 6 + Math.round(lift * 4);
|
const len = 6 + Math.round(lift * 4);
|
||||||
const top = POLE.top + 3;
|
const top = POLE.top + 3;
|
||||||
const height = 25;
|
const height = 25;
|
||||||
const freq = 2.4 + f.wind * 5;
|
|
||||||
for (let c = 0; c < len; c++) {
|
for (let c = 0; c < len; c++) {
|
||||||
const k = (c + 1) / len;
|
const k = (c + 1) / len;
|
||||||
const droop = (1 - lift) * c * 1.3;
|
const droop = (1 - lift) * c * 1.3;
|
||||||
const wave = Math.sin(f.t * freq - c * 0.8) * (0.4 + f.wind * 1.2) * k;
|
const wave = Math.sin(f.flutter * 0.9 - c * 0.6) * (0.3 + f.breeze * 0.8) * k;
|
||||||
const shade = Math.cos(f.t * freq - c * 0.8) < -0.4;
|
const shade = Math.cos(f.flutter * 0.9 - c * 0.6) < -0.4;
|
||||||
for (let r = 0; r < height - Math.round(c * 0.4); r++) {
|
for (let r = 0; r < height - Math.round(c * 0.4); r++) {
|
||||||
const col = sunlit(f, COLORS[Math.floor(r / 5) % 5], 0.35);
|
const col = sunlit(f, COLORS[Math.floor(r / 5) % 5], 0.35);
|
||||||
s.set(POLE.x + 2 + c, Math.round(top + r + droop + wave), shade ? scale(col, 0.75) : col);
|
s.set(POLE.x + 2 + c, Math.round(top + r + droop + wave), shade ? scale(col, 0.75) : col);
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,8 @@ export const tibetanMonastery: Meditation = {
|
||||||
description:
|
description:
|
||||||
'Sit with an old monastery on a Himalayan summit as a clear afternoon turns to cloud. Monks walk their circuit past the prayer wheels, a lammergeier circles and a snow leopard pauses on the rocks. Then the mist rises, the snow begins and the butter lamps are lit, one window at a time.',
|
'Sit with an old monastery on a Himalayan summit as a clear afternoon turns to cloud. Monks walk their circuit past the prayer wheels, a lammergeier circles and a snow leopard pauses on the rocks. Then the mist rises, the snow begins and the butter lamps are lit, one window at a time.',
|
||||||
music: 'audio/tibetan-monestary.mp3',
|
music: 'audio/tibetan-monestary.mp3',
|
||||||
|
// The 90 s wind track rises with the storm and plays through to the end.
|
||||||
|
ambience: { src: 'audio/wind.mp3', volume: 0.5, from: DURATION - 90, fadeIn: 8 },
|
||||||
duration: DURATION,
|
duration: DURATION,
|
||||||
previewTime: 252,
|
previewTime: 252,
|
||||||
createScene: () => scene,
|
createScene: () => scene,
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@ import { GATE } from './world';
|
||||||
const EAST = 232;
|
const EAST = 232;
|
||||||
const WEST = 66;
|
const WEST = 66;
|
||||||
const FRONT_Y = 147;
|
const FRONT_Y = 147;
|
||||||
const BACK_Y = 142;
|
/** One row above the building's base, so the walls hide the monks completely. */
|
||||||
|
const BACK_Y = 140;
|
||||||
const RUN = EAST - WEST;
|
const RUN = EAST - WEST;
|
||||||
const TURN = 8;
|
const TURN = 8;
|
||||||
const LAP = 2 * (RUN + TURN);
|
const LAP = 2 * (RUN + TURN);
|
||||||
|
|
@ -40,11 +41,11 @@ function poseAt(s: number): Pose {
|
||||||
if (u < RUN) return { x: EAST - u, y: FRONT_Y, facing: -1, behind: false, entering: 0 };
|
if (u < RUN) return { x: EAST - u, y: FRONT_Y, facing: -1, behind: false, entering: 0 };
|
||||||
if (u < RUN + TURN) {
|
if (u < RUN + TURN) {
|
||||||
const k = (u - RUN) / TURN;
|
const k = (u - RUN) / TURN;
|
||||||
return { x: WEST - 4 * Math.sin(Math.PI * k), y: FRONT_Y - 5 * k, facing: k < 0.5 ? -1 : 1, behind: k > 0.5, entering: 0 };
|
return { x: WEST - 4 * Math.sin(Math.PI * k), y: FRONT_Y - (FRONT_Y - BACK_Y) * k, facing: k < 0.5 ? -1 : 1, behind: k > 0.5, entering: 0 };
|
||||||
}
|
}
|
||||||
if (u < 2 * RUN + TURN) return { x: WEST + (u - RUN - TURN), y: BACK_Y, facing: 1, behind: true, entering: 0 };
|
if (u < 2 * RUN + TURN) return { x: WEST + (u - RUN - TURN), y: BACK_Y, facing: 1, behind: true, entering: 0 };
|
||||||
const k = (u - 2 * RUN - TURN) / TURN;
|
const k = (u - 2 * RUN - TURN) / TURN;
|
||||||
return { x: EAST + 4 * Math.sin(Math.PI * k), y: BACK_Y + 5 * k, facing: k < 0.5 ? 1 : -1, behind: k < 0.5, entering: 0 };
|
return { x: EAST + 4 * Math.sin(Math.PI * k), y: BACK_Y + (FRONT_Y - BACK_Y) * k, facing: k < 0.5 ? 1 : -1, behind: k < 0.5, entering: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
function monkS(t: number, i: number): number {
|
function monkS(t: number, i: number): number {
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,11 @@ export function windAt(t: number): number {
|
||||||
return windBase(t) * (0.72 + 0.56 * g);
|
return windBase(t) * (0.72 + 0.56 * g);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The steady part of the wind without the quick gusts, for things that should move calmly. */
|
||||||
|
function breezeAt(t: number): number {
|
||||||
|
return windBase(t) * (0.9 + 0.2 * noise1(t * 0.12, 5));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How far the air has moved since t = 0, in pixels at the snow's depth. Integrated from a table so
|
* How far the air has moved since t = 0, in pixels at the snow's depth. Integrated from a table so
|
||||||
* that particles riding the wind never jump when it changes, while staying a pure function of t.
|
* that particles riding the wind never jump when it changes, while staying a pure function of t.
|
||||||
|
|
@ -149,6 +154,10 @@ export interface Frame {
|
||||||
/** Mist streaming over the summit, 0–1. */
|
/** Mist streaming over the summit, 0–1. */
|
||||||
mist: number;
|
mist: number;
|
||||||
wind: number;
|
wind: number;
|
||||||
|
/** Smoothed wind for the prayer flags, 0–~1. */
|
||||||
|
breeze: number;
|
||||||
|
/** Flag ripple phase in radians: advances a little faster as the wind rises, but never jumps. */
|
||||||
|
flutter: number;
|
||||||
/** Total wind travel in pixels (see driftAt). */
|
/** Total wind travel in pixels (see driftAt). */
|
||||||
drift: number;
|
drift: number;
|
||||||
/** Falling snow density, 0–1. */
|
/** Falling snow density, 0–1. */
|
||||||
|
|
@ -219,6 +228,8 @@ export function frameAt(t: number): Frame {
|
||||||
fog: FOG(t),
|
fog: FOG(t),
|
||||||
mist: mist(t),
|
mist: mist(t),
|
||||||
wind: windAt(t),
|
wind: windAt(t),
|
||||||
|
breeze: breezeAt(t),
|
||||||
|
flutter: t * 1.1 + driftAt(t) * 0.035,
|
||||||
drift: driftAt(t),
|
drift: driftAt(t),
|
||||||
snow: snow(t),
|
snow: snow(t),
|
||||||
snowCover: snowCover(t),
|
snowCover: snowCover(t),
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,11 @@ export interface Meditation {
|
||||||
previewTime: number;
|
previewTime: number;
|
||||||
/** Optional music file, e.g. 'audio/northern-cabin.mp3' (placed in /public). */
|
/** Optional music file, e.g. 'audio/northern-cabin.mp3' (placed in /public). */
|
||||||
music?: string;
|
music?: string;
|
||||||
/** Optional ambient sound that loops seamlessly for the whole meditation, kept in step with the scene clock. */
|
/**
|
||||||
ambience?: { src: string; volume?: number };
|
* Optional ambient sound that loops seamlessly, kept in step with the scene clock. By default it
|
||||||
|
* plays for the whole meditation; with `from` it stays silent until then and fades in over `fadeIn`
|
||||||
|
* seconds, starting from the beginning of the file.
|
||||||
|
*/
|
||||||
|
ambience?: { src: string; volume?: number; from?: number; fadeIn?: number };
|
||||||
createScene(): Scene;
|
createScene(): Scene;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,8 @@ class Soundtrack {
|
||||||
|
|
||||||
constructor(m: Meditation, enabled: boolean) {
|
constructor(m: Meditation, enabled: boolean) {
|
||||||
this.music = new Music(enabled ? m.music : undefined, 0.75);
|
this.music = new Music(enabled ? m.music : undefined, 0.75);
|
||||||
this.ambience = new AmbientLoop(enabled ? m.ambience?.src : undefined, m.ambience?.volume);
|
const a = m.ambience;
|
||||||
|
this.ambience = new AmbientLoop(enabled ? a?.src : undefined, a?.volume, a?.fadeIn, undefined, a?.from);
|
||||||
}
|
}
|
||||||
|
|
||||||
play(t: number): void {
|
play(t: number): void {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue