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 130 131 | 25x 25x 25x 8x 8x 8x 8x 8x 8x 8x 18x 18x 18x 18x 51x 51x 51x 51x 108x 108x 108x 51x 51x 5x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 5x 5x 5x 5x 20x 20x 520x 355x 355x 32x 2x 3x 3x 25x 25x 5x 5x 7x 5x | import { AUDIO_BAND_COUNT, bandLevel } from '@be-music/player-skin-sdk';
/**
* Synesthesia's floor as a spectrum: the music's spectrum is written across the screen (bass on the left, highs on the
* right) and pushed out from the viewer into the distance, so the ground right in front of the camera shows what is
* playing now and the far floor what played a moment ago.
*/
/** Spectrum snapshots kept per second of history. */
export const SPECTRUM_HISTORY_HZ = 60;
/** How fast (world units per second) the spectrum travels away from the viewer across the floor. */
export const SPECTRUM_WAVE_SPEED = 900;
/** Tallest a ridge rises (world units) when its band is at full level. */
export const SPECTRUM_RIDGE_HEIGHT = 120;
/** A ring of the latest spectrum snapshots, sampled at {@link SPECTRUM_HISTORY_HZ}. */
export class SpectrumHistory {
public readonly bands: number;
public readonly capacity: number;
private readonly frames: Float32Array;
/** Slot of the newest snapshot. */
private head = 0;
/** How many slots hold a snapshot so far. */
private filled = 0;
/** Fraction of a tick carried over between pushes. */
private carry = 0;
/** Ticks in a row written with every band at zero. */
private quietTicks = 0;
public constructor(bands: number = AUDIO_BAND_COUNT, capacity: number = 4 * SPECTRUM_HISTORY_HZ) {
this.bands = bands;
this.capacity = capacity;
this.frames = new Float32Array(bands * capacity);
}
/** Advances the history by `dt` seconds, writing `levels` (one 0..1 value per band) into every tick that passed. */
public push(levels: ArrayLike<number>, dt: number): void {
this.carry += Math.max(0, dt) * SPECTRUM_HISTORY_HZ;
const ticks = Math.min(this.capacity, Math.floor(this.carry));
this.carry -= Math.floor(this.carry);
for (let tick = 0; tick < ticks; tick += 1) {
this.head = (this.head + 1) % this.capacity;
const base = this.head * this.bands;
let loud = false;
for (let band = 0; band < this.bands; band += 1) {
const level = levels[band] ?? 0;
this.frames[base + band] = level;
if (level !== 0) loud = true;
}
this.quietTicks = loud ? 0 : this.quietTicks + 1;
this.filled = Math.min(this.capacity, this.filled + 1);
}
}
/**
* Whether every snapshot held is silent, so every {@link sample} is 0: skins with their effects off (a silent audio
* drive) skip sampling the floor's thousands of points.
*/
public get silent(): boolean {
return this.quietTicks >= this.filled;
}
/**
* The level at `position` across the spectrum (0 = lowest band, 1 = highest, linearly blended between bands) as it was
* `ageSeconds` ago, blended between snapshots. 0 before the history began or past its end.
*/
public sample(position: number, ageSeconds: number): number {
const age = Math.max(0, ageSeconds) * SPECTRUM_HISTORY_HZ;
const older = Math.floor(age);
const blend = age - older;
const band = Math.max(0, Math.min(1, position)) * (this.bands - 1);
const low = Math.floor(band);
const high = Math.min(this.bands - 1, low + 1);
const across = band - low;
const newer = this.levelAt(older, low) + (this.levelAt(older, high) - this.levelAt(older, low)) * across;
const elder =
this.levelAt(older + 1, low) + (this.levelAt(older + 1, high) - this.levelAt(older + 1, low)) * across;
return newer + (elder - newer) * blend;
}
/** Sample all bands at one depth once for a floor row, before interpolating across the screen. */
public sampleRow(ageSeconds: number, levels: Float64Array): void {
const age = Math.max(0, ageSeconds) * SPECTRUM_HISTORY_HZ;
const older = Math.floor(age);
const blend = age - older;
for (let band = 0; band < this.bands; band++) {
const recent = this.levelAt(older, band);
levels[band] = recent + (this.levelAt(older + 1, band) - recent) * blend;
}
}
private levelAt(age: number, band: number): number {
if (age >= this.filled) return 0;
const slot = (this.head - age + this.capacity) % this.capacity;
return this.frames[slot * this.bands + band]!;
}
}
/** Writes each band's display level ({@link bandLevel}) of `bands` into `out` and returns it. */
export function spectrumLevels(bands: readonly number[], out: Float32Array): Float32Array {
for (let band = 0; band < out.length; band += 1) out[band] = bandLevel(bands, band, out.length);
return out;
}
/**
* How high (world units) the floor rises at `position` across the spectrum (0 = bass at the screen's left edge, 1 =
* highs at its right) and depth `z`: that band as it was when the wave now at `z` left the viewer. The level is eased in
* so quiet bands lie flat and loud ones stand up as ridges.
*/
export function spectrumRise(history: SpectrumHistory, position: number, z: number): number {
const level = history.sample(position, Math.max(0, z) / SPECTRUM_WAVE_SPEED);
return SPECTRUM_RIDGE_HEIGHT * level ** 1.5;
}
/** Widest gap (world units) between a floor row's points, used for the far rows. */
const ROW_STEP_MAX = 50;
/** Finest the floor rows get: {@link ROW_STEP_MAX} halved this many times. */
const ROW_STEP_HALVINGS = 3;
/**
* Gap (world units) between the points of a floor row at depth `z`, so they land about `screenGap` px apart on screen
* (`focal` is the projection's focal length) and near rows read as continuous waveforms. Steps are {@link ROW_STEP_MAX}
* halved a whole number of times, so a row moving closer only gains points in between — it never shifts the ones it has.
*/
export function floorRowStep(z: number, focal: number, screenGap: number): number {
const ideal = (screenGap * (focal + Math.max(0, z))) / focal;
let step = ROW_STEP_MAX;
for (let halving = 0; halving < ROW_STEP_HALVINGS && step / 2 >= ideal; halving += 1) step /= 2;
return step;
}
|