@dsebastien/music-notation 0.19.2 → 0.20.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.
- package/README.md +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +558 -3
- package/dist/index.js.map +8 -5
- package/dist/music-notation.css +35 -0
- package/dist/music-notation.publish.min.js +70 -35
- package/dist/music-notation.tab.min.js +42 -7
- package/dist/playback/bar-parts.d.ts +3 -0
- package/dist/playback/player.d.ts +5 -0
- package/dist/playback/sampled.d.ts +42 -0
- package/dist/playback/soundfont.d.ts +82 -0
- package/dist/playback/soundfont.fixture.d.ts +28 -0
- package/dist/publish/index.js +5 -3
- package/dist/publish/index.js.map +4 -4
- package/dist/tab/full-screen.d.ts +19 -0
- package/dist/tab/index.d.ts +14 -0
- package/dist/tab/index.js +362 -17
- package/dist/tab/index.js.map +7 -5
- package/dist/tab/instruments.d.ts +63 -0
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -191,7 +191,7 @@ Add this to your site's `publish.js`. It loads the bundle only on pages that hav
|
|
|
191
191
|
|
|
192
192
|
```js
|
|
193
193
|
;(() => {
|
|
194
|
-
const VERSION = '0.19.
|
|
194
|
+
const VERSION = '0.19.2'
|
|
195
195
|
const CDN = `https://cdn.jsdelivr.net/npm/@dsebastien/music-notation@${VERSION}/dist`
|
|
196
196
|
const selector = (languages) => languages.map((l) => `pre > code.language-${l}`).join(', ')
|
|
197
197
|
const BLOCKS = selector([
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export { renderGuitarDiagram, parseFrets, parseFingers, baseFretOf, barreKind, i
|
|
|
6
6
|
export { soundingChord, soundsLike, fretsFromCapo, capoTuning, parseCapo, parseFretsFrom, MAX_CAPO, type FretsFrom } from './chord/capo.js';
|
|
7
7
|
export { generateVoicings, fingerVoicing, voicingShape, MAX_GENERATED_STRINGS, type GeneratedVoicing, type VoicingOptions, type VoicingShape } from './chord/generate.js';
|
|
8
8
|
export { sequenceOf, scaleRun, lengthOf, lightPlayer, pluckedString, pianoTone, DEFAULT_SEQUENCE, DEFAULT_STRUM, type NoteEvent, type NotePlayer, type PluckOptions, type PlayHandle, type PlayKind, type Sound, type SequenceOptions } from './playback/player.js';
|
|
9
|
+
export { sampledPlayer, type SampledOptions } from './playback/sampled.js';
|
|
10
|
+
export { pageInstruments, type InstrumentSource } from './tab/instruments.js';
|
|
9
11
|
export { attachPlayback, type PlayOptions, type TimedBar } from './playback/controls.js';
|
|
10
12
|
export { volumeChanged } from './playback/live-volume.js';
|
|
11
13
|
export { attachCopy } from './playback/copy-image.js';
|
package/dist/index.js
CHANGED
|
@@ -5426,9 +5426,11 @@ var ICONS = {
|
|
|
5426
5426
|
loop: "M3 8a5 5 0 0 1 8.5-3.5L13 6V2.5M13 8a5 5 0 0 1-8.5 3.5L3 10v3.5",
|
|
5427
5427
|
count: "M5.5 14h5L9 2.5H7zM8 11.5l4.5-7",
|
|
5428
5428
|
click: "M3 5v6M8 5v6M13 5v6",
|
|
5429
|
-
copy: "M2.5 3.5h11v9h-11zM2.5 11l3.5-3.5 3 3 2-2 2.5 2.5M10.5 6.5h.01"
|
|
5429
|
+
copy: "M2.5 3.5h11v9h-11zM2.5 11l3.5-3.5 3 3 2-2 2.5 2.5M10.5 6.5h.01",
|
|
5430
|
+
full: "M2.5 6V2.5H6M10 2.5h3.5V6M13.5 10v3.5H10M6 13.5H2.5V10",
|
|
5431
|
+
exit: "M6 2.5V6H2.5M13.5 6H10V2.5M10 13.5V10h3.5M2.5 10H6v3.5"
|
|
5430
5432
|
};
|
|
5431
|
-
var STROKED = new Set(["loop", "count", "click", "copy"]);
|
|
5433
|
+
var STROKED = new Set(["loop", "count", "click", "copy", "full", "exit"]);
|
|
5432
5434
|
function icon(doc, name) {
|
|
5433
5435
|
const root = svg(doc, "svg", {
|
|
5434
5436
|
class: `mn-play-icon mn-play-icon-${name}${STROKED.has(name) ? " mn-play-icon-line" : ""}`,
|
|
@@ -14461,6 +14463,41 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
|
|
|
14461
14463
|
align-items: center;
|
|
14462
14464
|
gap: 2px 6px;
|
|
14463
14465
|
}
|
|
14466
|
+
|
|
14467
|
+
/* Full screen for a tab or a score (#113): the block fills the screen (the browser's full screen,
|
|
14468
|
+
or the window when the browser refuses it), the score in a scroll area of its own at the
|
|
14469
|
+
screen's width, the bar at the bottom. The background is the page's, set by the control. */
|
|
14470
|
+
.mn-tab.mn-fullscreen {
|
|
14471
|
+
position: fixed;
|
|
14472
|
+
inset: 0;
|
|
14473
|
+
z-index: 9999;
|
|
14474
|
+
display: flex;
|
|
14475
|
+
flex-direction: column;
|
|
14476
|
+
box-sizing: border-box;
|
|
14477
|
+
width: auto;
|
|
14478
|
+
max-width: none;
|
|
14479
|
+
height: auto;
|
|
14480
|
+
margin: 0;
|
|
14481
|
+
padding: 12px 16px 8px;
|
|
14482
|
+
overflow: hidden;
|
|
14483
|
+
background: Canvas;
|
|
14484
|
+
}
|
|
14485
|
+
|
|
14486
|
+
.mn-tab.mn-fullscreen .mn-tab-surface {
|
|
14487
|
+
flex: 1 1 auto;
|
|
14488
|
+
min-height: 0;
|
|
14489
|
+
overflow: auto;
|
|
14490
|
+
overscroll-behavior: contain;
|
|
14491
|
+
/* The scroll bar has its room from the start: the score is drawn once, at the width left. */
|
|
14492
|
+
scrollbar-gutter: stable;
|
|
14493
|
+
outline: none;
|
|
14494
|
+
}
|
|
14495
|
+
|
|
14496
|
+
.mn-tab.mn-fullscreen .mn-play.mn-play-static.mn-tab-bar {
|
|
14497
|
+
position: static;
|
|
14498
|
+
flex: none;
|
|
14499
|
+
margin-top: 8px;
|
|
14500
|
+
}
|
|
14464
14501
|
`;
|
|
14465
14502
|
|
|
14466
14503
|
// src/styles.ts
|
|
@@ -14474,6 +14511,522 @@ function injectStyles(doc) {
|
|
|
14474
14511
|
style.textContent = STYLES;
|
|
14475
14512
|
doc.head.appendChild(style);
|
|
14476
14513
|
}
|
|
14514
|
+
// src/tab/instruments.ts
|
|
14515
|
+
function pageInstruments(options) {
|
|
14516
|
+
if (!options || typeof options !== "object" || !("play" in options))
|
|
14517
|
+
return;
|
|
14518
|
+
const play = options.play;
|
|
14519
|
+
if (!play || typeof play !== "object" || !("instruments" in play))
|
|
14520
|
+
return;
|
|
14521
|
+
const folder = play.instruments;
|
|
14522
|
+
return typeof folder === "string" && folder.trim() ? folder.trim() : undefined;
|
|
14523
|
+
}
|
|
14524
|
+
function soundsOfScore(score) {
|
|
14525
|
+
const programs = new Set;
|
|
14526
|
+
let drums = false;
|
|
14527
|
+
for (const track of score.tracks) {
|
|
14528
|
+
if (track.isPercussion)
|
|
14529
|
+
drums = true;
|
|
14530
|
+
else
|
|
14531
|
+
programs.add(track.playbackInfo.program);
|
|
14532
|
+
}
|
|
14533
|
+
return { programs: [...programs].sort((a, b) => a - b), drums };
|
|
14534
|
+
}
|
|
14535
|
+
function filesOf(index, sounds) {
|
|
14536
|
+
const files = [];
|
|
14537
|
+
for (const program of sounds.programs) {
|
|
14538
|
+
const entry = index.programs[String(program)];
|
|
14539
|
+
if (!entry?.file)
|
|
14540
|
+
return null;
|
|
14541
|
+
files.push(entry.file);
|
|
14542
|
+
}
|
|
14543
|
+
if (sounds.drums) {
|
|
14544
|
+
if (!index.drums?.file)
|
|
14545
|
+
return null;
|
|
14546
|
+
files.push(index.drums.file);
|
|
14547
|
+
} else if (index.click?.file && sounds.click !== false)
|
|
14548
|
+
files.push(index.click.file);
|
|
14549
|
+
return files.length ? files : null;
|
|
14550
|
+
}
|
|
14551
|
+
var soundCaches = new WeakMap;
|
|
14552
|
+
var isIndex = (value) => typeof value === "object" && value !== null && ("programs" in value) && typeof value.programs === "object" && value.programs !== null;
|
|
14553
|
+
async function instrumentSounds(source, sounds, win) {
|
|
14554
|
+
let perWindow = soundCaches.get(win);
|
|
14555
|
+
if (!perWindow)
|
|
14556
|
+
soundCaches.set(win, perWindow = new Map);
|
|
14557
|
+
let cache = perWindow.get(source);
|
|
14558
|
+
if (!cache)
|
|
14559
|
+
perWindow.set(source, cache = { index: null, files: new Map });
|
|
14560
|
+
const read = async (file) => {
|
|
14561
|
+
try {
|
|
14562
|
+
if (typeof source === "function")
|
|
14563
|
+
return await source(file);
|
|
14564
|
+
const response = await win.fetch(`${source.replace(/\/+$/, "")}/${file}`);
|
|
14565
|
+
return response.ok ? new Uint8Array(await response.arrayBuffer()) : null;
|
|
14566
|
+
} catch {
|
|
14567
|
+
return null;
|
|
14568
|
+
}
|
|
14569
|
+
};
|
|
14570
|
+
const fileOf = (file) => {
|
|
14571
|
+
let bytes = cache.files.get(file);
|
|
14572
|
+
if (!bytes) {
|
|
14573
|
+
bytes = read(file);
|
|
14574
|
+
cache.files.set(file, bytes);
|
|
14575
|
+
bytes.then((b) => {
|
|
14576
|
+
if (!b)
|
|
14577
|
+
cache.files.delete(file);
|
|
14578
|
+
});
|
|
14579
|
+
}
|
|
14580
|
+
return bytes;
|
|
14581
|
+
};
|
|
14582
|
+
cache.index ??= read("index.json").then((bytes) => {
|
|
14583
|
+
if (!bytes)
|
|
14584
|
+
return null;
|
|
14585
|
+
try {
|
|
14586
|
+
const value = JSON.parse(new TextDecoder().decode(bytes));
|
|
14587
|
+
return isIndex(value) ? value : null;
|
|
14588
|
+
} catch {
|
|
14589
|
+
return null;
|
|
14590
|
+
}
|
|
14591
|
+
});
|
|
14592
|
+
const index = await cache.index;
|
|
14593
|
+
if (!index) {
|
|
14594
|
+
cache.index = null;
|
|
14595
|
+
return null;
|
|
14596
|
+
}
|
|
14597
|
+
const files = filesOf(index, sounds);
|
|
14598
|
+
if (!files)
|
|
14599
|
+
return null;
|
|
14600
|
+
const all = await Promise.all(files.map(fileOf));
|
|
14601
|
+
return all.every((b) => b !== null) ? all : null;
|
|
14602
|
+
}
|
|
14603
|
+
|
|
14604
|
+
// src/playback/soundfont.ts
|
|
14605
|
+
var GEN = {
|
|
14606
|
+
filter: 8,
|
|
14607
|
+
filterQ: 9,
|
|
14608
|
+
filterAmount: 11,
|
|
14609
|
+
modAttack: 26,
|
|
14610
|
+
modHold: 27,
|
|
14611
|
+
modDecay: 28,
|
|
14612
|
+
modSustain: 29,
|
|
14613
|
+
attack: 34,
|
|
14614
|
+
hold: 35,
|
|
14615
|
+
decay: 36,
|
|
14616
|
+
sustain: 37,
|
|
14617
|
+
release: 38,
|
|
14618
|
+
instrument: 41,
|
|
14619
|
+
keyRange: 43,
|
|
14620
|
+
velRange: 44,
|
|
14621
|
+
attenuation: 48,
|
|
14622
|
+
coarse: 51,
|
|
14623
|
+
fine: 52,
|
|
14624
|
+
sample: 53,
|
|
14625
|
+
modes: 54,
|
|
14626
|
+
scale: 56,
|
|
14627
|
+
rootKey: 58
|
|
14628
|
+
};
|
|
14629
|
+
var DEFAULTS = {
|
|
14630
|
+
[GEN.filter]: 13500,
|
|
14631
|
+
[GEN.filterQ]: 0,
|
|
14632
|
+
[GEN.filterAmount]: 0,
|
|
14633
|
+
[GEN.modAttack]: -12000,
|
|
14634
|
+
[GEN.modHold]: -12000,
|
|
14635
|
+
[GEN.modDecay]: -12000,
|
|
14636
|
+
[GEN.modSustain]: 0,
|
|
14637
|
+
[GEN.attack]: -12000,
|
|
14638
|
+
[GEN.hold]: -12000,
|
|
14639
|
+
[GEN.decay]: -12000,
|
|
14640
|
+
[GEN.sustain]: 0,
|
|
14641
|
+
[GEN.release]: -12000,
|
|
14642
|
+
[GEN.attenuation]: 0,
|
|
14643
|
+
[GEN.coarse]: 0,
|
|
14644
|
+
[GEN.fine]: 0,
|
|
14645
|
+
[GEN.modes]: 0,
|
|
14646
|
+
[GEN.scale]: 100,
|
|
14647
|
+
[GEN.rootKey]: -1
|
|
14648
|
+
};
|
|
14649
|
+
var text2 = (bytes, start, length) => {
|
|
14650
|
+
let end = start;
|
|
14651
|
+
while (end < start + length && bytes[end] !== 0)
|
|
14652
|
+
end++;
|
|
14653
|
+
return new TextDecoder("latin1").decode(bytes.subarray(start, end));
|
|
14654
|
+
};
|
|
14655
|
+
var view = (bytes) => new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
14656
|
+
var signed = (n) => n > 32767 ? n - 65536 : n;
|
|
14657
|
+
var seconds = (timecents) => timecents <= -11950 ? 0 : Math.pow(2, timecents / 1200);
|
|
14658
|
+
function chunks(bytes) {
|
|
14659
|
+
const out = [];
|
|
14660
|
+
const v = view(bytes);
|
|
14661
|
+
let at = 0;
|
|
14662
|
+
while (at + 8 <= bytes.length) {
|
|
14663
|
+
const size = v.getUint32(at + 4, true);
|
|
14664
|
+
out.push([text2(bytes, at, 4), bytes.subarray(at + 8, at + 8 + size)]);
|
|
14665
|
+
at += 8 + size + size % 2;
|
|
14666
|
+
}
|
|
14667
|
+
return out;
|
|
14668
|
+
}
|
|
14669
|
+
function zones(bags, gens, from, to) {
|
|
14670
|
+
const b = view(bags);
|
|
14671
|
+
const g = view(gens);
|
|
14672
|
+
const out = [];
|
|
14673
|
+
for (let z = from;z < to; z++) {
|
|
14674
|
+
const zone = new Map;
|
|
14675
|
+
const end = b.getUint16((z + 1) * 4, true);
|
|
14676
|
+
for (let i = b.getUint16(z * 4, true);i < end; i++)
|
|
14677
|
+
zone.set(g.getUint16(i * 4, true), g.getUint16(i * 4 + 2, true));
|
|
14678
|
+
out.push(zone);
|
|
14679
|
+
}
|
|
14680
|
+
return out;
|
|
14681
|
+
}
|
|
14682
|
+
function readSoundfont(bytes) {
|
|
14683
|
+
if (text2(bytes, 0, 4) !== "RIFF" || text2(bytes, 8, 4) !== "sfbk")
|
|
14684
|
+
throw new Error("Not a soundfont");
|
|
14685
|
+
const t = {};
|
|
14686
|
+
let data = null;
|
|
14687
|
+
for (const [id, body] of chunks(bytes.subarray(12))) {
|
|
14688
|
+
if (id !== "LIST")
|
|
14689
|
+
continue;
|
|
14690
|
+
const kind = text2(body, 0, 4);
|
|
14691
|
+
for (const [sub, part] of chunks(body.subarray(4))) {
|
|
14692
|
+
if (kind === "sdta" && sub === "smpl")
|
|
14693
|
+
data = part;
|
|
14694
|
+
else if (kind === "pdta")
|
|
14695
|
+
t[sub] = part;
|
|
14696
|
+
}
|
|
14697
|
+
}
|
|
14698
|
+
const names = ["phdr", "pbag", "pgen", "inst", "ibag", "igen", "shdr"];
|
|
14699
|
+
if (!data || names.some((n) => !t[n]))
|
|
14700
|
+
throw new Error("Not a complete soundfont");
|
|
14701
|
+
const [phdr, pbag, pgen, inst, ibag, igen, shdr] = names.map((n) => t[n]);
|
|
14702
|
+
const sv = view(shdr);
|
|
14703
|
+
const samples = [];
|
|
14704
|
+
for (let i = 0;i < shdr.length / 46 - 1; i++) {
|
|
14705
|
+
const at = i * 46;
|
|
14706
|
+
const start = sv.getUint32(at + 20, true);
|
|
14707
|
+
const compressed = (sv.getUint16(at + 44, true) & 16) !== 0;
|
|
14708
|
+
const key = sv.getUint8(at + 40);
|
|
14709
|
+
samples.push({
|
|
14710
|
+
name: text2(shdr, at, 20),
|
|
14711
|
+
start,
|
|
14712
|
+
end: sv.getUint32(at + 24, true),
|
|
14713
|
+
loopStart: sv.getUint32(at + 28, true) - (compressed ? 0 : start),
|
|
14714
|
+
loopEnd: sv.getUint32(at + 32, true) - (compressed ? 0 : start),
|
|
14715
|
+
rate: sv.getUint32(at + 36, true),
|
|
14716
|
+
key: key > 127 ? 60 : key,
|
|
14717
|
+
correction: sv.getInt8(at + 41),
|
|
14718
|
+
compressed
|
|
14719
|
+
});
|
|
14720
|
+
}
|
|
14721
|
+
const pv = view(phdr);
|
|
14722
|
+
const iv = view(inst);
|
|
14723
|
+
const presets = [];
|
|
14724
|
+
for (let p = 0;p < phdr.length / 38 - 1; p++) {
|
|
14725
|
+
const regions = [];
|
|
14726
|
+
const presetZones = zones(pbag, pgen, pv.getUint16(p * 38 + 24, true), pv.getUint16((p + 1) * 38 + 24, true));
|
|
14727
|
+
const presetGlobal = presetZones[0] && !presetZones[0].has(GEN.instrument) ? presetZones[0] : null;
|
|
14728
|
+
for (const presetZone of presetZones) {
|
|
14729
|
+
const instrument = presetZone.get(GEN.instrument);
|
|
14730
|
+
if (instrument === undefined || instrument >= inst.length / 22 - 1)
|
|
14731
|
+
continue;
|
|
14732
|
+
const instrumentZones = zones(ibag, igen, iv.getUint16(instrument * 22 + 20, true), iv.getUint16((instrument + 1) * 22 + 20, true));
|
|
14733
|
+
const global = instrumentZones[0] && !instrumentZones[0].has(GEN.sample) ? instrumentZones[0] : null;
|
|
14734
|
+
const added = (gen) => signed(presetZone.get(gen) ?? presetGlobal?.get(gen) ?? 0);
|
|
14735
|
+
const range = (zone, gen) => {
|
|
14736
|
+
const amount = zone.get(gen);
|
|
14737
|
+
return amount === undefined ? [0, 127] : [amount & 255, amount >> 8];
|
|
14738
|
+
};
|
|
14739
|
+
const [pLoKey, pHiKey] = range(presetZone, GEN.keyRange);
|
|
14740
|
+
const [pLoVel, pHiVel] = range(presetZone, GEN.velRange);
|
|
14741
|
+
for (const zone of instrumentZones) {
|
|
14742
|
+
const sample = zone.get(GEN.sample);
|
|
14743
|
+
if (sample === undefined || !samples[sample])
|
|
14744
|
+
continue;
|
|
14745
|
+
const own = (gen) => signed(zone.get(gen) ?? global?.get(gen) ?? (DEFAULTS[gen] ?? 0) & 65535);
|
|
14746
|
+
const value = (gen) => own(gen) + added(gen);
|
|
14747
|
+
const keys = range(zone.has(GEN.keyRange) ? zone : global ?? zone, GEN.keyRange);
|
|
14748
|
+
const vels = range(zone.has(GEN.velRange) ? zone : global ?? zone, GEN.velRange);
|
|
14749
|
+
const loKey = Math.max(keys[0], pLoKey);
|
|
14750
|
+
const hiKey = Math.min(keys[1], pHiKey);
|
|
14751
|
+
const loVel = Math.max(vels[0], pLoVel);
|
|
14752
|
+
const hiVel = Math.min(vels[1], pHiVel);
|
|
14753
|
+
if (loKey > hiKey || loVel > hiVel)
|
|
14754
|
+
continue;
|
|
14755
|
+
const info = samples[sample];
|
|
14756
|
+
const root = own(GEN.rootKey);
|
|
14757
|
+
const cutoff = value(GEN.filter);
|
|
14758
|
+
const amount = value(GEN.filterAmount);
|
|
14759
|
+
regions.push({
|
|
14760
|
+
loKey,
|
|
14761
|
+
hiKey,
|
|
14762
|
+
loVel,
|
|
14763
|
+
hiVel,
|
|
14764
|
+
sample,
|
|
14765
|
+
rootKey: root >= 0 && root <= 127 ? root : info.key,
|
|
14766
|
+
tune: value(GEN.coarse) * 100 + value(GEN.fine) + info.correction,
|
|
14767
|
+
scale: own(GEN.scale),
|
|
14768
|
+
attenuation: value(GEN.attenuation) / 10,
|
|
14769
|
+
loop: (own(GEN.modes) & 1) === 1 && info.loopEnd > info.loopStart,
|
|
14770
|
+
attack: seconds(value(GEN.attack)),
|
|
14771
|
+
hold: seconds(value(GEN.hold)),
|
|
14772
|
+
decay: seconds(value(GEN.decay)),
|
|
14773
|
+
sustain: Math.max(0, value(GEN.sustain)) / 10,
|
|
14774
|
+
release: seconds(value(GEN.release)),
|
|
14775
|
+
filter: cutoff >= 13500 && amount <= 0 ? null : 8.176 * Math.pow(2, cutoff / 1200),
|
|
14776
|
+
filterQ: Math.max(0, value(GEN.filterQ)) / 10,
|
|
14777
|
+
filterEnvelope: {
|
|
14778
|
+
amount,
|
|
14779
|
+
attack: seconds(value(GEN.modAttack)),
|
|
14780
|
+
hold: seconds(value(GEN.modHold)),
|
|
14781
|
+
decay: seconds(value(GEN.modDecay)),
|
|
14782
|
+
sustain: 1 - Math.min(1000, Math.max(0, value(GEN.modSustain))) / 1000
|
|
14783
|
+
}
|
|
14784
|
+
});
|
|
14785
|
+
}
|
|
14786
|
+
}
|
|
14787
|
+
presets.push({
|
|
14788
|
+
name: text2(phdr, p * 38, 20),
|
|
14789
|
+
program: pv.getUint16(p * 38 + 20, true),
|
|
14790
|
+
bank: pv.getUint16(p * 38 + 22, true),
|
|
14791
|
+
regions
|
|
14792
|
+
});
|
|
14793
|
+
}
|
|
14794
|
+
return { presets, samples, data };
|
|
14795
|
+
}
|
|
14796
|
+
function regionsFor(preset, key, velocity) {
|
|
14797
|
+
return preset.regions.filter((r) => key >= r.loKey && key <= r.hiKey && velocity >= r.loVel && velocity <= r.hiVel);
|
|
14798
|
+
}
|
|
14799
|
+
|
|
14800
|
+
// src/playback/sampled.ts
|
|
14801
|
+
var PROGRAMS = { guitar: 25, piano: 0 };
|
|
14802
|
+
var MUTED = 28;
|
|
14803
|
+
var CLICK_KEY = 33;
|
|
14804
|
+
var LONGEST_RELEASE = 2.5;
|
|
14805
|
+
var SILENCE = 0.00001;
|
|
14806
|
+
function loopWithin(loop, duration) {
|
|
14807
|
+
if (!loop || !(loop.end > loop.start))
|
|
14808
|
+
return null;
|
|
14809
|
+
if (loop.end <= duration)
|
|
14810
|
+
return loop;
|
|
14811
|
+
const back = loop.end - duration;
|
|
14812
|
+
if (loop.start - back < 0)
|
|
14813
|
+
return null;
|
|
14814
|
+
return { start: loop.start - back, end: duration };
|
|
14815
|
+
}
|
|
14816
|
+
function sampledPlayer(win, source, fallback, options = {}) {
|
|
14817
|
+
const voices = new Map;
|
|
14818
|
+
const contextOf = () => options.context?.() ?? contextFor(win);
|
|
14819
|
+
const load = async (kind, ctx) => {
|
|
14820
|
+
const sounds = kind === "click" ? { programs: [], drums: false } : {
|
|
14821
|
+
programs: [kind === "muted" ? MUTED : PROGRAMS[kind]],
|
|
14822
|
+
drums: false,
|
|
14823
|
+
click: false
|
|
14824
|
+
};
|
|
14825
|
+
const files = await instrumentSounds(source, sounds, win);
|
|
14826
|
+
const bytes = files?.[0];
|
|
14827
|
+
if (!bytes)
|
|
14828
|
+
return null;
|
|
14829
|
+
try {
|
|
14830
|
+
const font = readSoundfont(bytes);
|
|
14831
|
+
const preset = font.presets[0];
|
|
14832
|
+
if (!preset?.regions.length)
|
|
14833
|
+
return null;
|
|
14834
|
+
const buffers = new Map;
|
|
14835
|
+
const loops = new Map;
|
|
14836
|
+
await Promise.all([...new Set(preset.regions.map((r) => r.sample))].map(async (index) => {
|
|
14837
|
+
const sample = font.samples[index];
|
|
14838
|
+
loops.set(index, {
|
|
14839
|
+
start: sample.loopStart / sample.rate,
|
|
14840
|
+
end: sample.loopEnd / sample.rate
|
|
14841
|
+
});
|
|
14842
|
+
if (sample.compressed) {
|
|
14843
|
+
const ogg = font.data.slice(sample.start, sample.end);
|
|
14844
|
+
buffers.set(index, await ctx.decodeAudioData(ogg.buffer));
|
|
14845
|
+
return;
|
|
14846
|
+
}
|
|
14847
|
+
const points = sample.end - sample.start;
|
|
14848
|
+
const buffer = ctx.createBuffer(1, Math.max(1, points), sample.rate);
|
|
14849
|
+
const out = buffer.getChannelData(0);
|
|
14850
|
+
const data = new DataView(font.data.buffer, font.data.byteOffset + sample.start * 2, points * 2);
|
|
14851
|
+
for (let i = 0;i < points; i++)
|
|
14852
|
+
out[i] = data.getInt16(i * 2, true) / 32768;
|
|
14853
|
+
buffers.set(index, buffer);
|
|
14854
|
+
}));
|
|
14855
|
+
return { preset, buffers, loops };
|
|
14856
|
+
} catch {
|
|
14857
|
+
return null;
|
|
14858
|
+
}
|
|
14859
|
+
};
|
|
14860
|
+
const voiceOf = (kind, ctx) => {
|
|
14861
|
+
let voice = voices.get(kind);
|
|
14862
|
+
if (!voice) {
|
|
14863
|
+
voice = load(kind, ctx);
|
|
14864
|
+
voices.set(kind, voice);
|
|
14865
|
+
voice.then((v) => {
|
|
14866
|
+
if (!v)
|
|
14867
|
+
voices.delete(kind);
|
|
14868
|
+
});
|
|
14869
|
+
}
|
|
14870
|
+
return voice;
|
|
14871
|
+
};
|
|
14872
|
+
const kindOf = (e, sound) => e.kind === "click" ? "click" : e.kind === "muted" && sound === "guitar" ? "muted" : sound;
|
|
14873
|
+
const sound = (ctx, out, voice, region, key, level, at, end) => {
|
|
14874
|
+
const buffer = voice.buffers.get(region.sample);
|
|
14875
|
+
if (!buffer)
|
|
14876
|
+
return null;
|
|
14877
|
+
const source = ctx.createBufferSource();
|
|
14878
|
+
source.buffer = buffer;
|
|
14879
|
+
source.playbackRate.value = Math.pow(2, ((key - region.rootKey) * region.scale + region.tune) / 1200);
|
|
14880
|
+
const loop = region.loop ? loopWithin(voice.loops.get(region.sample), buffer.duration) : null;
|
|
14881
|
+
if (loop) {
|
|
14882
|
+
source.loop = true;
|
|
14883
|
+
source.loopStart = loop.start;
|
|
14884
|
+
source.loopEnd = loop.end;
|
|
14885
|
+
}
|
|
14886
|
+
const peak = level * Math.pow(10, -region.attenuation / 20) * Math.SQRT1_2;
|
|
14887
|
+
const env = ctx.createGain();
|
|
14888
|
+
const gain = env.gain;
|
|
14889
|
+
const held = at + region.attack + region.hold;
|
|
14890
|
+
if (region.attack > 0.002) {
|
|
14891
|
+
gain.setValueAtTime(0, at);
|
|
14892
|
+
gain.linearRampToValueAtTime(peak, at + region.attack);
|
|
14893
|
+
} else
|
|
14894
|
+
gain.setValueAtTime(peak, at);
|
|
14895
|
+
gain.setValueAtTime(peak, held);
|
|
14896
|
+
const release = Math.min(Math.max(region.release, 0.02), LONGEST_RELEASE);
|
|
14897
|
+
const off = Math.max(end, at + 0.006);
|
|
14898
|
+
if (region.decay > 0 && region.sustain > 0 && off > held) {
|
|
14899
|
+
const floor = Math.min(region.sustain, 100);
|
|
14900
|
+
const reached = held + region.decay * floor / 100;
|
|
14901
|
+
const down = reached <= off ? floor : 100 * (off - held) / region.decay;
|
|
14902
|
+
gain.exponentialRampToValueAtTime(Math.max(peak * Math.pow(10, -down / 20), peak * SILENCE), Math.min(reached, off));
|
|
14903
|
+
}
|
|
14904
|
+
gain.setTargetAtTime(0, off, release / 11.5);
|
|
14905
|
+
let node = source;
|
|
14906
|
+
if (region.filter !== null) {
|
|
14907
|
+
const filter = ctx.createBiquadFilter();
|
|
14908
|
+
filter.type = "lowpass";
|
|
14909
|
+
filter.frequency.value = Math.min(region.filter, ctx.sampleRate / 2 - 1);
|
|
14910
|
+
filter.Q.value = region.filterQ;
|
|
14911
|
+
const { amount, attack, hold, decay, sustain: stays } = region.filterEnvelope;
|
|
14912
|
+
if (amount) {
|
|
14913
|
+
const start = at + attack;
|
|
14914
|
+
if (attack > 0.002) {
|
|
14915
|
+
filter.detune.setValueAtTime(0, at);
|
|
14916
|
+
filter.detune.linearRampToValueAtTime(amount, start);
|
|
14917
|
+
} else
|
|
14918
|
+
filter.detune.setValueAtTime(amount, at);
|
|
14919
|
+
filter.detune.setValueAtTime(amount, start + hold);
|
|
14920
|
+
if (decay > 0 && stays < 1)
|
|
14921
|
+
filter.detune.linearRampToValueAtTime(amount * stays, start + hold + decay * (1 - stays));
|
|
14922
|
+
}
|
|
14923
|
+
source.connect(filter);
|
|
14924
|
+
node = filter;
|
|
14925
|
+
}
|
|
14926
|
+
node.connect(env);
|
|
14927
|
+
env.connect(out);
|
|
14928
|
+
source.start(at);
|
|
14929
|
+
const over = off + release;
|
|
14930
|
+
source.stop(over);
|
|
14931
|
+
return { source, over };
|
|
14932
|
+
};
|
|
14933
|
+
return {
|
|
14934
|
+
reportsStart: true,
|
|
14935
|
+
play(events, { sound: instrument, volume, onEnd, onStart }) {
|
|
14936
|
+
const ctx = contextOf();
|
|
14937
|
+
if (!ctx)
|
|
14938
|
+
return fallback.play(events, { sound: instrument, volume, onEnd, onStart });
|
|
14939
|
+
let stopped = false;
|
|
14940
|
+
let handle = null;
|
|
14941
|
+
let level = Number.isFinite(volume) ? Math.min(1, Math.max(0, volume)) : 0.8;
|
|
14942
|
+
const resumed = "resume" in ctx ? ctx.resume() : Promise.resolve();
|
|
14943
|
+
const kinds = [...new Set(events.map((e) => kindOf(e, instrument)))];
|
|
14944
|
+
Promise.all(kinds.map((kind) => voiceOf(kind, ctx))).then((loaded) => {
|
|
14945
|
+
if (stopped)
|
|
14946
|
+
return;
|
|
14947
|
+
if (loaded.some((v) => !v)) {
|
|
14948
|
+
handle = fallback.play(events, {
|
|
14949
|
+
sound: instrument,
|
|
14950
|
+
volume: level,
|
|
14951
|
+
...onEnd ? { onEnd } : {},
|
|
14952
|
+
...onStart ? { onStart } : {}
|
|
14953
|
+
});
|
|
14954
|
+
if (!fallback.reportsStart)
|
|
14955
|
+
onStart?.();
|
|
14956
|
+
return;
|
|
14957
|
+
}
|
|
14958
|
+
const byKind = new Map(kinds.map((kind, i) => [kind, loaded[i]]));
|
|
14959
|
+
const master = ctx.createGain();
|
|
14960
|
+
master.gain.value = level;
|
|
14961
|
+
master.connect(ctx.destination);
|
|
14962
|
+
const start = ctx.currentTime + 0.05;
|
|
14963
|
+
const sources = [];
|
|
14964
|
+
let tail = 0;
|
|
14965
|
+
for (const e of events) {
|
|
14966
|
+
const kind = kindOf(e, instrument);
|
|
14967
|
+
const voice = byKind.get(kind);
|
|
14968
|
+
const velocity = e.velocity === undefined || !Number.isFinite(e.velocity) ? 100 : Math.round(Math.min(127, Math.max(1, e.velocity)));
|
|
14969
|
+
const key = kind === "click" ? CLICK_KEY : e.midi;
|
|
14970
|
+
const at = start + e.time;
|
|
14971
|
+
for (const region of regionsFor(voice.preset, key, velocity)) {
|
|
14972
|
+
const made = sound(ctx, master, voice, region, key, velocity / 127, at, at + e.duration);
|
|
14973
|
+
if (!made)
|
|
14974
|
+
continue;
|
|
14975
|
+
sources.push(made.source);
|
|
14976
|
+
tail = Math.max(tail, made.over - start);
|
|
14977
|
+
}
|
|
14978
|
+
}
|
|
14979
|
+
const finish = () => master.disconnect();
|
|
14980
|
+
let heard;
|
|
14981
|
+
const announce = () => {
|
|
14982
|
+
if (stopped || !onStart)
|
|
14983
|
+
return;
|
|
14984
|
+
const latency = "outputLatency" in ctx && Number.isFinite(ctx.outputLatency) ? ctx.outputLatency : 0;
|
|
14985
|
+
heard = win.setTimeout(() => {
|
|
14986
|
+
if (!stopped)
|
|
14987
|
+
onStart();
|
|
14988
|
+
}, Math.max(0, start - ctx.currentTime + latency) * 1000);
|
|
14989
|
+
};
|
|
14990
|
+
if (onStart)
|
|
14991
|
+
Promise.resolve(resumed).then(announce, announce);
|
|
14992
|
+
const timer = win.setTimeout(() => onEnd?.(), (lengthOf(events) + 0.15) * 1000);
|
|
14993
|
+
let cleanup = win.setTimeout(finish, (tail + 0.15) * 1000);
|
|
14994
|
+
handle = {
|
|
14995
|
+
stop() {
|
|
14996
|
+
win.clearTimeout(timer);
|
|
14997
|
+
win.clearTimeout(heard);
|
|
14998
|
+
win.clearTimeout(cleanup);
|
|
14999
|
+
const now = ctx.currentTime;
|
|
15000
|
+
master.gain.setTargetAtTime(0, now, 0.01);
|
|
15001
|
+
for (const s of sources) {
|
|
15002
|
+
try {
|
|
15003
|
+
s.stop(now + 0.05);
|
|
15004
|
+
} catch {}
|
|
15005
|
+
}
|
|
15006
|
+
cleanup = win.setTimeout(finish, 80);
|
|
15007
|
+
},
|
|
15008
|
+
setVolume(next) {
|
|
15009
|
+
master.gain.setTargetAtTime(next, ctx.currentTime, 0.015);
|
|
15010
|
+
}
|
|
15011
|
+
};
|
|
15012
|
+
});
|
|
15013
|
+
return {
|
|
15014
|
+
stop() {
|
|
15015
|
+
if (stopped)
|
|
15016
|
+
return;
|
|
15017
|
+
stopped = true;
|
|
15018
|
+
handle?.stop();
|
|
15019
|
+
},
|
|
15020
|
+
setVolume(next) {
|
|
15021
|
+
if (stopped || !Number.isFinite(next))
|
|
15022
|
+
return;
|
|
15023
|
+
level = Math.min(1, Math.max(0, next));
|
|
15024
|
+
handle?.setVolume?.(level);
|
|
15025
|
+
}
|
|
15026
|
+
};
|
|
15027
|
+
}
|
|
15028
|
+
};
|
|
15029
|
+
}
|
|
14477
15030
|
// src/convert/text-tab.ts
|
|
14478
15031
|
var STRING_PREFIX = /^\s*([A-Ga-g][#b]?|[1-8](?=\s*[|:]))?\s*[|:]?/;
|
|
14479
15032
|
var STRING_BODY = /^[-0-9xX|:hpbrsvt/\\~^().*<>\s]*$/;
|
|
@@ -15470,6 +16023,7 @@ export {
|
|
|
15470
16023
|
noteOf,
|
|
15471
16024
|
noteTargets,
|
|
15472
16025
|
numeralOf,
|
|
16026
|
+
pageInstruments,
|
|
15473
16027
|
parentMajor,
|
|
15474
16028
|
parseCapo,
|
|
15475
16029
|
parseChord,
|
|
@@ -15527,6 +16081,7 @@ export {
|
|
|
15527
16081
|
rootMarkOf,
|
|
15528
16082
|
runPasses,
|
|
15529
16083
|
runProgression,
|
|
16084
|
+
sampledPlayer,
|
|
15530
16085
|
scaleFingers,
|
|
15531
16086
|
scaleFormula,
|
|
15532
16087
|
scaleName,
|
|
@@ -15569,5 +16124,5 @@ export {
|
|
|
15569
16124
|
withTonicizations
|
|
15570
16125
|
};
|
|
15571
16126
|
|
|
15572
|
-
//# debugId=
|
|
16127
|
+
//# debugId=BAB348232536EC5A64756E2164756E21
|
|
15573
16128
|
//# sourceMappingURL=index.js.map
|