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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | 25x 29x 29x 29x 29x 29x 29x 134965x 134965x 134965x 134965x 29x 70x 70x 50x 19x 10x 9x 9x 70x 70x 107x 90x 17x 17x 17x 17x 963348x 483x 963348x 963348x 963348x 963348x 963348x 17x 9x 9x 4x 5x 5x 50x 50x 50x 50x 50x 50x 101x 101x 101x 101x 101x 50x 101x 50x 50x 101x 50x 1x 49x 49x 86x 49x 4843857x 2386x 4843857x 9008561x 9008561x 48x 5x 5x 5x 10x 10x 10x 10x 4x 4x 4x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 9008561x 3x 9008558x 1x 9008557x 5x 9008548x 2x 2x 2x 2x 2x 1x 2x 2x 69x 69x 69x 9x 9x 9x 9x 9x 19x 19x 19x 10x 9x 9x 9x 9x 348x 348x 1373x 348x 9x 9x 9x 9x 27705x 27705x 27795x 27701x 27701x 27705x 4x 5x 10x 10x 10x 10x 10x 10x 10x 10x | import { OggVorbisDecoder } from '@wasm-audio-decoders/ogg-vorbis';
import { clampSignedUnit, extname, isAbortError, throwIfAborted } from '@be-music/shared-utils/core';
import { MPEGDecoder } from 'mpg123-decoder';
import { OggOpusDecoder } from 'ogg-opus-decoder';
export interface DecodedAudio {
sampleRate: number;
left: Float32Array;
right?: Float32Array;
}
interface DecoderOutput {
sampleRate: number;
channelData?: readonly Float32Array[];
}
interface ManagedDecoder {
ready: Promise<unknown>;
free(): void;
}
const MPG123_SUPPRESSED_LOG_PATTERNS = [
/\bcoreaudio\.c:\d+\]\s*warning:\s*didn't have any audio data in callback \(buffer underflow\)/i,
];
export function createFallbackTone(
sampleKey: string,
sampleRate: number,
seconds: number,
): {
left: Float32Array;
right: Float32Array;
} {
const frameLength = Math.max(1, Math.round(sampleRate * seconds));
const left = new Float32Array(frameLength);
const right = new Float32Array(frameLength);
const seed = Number.parseInt(sampleKey, 36);
const frequency = 220 + ((Number.isFinite(seed) ? seed : 1) % 36) * 18;
for (let index = 0; index < frameLength; index += 1) {
const envelope = Math.max(0, 1 - index / frameLength);
const value = Math.sin((2 * Math.PI * frequency * index) / sampleRate) * envelope * 0.3;
left[index] = value;
right[index] = value;
}
return { left, right };
}
export async function decodeAudioSample(
buffer: Uint8Array,
pathHint?: string,
signal?: AbortSignal,
): Promise<DecodedAudio> {
throwIfAborted(signal);
if (isWAVBuffer(buffer)) {
return decodeWAV(buffer, signal);
}
if (isMp3Buffer(buffer)) {
return decodeMp3(buffer, signal);
}
Eif (isOggBuffer(buffer)) {
return decodeOggLike(buffer, signal);
}
const extension = pathHint ? extname(pathHint).toLowerCase() : '';
const extensionDecoded = decodeAudioSampleByExtension(buffer, extension, signal);
Iif (extensionDecoded) {
return extensionDecoded;
}
try {
return decodeWAV(buffer, signal);
} catch (error) {
if (isAbortError(error)) {
throw error;
}
try {
return decodeMp3(buffer, signal);
} catch (nestedError) {
if (isAbortError(nestedError)) {
throw nestedError;
}
return decodeOggLike(buffer, signal);
}
}
}
function decodeAudioSampleByExtension(
buffer: Uint8Array,
extension: string,
signal?: AbortSignal,
): Promise<DecodedAudio> | DecodedAudio | undefined {
if (extension === '.ogg' || extension === '.oga' || extension === '.opus') {
return decodeOggLike(buffer, signal);
}
if (extension === '.mp3') {
return decodeMp3(buffer, signal);
}
if (extension === '.wav') {
return decodeWAV(buffer, signal);
}
return undefined;
}
export function resampleLinear(
input: Float32Array,
inputRate: number,
outputRate: number,
signal?: AbortSignal,
): Float32Array {
if (inputRate === outputRate) {
return input;
}
const outputLength = Math.max(1, Math.round((input.length * outputRate) / inputRate));
const output = new Float32Array(outputLength);
const ratio = inputRate / outputRate;
for (let index = 0; index < outputLength; index += 1) {
if ((index & 0x7ff) === 0) {
throwIfAborted(signal);
}
const source = index * ratio;
const left = Math.floor(source);
const right = Math.min(left + 1, input.length - 1);
const fraction = source - left;
output[index] = input[left] * (1 - fraction) + input[right] * fraction;
}
return output;
}
async function decodeOggLike(buffer: Uint8Array, signal?: AbortSignal): Promise<DecodedAudio> {
throwIfAborted(signal);
if (isOggOpusBuffer(buffer)) {
return decodeOggOpus(buffer, signal);
}
try {
return await decodeOggVorbis(buffer, signal);
} catch (error) {
if (isAbortError(error)) {
throw error;
}
return decodeOggOpus(buffer, signal);
}
}
function decodeWAV(buffer: Uint8Array, signal?: AbortSignal): DecodedAudio {
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
Iif (readASCII(view, 0, 4) !== 'RIFF' || readASCII(view, 8, 4) !== 'WAVE') {
throw new Error('Unsupported file format. Only RIFF/WAVE is supported for samples.');
}
let offset = 12;
let format:
| {
audioFormat: number;
channels: number;
sampleRate: number;
blockAlign: number;
bitsPerSample: number;
}
| undefined;
let pcmOffset = -1;
let pcmSize = 0;
while (offset + 8 <= view.byteLength) {
throwIfAborted(signal);
const id = readASCII(view, offset, 4);
const size = view.getUint32(offset + 4, true);
const bodyOffset = offset + 8;
if (id === 'fmt ') {
format = {
audioFormat: view.getUint16(bodyOffset, true),
channels: view.getUint16(bodyOffset + 2, true),
sampleRate: view.getUint32(bodyOffset + 4, true),
blockAlign: view.getUint16(bodyOffset + 12, true),
bitsPerSample: view.getUint16(bodyOffset + 14, true),
};
}
if (id === 'data') {
pcmOffset = bodyOffset;
pcmSize = size;
}
offset = bodyOffset + size + (size % 2);
}
if (!format || pcmOffset < 0 || pcmSize <= 0) {
throw new Error('Invalid WAV file. Missing fmt/data chunks.');
}
const frameCount = Math.floor(pcmSize / format.blockAlign);
const channels = Math.max(1, format.channels);
const channelBuffers = Array.from({ length: channels }, () => new Float32Array(frameCount));
for (let frame = 0; frame < frameCount; frame += 1) {
if ((frame & 0x7ff) === 0) {
throwIfAborted(signal);
}
for (let channel = 0; channel < channels; channel += 1) {
const sampleOffset = pcmOffset + frame * format.blockAlign + channel * (format.bitsPerSample / 8);
channelBuffers[channel][frame] = decodeSample(view, sampleOffset, format.audioFormat, format.bitsPerSample);
}
}
return {
sampleRate: format.sampleRate,
left: channelBuffers[0],
right: channelBuffers[1],
};
}
async function decodeOggVorbis(buffer: Uint8Array, signal?: AbortSignal): Promise<DecodedAudio> {
return decodeWithManagedDecoder(
() => new OggVorbisDecoder(),
(decoder, input) => decoder.decodeFile(input),
buffer,
signal,
'OGG',
);
}
async function decodeMp3(buffer: Uint8Array, signal?: AbortSignal): Promise<DecodedAudio> {
return withSuppressedMpg123Warnings(async () => {
return decodeWithManagedDecoder(
() => new MPEGDecoder(),
(decoder, input) => decoder.decode(input),
buffer,
signal,
'MP3',
);
});
}
async function decodeOggOpus(buffer: Uint8Array, signal?: AbortSignal): Promise<DecodedAudio> {
return decodeWithManagedDecoder(
() => new OggOpusDecoder(),
(decoder, input) => decoder.decodeFile(input),
buffer,
signal,
'Opus',
);
}
async function decodeWithManagedDecoder<TDecoded extends DecoderOutput, TDecoder extends ManagedDecoder>(
createDecoder: () => TDecoder,
decode: (decoder: TDecoder, input: Uint8Array) => Promise<TDecoded> | TDecoded,
buffer: Uint8Array,
signal: AbortSignal | undefined,
formatLabel: string,
): Promise<DecodedAudio> {
throwIfAborted(signal);
const decoder = createDecoder();
await decoder.ready;
throwIfAborted(signal);
try {
throwIfAborted(signal);
const decoded = await decode(decoder, createDecoderInput(buffer));
throwIfAborted(signal);
return createDecodedAudioResult(decoded, formatLabel);
} finally {
decoder.free();
}
}
function createDecoderInput(buffer: Uint8Array): Uint8Array {
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
}
function createDecodedAudioResult(decoded: DecoderOutput, formatLabel: string): DecodedAudio {
const channels = decoded.channelData ?? [];
Iif (channels.length === 0) {
throw new Error(`Failed to decode ${formatLabel} file: no channel data.`);
}
return {
sampleRate: decoded.sampleRate,
left: channels[0],
right: channels[1],
};
}
function decodeSample(view: DataView, offset: number, audioFormat: number, bitsPerSample: number): number {
if (audioFormat === 3 && bitsPerSample === 32) {
return clampSignedUnit(view.getFloat32(offset, true));
}
if (audioFormat !== 1) {
throw new Error(`Unsupported WAV encoding format: ${audioFormat}`);
}
switch (bitsPerSample) {
case 8:
return (view.getUint8(offset) - 128) / 128;
case 16:
return view.getInt16(offset, true) / 32768;
case 24: {
const byte0 = view.getUint8(offset);
const byte1 = view.getUint8(offset + 1);
const byte2 = view.getUint8(offset + 2);
let sample = byte0 | (byte1 << 8) | (byte2 << 16);
if (sample & 0x800000) {
sample |= ~0xffffff;
}
return sample / 8388608;
}
case 32:
return view.getInt32(offset, true) / 2147483648;
default:
throw new Error(`Unsupported PCM bit depth: ${bitsPerSample}`);
}
}
function isWAVBuffer(buffer: Uint8Array): boolean {
Iif (buffer.byteLength < 12) {
return false;
}
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
return readASCII(view, 0, 4) === 'RIFF' && readASCII(view, 8, 4) === 'WAVE';
}
function isOggBuffer(buffer: Uint8Array): boolean {
Iif (buffer.byteLength < 4) {
return false;
}
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
return readASCII(view, 0, 4) === 'OggS';
}
function isOggOpusBuffer(buffer: Uint8Array): boolean {
Iif (buffer.byteLength < 32) {
return false;
}
return includesASCII(buffer, 'OpusHead');
}
function isMp3Buffer(buffer: Uint8Array): boolean {
Eif (buffer.byteLength >= 3) {
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
if (readASCII(view, 0, 3) === 'ID3') {
return true;
}
}
Iif (buffer.byteLength < 2) {
return false;
}
const header0 = buffer[0];
const header1 = buffer[1];
return header0 === 0xff && (header1 & 0xe0) === 0xe0;
}
function readASCII(view: DataView, offset: number, length: number): string {
let result = '';
for (let index = 0; index < length; index += 1) {
result += String.fromCharCode(view.getUint8(offset + index));
}
return result;
}
function includesASCII(buffer: Uint8Array, needle: string): boolean {
const needleLength = needle.length;
Iif (needleLength === 0) {
return true;
}
const maxStart = buffer.byteLength - needleLength;
for (let start = 0; start <= maxStart; start += 1) {
let matches = true;
for (let index = 0; index < needleLength; index += 1) {
if (buffer[start + index] !== (needle.charCodeAt(index) & 0xff)) {
matches = false;
break;
}
}
if (matches) {
return true;
}
}
return false;
}
function decodeUTF8(bytes: Uint8Array): string {
return new TextDecoder().decode(bytes);
}
async function withSuppressedMpg123Warnings<T>(fn: () => Promise<T>): Promise<T> {
const originalConsoleError = console.error;
const originalStderrWrite = process.stderr.write.bind(process.stderr);
console.error = (...args: unknown[]) => {
const message = args.map((value) => String(value)).join(' ');
if (MPG123_SUPPRESSED_LOG_PATTERNS.some((pattern) => pattern.test(message))) {
return;
}
originalConsoleError(...args);
};
process.stderr.write = ((
chunk: string | Uint8Array,
encoding?: BufferEncoding | ((error?: Error | null) => void),
callback?: (error?: Error | null) => void,
): boolean => {
const message = typeof chunk === 'string' ? chunk : decodeUTF8(chunk);
if (MPG123_SUPPRESSED_LOG_PATTERNS.some((pattern) => pattern.test(message))) {
if (typeof encoding === 'function') {
encoding();
}
callback?.();
return true;
}
if (typeof encoding === 'function') {
return originalStderrWrite(chunk, encoding);
}
return originalStderrWrite(chunk, encoding, callback);
}) as typeof process.stderr.write;
try {
return await fn();
} finally {
console.error = originalConsoleError;
process.stderr.write = originalStderrWrite;
}
}
|