@schmooky/zvuk 1.7.2 → 1.8.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/dist/cli.js +1 -1
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +167 -2
- package/dist/index.js +418 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -617,7 +617,6 @@ var Bus = class {
|
|
|
617
617
|
output;
|
|
618
618
|
_level;
|
|
619
619
|
_muted;
|
|
620
|
-
_baseLevel;
|
|
621
620
|
ctx;
|
|
622
621
|
_concurrency;
|
|
623
622
|
_voices = /* @__PURE__ */ new Set();
|
|
@@ -631,7 +630,6 @@ var Bus = class {
|
|
|
631
630
|
this.name = name;
|
|
632
631
|
this._level = config.level ?? 1;
|
|
633
632
|
this._muted = config.mute ?? false;
|
|
634
|
-
this._baseLevel = this._level;
|
|
635
633
|
this._concurrency = config.concurrency ?? null;
|
|
636
634
|
this.input = ctx.createGain();
|
|
637
635
|
this.fxInput = ctx.createGain();
|
|
@@ -645,7 +643,6 @@ var Bus = class {
|
|
|
645
643
|
}
|
|
646
644
|
set level(v) {
|
|
647
645
|
this._level = clamp01(v);
|
|
648
|
-
this._baseLevel = this._level;
|
|
649
646
|
if (!this._muted) this.rampOutput(this._level, 0.01);
|
|
650
647
|
}
|
|
651
648
|
get muted() {
|
|
@@ -659,7 +656,6 @@ var Bus = class {
|
|
|
659
656
|
fadeTo(target, duration, curve = "linear") {
|
|
660
657
|
const to = clamp01(target);
|
|
661
658
|
this._level = to;
|
|
662
|
-
this._baseLevel = to;
|
|
663
659
|
return this.ramp(to, duration, curve);
|
|
664
660
|
}
|
|
665
661
|
/** Active voice count on this bus. */
|
|
@@ -903,7 +899,7 @@ function canPlay(mime) {
|
|
|
903
899
|
}
|
|
904
900
|
function mimeForUrl(url) {
|
|
905
901
|
const m = /\.([a-z0-9]+)(?:[?#]|$)/i.exec(url);
|
|
906
|
-
if (!m
|
|
902
|
+
if (!m?.[1]) return null;
|
|
907
903
|
return EXT_TO_MIME[m[1].toLowerCase()] ?? null;
|
|
908
904
|
}
|
|
909
905
|
function pickSource(urls) {
|
|
@@ -1277,8 +1273,379 @@ var Scheduler = class {
|
|
|
1277
1273
|
}
|
|
1278
1274
|
};
|
|
1279
1275
|
|
|
1280
|
-
// src/sources/
|
|
1276
|
+
// src/sources/music.ts
|
|
1281
1277
|
var DEFAULT_STOP_FADE_SEC = 8e-3;
|
|
1278
|
+
var SKIP_NOW_FADE_SEC = 0.05;
|
|
1279
|
+
var Music = class {
|
|
1280
|
+
name;
|
|
1281
|
+
deps;
|
|
1282
|
+
constructor(name, deps) {
|
|
1283
|
+
this.name = name;
|
|
1284
|
+
this.deps = deps;
|
|
1285
|
+
}
|
|
1286
|
+
get loopDuration() {
|
|
1287
|
+
return this.deps.buffers.loop.duration;
|
|
1288
|
+
}
|
|
1289
|
+
get hasIntro() {
|
|
1290
|
+
return this.deps.buffers.intro != null;
|
|
1291
|
+
}
|
|
1292
|
+
get hasOutro() {
|
|
1293
|
+
return this.deps.buffers.outro != null;
|
|
1294
|
+
}
|
|
1295
|
+
play(options = {}) {
|
|
1296
|
+
return new MusicVoice({
|
|
1297
|
+
ctx: this.deps.ctx,
|
|
1298
|
+
buffers: this.deps.buffers,
|
|
1299
|
+
destination: this.deps.destination,
|
|
1300
|
+
loopCrossfade: this.deps.loopCrossfade,
|
|
1301
|
+
defaultStopFade: this.deps.defaultStopFade,
|
|
1302
|
+
options
|
|
1303
|
+
});
|
|
1304
|
+
}
|
|
1305
|
+
};
|
|
1306
|
+
var MusicVoice = class {
|
|
1307
|
+
ended;
|
|
1308
|
+
ctx;
|
|
1309
|
+
buffers;
|
|
1310
|
+
destination;
|
|
1311
|
+
gain;
|
|
1312
|
+
state = "intro";
|
|
1313
|
+
done = false;
|
|
1314
|
+
resolveEnded;
|
|
1315
|
+
stopFade;
|
|
1316
|
+
loopCrossfadeSec;
|
|
1317
|
+
// Live audio sources + their per-segment gain nodes. The chain is the
|
|
1318
|
+
// (1+) loop iterations under crossfade-mode; when crossfade is off this
|
|
1319
|
+
// is at most one live loop source.
|
|
1320
|
+
introSource = null;
|
|
1321
|
+
outroSource = null;
|
|
1322
|
+
loopChain = [];
|
|
1323
|
+
loopArmTimer = null;
|
|
1324
|
+
outroTimer = null;
|
|
1325
|
+
// Wall-clock anchor for the loop chain — when the next iteration is due
|
|
1326
|
+
// to start in audioContext time. Used by skipToOutro({ at: 'loop-end' }).
|
|
1327
|
+
nextLoopBoundaryAt = Number.POSITIVE_INFINITY;
|
|
1328
|
+
constructor(deps) {
|
|
1329
|
+
this.ctx = deps.ctx;
|
|
1330
|
+
this.buffers = deps.buffers;
|
|
1331
|
+
this.destination = deps.destination;
|
|
1332
|
+
this.loopCrossfadeSec = Math.max(0, deps.loopCrossfade);
|
|
1333
|
+
this.stopFade = Math.max(0, deps.defaultStopFade ?? DEFAULT_STOP_FADE_SEC);
|
|
1334
|
+
this.gain = this.ctx.createGain();
|
|
1335
|
+
const initialVolume = clamp012(deps.options.volume ?? 1);
|
|
1336
|
+
const fadeIn = Math.max(0, deps.options.fadeIn ?? 0);
|
|
1337
|
+
if (fadeIn > 0) {
|
|
1338
|
+
this.gain.gain.setValueAtTime(0, this.ctx.currentTime);
|
|
1339
|
+
this.gain.gain.linearRampToValueAtTime(initialVolume, this.ctx.currentTime + fadeIn);
|
|
1340
|
+
} else {
|
|
1341
|
+
this.gain.gain.value = initialVolume;
|
|
1342
|
+
}
|
|
1343
|
+
this.gain.connect(this.destination);
|
|
1344
|
+
this.ended = new Promise((resolve) => {
|
|
1345
|
+
this.resolveEnded = resolve;
|
|
1346
|
+
});
|
|
1347
|
+
this.start(this.ctx.currentTime);
|
|
1348
|
+
}
|
|
1349
|
+
/** Currently-sounding part. Transitions automatically as parts hand off. */
|
|
1350
|
+
get currentPart() {
|
|
1351
|
+
return this.state;
|
|
1352
|
+
}
|
|
1353
|
+
fade(opts) {
|
|
1354
|
+
const param = this.gain.gain;
|
|
1355
|
+
const now = this.ctx.currentTime;
|
|
1356
|
+
applyRamp(param, now, clamp012(opts.to), opts.duration, opts.curve ?? "linear");
|
|
1357
|
+
return new Promise((res) => setTimeout(res, Math.max(0, opts.duration) * 1e3));
|
|
1358
|
+
}
|
|
1359
|
+
/**
|
|
1360
|
+
* Stop the music with the same click-free fade-out semantics as
|
|
1361
|
+
* `voice.stop()`. Skips the outro — call `skipToOutro` first if you
|
|
1362
|
+
* want the music to end musically.
|
|
1363
|
+
*/
|
|
1364
|
+
stop(opts = {}) {
|
|
1365
|
+
if (this.done) return;
|
|
1366
|
+
const fade = Math.max(0, opts.fade ?? this.stopFade);
|
|
1367
|
+
this.cancelTimers();
|
|
1368
|
+
if (fade === 0) {
|
|
1369
|
+
this.stopAllSources(0);
|
|
1370
|
+
this.finish();
|
|
1371
|
+
return;
|
|
1372
|
+
}
|
|
1373
|
+
const now = this.ctx.currentTime;
|
|
1374
|
+
const stopAt = now + fade;
|
|
1375
|
+
try {
|
|
1376
|
+
const param = this.gain.gain;
|
|
1377
|
+
param.cancelScheduledValues(now);
|
|
1378
|
+
param.setValueAtTime(param.value, now);
|
|
1379
|
+
param.linearRampToValueAtTime(0, stopAt);
|
|
1380
|
+
this.stopAllSources(stopAt);
|
|
1381
|
+
} catch {
|
|
1382
|
+
this.finish();
|
|
1383
|
+
return;
|
|
1384
|
+
}
|
|
1385
|
+
setTimeout(() => this.finish(), fade * 1e3);
|
|
1386
|
+
}
|
|
1387
|
+
/**
|
|
1388
|
+
* Schedule the outro. With `at: 'loop-end'` (default) the outro starts
|
|
1389
|
+
* at the next natural loop boundary so the music ends musically. With
|
|
1390
|
+
* `at: 'now'` the loop fades out (~50 ms) and the outro starts
|
|
1391
|
+
* immediately — useful when responsiveness matters more than musicality
|
|
1392
|
+
* (e.g. user pressed Stop).
|
|
1393
|
+
*
|
|
1394
|
+
* No-op if there is no outro buffer, or if the music is already past
|
|
1395
|
+
* the loop part. Calling `skipToOutro` more than once is a no-op too —
|
|
1396
|
+
* the first call wins.
|
|
1397
|
+
*/
|
|
1398
|
+
skipToOutro(opts = {}) {
|
|
1399
|
+
if (this.done) return;
|
|
1400
|
+
if (this.state === "outro" || this.state === "ended") return;
|
|
1401
|
+
if (!this.buffers.outro) {
|
|
1402
|
+
this.stop();
|
|
1403
|
+
return;
|
|
1404
|
+
}
|
|
1405
|
+
const at = opts.at ?? "loop-end";
|
|
1406
|
+
if (at === "now") {
|
|
1407
|
+
this.skipToOutroNow();
|
|
1408
|
+
} else {
|
|
1409
|
+
this.skipToOutroAtLoopEnd();
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
start(when) {
|
|
1413
|
+
if (this.buffers.intro) {
|
|
1414
|
+
this.scheduleIntro(when);
|
|
1415
|
+
this.scheduleLoopStart(when + this.buffers.intro.duration);
|
|
1416
|
+
} else {
|
|
1417
|
+
this.state = "loop";
|
|
1418
|
+
this.scheduleLoopStart(when);
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
scheduleIntro(when) {
|
|
1422
|
+
const src = this.ctx.createBufferSource();
|
|
1423
|
+
src.buffer = this.buffers.intro;
|
|
1424
|
+
src.connect(this.gain);
|
|
1425
|
+
src.start(when);
|
|
1426
|
+
src.onended = () => {
|
|
1427
|
+
if (this.introSource === src) this.introSource = null;
|
|
1428
|
+
};
|
|
1429
|
+
this.introSource = src;
|
|
1430
|
+
}
|
|
1431
|
+
scheduleLoopStart(when) {
|
|
1432
|
+
if (this.done) return;
|
|
1433
|
+
const segment = this.spawnLoopSegment(
|
|
1434
|
+
when,
|
|
1435
|
+
/* fadeIn */
|
|
1436
|
+
false
|
|
1437
|
+
);
|
|
1438
|
+
this.nextLoopBoundaryAt = when + this.buffers.loop.duration;
|
|
1439
|
+
if (this.loopCrossfadeSec > 0 && this.crossfadeViable()) {
|
|
1440
|
+
this.armNextLoopSegment(when);
|
|
1441
|
+
} else {
|
|
1442
|
+
segment.source.loop = true;
|
|
1443
|
+
const lr = this.buffers.loop;
|
|
1444
|
+
segment.source.loopStart = 0;
|
|
1445
|
+
segment.source.loopEnd = lr.duration;
|
|
1446
|
+
if (!this.buffers.intro) this.state = "loop";
|
|
1447
|
+
else {
|
|
1448
|
+
const ms = Math.max(0, (when - this.ctx.currentTime) * 1e3);
|
|
1449
|
+
setTimeout(() => {
|
|
1450
|
+
if (!this.done && this.state === "intro") this.state = "loop";
|
|
1451
|
+
}, ms);
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
spawnLoopSegment(when, fadeIn) {
|
|
1456
|
+
const src = this.ctx.createBufferSource();
|
|
1457
|
+
src.buffer = this.buffers.loop;
|
|
1458
|
+
src.loop = false;
|
|
1459
|
+
const segGain = this.ctx.createGain();
|
|
1460
|
+
if (fadeIn) {
|
|
1461
|
+
segGain.gain.setValueAtTime(0, when);
|
|
1462
|
+
segGain.gain.linearRampToValueAtTime(1, when + this.loopCrossfadeSec);
|
|
1463
|
+
} else {
|
|
1464
|
+
segGain.gain.setValueAtTime(1, when);
|
|
1465
|
+
}
|
|
1466
|
+
if (this.crossfadeViable()) {
|
|
1467
|
+
const fadeOutAt = when + this.buffers.loop.duration - this.loopCrossfadeSec;
|
|
1468
|
+
segGain.gain.setValueAtTime(1, fadeOutAt);
|
|
1469
|
+
segGain.gain.linearRampToValueAtTime(0, when + this.buffers.loop.duration);
|
|
1470
|
+
}
|
|
1471
|
+
src.connect(segGain).connect(this.gain);
|
|
1472
|
+
try {
|
|
1473
|
+
if (this.crossfadeViable()) {
|
|
1474
|
+
src.start(when, 0, this.buffers.loop.duration);
|
|
1475
|
+
} else {
|
|
1476
|
+
src.start(when);
|
|
1477
|
+
}
|
|
1478
|
+
} catch {
|
|
1479
|
+
}
|
|
1480
|
+
src.onended = () => this.releaseLoopSegment(src, segGain);
|
|
1481
|
+
const entry = { source: src, gain: segGain, startTime: when };
|
|
1482
|
+
this.loopChain.push(entry);
|
|
1483
|
+
return entry;
|
|
1484
|
+
}
|
|
1485
|
+
armNextLoopSegment(currentSegmentStart) {
|
|
1486
|
+
if (this.loopArmTimer != null) {
|
|
1487
|
+
clearTimeout(this.loopArmTimer);
|
|
1488
|
+
this.loopArmTimer = null;
|
|
1489
|
+
}
|
|
1490
|
+
const nextStart = currentSegmentStart + this.buffers.loop.duration - this.loopCrossfadeSec;
|
|
1491
|
+
const SAFETY_LEAD_MS = 50;
|
|
1492
|
+
const delayMs = Math.max(0, (nextStart - this.ctx.currentTime) * 1e3 - SAFETY_LEAD_MS);
|
|
1493
|
+
this.loopArmTimer = setTimeout(() => {
|
|
1494
|
+
this.loopArmTimer = null;
|
|
1495
|
+
if (this.done || this.state !== "loop" && this.state !== "intro") return;
|
|
1496
|
+
this.state = "loop";
|
|
1497
|
+
this.spawnLoopSegment(
|
|
1498
|
+
nextStart,
|
|
1499
|
+
/* fadeIn */
|
|
1500
|
+
true
|
|
1501
|
+
);
|
|
1502
|
+
this.nextLoopBoundaryAt = nextStart + this.buffers.loop.duration;
|
|
1503
|
+
this.armNextLoopSegment(nextStart);
|
|
1504
|
+
}, delayMs);
|
|
1505
|
+
}
|
|
1506
|
+
crossfadeViable() {
|
|
1507
|
+
return this.loopCrossfadeSec > 0 && this.buffers.loop.duration > this.loopCrossfadeSec * 2;
|
|
1508
|
+
}
|
|
1509
|
+
releaseLoopSegment(src, gain) {
|
|
1510
|
+
const idx = this.loopChain.findIndex((e) => e.source === src);
|
|
1511
|
+
if (idx >= 0) this.loopChain.splice(idx, 1);
|
|
1512
|
+
try {
|
|
1513
|
+
src.disconnect();
|
|
1514
|
+
gain.disconnect();
|
|
1515
|
+
} catch {
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
skipToOutroAtLoopEnd() {
|
|
1519
|
+
if (this.loopArmTimer != null) {
|
|
1520
|
+
clearTimeout(this.loopArmTimer);
|
|
1521
|
+
this.loopArmTimer = null;
|
|
1522
|
+
}
|
|
1523
|
+
let outroAt;
|
|
1524
|
+
if (this.state === "intro") {
|
|
1525
|
+
outroAt = this.ctx.currentTime + (this.buffers.intro?.duration ?? 0);
|
|
1526
|
+
this.cancelLoopArm();
|
|
1527
|
+
} else {
|
|
1528
|
+
outroAt = this.nextLoopBoundaryAt;
|
|
1529
|
+
}
|
|
1530
|
+
this.stopLoopSourcesAt(outroAt);
|
|
1531
|
+
this.scheduleOutro(outroAt);
|
|
1532
|
+
}
|
|
1533
|
+
skipToOutroNow() {
|
|
1534
|
+
this.cancelLoopArm();
|
|
1535
|
+
const now = this.ctx.currentTime;
|
|
1536
|
+
const fadeEnd = now + SKIP_NOW_FADE_SEC;
|
|
1537
|
+
for (const entry of this.loopChain) {
|
|
1538
|
+
try {
|
|
1539
|
+
const param = entry.gain.gain;
|
|
1540
|
+
param.cancelScheduledValues(now);
|
|
1541
|
+
param.setValueAtTime(param.value, now);
|
|
1542
|
+
param.linearRampToValueAtTime(0, fadeEnd);
|
|
1543
|
+
} catch {
|
|
1544
|
+
}
|
|
1545
|
+
try {
|
|
1546
|
+
entry.source.stop(fadeEnd);
|
|
1547
|
+
} catch {
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
if (this.introSource) {
|
|
1551
|
+
try {
|
|
1552
|
+
this.introSource.stop(fadeEnd);
|
|
1553
|
+
} catch {
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
this.scheduleOutro(fadeEnd);
|
|
1557
|
+
}
|
|
1558
|
+
cancelLoopArm() {
|
|
1559
|
+
if (this.loopArmTimer != null) {
|
|
1560
|
+
clearTimeout(this.loopArmTimer);
|
|
1561
|
+
this.loopArmTimer = null;
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
stopLoopSourcesAt(when) {
|
|
1565
|
+
for (const entry of this.loopChain) {
|
|
1566
|
+
try {
|
|
1567
|
+
entry.source.stop(when);
|
|
1568
|
+
} catch {
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
scheduleOutro(when) {
|
|
1573
|
+
const buf = this.buffers.outro;
|
|
1574
|
+
if (!buf) {
|
|
1575
|
+
const ms2 = Math.max(0, (when - this.ctx.currentTime) * 1e3);
|
|
1576
|
+
this.outroTimer = setTimeout(() => this.finish(), ms2);
|
|
1577
|
+
return;
|
|
1578
|
+
}
|
|
1579
|
+
const src = this.ctx.createBufferSource();
|
|
1580
|
+
src.buffer = buf;
|
|
1581
|
+
src.connect(this.gain);
|
|
1582
|
+
try {
|
|
1583
|
+
src.start(when);
|
|
1584
|
+
} catch {
|
|
1585
|
+
}
|
|
1586
|
+
src.onended = () => {
|
|
1587
|
+
if (this.outroSource === src) this.outroSource = null;
|
|
1588
|
+
this.finish();
|
|
1589
|
+
};
|
|
1590
|
+
this.outroSource = src;
|
|
1591
|
+
const ms = Math.max(0, (when - this.ctx.currentTime) * 1e3);
|
|
1592
|
+
this.outroTimer = setTimeout(() => {
|
|
1593
|
+
this.outroTimer = null;
|
|
1594
|
+
if (!this.done) this.state = "outro";
|
|
1595
|
+
}, ms);
|
|
1596
|
+
}
|
|
1597
|
+
cancelTimers() {
|
|
1598
|
+
if (this.loopArmTimer != null) {
|
|
1599
|
+
clearTimeout(this.loopArmTimer);
|
|
1600
|
+
this.loopArmTimer = null;
|
|
1601
|
+
}
|
|
1602
|
+
if (this.outroTimer != null) {
|
|
1603
|
+
clearTimeout(this.outroTimer);
|
|
1604
|
+
this.outroTimer = null;
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
stopAllSources(when) {
|
|
1608
|
+
const sources = [];
|
|
1609
|
+
if (this.introSource) sources.push(this.introSource);
|
|
1610
|
+
for (const e of this.loopChain) sources.push(e.source);
|
|
1611
|
+
if (this.outroSource) sources.push(this.outroSource);
|
|
1612
|
+
for (const src of sources) {
|
|
1613
|
+
try {
|
|
1614
|
+
src.onended = null;
|
|
1615
|
+
if (when === 0) src.stop();
|
|
1616
|
+
else src.stop(when);
|
|
1617
|
+
} catch {
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
finish() {
|
|
1622
|
+
if (this.done) return;
|
|
1623
|
+
this.done = true;
|
|
1624
|
+
this.state = "ended";
|
|
1625
|
+
this.cancelTimers();
|
|
1626
|
+
try {
|
|
1627
|
+
if (this.introSource) this.introSource.disconnect();
|
|
1628
|
+
for (const e of this.loopChain) {
|
|
1629
|
+
e.source.disconnect();
|
|
1630
|
+
e.gain.disconnect();
|
|
1631
|
+
}
|
|
1632
|
+
if (this.outroSource) this.outroSource.disconnect();
|
|
1633
|
+
this.gain.disconnect();
|
|
1634
|
+
} catch {
|
|
1635
|
+
}
|
|
1636
|
+
this.introSource = null;
|
|
1637
|
+
this.outroSource = null;
|
|
1638
|
+
this.loopChain = [];
|
|
1639
|
+
this.resolveEnded();
|
|
1640
|
+
}
|
|
1641
|
+
};
|
|
1642
|
+
function clamp012(v) {
|
|
1643
|
+
if (Number.isNaN(v)) return 0;
|
|
1644
|
+
return Math.min(1, Math.max(0, v));
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
// src/sources/voice.ts
|
|
1648
|
+
var DEFAULT_STOP_FADE_SEC2 = 8e-3;
|
|
1282
1649
|
var Voice = class _Voice {
|
|
1283
1650
|
id;
|
|
1284
1651
|
priority;
|
|
@@ -1342,10 +1709,10 @@ var Voice = class _Voice {
|
|
|
1342
1709
|
this.regionDuration = options.duration;
|
|
1343
1710
|
this.loopStart = options.loopStart;
|
|
1344
1711
|
this.loopEnd = options.loopEnd;
|
|
1345
|
-
this.stopFade = Math.max(0, deps.defaultStopFade ??
|
|
1712
|
+
this.stopFade = Math.max(0, deps.defaultStopFade ?? DEFAULT_STOP_FADE_SEC2);
|
|
1346
1713
|
this.gain = ctx.createGain();
|
|
1347
1714
|
const initialVolume = resolveJittered(options.volume ?? 1);
|
|
1348
|
-
this.gain.gain.value =
|
|
1715
|
+
this.gain.gain.value = clamp013(initialVolume);
|
|
1349
1716
|
this.gain.connect(destination);
|
|
1350
1717
|
this.startCtxTime = ctx.currentTime;
|
|
1351
1718
|
this.ended = new Promise((resolve) => {
|
|
@@ -1389,7 +1756,7 @@ var Voice = class _Voice {
|
|
|
1389
1756
|
fade(opts) {
|
|
1390
1757
|
const param = this.gain.gain;
|
|
1391
1758
|
const now = this.ctx.currentTime;
|
|
1392
|
-
applyRamp(param, now,
|
|
1759
|
+
applyRamp(param, now, clamp013(opts.to), opts.duration, opts.curve ?? "linear");
|
|
1393
1760
|
return new Promise((res) => setTimeout(res, Math.max(0, opts.duration) * 1e3));
|
|
1394
1761
|
}
|
|
1395
1762
|
/**
|
|
@@ -1748,7 +2115,7 @@ function resolveJittered(v, base = 1) {
|
|
|
1748
2115
|
const j = v.jitter;
|
|
1749
2116
|
return base + (Math.random() * 2 - 1) * j;
|
|
1750
2117
|
}
|
|
1751
|
-
function
|
|
2118
|
+
function clamp013(v) {
|
|
1752
2119
|
if (Number.isNaN(v)) return 0;
|
|
1753
2120
|
return Math.min(1, Math.max(0, v));
|
|
1754
2121
|
}
|
|
@@ -1877,7 +2244,7 @@ var StreamSound = class {
|
|
|
1877
2244
|
async play(options = {}) {
|
|
1878
2245
|
this.ensure();
|
|
1879
2246
|
if (!this.el) return;
|
|
1880
|
-
if (this.gain) this.gain.gain.value =
|
|
2247
|
+
if (this.gain) this.gain.gain.value = clamp014(options.volume ?? 1);
|
|
1881
2248
|
this.el.loop = options.loop ?? false;
|
|
1882
2249
|
try {
|
|
1883
2250
|
await this.el.play();
|
|
@@ -1912,11 +2279,11 @@ var StreamSound = class {
|
|
|
1912
2279
|
return this.el?.paused ?? true;
|
|
1913
2280
|
}
|
|
1914
2281
|
setVolume(v) {
|
|
1915
|
-
if (this.gain) this.gain.gain.value =
|
|
2282
|
+
if (this.gain) this.gain.gain.value = clamp014(v);
|
|
1916
2283
|
}
|
|
1917
2284
|
fade(opts) {
|
|
1918
2285
|
if (!this.gain) return Promise.resolve();
|
|
1919
|
-
applyRamp(this.gain.gain, this.ctx.currentTime,
|
|
2286
|
+
applyRamp(this.gain.gain, this.ctx.currentTime, clamp014(opts.to), opts.duration, opts.curve ?? "linear");
|
|
1920
2287
|
return new Promise((res) => setTimeout(res, Math.max(0, opts.duration) * 1e3));
|
|
1921
2288
|
}
|
|
1922
2289
|
dispose() {
|
|
@@ -1935,7 +2302,7 @@ var StreamSound = class {
|
|
|
1935
2302
|
this.gain = null;
|
|
1936
2303
|
}
|
|
1937
2304
|
};
|
|
1938
|
-
function
|
|
2305
|
+
function clamp014(v) {
|
|
1939
2306
|
if (Number.isNaN(v)) return 0;
|
|
1940
2307
|
return Math.min(1, Math.max(0, v));
|
|
1941
2308
|
}
|
|
@@ -2113,6 +2480,7 @@ var EngineImpl = class {
|
|
|
2113
2480
|
sounds = /* @__PURE__ */ new Map();
|
|
2114
2481
|
sprites = /* @__PURE__ */ new Map();
|
|
2115
2482
|
streams = /* @__PURE__ */ new Map();
|
|
2483
|
+
musics = /* @__PURE__ */ new Map();
|
|
2116
2484
|
voices = /* @__PURE__ */ new Map();
|
|
2117
2485
|
parameters = /* @__PURE__ */ new Map();
|
|
2118
2486
|
config;
|
|
@@ -2154,6 +2522,7 @@ var EngineImpl = class {
|
|
|
2154
2522
|
this.master = null;
|
|
2155
2523
|
this.sounds.clear();
|
|
2156
2524
|
this.sprites.clear();
|
|
2525
|
+
this.musics.clear();
|
|
2157
2526
|
for (const s of this.streams.values()) s.dispose();
|
|
2158
2527
|
this.streams.clear();
|
|
2159
2528
|
this.parameters.clear();
|
|
@@ -2243,6 +2612,40 @@ var EngineImpl = class {
|
|
|
2243
2612
|
hasSprite(name) {
|
|
2244
2613
|
return this.sprites.has(name);
|
|
2245
2614
|
}
|
|
2615
|
+
async loadMusic(name, parts, options = {}) {
|
|
2616
|
+
if (this.host.state === "closed") throw new EngineClosedError();
|
|
2617
|
+
this.host.touch();
|
|
2618
|
+
this.ensureGraph();
|
|
2619
|
+
const decodeOpts = { signal: options.signal };
|
|
2620
|
+
const loopBuf = await this.resolveBuffer(`__music:${name}:loop`, parts.loop, decodeOpts);
|
|
2621
|
+
const introBuf = parts.intro ? await this.resolveBuffer(`__music:${name}:intro`, parts.intro, decodeOpts) : void 0;
|
|
2622
|
+
const outroBuf = parts.outro ? await this.resolveBuffer(`__music:${name}:outro`, parts.outro, decodeOpts) : void 0;
|
|
2623
|
+
const buffers = {
|
|
2624
|
+
intro: introBuf ? options.normalize ? applyLoudnessNormalization(introBuf, options.normalize) : introBuf : void 0,
|
|
2625
|
+
loop: options.normalize ? applyLoudnessNormalization(loopBuf, options.normalize) : loopBuf,
|
|
2626
|
+
outro: outroBuf ? options.normalize ? applyLoudnessNormalization(outroBuf, options.normalize) : outroBuf : void 0
|
|
2627
|
+
};
|
|
2628
|
+
const busName = options.bus ?? this.busOrder[0];
|
|
2629
|
+
const bus = this.buses.get(busName);
|
|
2630
|
+
if (!bus) throw new BusNotFoundError(suggest(busName, [...this.buses.keys()]));
|
|
2631
|
+
const music = new Music(name, {
|
|
2632
|
+
ctx: this.host.touch(),
|
|
2633
|
+
buffers,
|
|
2634
|
+
destination: bus.input,
|
|
2635
|
+
loopCrossfade: options.loopCrossfade ?? 0,
|
|
2636
|
+
defaultStopFade: this.config.voice?.stopFade
|
|
2637
|
+
});
|
|
2638
|
+
this.musics.set(name, music);
|
|
2639
|
+
return music;
|
|
2640
|
+
}
|
|
2641
|
+
music(name) {
|
|
2642
|
+
const m = this.musics.get(name);
|
|
2643
|
+
if (!m) throw new SoundNotFoundError(suggest(name, [...this.musics.keys()]));
|
|
2644
|
+
return m;
|
|
2645
|
+
}
|
|
2646
|
+
hasMusic(name) {
|
|
2647
|
+
return this.musics.has(name);
|
|
2648
|
+
}
|
|
2246
2649
|
loadStream(name, url, options = {}) {
|
|
2247
2650
|
if (this.host.state === "closed") throw new EngineClosedError();
|
|
2248
2651
|
this.host.touch();
|
|
@@ -2448,6 +2851,6 @@ function levenshtein(a, b) {
|
|
|
2448
2851
|
return prev[b.length];
|
|
2449
2852
|
}
|
|
2450
2853
|
|
|
2451
|
-
export { AggregateDecodeError, Bus, BusNotFoundError, Compressor, DecodeError, Ducker, EngineClosedError, Filter, Master, Parameter, PreloadError, Reverb, Snapshot, Sound, SoundNotFoundError, Spatializer, Sprite, StreamSound, StretchProcessor, Voice, ZvukError, applyLoudnessNormalization, canPlay, computeNormalizationGain, createEngine, createStretchWorkletNode, ensureStretchWorklet, mimeForUrl, pickSource, pickSourceOrder };
|
|
2854
|
+
export { AggregateDecodeError, Bus, BusNotFoundError, Compressor, DecodeError, Ducker, EngineClosedError, Filter, Master, Music, MusicVoice, Parameter, PreloadError, Reverb, Snapshot, Sound, SoundNotFoundError, Spatializer, Sprite, StreamSound, StretchProcessor, Voice, ZvukError, applyLoudnessNormalization, canPlay, computeNormalizationGain, createEngine, createStretchWorkletNode, ensureStretchWorklet, mimeForUrl, pickSource, pickSourceOrder };
|
|
2452
2855
|
//# sourceMappingURL=index.js.map
|
|
2453
2856
|
//# sourceMappingURL=index.js.map
|