markdown-up 4.1.0 → 4.1.1
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/lib/app.js +53 -0
- package/lib/scriptLibrary.js +266 -0
- package/package.json +1 -1
package/lib/app.js
CHANGED
|
@@ -117,6 +117,56 @@ export class MarkdownUp {
|
|
|
117
117
|
this.runtimeWindowResize = null;
|
|
118
118
|
this.runtimeTimeoutId = null;
|
|
119
119
|
this.runtimeCount = 0;
|
|
120
|
+
|
|
121
|
+
// The shared Web Audio state (for windowPlaySound), created on first use. The noise buffer is
|
|
122
|
+
// a one-second white-noise buffer reused for drum and explosion sounds.
|
|
123
|
+
this.audioContext = null;
|
|
124
|
+
this.audioNoiseBuffer = null;
|
|
125
|
+
|
|
126
|
+
// Track the live keyboard state (for windowKeyState polling). Keys are physical key codes
|
|
127
|
+
// (e.g. "ArrowUp"); "ctrl"/"shift"/"alt"/"meta" hold the latest modifier state.
|
|
128
|
+
this.keyState = {};
|
|
129
|
+
this.window.addEventListener('keydown', (event) => this.updateKeyState(event, true));
|
|
130
|
+
this.window.addEventListener('keyup', (event) => this.updateKeyState(event, false));
|
|
131
|
+
this.window.addEventListener('blur', () => {
|
|
132
|
+
for (const code of Object.keys(this.keyState)) {
|
|
133
|
+
this.keyState[code] = false;
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
// Record a keyboard key's down/up state along with the current modifier flags
|
|
140
|
+
updateKeyState(event, down) {
|
|
141
|
+
this.keyState[event.code] = down;
|
|
142
|
+
this.keyState.ctrl = event.ctrlKey;
|
|
143
|
+
this.keyState.shift = event.shiftKey;
|
|
144
|
+
this.keyState.alt = event.altKey;
|
|
145
|
+
this.keyState.meta = event.metaKey;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
// Get the shared Web Audio context, creating it on first use
|
|
150
|
+
getAudioContext() {
|
|
151
|
+
if (this.audioContext === null) {
|
|
152
|
+
this.audioContext = new this.window.AudioContext();
|
|
153
|
+
}
|
|
154
|
+
return this.audioContext;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
// Get the shared one-second white-noise buffer (for windowPlaySound drums), creating it on first use
|
|
159
|
+
getAudioNoiseBuffer() {
|
|
160
|
+
const audioContext = this.getAudioContext();
|
|
161
|
+
if (this.audioNoiseBuffer === null) {
|
|
162
|
+
const {sampleRate} = audioContext;
|
|
163
|
+
this.audioNoiseBuffer = audioContext.createBuffer(1, sampleRate, sampleRate);
|
|
164
|
+
const data = this.audioNoiseBuffer.getChannelData(0);
|
|
165
|
+
for (let ix = 0; ix < data.length; ix++) {
|
|
166
|
+
data[ix] = Math.random() * 2 - 1;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return this.audioNoiseBuffer;
|
|
120
170
|
}
|
|
121
171
|
|
|
122
172
|
|
|
@@ -464,10 +514,13 @@ export class MarkdownUp {
|
|
|
464
514
|
this.window.console.log(text);
|
|
465
515
|
};
|
|
466
516
|
const scriptOptions = {
|
|
517
|
+
'audioContextFn': () => this.getAudioContext(),
|
|
518
|
+
'audioNoiseBufferFn': () => this.getAudioNoiseBuffer(),
|
|
467
519
|
'debug': 'debug' in this.paramsSession,
|
|
468
520
|
// eslint-disable-next-line require-await
|
|
469
521
|
'fetchFn': async (fetchURL, options) => this.window.fetch(fetchURL, options),
|
|
470
522
|
'fontSize': this.paramsLocal.fontSize ?? this.fontSize,
|
|
523
|
+
'keyStateFn': () => this.keyState,
|
|
471
524
|
logFn,
|
|
472
525
|
markdownOptions,
|
|
473
526
|
'params': this.params,
|
package/lib/scriptLibrary.js
CHANGED
|
@@ -425,6 +425,270 @@ function windowHeight(unusedArgs, options) {
|
|
|
425
425
|
}
|
|
426
426
|
|
|
427
427
|
|
|
428
|
+
// $function: windowKeyState
|
|
429
|
+
// $group: window
|
|
430
|
+
// $doc: Test whether a key combination is currently held down. Unlike `documentSetKeyDown`, which
|
|
431
|
+
// $doc: fires a callback once per key press, this polls the live keyboard state and is intended for
|
|
432
|
+
// $doc: game loops and animations. The modifier-key states must match exactly, so by default the key
|
|
433
|
+
// $doc: must be pressed with no modifier keys held. For example:
|
|
434
|
+
// $doc:
|
|
435
|
+
// $doc: ```barescript
|
|
436
|
+
// $doc: if windowKeyState('ArrowLeft'):
|
|
437
|
+
// $doc: playerX = playerX - playerSpeed
|
|
438
|
+
// $doc: endif
|
|
439
|
+
// $doc: if windowKeyState('ArrowRight'):
|
|
440
|
+
// $doc: playerX = playerX + playerSpeed
|
|
441
|
+
// $doc: endif
|
|
442
|
+
// $doc: ```
|
|
443
|
+
// $doc:
|
|
444
|
+
// $doc: The key is matched against the physical key code (e.g., "ArrowUp", "KeyW", "Space", "Enter").
|
|
445
|
+
// $arg key: The physical key code (e.g., "ArrowUp", "ArrowDown", "KeyA", "KeyW", "Space")
|
|
446
|
+
// $arg ctrl: If true, the control key must be down; if false (the default), it must be up
|
|
447
|
+
// $arg shift: If true, the shift key must be down; if false (the default), it must be up
|
|
448
|
+
// $arg alt: If true, the alt key must be down; if false (the default), it must be up
|
|
449
|
+
// $arg meta: If true, the meta (command) key must be down; if false (the default), it must be up
|
|
450
|
+
// $return: true if the key is down and all modifier-key states match, false otherwise
|
|
451
|
+
function windowKeyState(args, options) {
|
|
452
|
+
const [key, ctrl, shift, alt, meta] = valueArgsValidate(windowKeyStateArgs, args);
|
|
453
|
+
const keyState = options.keyStateFn();
|
|
454
|
+
return !!keyState[key] &&
|
|
455
|
+
!ctrl === !keyState.ctrl && !shift === !keyState.shift && !alt === !keyState.alt && !meta === !keyState.meta;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const windowKeyStateArgs = valueArgsModel([
|
|
459
|
+
{'name': 'key', 'type': 'string'},
|
|
460
|
+
{'name': 'ctrl', 'type': 'boolean'},
|
|
461
|
+
{'name': 'shift', 'type': 'boolean'},
|
|
462
|
+
{'name': 'alt', 'type': 'boolean'},
|
|
463
|
+
{'name': 'meta', 'type': 'boolean'}
|
|
464
|
+
]);
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
// $function: windowPlaySound
|
|
468
|
+
// $group: window
|
|
469
|
+
// $doc: Play a generated sound effect. Unknown sounds are ignored. The sound name is one of:
|
|
470
|
+
// $doc:
|
|
471
|
+
// $doc: - **UI** - "beep", "click", "error", "success", "warning"
|
|
472
|
+
// $doc: - **Arcade** - "coin", "jump", "laser", "explosion", "powerup", "powerdown", "hit", "blip", "gameover"
|
|
473
|
+
// $doc: - **Notes** - "noteC4", "noteCs4", "noteD4", "noteDs4", "noteE4", "noteF4", "noteFs4", "noteG4",
|
|
474
|
+
// $doc: "noteGs4", "noteA4", "noteAs4", "noteB4", "noteC5"
|
|
475
|
+
// $doc: - **Drums** - "drumKick", "drumSnare", "drumHihat", "drumOpenhat", "drumTomLow", "drumTomMid",
|
|
476
|
+
// $doc: "drumTomHigh", "drumClap", "drumCrash", "drumRide"
|
|
477
|
+
// $arg sound: The sound name
|
|
478
|
+
function windowPlaySound(args, options) {
|
|
479
|
+
const [sound] = valueArgsValidate(windowPlaySoundArgs, args);
|
|
480
|
+
|
|
481
|
+
// Unknown sound? Debug-log and do nothing else.
|
|
482
|
+
const tones = windowPlaySoundTones[sound] ?? null;
|
|
483
|
+
if (tones === null) {
|
|
484
|
+
if (options.debug) {
|
|
485
|
+
options.logFn(`MarkdownUp: Unknown windowPlaySound sound "${sound}"`);
|
|
486
|
+
}
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// Get the audio context and resume it if suspended (required by most browsers)
|
|
491
|
+
const audioContext = options.audioContextFn();
|
|
492
|
+
if (audioContext.state === 'suspended') {
|
|
493
|
+
audioContext.resume();
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Gain envelope: a near-silent floor (exponential ramps cannot reach zero) and a short attack (seconds)
|
|
497
|
+
const silence = 0.0001;
|
|
498
|
+
const attack = 0.005;
|
|
499
|
+
|
|
500
|
+
// Schedule each tone in the sound's sequence
|
|
501
|
+
let startTime = audioContext.currentTime;
|
|
502
|
+
for (const tone of tones) {
|
|
503
|
+
const {type, frequency, duration, volume} = tone;
|
|
504
|
+
const frequencyEnd = tone.frequencyEnd ?? null;
|
|
505
|
+
const filter = tone.filter ?? null;
|
|
506
|
+
startTime += (tone.gap ?? 0) / 1000;
|
|
507
|
+
const stopTime = startTime + duration / 1000;
|
|
508
|
+
|
|
509
|
+
// Create the source node - a white-noise burst (for drums and explosions) or an oscillator tone
|
|
510
|
+
let source;
|
|
511
|
+
if (type === 'noise') {
|
|
512
|
+
source = audioContext.createBufferSource();
|
|
513
|
+
source.buffer = options.audioNoiseBufferFn();
|
|
514
|
+
// Loop the (one-second) noise buffer so tones longer than it still sustain
|
|
515
|
+
source.loop = true;
|
|
516
|
+
} else {
|
|
517
|
+
source = audioContext.createOscillator();
|
|
518
|
+
source.type = type;
|
|
519
|
+
source.frequency.setValueAtTime(frequency, startTime);
|
|
520
|
+
if (frequencyEnd !== null) {
|
|
521
|
+
source.frequency.exponentialRampToValueAtTime(frequencyEnd, stopTime);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// Optional band filter (e.g., the high frequencies of a cymbal or the low boom of an explosion)
|
|
526
|
+
let output = source;
|
|
527
|
+
if (filter !== null) {
|
|
528
|
+
const filterNode = audioContext.createBiquadFilter();
|
|
529
|
+
filterNode.type = filter.type;
|
|
530
|
+
filterNode.frequency.value = filter.frequency;
|
|
531
|
+
source.connect(filterNode);
|
|
532
|
+
output = filterNode;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Apply a percussive gain envelope, clamping the attack to the stop time so the ramp events
|
|
536
|
+
// stay ordered even for very short tones
|
|
537
|
+
const gain = audioContext.createGain();
|
|
538
|
+
const attackTime = Math.min(startTime + attack, stopTime);
|
|
539
|
+
gain.gain.setValueAtTime(silence, startTime);
|
|
540
|
+
gain.gain.exponentialRampToValueAtTime(volume, attackTime);
|
|
541
|
+
gain.gain.exponentialRampToValueAtTime(silence, stopTime);
|
|
542
|
+
output.connect(gain);
|
|
543
|
+
gain.connect(audioContext.destination);
|
|
544
|
+
|
|
545
|
+
source.start(startTime);
|
|
546
|
+
source.stop(stopTime);
|
|
547
|
+
startTime = stopTime;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const windowPlaySoundArgs = valueArgsModel([
|
|
552
|
+
{'name': 'sound', 'type': 'string'}
|
|
553
|
+
]);
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
// The generated sounds, each a sequence of tones. A tone is one of an oscillator ("sine", "square",
|
|
557
|
+
// "triangle", "sawtooth") or a "noise" burst. "frequencyEnd" ramps the oscillator pitch for sweeps,
|
|
558
|
+
// "filter" band-passes the source (e.g. cymbals), and "gap" is the silence, in milliseconds, before it.
|
|
559
|
+
const windowPlaySoundTones = {
|
|
560
|
+
// UI
|
|
561
|
+
'beep': [
|
|
562
|
+
{'type': 'sine', 'frequency': 880, 'duration': 180, 'volume': 0.3}
|
|
563
|
+
],
|
|
564
|
+
'click': [
|
|
565
|
+
{'type': 'square', 'frequency': 1200, 'duration': 40, 'volume': 0.2}
|
|
566
|
+
],
|
|
567
|
+
'error': [
|
|
568
|
+
{'type': 'square', 'frequency': 350, 'duration': 250, 'volume': 0.4}
|
|
569
|
+
],
|
|
570
|
+
'success': [
|
|
571
|
+
{'type': 'sine', 'frequency': 660, 'duration': 120, 'volume': 0.3},
|
|
572
|
+
{'type': 'sine', 'frequency': 990, 'duration': 200, 'volume': 0.3}
|
|
573
|
+
],
|
|
574
|
+
'warning': [
|
|
575
|
+
{'type': 'triangle', 'frequency': 440, 'duration': 160, 'volume': 0.4},
|
|
576
|
+
{'type': 'triangle', 'frequency': 440, 'duration': 160, 'volume': 0.4, 'gap': 80}
|
|
577
|
+
],
|
|
578
|
+
|
|
579
|
+
// Arcade
|
|
580
|
+
'coin': [
|
|
581
|
+
{'type': 'square', 'frequency': 988, 'duration': 80, 'volume': 0.3},
|
|
582
|
+
{'type': 'square', 'frequency': 1319, 'duration': 200, 'volume': 0.3}
|
|
583
|
+
],
|
|
584
|
+
'jump': [
|
|
585
|
+
{'type': 'square', 'frequency': 300, 'frequencyEnd': 800, 'duration': 150, 'volume': 0.3}
|
|
586
|
+
],
|
|
587
|
+
'laser': [
|
|
588
|
+
{'type': 'sawtooth', 'frequency': 1200, 'frequencyEnd': 200, 'duration': 250, 'volume': 0.4}
|
|
589
|
+
],
|
|
590
|
+
'explosion': [
|
|
591
|
+
{'type': 'noise', 'duration': 1600, 'volume': 0.9, 'filter': {'type': 'lowpass', 'frequency': 800}}
|
|
592
|
+
],
|
|
593
|
+
'powerup': [
|
|
594
|
+
{'type': 'square', 'frequency': 523, 'duration': 80, 'volume': 0.3},
|
|
595
|
+
{'type': 'square', 'frequency': 659, 'duration': 80, 'volume': 0.3},
|
|
596
|
+
{'type': 'square', 'frequency': 784, 'duration': 80, 'volume': 0.3},
|
|
597
|
+
{'type': 'square', 'frequency': 1047, 'duration': 160, 'volume': 0.3}
|
|
598
|
+
],
|
|
599
|
+
'powerdown': [
|
|
600
|
+
{'type': 'square', 'frequency': 1047, 'duration': 80, 'volume': 0.3},
|
|
601
|
+
{'type': 'square', 'frequency': 784, 'duration': 80, 'volume': 0.3},
|
|
602
|
+
{'type': 'square', 'frequency': 659, 'duration': 80, 'volume': 0.3},
|
|
603
|
+
{'type': 'square', 'frequency': 523, 'duration': 160, 'volume': 0.3}
|
|
604
|
+
],
|
|
605
|
+
'hit': [
|
|
606
|
+
{'type': 'square', 'frequency': 400, 'frequencyEnd': 100, 'duration': 120, 'volume': 0.3}
|
|
607
|
+
],
|
|
608
|
+
'blip': [
|
|
609
|
+
{'type': 'square', 'frequency': 1000, 'duration': 40, 'volume': 0.25}
|
|
610
|
+
],
|
|
611
|
+
'gameover': [
|
|
612
|
+
{'type': 'square', 'frequency': 392, 'duration': 180, 'volume': 0.3},
|
|
613
|
+
{'type': 'square', 'frequency': 330, 'duration': 180, 'volume': 0.3},
|
|
614
|
+
{'type': 'square', 'frequency': 262, 'duration': 320, 'volume': 0.3}
|
|
615
|
+
],
|
|
616
|
+
|
|
617
|
+
// Musical notes (one chromatic octave, C4 - C5, equal temperament)
|
|
618
|
+
'noteC4': [
|
|
619
|
+
{'type': 'triangle', 'frequency': 261.63, 'duration': 400, 'volume': 0.3}
|
|
620
|
+
],
|
|
621
|
+
'noteCs4': [
|
|
622
|
+
{'type': 'triangle', 'frequency': 277.18, 'duration': 400, 'volume': 0.3}
|
|
623
|
+
],
|
|
624
|
+
'noteD4': [
|
|
625
|
+
{'type': 'triangle', 'frequency': 293.66, 'duration': 400, 'volume': 0.3}
|
|
626
|
+
],
|
|
627
|
+
'noteDs4': [
|
|
628
|
+
{'type': 'triangle', 'frequency': 311.13, 'duration': 400, 'volume': 0.3}
|
|
629
|
+
],
|
|
630
|
+
'noteE4': [
|
|
631
|
+
{'type': 'triangle', 'frequency': 329.63, 'duration': 400, 'volume': 0.3}
|
|
632
|
+
],
|
|
633
|
+
'noteF4': [
|
|
634
|
+
{'type': 'triangle', 'frequency': 349.23, 'duration': 400, 'volume': 0.3}
|
|
635
|
+
],
|
|
636
|
+
'noteFs4': [
|
|
637
|
+
{'type': 'triangle', 'frequency': 369.99, 'duration': 400, 'volume': 0.3}
|
|
638
|
+
],
|
|
639
|
+
'noteG4': [
|
|
640
|
+
{'type': 'triangle', 'frequency': 392.00, 'duration': 400, 'volume': 0.3}
|
|
641
|
+
],
|
|
642
|
+
'noteGs4': [
|
|
643
|
+
{'type': 'triangle', 'frequency': 415.30, 'duration': 400, 'volume': 0.3}
|
|
644
|
+
],
|
|
645
|
+
'noteA4': [
|
|
646
|
+
{'type': 'triangle', 'frequency': 440.00, 'duration': 400, 'volume': 0.3}
|
|
647
|
+
],
|
|
648
|
+
'noteAs4': [
|
|
649
|
+
{'type': 'triangle', 'frequency': 466.16, 'duration': 400, 'volume': 0.3}
|
|
650
|
+
],
|
|
651
|
+
'noteB4': [
|
|
652
|
+
{'type': 'triangle', 'frequency': 493.88, 'duration': 400, 'volume': 0.3}
|
|
653
|
+
],
|
|
654
|
+
'noteC5': [
|
|
655
|
+
{'type': 'triangle', 'frequency': 523.25, 'duration': 400, 'volume': 0.3}
|
|
656
|
+
],
|
|
657
|
+
|
|
658
|
+
// Drum set
|
|
659
|
+
'drumKick': [
|
|
660
|
+
{'type': 'sine', 'frequency': 150, 'frequencyEnd': 50, 'duration': 180, 'volume': 0.9}
|
|
661
|
+
],
|
|
662
|
+
'drumSnare': [
|
|
663
|
+
{'type': 'noise', 'duration': 200, 'volume': 0.6, 'filter': {'type': 'highpass', 'frequency': 1500}}
|
|
664
|
+
],
|
|
665
|
+
'drumHihat': [
|
|
666
|
+
{'type': 'noise', 'duration': 50, 'volume': 0.4, 'filter': {'type': 'highpass', 'frequency': 7000}}
|
|
667
|
+
],
|
|
668
|
+
'drumOpenhat': [
|
|
669
|
+
{'type': 'noise', 'duration': 300, 'volume': 0.4, 'filter': {'type': 'highpass', 'frequency': 7000}}
|
|
670
|
+
],
|
|
671
|
+
'drumTomLow': [
|
|
672
|
+
{'type': 'sine', 'frequency': 160, 'frequencyEnd': 80, 'duration': 260, 'volume': 0.7}
|
|
673
|
+
],
|
|
674
|
+
'drumTomMid': [
|
|
675
|
+
{'type': 'sine', 'frequency': 220, 'frequencyEnd': 110, 'duration': 220, 'volume': 0.7}
|
|
676
|
+
],
|
|
677
|
+
'drumTomHigh': [
|
|
678
|
+
{'type': 'sine', 'frequency': 300, 'frequencyEnd': 150, 'duration': 200, 'volume': 0.7}
|
|
679
|
+
],
|
|
680
|
+
'drumClap': [
|
|
681
|
+
{'type': 'noise', 'duration': 120, 'volume': 0.5, 'filter': {'type': 'highpass', 'frequency': 1000}}
|
|
682
|
+
],
|
|
683
|
+
'drumCrash': [
|
|
684
|
+
{'type': 'noise', 'duration': 800, 'volume': 0.5, 'filter': {'type': 'highpass', 'frequency': 5000}}
|
|
685
|
+
],
|
|
686
|
+
'drumRide': [
|
|
687
|
+
{'type': 'noise', 'duration': 500, 'volume': 0.35, 'filter': {'type': 'highpass', 'frequency': 8000}}
|
|
688
|
+
]
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
|
|
428
692
|
// $function: windowSetLocation
|
|
429
693
|
// $group: window
|
|
430
694
|
// $doc: Navigate the browser window to a location URL
|
|
@@ -547,6 +811,8 @@ export const markdownScriptFunctions = {
|
|
|
547
811
|
windowClipboardRead,
|
|
548
812
|
windowClipboardWrite,
|
|
549
813
|
windowHeight,
|
|
814
|
+
windowKeyState,
|
|
815
|
+
windowPlaySound,
|
|
550
816
|
windowSetLocation,
|
|
551
817
|
windowSetResize,
|
|
552
818
|
windowSetTimeout,
|