@dsebastien/music-notation 0.25.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,7 @@ import type * as AT from '@coderline/alphatab';
9
9
  import { type Language } from '../i18n/language.js';
10
10
  import type { NotePlayer } from '../playback/player.js';
11
11
  import { type Engine } from '../playback/engine.js';
12
+ import type { PlayEffects } from '../playback/effects.js';
12
13
  import { type Naming } from '../theory/naming.js';
13
14
  import { type TempoUnit } from './tempo.js';
14
15
  import { type InstrumentSource } from './instruments.js';
@@ -93,6 +94,12 @@ export interface TabOptions {
93
94
  * #85); 0.8 by default. The bar has no volume of its own (#82).
94
95
  */
95
96
  volume?: number | (() => number);
97
+ /**
98
+ * The room and the ceiling this tab's sound goes through (#103): the page's or the app's
99
+ * choice (`reverb`, `room`, `dry`), read each time the tab starts to play. The light room by
100
+ * default.
101
+ */
102
+ effects?: PlayEffects | (() => PlayEffects | null | undefined);
96
103
  /**
97
104
  * The Full screen button at the end of the player bar (default true; #113): the block fills
98
105
  * the screen, the score is drawn again at that width and scrolls in an area of its own, where
package/dist/tab/index.js CHANGED
@@ -3219,6 +3219,450 @@ function followVolume(read, handles, start) {
3219
3219
  };
3220
3220
  }
3221
3221
 
3222
+ // src/playback/effects.ts
3223
+ var DEFAULT_REVERB = 0.125;
3224
+ var ROOM_SECONDS = 1.2;
3225
+ var ROOM_RANGE = [0.2, 3];
3226
+ function roomSeconds(value) {
3227
+ return typeof value === "number" && Number.isFinite(value) ? Math.min(ROOM_RANGE[1], Math.max(ROOM_RANGE[0], value)) : ROOM_SECONDS;
3228
+ }
3229
+ function reverbLevel(value) {
3230
+ return typeof value === "number" && Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : DEFAULT_REVERB;
3231
+ }
3232
+ function pageEffects(options) {
3233
+ if (!options || typeof options !== "object" || !("play" in options))
3234
+ return null;
3235
+ const play = options.play;
3236
+ if (!play || typeof play !== "object")
3237
+ return null;
3238
+ const read = (key) => {
3239
+ const value = play[key];
3240
+ if (typeof value !== "function")
3241
+ return value;
3242
+ try {
3243
+ return value();
3244
+ } catch {
3245
+ return;
3246
+ }
3247
+ };
3248
+ const reverb = read("reverb");
3249
+ const room = read("room");
3250
+ const amp = read("amp");
3251
+ const drive = amp && typeof amp === "object" ? amp["drive"] : null;
3252
+ const cabinet = amp && typeof amp === "object" && amp["cabinet"];
3253
+ const effects = {
3254
+ ...typeof reverb === "number" && Number.isFinite(reverb) ? { reverb: Math.min(1, Math.max(0, reverb)) } : {},
3255
+ ...typeof room === "number" && Number.isFinite(room) ? { room: roomSeconds(room) } : {},
3256
+ ...read("dry") === true ? { dry: true } : {},
3257
+ ...cabinet === true || typeof drive === "number" && Number.isFinite(drive) ? {
3258
+ amp: {
3259
+ ...typeof drive === "number" && Number.isFinite(drive) ? { drive: Math.min(1, Math.max(0, drive)) } : {},
3260
+ ...cabinet === true ? { cabinet: true } : {}
3261
+ }
3262
+ } : {}
3263
+ };
3264
+ return Object.keys(effects).length ? effects : null;
3265
+ }
3266
+ function seeded(seed) {
3267
+ let a = seed >>> 0;
3268
+ return () => {
3269
+ a = a + 1831565813 >>> 0;
3270
+ let t = a;
3271
+ t = Math.imul(t ^ t >>> 15, t | 1);
3272
+ t ^= t + Math.imul(t ^ t >>> 7, t | 61);
3273
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
3274
+ };
3275
+ }
3276
+ var ROOM_SHAPE = { bright: 4000, dark: 800, rise: 0.02, width: 0.5 };
3277
+ var ROOM_BAND = [300, 3000];
3278
+ var ROOM_WIDE_FROM = 400;
3279
+ var ROOM_SLOPE = 3;
3280
+ function roomImpulse(sampleRate, seconds = ROOM_SECONDS, shape = {}) {
3281
+ const { bright, dark, rise: riseSeconds, width } = { ...ROOM_SHAPE, ...shape };
3282
+ const length = Math.max(1, Math.round(sampleRate * seconds));
3283
+ const start = Math.min(length, Math.round(sampleRate * 0.012));
3284
+ const fade = Math.max(1, Math.round(length * 0.1));
3285
+ const fall = Math.pow(10, -3 / (sampleRate * seconds));
3286
+ const darker = Math.pow(dark / bright, 1 / (sampleRate * seconds));
3287
+ const rise = Math.max(1, Math.round(sampleRate * Math.max(0, riseSeconds)));
3288
+ const keepAt = (cutoff) => Math.exp(-2 * Math.PI * Math.min(cutoff, sampleRate * 0.45) / sampleRate);
3289
+ const shapeAt = (i) => (i - start < rise ? (i - start) / rise : 1) * (i > length - fade ? (length - i) / fade : 1);
3290
+ const make = (seed) => {
3291
+ const random = seeded(seed);
3292
+ const data = new Float32Array(length);
3293
+ let level = Math.pow(fall, start);
3294
+ let cutoff = bright * Math.pow(darker, start);
3295
+ let keep = 0;
3296
+ const lows = new Float64Array(ROOM_SLOPE);
3297
+ for (let i = start;i < length; i++) {
3298
+ if ((i - start) % 64 === 0)
3299
+ keep = keepAt(cutoff);
3300
+ let value = random() * 2 - 1;
3301
+ for (let pole = 0;pole < ROOM_SLOPE; pole++) {
3302
+ value = keep * lows[pole] + (1 - keep) * value;
3303
+ lows[pole] = value;
3304
+ }
3305
+ data[i] = value * level * shapeAt(i);
3306
+ level *= fall;
3307
+ cutoff *= darker;
3308
+ }
3309
+ return data;
3310
+ };
3311
+ let power = 0;
3312
+ const pitches = 8;
3313
+ for (let p = 0;p < pitches; p++) {
3314
+ const hz = ROOM_BAND[0] * Math.pow(ROOM_BAND[1] / ROOM_BAND[0], (p + 0.5) / pitches);
3315
+ const cos = Math.cos(2 * Math.PI * hz / sampleRate);
3316
+ let level = Math.pow(fall, start);
3317
+ let cutoff = bright * Math.pow(darker, start);
3318
+ const step = Math.pow(fall, 64);
3319
+ const darkerStep = Math.pow(darker, 64);
3320
+ for (let i = start;i < length; i += 64) {
3321
+ const keep = keepAt(cutoff);
3322
+ const pole = (1 - keep) * (1 - keep) / (1 - 2 * keep * cos + keep * keep);
3323
+ const size = Math.min(64, length - i);
3324
+ const here = level * shapeAt(i + (size >> 1));
3325
+ power += size * here * here * Math.pow(pole, ROOM_SLOPE) / 3;
3326
+ level *= step;
3327
+ cutoff *= darkerStep;
3328
+ }
3329
+ }
3330
+ const scale = power > 0 ? 1 / Math.sqrt(power / pitches) : 0;
3331
+ const middle = make(625341585);
3332
+ const apart = make(1374496523);
3333
+ const pass = Math.exp(-2 * Math.PI * ROOM_WIDE_FROM / sampleRate);
3334
+ for (let turn = 0;turn < 2; turn++) {
3335
+ let low = 0;
3336
+ for (let i = 0;i < length; i++) {
3337
+ low = pass * low + (1 - pass) * apart[i];
3338
+ apart[i] = apart[i] - low;
3339
+ }
3340
+ }
3341
+ const angle = Math.min(1, Math.max(0, width)) * Math.PI / 4;
3342
+ const left = new Float32Array(length);
3343
+ const right = new Float32Array(length);
3344
+ for (let i = 0;i < length; i++) {
3345
+ const m = middle[i] * Math.cos(angle) * scale;
3346
+ const a = apart[i] * Math.sin(angle) * scale;
3347
+ left[i] = m + a;
3348
+ right[i] = m - a;
3349
+ }
3350
+ return [left, right];
3351
+ }
3352
+ var CEILING = { knee: 0.9, range: 2 };
3353
+ function ceilingCurve(points = 4097) {
3354
+ const { knee, range } = CEILING;
3355
+ const curve = new Float32Array(points);
3356
+ for (let i = 0;i < points; i++) {
3357
+ const level = (i / (points - 1) * 2 - 1) * range;
3358
+ const size = Math.abs(level);
3359
+ curve[i] = Math.sign(level) * (size <= knee ? size : knee + (1 - knee) * Math.tanh((size - knee) / (1 - knee)));
3360
+ }
3361
+ return curve;
3362
+ }
3363
+ var ROOM_LOW_CUT = 180;
3364
+ var chains = new WeakMap;
3365
+ var canBuild = (ctx) => typeof ctx.createConvolver === "function" && typeof ctx.createBuffer === "function";
3366
+ var rooms = new Map;
3367
+ function roomBuffer(ctx, seconds) {
3368
+ const key = `${ctx.sampleRate}:${seconds}`;
3369
+ let sides = rooms.get(key);
3370
+ if (!sides) {
3371
+ sides = roomImpulse(ctx.sampleRate, seconds);
3372
+ if (rooms.size >= 3)
3373
+ rooms.delete(rooms.keys().next().value);
3374
+ rooms.set(key, sides);
3375
+ }
3376
+ const [left, right] = sides;
3377
+ const impulse = ctx.createBuffer(2, left.length, ctx.sampleRate);
3378
+ impulse.getChannelData(0).set(left);
3379
+ impulse.getChannelData(1).set(right);
3380
+ return impulse;
3381
+ }
3382
+ function build(ctx, speakers, seconds) {
3383
+ const input = ctx.createGain();
3384
+ const wet = ctx.createGain();
3385
+ wet.gain.value = 0;
3386
+ const room = ctx.createConvolver();
3387
+ room.normalize = false;
3388
+ room.buffer = roomBuffer(ctx, seconds);
3389
+ const sum = ctx.createGain();
3390
+ input.connect(sum);
3391
+ input.connect(room);
3392
+ if (typeof ctx.createBiquadFilter === "function") {
3393
+ const lowCut = ctx.createBiquadFilter();
3394
+ lowCut.type = "highpass";
3395
+ lowCut.frequency.value = ROOM_LOW_CUT;
3396
+ lowCut.Q.value = 0.7;
3397
+ room.connect(lowCut);
3398
+ lowCut.connect(wet);
3399
+ } else
3400
+ room.connect(wet);
3401
+ wet.connect(sum);
3402
+ if (typeof ctx.createWaveShaper === "function") {
3403
+ const ceiling = ctx.createWaveShaper();
3404
+ ceiling.curve = ceilingCurve();
3405
+ sum.gain.value = 1 / CEILING.range;
3406
+ sum.connect(ceiling);
3407
+ ceiling.connect(speakers);
3408
+ } else
3409
+ sum.connect(speakers);
3410
+ return { input, wet, room, seconds };
3411
+ }
3412
+ function outputFor(ctx, effects, speakers = ctx.destination) {
3413
+ if (effects?.dry || !canBuild(ctx))
3414
+ return speakers;
3415
+ const seconds = roomSeconds(effects?.room);
3416
+ let chain = chains.get(ctx);
3417
+ if (!chain) {
3418
+ try {
3419
+ chain = build(ctx, speakers, seconds);
3420
+ } catch {
3421
+ return speakers;
3422
+ }
3423
+ chains.set(ctx, chain);
3424
+ } else if (chain.seconds !== seconds) {
3425
+ try {
3426
+ chain.room.buffer = roomBuffer(ctx, seconds);
3427
+ chain.seconds = seconds;
3428
+ } catch {}
3429
+ }
3430
+ const level = reverbLevel(effects?.reverb);
3431
+ if (typeof chain.wet.gain.setTargetAtTime === "function")
3432
+ chain.wet.gain.setTargetAtTime(level, ctx.currentTime, 0.02);
3433
+ else
3434
+ chain.wet.gain.value = level;
3435
+ return chain.input;
3436
+ }
3437
+ var driveAmount = (drive) => 1 + 14 * Math.min(1, Math.max(0, drive));
3438
+ function driveCurve(drive, points = 2048) {
3439
+ const amount = driveAmount(drive);
3440
+ const curve = new Float32Array(points);
3441
+ const ceiling = Math.tanh(amount);
3442
+ for (let i = 0;i < points; i++) {
3443
+ const x = i / (points - 1) * 2 - 1;
3444
+ curve[i] = Math.tanh(amount * x) / ceiling;
3445
+ }
3446
+ return curve;
3447
+ }
3448
+ var DRIVE_REFERENCE = 0.2;
3449
+ function driveLevel(drive, reference = DRIVE_REFERENCE) {
3450
+ const amount = driveAmount(drive);
3451
+ const ceiling = Math.tanh(amount);
3452
+ let before = 0;
3453
+ let after = 0;
3454
+ for (let i = 0;i < 256; i++) {
3455
+ const x = reference * Math.sin(2 * Math.PI * i / 256);
3456
+ const y = Math.tanh(amount * x) / ceiling;
3457
+ before += x * x;
3458
+ after += y * y;
3459
+ }
3460
+ return after > 0 ? Math.sqrt(before / after) : 1;
3461
+ }
3462
+ var CABINET_TRIM_DB = -2;
3463
+ function ampFor(ctx, amp) {
3464
+ const drive = typeof amp?.drive === "number" ? Math.min(1, Math.max(0, amp.drive)) : 0;
3465
+ const cabinet = amp?.cabinet === true;
3466
+ if (!drive && !cabinet)
3467
+ return null;
3468
+ if (typeof ctx.createWaveShaper !== "function" || typeof ctx.createBiquadFilter !== "function")
3469
+ return null;
3470
+ const input = ctx.createGain();
3471
+ let last = input;
3472
+ const filter = (type, frequency, q, gain = 0) => {
3473
+ const node = ctx.createBiquadFilter();
3474
+ node.type = type;
3475
+ node.frequency.value = frequency;
3476
+ node.Q.value = q;
3477
+ node.gain.value = gain;
3478
+ last.connect(node);
3479
+ last = node;
3480
+ };
3481
+ if (drive) {
3482
+ filter("highpass", 120, 0.7);
3483
+ const shaper = ctx.createWaveShaper();
3484
+ shaper.curve = driveCurve(drive);
3485
+ shaper.oversample = "4x";
3486
+ const level = ctx.createGain();
3487
+ level.gain.value = driveLevel(drive);
3488
+ last.connect(shaper);
3489
+ shaper.connect(level);
3490
+ last = level;
3491
+ }
3492
+ if (cabinet) {
3493
+ filter("highpass", 90, 0.7);
3494
+ filter("peaking", 2200, 1, 4);
3495
+ filter("lowpass", 4800, 0.7);
3496
+ filter("lowpass", 6500, 0.7);
3497
+ const level = ctx.createGain();
3498
+ level.gain.value = Math.pow(10, CABINET_TRIM_DB / 20);
3499
+ last.connect(level);
3500
+ last = level;
3501
+ }
3502
+ return { input, output: last };
3503
+ }
3504
+
3505
+ // src/tab/room.ts
3506
+ var isOutputNode = (value) => typeof value === "object" && value !== null && typeof value.connect === "function" && typeof value.disconnect === "function" && !("start" in value);
3507
+ var LOOKS = [0, 20, 60, 150, 400, 1000];
3508
+ function outputNodeOf(player) {
3509
+ const output = player?.output;
3510
+ const ctx = output?.context;
3511
+ if (!output || !ctx)
3512
+ return null;
3513
+ for (const field of Object.keys(output)) {
3514
+ const value = output[field];
3515
+ if (value !== output.source && value !== ctx && isOutputNode(value))
3516
+ return { ctx, node: value };
3517
+ }
3518
+ return null;
3519
+ }
3520
+ function contextOf(player) {
3521
+ return player?.output?.context ?? null;
3522
+ }
3523
+ var taken = new WeakMap;
3524
+ function throughRoom(api, playing, room, win) {
3525
+ const routed = new WeakSet;
3526
+ const self = {};
3527
+ let timers = [];
3528
+ let alive = true;
3529
+ let mine = null;
3530
+ let amp = null;
3531
+ let leaving = [];
3532
+ const cut = (node) => {
3533
+ try {
3534
+ node.disconnect();
3535
+ } catch {}
3536
+ };
3537
+ const entryFor = (ctx, speakers) => {
3538
+ let effects;
3539
+ let guitar = null;
3540
+ try {
3541
+ effects = room.effects() ?? undefined;
3542
+ guitar = room.guitar?.() ?? null;
3543
+ } catch {
3544
+ effects = undefined;
3545
+ }
3546
+ const out = effects?.dry ? speakers : outputFor(ctx, effects, speakers);
3547
+ const wish = out !== speakers && (guitar === "clean" || guitar === "overdrive") ? effects?.amp : undefined;
3548
+ const key = wish ? `${wish.drive ?? 0}:${wish.cabinet === true}` : "";
3549
+ if (amp && (amp.key !== key || amp.ctx !== ctx || amp.out !== out)) {
3550
+ const going = { id: 0, node: amp.output };
3551
+ amp = null;
3552
+ going.id = win.setTimeout(() => {
3553
+ cut(going.node);
3554
+ leaving = leaving.filter((one) => one !== going);
3555
+ }, 1000);
3556
+ leaving.push(going);
3557
+ }
3558
+ if (!key)
3559
+ return out;
3560
+ if (!amp) {
3561
+ const made = ampFor(ctx, wish);
3562
+ if (!made)
3563
+ return out;
3564
+ made.output.connect(out);
3565
+ amp = { key, ctx, out, ...made };
3566
+ }
3567
+ return amp.input;
3568
+ };
3569
+ const take = () => {
3570
+ const ctx = contextOf(api.player);
3571
+ if (!ctx)
3572
+ return false;
3573
+ const entry = (speakers) => entryFor(ctx, speakers);
3574
+ const had = taken.get(ctx);
3575
+ if (had) {
3576
+ had.owner = self;
3577
+ had.entry = entry;
3578
+ mine = had;
3579
+ return true;
3580
+ }
3581
+ try {
3582
+ const holder = { speakers: ctx.destination, owner: self, entry, asked: 0 };
3583
+ Object.defineProperty(ctx, "destination", {
3584
+ configurable: true,
3585
+ get: () => {
3586
+ holder.asked++;
3587
+ try {
3588
+ return holder.entry ? holder.entry(holder.speakers) : holder.speakers;
3589
+ } catch {
3590
+ return holder.speakers;
3591
+ }
3592
+ }
3593
+ });
3594
+ taken.set(ctx, holder);
3595
+ mine = holder;
3596
+ return true;
3597
+ } catch {
3598
+ return false;
3599
+ }
3600
+ };
3601
+ const route = (since) => {
3602
+ if (!alive)
3603
+ return;
3604
+ const found = outputNodeOf(api.player);
3605
+ if (!found || routed.has(found.node))
3606
+ return;
3607
+ const holder = taken.get(found.ctx);
3608
+ if (holder && (since === null || holder.asked > since))
3609
+ return;
3610
+ try {
3611
+ const speakers = holder ? holder.speakers : found.ctx.destination;
3612
+ const entry = entryFor(found.ctx, speakers);
3613
+ if (entry === speakers)
3614
+ return;
3615
+ found.node.disconnect();
3616
+ found.node.connect(entry);
3617
+ routed.add(found.node);
3618
+ } catch {}
3619
+ };
3620
+ const look = () => {
3621
+ if (!alive)
3622
+ return;
3623
+ for (const id of timers)
3624
+ win.clearTimeout(id);
3625
+ timers = [];
3626
+ const before = contextOf(api.player);
3627
+ if (before && taken.has(before) && take())
3628
+ return;
3629
+ const since = take() ? mine?.asked ?? 0 : null;
3630
+ timers = LOOKS.map((ms) => win.setTimeout(() => route(since), ms));
3631
+ };
3632
+ take();
3633
+ const ready = api.playerReady;
3634
+ if (typeof ready?.on === "function")
3635
+ ready.on(() => {
3636
+ if (alive)
3637
+ take();
3638
+ });
3639
+ api.playerStateChanged.on((e) => {
3640
+ if (alive && playing(e.state))
3641
+ look();
3642
+ });
3643
+ return {
3644
+ look,
3645
+ stop() {
3646
+ alive = false;
3647
+ for (const id of timers)
3648
+ win.clearTimeout(id);
3649
+ timers = [];
3650
+ for (const { id, node } of leaving) {
3651
+ win.clearTimeout(id);
3652
+ cut(node);
3653
+ }
3654
+ leaving = [];
3655
+ if (amp)
3656
+ cut(amp.output);
3657
+ amp = null;
3658
+ if (mine && mine.owner === self) {
3659
+ mine.owner = null;
3660
+ mine.entry = null;
3661
+ }
3662
+ }
3663
+ };
3664
+ }
3665
+
3222
3666
  // src/playback/full-screen.ts
3223
3667
  var SCROLL_KEYS = new Set(["PageUp", "PageDown", "Home", "End", "ArrowUp", "ArrowDown"]);
3224
3668
  function backgroundBehind(el) {
@@ -4442,6 +4886,11 @@ function renderTab(language, source, host, options) {
4442
4886
  };
4443
4887
  const current = new at.AlphaTabApi(surface, settings);
4444
4888
  api = current;
4889
+ const roomWindow = host.ownerDocument.defaultView;
4890
+ if (roomWindow)
4891
+ throughRoom(current, (state) => Number(state) === Number(at.synth.PlayerState.Playing), {
4892
+ effects: () => typeof options.effects === "function" ? options.effects() : options.effects
4893
+ }, roomWindow);
4445
4894
  current.settings.player.scrollElement = surface;
4446
4895
  if (gone) {
4447
4896
  current.destroy();
@@ -4745,6 +5194,9 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
4745
5194
  }
4746
5195
  });
4747
5196
  const current = api;
5197
+ const view = doc.defaultView;
5198
+ if (view)
5199
+ room = throughRoom(current, (state) => Number(state) === Number(alphaTab.synth.PlayerState.Playing), { effects: () => asked.effects, guitar: () => asked.guitar }, view);
4748
5200
  ready = new Promise((resolve) => {
4749
5201
  let soundFont = false;
4750
5202
  let player = !current.playerReady;
@@ -4812,6 +5264,11 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
4812
5264
  return more;
4813
5265
  };
4814
5266
  const inner = soundfontPlayer(alphaTab, () => api);
5267
+ let room = null;
5268
+ const asked = {
5269
+ effects: undefined,
5270
+ guitar: null
5271
+ };
4815
5272
  return {
4816
5273
  reportsStart: true,
4817
5274
  play(events, options) {
@@ -4823,7 +5280,10 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
4823
5280
  return;
4824
5281
  try {
4825
5282
  if (ok) {
5283
+ asked.effects = options.effects;
5284
+ asked.guitar = options.sound === "guitar" ? options.guitar ?? "steel" : null;
4826
5285
  handle = inner.play(events, { ...options, volume });
5286
+ room?.look();
4827
5287
  return;
4828
5288
  }
4829
5289
  } catch {}
@@ -4890,5 +5350,5 @@ export {
4890
5350
  textTabToAlphaTex
4891
5351
  };
4892
5352
 
4893
- //# debugId=21F24532A6AFD41364756E2164756E21
5353
+ //# debugId=33BEEC591056A9AB64756E2164756E21
4894
5354
  //# sourceMappingURL=index.js.map