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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | 8x 47x 33x 10x 24x 10x 24x 26x 24x 11x 11x 11x 11x 11x 11x 11x 11x 9x 9x 9x 9x 1x 1x 8x 8x 8x 16x 8x 6x 6x 6x 6x 9x 6x 6x 6x 9x 9x 6x 9x 9x 9x 9x 6x 6x 6x 6x 6x 14x 7x 7x 7x 16x 17x 16x 16x 17x 17x 17x 17x 17x 16x 8x 7x 7x 7x 15x 15x 10x 10x 10x 10x 10x 13x 13x 1x 1x 12x 11x 11x 8x 5x 3x 3x 3x 3x 4x 3x | // The keep-pitch samples of a chart's bar auditions (`ChartPreviewEngine.playWindow`), stretched to the play tempo:
// the bar picked first, and in the background what every other bar sounds, nearest the bar picked first.
import { stretchAudioBuffers, type TimeStretchAlgorithm } from '../runtime/time-stretch.ts';
/** The tempo and stretcher a chart's samples are stretched for. */
export interface PreviewStretchSpec {
/** The play tempo (`ChartPreviewWindow.tempo`): chart seconds pass this many times as fast. */
readonly rate: number;
readonly algorithm: TimeStretchAlgorithm;
}
/** A part of a sample, in its own seconds: `[startSeconds, endSeconds)`. */
export interface PreviewStretchSegment {
readonly startSeconds: number;
readonly endSeconds: number;
}
/** A bar a picker can audition, in chart seconds: `[startSeconds, endSeconds)`. */
export interface PreviewStretchBar {
readonly startSeconds: number;
readonly endSeconds: number;
}
/** A sample a bar audition sounds stretched: the whole of it, or (`segment`) the part the bar sounds. */
export interface PreviewStretchRequest {
readonly path: string;
readonly source: AudioBuffer;
readonly segment?: PreviewStretchSegment;
}
/** A stretched sample, and where in the source sample it starts (0 for the whole sample). */
export interface PreviewStretchResult {
readonly buffer: AudioBuffer;
readonly segmentStart: number;
}
/** What the store keeps for one tempo and stretcher. */
interface StretchState {
readonly spec: PreviewStretchSpec;
/** Stretched or being stretched, by {@link requestKey}. */
readonly stretched: Map<string, Promise<AudioBuffer | undefined>>;
/** Whole samples stretched, by path: any part of them is taken from there. */
readonly whole: Map<string, AudioBuffer>;
/** The bars the background has done. */
readonly doneBars: Set<string>;
/** The bars the background still does, nearest the last bar picked first. */
queue: PreviewStretchBar[];
/** The background runs while true: a picker is open on the chart. */
active: boolean;
}
/** Whether two specs stretch alike. */
function sameSpec(left: PreviewStretchSpec, right: PreviewStretchSpec): boolean {
return left.rate === right.rate && left.algorithm === right.algorithm;
}
/** The key a request is kept under: its path, and its part for one of a long sample. */
function requestKey(request: Pick<PreviewStretchRequest, 'path' | 'segment'>): string {
return request.segment === undefined
? request.path
: `${request.path}\0${request.segment.startSeconds}-${request.segment.endSeconds}`;
}
function barKey(bar: PreviewStretchBar): string {
return `${bar.startSeconds}-${bar.endSeconds}`;
}
/**
* Pure helper: `bars` ordered for the background, nearest `aroundSeconds` first (by the distance from it to the bar,
* 0 for the bar it lies in), ties in the order given.
*/
export function orderPreviewStretchBars<Bar extends PreviewStretchBar>(
bars: readonly Bar[],
aroundSeconds: number,
): Bar[] {
const distance = (bar: PreviewStretchBar): number =>
aroundSeconds < bar.startSeconds
? bar.startSeconds - aroundSeconds
: aroundSeconds >= bar.endSeconds
? aroundSeconds - bar.endSeconds
: 0;
return bars
.map((bar, index) => ({ bar, index, distance: distance(bar) }))
.sort((left, right) => left.distance - right.distance || left.index - right.index)
.map(({ bar }) => bar);
}
/**
* The stretched samples of one chart's bar auditions. A bar picked has its samples stretched first, together. While
* no bar waits, the background stretches what every other bar sounds, one sample at a time and nearest the last bar
* picked first, so the bars after find theirs ready; each job is a bar's part of one sample, so a bar picked waits for
* one such job at most. A new tempo or stretcher drops what was stretched and starts again.
*/
export class PreviewStretchStore {
private readonly context: BaseAudioContext;
private readonly requestsFor: (bar: PreviewStretchBar, spec: PreviewStretchSpec) => Promise<PreviewStretchRequest[]>;
private readonly yieldToPage: () => Promise<void>;
private state: StretchState | undefined;
private bars: readonly PreviewStretchBar[] = [];
private aroundSeconds = 0;
private foreground = 0;
private resumeBackground: (() => void) | undefined;
private running = false;
private disposed = false;
/**
* @param requestsFor What a bar sounds stretched at `spec` (its samples decoded).
* @param yieldToPage Waits before the next background job, so the page's own work runs between them.
*/
public constructor(
context: BaseAudioContext,
requestsFor: (bar: PreviewStretchBar, spec: PreviewStretchSpec) => Promise<PreviewStretchRequest[]>,
yieldToPage: () => Promise<void> = () => new Promise((resolve) => setTimeout(resolve, 0)),
) {
this.context = context;
this.requestsFor = requestsFor;
this.yieldToPage = yieldToPage;
}
/**
* Stretches the bars for `spec` in the background from now on, nearest `aroundSeconds` first: a picker opened on the
* chart, a tempo changed in one, or a bar picked. `bars` replaces the bars to do; absent keeps the last. A spec other
* than the last drops what was stretched for it. `undefined` stops the background and keeps what was stretched.
*/
public prepare(
spec: PreviewStretchSpec | undefined,
bars?: readonly PreviewStretchBar[],
aroundSeconds: number = this.aroundSeconds,
): void {
Iif (this.disposed) return;
if (bars !== undefined) this.bars = bars;
this.aroundSeconds = aroundSeconds;
if (spec === undefined) {
Eif (this.state !== undefined) this.state.active = false;
return;
}
const state = this.stateFor(spec);
state.active = true;
state.queue = orderPreviewStretchBars(
this.bars.filter((bar) => !state.doneBars.has(barKey(bar))),
aroundSeconds,
);
void this.runBackground();
}
/**
* The samples a bar sounds, stretched for `spec`, in the order asked for: a part of a sample whose whole is
* stretched comes from there; what is not stretched yet goes to the stretcher together, ahead of the background,
* which waits meanwhile. `undefined` for a sample the stretcher cannot take.
*/
public async stretch(
spec: PreviewStretchSpec,
requests: readonly PreviewStretchRequest[],
): Promise<(PreviewStretchResult | undefined)[]> {
const state = this.stateFor(spec);
this.foreground += 1;
try {
const pending = requests.filter(
(request) => !state.whole.has(request.path) && !state.stretched.has(requestKey(request)),
);
// Duplicates of one key go once.
const unique = [...new Map(pending.map((request) => [requestKey(request), request])).values()];
if (unique.length > 0) void this.startStretch(state, unique);
const buffers = await Promise.all(
requests.map((request) => {
const whole = state.whole.get(request.path);
return whole !== undefined ? Promise.resolve(whole) : state.stretched.get(requestKey(request))!;
}),
);
return buffers.map((buffer, index) => {
Iif (buffer === undefined) return undefined;
const request = requests[index]!;
const whole = state.whole.get(request.path) === buffer;
return { buffer, segmentStart: whole ? 0 : (request.segment?.startSeconds ?? 0) };
});
} finally {
this.foreground -= 1;
Eif (this.foreground === 0) {
const resume = this.resumeBackground;
this.resumeBackground = undefined;
resume?.();
}
}
}
/** Stops the background for good. */
public dispose(): void {
this.disposed = true;
if (this.state !== undefined) this.state.active = false;
this.resumeBackground?.();
this.resumeBackground = undefined;
}
/** The state for `spec`: the current one, or a fresh one in its place. */
private stateFor(spec: PreviewStretchSpec): StretchState {
if (this.state !== undefined && sameSpec(this.state.spec, spec)) return this.state;
if (this.state !== undefined) this.state.active = false;
this.state = { spec, stretched: new Map(), whole: new Map(), doneBars: new Set(), queue: [], active: false };
return this.state;
}
/** Sends `requests` to the stretcher in one call and keeps each result under its key. */
private startStretch(state: StretchState, requests: readonly PreviewStretchRequest[]): Promise<unknown> {
const parts = requests.map(({ source, segment }) =>
segment === undefined ? source : sliceAudioBuffer(this.context, source, segment),
);
const batch: Promise<(AudioBuffer | undefined)[]> = this.disposed
? Promise.resolve(parts.map(() => undefined))
: stretchAudioBuffers(this.context, parts, state.spec.rate, state.spec.algorithm).catch(
// Samples the stretcher cannot take play at the tempo, their pitch following.
() => parts.map(() => undefined),
);
requests.forEach((request, index) => {
const promise = batch.then((stretched) => {
const buffer = stretched[index];
if (buffer !== undefined && request.segment === undefined) state.whole.set(request.path, buffer);
return buffer;
});
state.stretched.set(requestKey(request), promise);
});
return batch;
}
/** Does the queued bars, a sample at a time, while no bar waits and the state stays current. */
private async runBackground(): Promise<void> {
if (this.running) return;
this.running = true;
try {
for (;;) {
const state = this.state;
if (this.disposed || state === undefined || !state.active || state.queue.length === 0) return;
const bar = state.queue.shift()!;
Iif (state.doneBars.has(barKey(bar))) continue;
const requests = await this.requestsFor(bar, state.spec);
let finished = true;
for (const request of requests) {
Iif (this.foreground > 0)
await new Promise<void>((resolve) => {
this.resumeBackground = resolve;
});
if (this.disposed || this.state !== state || !state.active) {
finished = false;
break;
}
if (state.whole.has(request.path) || state.stretched.has(requestKey(request))) continue;
await this.startStretch(state, [request]);
await this.yieldToPage();
}
if (finished) state.doneBars.add(barKey(bar));
}
} finally {
this.running = false;
}
}
}
/** The part `segment` of `buffer`, in its own seconds, as a buffer of its own. */
export function sliceAudioBuffer(
context: BaseAudioContext,
buffer: AudioBuffer,
segment: PreviewStretchSegment,
): AudioBuffer {
const from = Math.max(0, Math.floor(segment.startSeconds * buffer.sampleRate));
const to = Math.min(buffer.length, Math.max(from + 1, Math.ceil(segment.endSeconds * buffer.sampleRate)));
const part = context.createBuffer(buffer.numberOfChannels, to - from, buffer.sampleRate);
for (let channel = 0; channel < buffer.numberOfChannels; channel += 1)
part.copyToChannel(buffer.getChannelData(channel).subarray(from, to), channel);
return part;
}
|