tetorica-fm2612 0.2.21 → 0.2.22

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.
Files changed (107) hide show
  1. package/README.md +47 -0
  2. package/RUNTIME.md +1 -1
  3. package/chip_worklet_transport.d.ts +10 -0
  4. package/chip_worklet_transport.js +5 -0
  5. package/generated/ymf262_wasm.wasm +0 -0
  6. package/generated/ymf278b_wasm.wasm +0 -0
  7. package/native_runtime_synth.d.ts +10 -4
  8. package/native_runtime_synth.js +47 -12
  9. package/node/chip_transports.d.mts +67 -0
  10. package/node/chip_transports.mjs +14 -2
  11. package/opl_synth.d.ts +200 -0
  12. package/opl_synth.js +362 -0
  13. package/package.json +6 -1
  14. package/playground_audition_guard.d.ts +21 -0
  15. package/playground_audition_guard.js +39 -0
  16. package/playground_chip_clock.d.ts +6 -1
  17. package/playground_chip_clock.js +13 -4
  18. package/playground_chip_inspector.d.ts +1 -0
  19. package/playground_chip_inspector.js +7 -5
  20. package/playground_chip_inspector_state.d.ts +41 -15
  21. package/playground_chip_inspector_state.js +53 -12
  22. package/playground_dx7.d.ts +21 -4
  23. package/playground_dx7.js +45 -6
  24. package/playground_dx7_audio.d.ts +2 -2
  25. package/playground_dx7_audio.js +5 -2
  26. package/playground_dx7_inspector.d.ts +105 -0
  27. package/playground_dx7_inspector.js +88 -0
  28. package/playground_dx7_worklet.js +97 -23
  29. package/playground_gameboy.d.ts +1 -0
  30. package/playground_gameboy_audio.js +1 -1
  31. package/playground_huc6280.d.ts +1 -0
  32. package/playground_huc6280.js +3 -2
  33. package/playground_huc6280_audio.js +1 -0
  34. package/playground_huc6280_worklet.js +9 -1
  35. package/playground_keyboard_channels.d.ts +21 -0
  36. package/playground_keyboard_channels.js +48 -0
  37. package/playground_logic_worker.js +10 -3
  38. package/playground_native_primary.d.ts +42 -2
  39. package/playground_native_primary.js +13 -3
  40. package/playground_nes.d.ts +2 -2
  41. package/playground_nes.js +8 -9
  42. package/playground_nes_audio.js +1 -0
  43. package/playground_okim6258.d.ts +1 -0
  44. package/playground_okim6258.js +3 -2
  45. package/playground_okim6258_audio.js +1 -0
  46. package/playground_okim6258_worklet.js +2 -1
  47. package/playground_opl.d.ts +14 -0
  48. package/playground_opl.js +39 -0
  49. package/playground_opl_audio.d.ts +18 -0
  50. package/playground_opl_audio.js +63 -0
  51. package/playground_opl_inspector.d.ts +79 -0
  52. package/playground_opl_inspector.js +61 -0
  53. package/playground_opl_worklet.d.ts +1 -0
  54. package/playground_opl_worklet.js +116 -0
  55. package/playground_opn.d.ts +1 -0
  56. package/playground_opn_audio.js +2 -2
  57. package/playground_opn_inspector.d.ts +49 -0
  58. package/playground_opn_inspector.js +105 -0
  59. package/playground_opn_worklet.js +9 -2
  60. package/playground_performance_processor.d.ts +25 -7
  61. package/playground_performance_processor.js +91 -12
  62. package/playground_pwm_audio.js +1 -1
  63. package/playground_rf5c164.d.ts +1 -0
  64. package/playground_rf5c164.js +3 -1
  65. package/playground_rf5c164_audio.js +1 -0
  66. package/playground_runtime.d.ts +3 -2
  67. package/playground_runtime.js +103 -20
  68. package/playground_segapsg.d.ts +1 -0
  69. package/playground_segapsg.js +8 -4
  70. package/playground_segapsg_audio.js +1 -0
  71. package/playground_segapsg_worklet.js +17 -6
  72. package/playground_snes.d.ts +1 -0
  73. package/playground_snes.js +1 -0
  74. package/playground_snes_audio.js +1 -1
  75. package/playground_snes_inspector.d.ts +61 -0
  76. package/playground_snes_inspector.js +48 -0
  77. package/playground_snes_worklet.d.ts +4 -1
  78. package/playground_snes_worklet.js +19 -1
  79. package/playground_soundchips.js +1 -1
  80. package/playground_ym2151.d.ts +3 -1
  81. package/playground_ym2151.js +2 -1
  82. package/playground_ym2151_audio.js +1 -1
  83. package/playground_ym2151_worklet.js +10 -2
  84. package/playground_ym2608.d.ts +1 -0
  85. package/playground_ym2608_audio.js +1 -1
  86. package/playground_ym2608_worklet.js +11 -2
  87. package/rf5c164-worklet.js +13 -5
  88. package/runtime-manifest.json +13 -0
  89. package/soundchip-output-worklet.js +21 -4
  90. package/soundchip.d.ts +1 -1
  91. package/soundchip.js +1 -1
  92. package/soundchip.md +20 -0
  93. package/soundchip_worklet.js +1 -1
  94. package/tetorica_synth.js +2 -2
  95. package/y8950synth.d.ts +19 -0
  96. package/y8950synth.js +15 -0
  97. package/ym2203-worklet.js +6 -1
  98. package/ym2608-worklet.js +6 -1
  99. package/ym2610b-worklet.js +6 -1
  100. package/ym2612-worklet-nuked.js +15 -3
  101. package/ym2612-worklet.js +15 -3
  102. package/ym3526synth.d.ts +19 -0
  103. package/ym3526synth.js +15 -0
  104. package/ym3812synth.d.ts +19 -0
  105. package/ym3812synth.js +15 -0
  106. package/ymf262synth.d.ts +19 -0
  107. package/ymf262synth.js +15 -0
package/README.md CHANGED
@@ -32,6 +32,7 @@ entry points, rather than every internal Worker or Worklet module.
32
32
  | YM2608 FM, SSG, rhythm and ADPCM-B | `tetorica-fm2612/ym2608synth.js` | `YM2608Synth`, `YM2608DirectTransport`, `YM2608WorkletTransport` |
33
33
  | YM2610 / YM2610B FM, SSG and ADPCM | `tetorica-fm2612/ym2610bsynth.js` | `YM2610BSynth`, `YM2610BDirectTransport`, `NeoGeoFMSynth`, `NeoGeoSynth` |
34
34
  | YM2151 FM | `tetorica-fm2612/ym2151synth.js` | `YM2151Synth`, `YM2151DirectTransport`, `YM2151WorkletTransport` |
35
+ | OPL / OPL2 / Y8950 FM / OPL3 | `tetorica-fm2612/opl_synth.js` | `OPLSynth`, `OPLDirectTransport`, `OPL_PRESETS` |
35
36
  | Game Boy pulse, wave and noise | `tetorica-fm2612/gameboysynth.js` | `GameboySynth`, `GameboyDirectTransport` |
36
37
  | NES APU and FDS | `tetorica-fm2612/nesapusynth.js` | `NesApuSynth`, `NesApuDirectTransport`, `NesApuWorkletTransport` |
37
38
  | Sega PSG tone and noise | `tetorica-fm2612/segapsgsynth.js` | `SegaPSGSynth`, `SegaPSGDirectTransport`, `psgPeriodFromFrequency`, `psgPeriodFromNote` |
@@ -88,6 +89,52 @@ Declarations are shipped beside each module, for example
88
89
  functions/classes, parameter types, options and return types. Plain JavaScript
89
90
  projects can use the same editor assistance with JSDoc-aware tooling.
90
91
 
92
+ ### OPL family instruments
93
+
94
+ Available in `tetorica-fm2612@0.2.22` and later.
95
+
96
+ `OPLSynth` shares one API across `ym3526`, `ym3812`, `y8950` (FM only), and
97
+ `ymf262`. Channels are zero-based: 0–8 for the first three, 0–17 for OPL3.
98
+ Operator indices are modulator/carrier 0/1; enabled OPL3 pairs use 0–3.
99
+ `initialize()` explicitly resets the chip and prepares silent voices. Constructing
100
+ a Synth alone never resets or writes to a borrowed chip.
101
+
102
+ ```js
103
+ import {createSoundChip, encodeWav} from 'tetorica-fm2612';
104
+ import {OPLSynth, OPLDirectTransport, OPL_PRESETS} from 'tetorica-fm2612/opl_synth.js';
105
+
106
+ const chip = await createSoundChip('ym3812');
107
+ try {
108
+ const opl = new OPLSynth({chip:'ym3812', transport:new OPLDirectTransport(chip)});
109
+ opl.initialize();
110
+ opl.setPreset(0, OPL_PRESETS.sine);
111
+ opl.noteOn(0, 'A4');
112
+ const pcm = chip.generateStereo(Math.round(chip.sampleRate() * 0.5));
113
+ const wav = encodeWav({...pcm, sampleRate:chip.sampleRate()});
114
+ opl.noteOff(0);
115
+ // Save wav to a file, or send pcm to your own output.
116
+ } finally { chip.dispose(); }
117
+ ```
118
+
119
+ Use `OPLWorkletTransport` from `chip_worklet_transport.js` with a
120
+ `createSoundChip(name, {execution:'worklet'})` endpoint for browser audio.
121
+ `OPLAudifyTransport` from `node/transports` provides optional Node device output.
122
+ The chip-specific `ym3526synth.js`, `ym3812synth.js`, `y8950synth.js` and
123
+ `ymf262synth.js` entry points also export named Synth and DirectTransport wrappers.
124
+
125
+ OPL3 enables stereo with `setPan(ch, left, right)` and pairs with
126
+ `setFourOp(ch, true)` for primary channels 0–2 and 9–11. Each enabled pair owns
127
+ the secondary channel three positions later; address its four operators through
128
+ the primary. `setPreset(0, OPL_PRESETS['four-op-bell'])` configures such a pair.
129
+ Mono chips send the same output to both speakers. Their operators support sine
130
+ only on YM3526/Y8950, four waveforms on YM3812, and eight on YMF262.
131
+
132
+ Playground modes `ym3526`, `ym3812`, `y8950` and `ymf262` expose the primary
133
+ instrument as `opl` and provide `OPL_PRESETS`. Use `opl.noteOn()` / `noteOff()`;
134
+ the OPN `fm` and `play()` conveniences are absent. Operators and Keyboard share
135
+ this same primary instrument. OPLL/YM2413 is a separate API and is not included
136
+ in this high-level OPL family implementation.
137
+
91
138
  `node_modules/tetorica-fm2612/runtime-manifest.json` lists every shipped runtime
92
139
  module and asset. It is a distribution inventory, including internal modules;
93
140
  start with the task table above when choosing an application API. See the
package/RUNTIME.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Full web runtime
2
2
 
3
- Includes all 202 top-level web runtime modules and 24 generated chip/engine pairs.
3
+ Includes all 215 top-level web runtime modules and 24 generated chip/engine pairs.
4
4
 
5
5
  ay8910, gameboy_apu, huc6280, k051649, nuked_opn2, okim6258, rf5c164, segapcm, segapsg, spc, y8950, ym2151, ym2203, ym2413, ym2608, ym2610b, ym2612, ym3438, ym3526, ym3812, ymf262, ymf276, ymf278b, ymf288
6
6
 
@@ -21,3 +21,13 @@ export declare class SegaPSGWorkletTransport extends ChipWorkletTransport {
21
21
  }
22
22
  export declare class YM2151WorkletTransport extends ChipWorkletTransport {
23
23
  }
24
+ export declare class OPLWorkletTransport extends ChipWorkletTransport {
25
+ }
26
+ export declare class YM3526WorkletTransport extends OPLWorkletTransport {
27
+ }
28
+ export declare class YM3812WorkletTransport extends OPLWorkletTransport {
29
+ }
30
+ export declare class Y8950WorkletTransport extends OPLWorkletTransport {
31
+ }
32
+ export declare class YMF262WorkletTransport extends OPLWorkletTransport {
33
+ }
@@ -20,3 +20,8 @@ export class ChipWorkletTransport {
20
20
  export class GameboyWorkletTransport extends ChipWorkletTransport {}
21
21
  export class SegaPSGWorkletTransport extends ChipWorkletTransport {}
22
22
  export class YM2151WorkletTransport extends ChipWorkletTransport {}
23
+ export class OPLWorkletTransport extends ChipWorkletTransport {}
24
+ export class YM3526WorkletTransport extends OPLWorkletTransport {}
25
+ export class YM3812WorkletTransport extends OPLWorkletTransport {}
26
+ export class Y8950WorkletTransport extends OPLWorkletTransport {}
27
+ export class YMF262WorkletTransport extends OPLWorkletTransport {}
Binary file
Binary file
@@ -6,8 +6,10 @@ import { createYm2151Client } from './playground_ym2151.js';
6
6
  import { createOki6258Client } from './playground_okim6258.js';
7
7
  import { createHuc6280Client } from './playground_huc6280.js';
8
8
  import { createSegaPsgClient } from './playground_segapsg.js';
9
+ import { createOplClient } from './playground_opl.js';
10
+ import { createDx7Client } from './playground_dx7.js';
9
11
  export type NativeRuntimeSynthOptions = Pick<import('./megasynth.js').MegaSynthOptions, 'audioContext' | 'outputNode' | 'sampleOutputNode' | 'masterVolume' | 'mixer'> & {
10
- chip?: 'nes' | 'snes' | 'x68000' | 'huc6280' | 'segapsg';
12
+ chip?: 'nes' | 'snes' | 'x68000' | 'huc6280' | 'segapsg' | 'dx7' | 'ym3526' | 'ym3812' | 'y8950' | 'ymf262';
11
13
  clock?: number;
12
14
  stereoEnabled?: boolean;
13
15
  fds?: boolean;
@@ -21,7 +23,7 @@ export type NativeRuntimeSynthOptions = Pick<import('./megasynth.js').MegaSynthO
21
23
  };
22
24
  };
23
25
  /** @typedef {Pick<import('./megasynth.js').MegaSynthOptions,'audioContext'|'outputNode'|'sampleOutputNode'|'masterVolume'|'mixer'> & {
24
- * chip?:'nes'|'snes'|'x68000'|'huc6280'|'segapsg', clock?:number, stereoEnabled?:boolean, fds?:boolean,
26
+ * chip?:'nes'|'snes'|'x68000'|'huc6280'|'segapsg'|'dx7'|'ym3526'|'ym3812'|'y8950'|'ymf262', clock?:number, stereoEnabled?:boolean, fds?:boolean,
25
27
  * wasmUrl?:string|URL, workletUrl?:string|URL, fetch?:typeof globalThis.fetch,
26
28
  * extraOki?:{fetch?:typeof globalThis.fetch,wasmUrl?:string|URL,workletUrl?:string|URL}
27
29
  * }} NativeRuntimeSynthOptions */
@@ -45,7 +47,7 @@ export declare class NativeRuntimeSynth {
45
47
  node: any;
46
48
  fm: any;
47
49
  psg: any;
48
- /** @type {{nes?:ReturnType<typeof createNesClient>,snes?:ReturnType<typeof createSnesClient>,opm?:ReturnType<typeof createYm2151Client>,adpcm?:ReturnType<typeof createOki6258Client>,pce?:ReturnType<typeof createHuc6280Client>,psg?:ReturnType<typeof createSegaPsgClient>}} */
50
+ /** @type {{nes?:ReturnType<typeof createNesClient>,snes?:ReturnType<typeof createSnesClient>,opm?:ReturnType<typeof createYm2151Client>,adpcm?:ReturnType<typeof createOki6258Client>,pce?:ReturnType<typeof createHuc6280Client>,psg?:ReturnType<typeof createSegaPsgClient>,dx7?:ReturnType<typeof createDx7Client>,opl?:ReturnType<typeof createOplClient>}} */
49
51
  primaryApis: {
50
52
  nes?: ReturnType<typeof createNesClient>;
51
53
  snes?: ReturnType<typeof createSnesClient>;
@@ -53,12 +55,16 @@ export declare class NativeRuntimeSynth {
53
55
  adpcm?: ReturnType<typeof createOki6258Client>;
54
56
  pce?: ReturnType<typeof createHuc6280Client>;
55
57
  psg?: ReturnType<typeof createSegaPsgClient>;
58
+ dx7?: ReturnType<typeof createDx7Client>;
59
+ opl?: ReturnType<typeof createOplClient>;
56
60
  };
57
61
  /** @type {ReturnType<typeof createNesClient>|null} */ nes: ReturnType<typeof createNesClient> | null;
58
62
  /** @type {ReturnType<typeof createSnesClient>|null} */ snes: ReturnType<typeof createSnesClient> | null;
59
63
  /** @type {ReturnType<typeof createYm2151Client>|null} */ opm: ReturnType<typeof createYm2151Client> | null;
60
64
  /** @type {ReturnType<typeof createOki6258Client>|null} */ adpcm: ReturnType<typeof createOki6258Client> | null;
61
65
  /** @type {ReturnType<typeof createHuc6280Client>|null} */ pce: ReturnType<typeof createHuc6280Client> | null;
66
+ /** @type {ReturnType<typeof createDx7Client>|null} */ dx7: ReturnType<typeof createDx7Client> | null;
67
+ /** @type {ReturnType<typeof createOplClient>|null} */ opl: ReturnType<typeof createOplClient> | null;
62
68
  primaryEndpoints: any[];
63
69
  listeners: Set<any>;
64
70
  readyPromise: Promise<void> | null;
@@ -92,7 +98,7 @@ export declare class NativeRuntimeSynth {
92
98
  connect(effect: any): this;
93
99
  connectOutput(node?: null): this;
94
100
  /** Stop voices while keeping uploaded samples, patches, waveforms and clocks. */
95
- stopAllNative(): void;
101
+ stopAllNative(): Promise<void> | undefined;
96
102
  reset(): void;
97
103
  close(): Promise<void>;
98
104
  }
@@ -12,11 +12,15 @@ import {createHuc6280Audio} from './playground_huc6280_audio.js';
12
12
  import {createHuc6280Client} from './playground_huc6280.js';
13
13
  import {createSegaPsgAudio} from './playground_segapsg_audio.js';
14
14
  import {createSegaPsgClient} from './playground_segapsg.js';
15
+ import {createOplAudio} from './playground_opl_audio.js';
16
+ import {createOplClient, OPL_CHIPS} from './playground_opl.js';
17
+ import {createDx7Audio} from './playground_dx7_audio.js';
18
+ import {createDx7Client} from './playground_dx7.js';
15
19
 
16
- const PRIMARY_CHIPS = Object.freeze(['nes', 'snes', 'x68000', 'huc6280', 'segapsg']);
20
+ const PRIMARY_CHIPS = Object.freeze(['nes', 'snes', 'x68000', 'huc6280', 'segapsg', 'dx7', 'ym3526', 'ym3812', 'y8950', 'ymf262']);
17
21
 
18
22
  /** @typedef {Pick<import('./megasynth.js').MegaSynthOptions,'audioContext'|'outputNode'|'sampleOutputNode'|'masterVolume'|'mixer'> & {
19
- * chip?:'nes'|'snes'|'x68000'|'huc6280'|'segapsg', clock?:number, stereoEnabled?:boolean, fds?:boolean,
23
+ * chip?:'nes'|'snes'|'x68000'|'huc6280'|'segapsg'|'dx7'|'ym3526'|'ym3812'|'y8950'|'ymf262', clock?:number, stereoEnabled?:boolean, fds?:boolean,
20
24
  * wasmUrl?:string|URL, workletUrl?:string|URL, fetch?:typeof globalThis.fetch,
21
25
  * extraOki?:{fetch?:typeof globalThis.fetch,wasmUrl?:string|URL,workletUrl?:string|URL}
22
26
  * }} NativeRuntimeSynthOptions */
@@ -41,13 +45,15 @@ export class NativeRuntimeSynth {
41
45
  this.node = null;
42
46
  this.fm = null;
43
47
  this.psg = null;
44
- /** @type {{nes?:ReturnType<typeof createNesClient>,snes?:ReturnType<typeof createSnesClient>,opm?:ReturnType<typeof createYm2151Client>,adpcm?:ReturnType<typeof createOki6258Client>,pce?:ReturnType<typeof createHuc6280Client>,psg?:ReturnType<typeof createSegaPsgClient>}} */
48
+ /** @type {{nes?:ReturnType<typeof createNesClient>,snes?:ReturnType<typeof createSnesClient>,opm?:ReturnType<typeof createYm2151Client>,adpcm?:ReturnType<typeof createOki6258Client>,pce?:ReturnType<typeof createHuc6280Client>,psg?:ReturnType<typeof createSegaPsgClient>,dx7?:ReturnType<typeof createDx7Client>,opl?:ReturnType<typeof createOplClient>}} */
45
49
  this.primaryApis = {};
46
50
  /** @type {ReturnType<typeof createNesClient>|null} */ this.nes = null;
47
51
  /** @type {ReturnType<typeof createSnesClient>|null} */ this.snes = null;
48
52
  /** @type {ReturnType<typeof createYm2151Client>|null} */ this.opm = null;
49
53
  /** @type {ReturnType<typeof createOki6258Client>|null} */ this.adpcm = null;
50
54
  /** @type {ReturnType<typeof createHuc6280Client>|null} */ this.pce = null;
55
+ /** @type {ReturnType<typeof createDx7Client>|null} */ this.dx7 = null;
56
+ /** @type {ReturnType<typeof createOplClient>|null} */ this.opl = null;
51
57
  this.primaryEndpoints = [];
52
58
  this.listeners = new Set();
53
59
  this.readyPromise = null;
@@ -117,24 +123,37 @@ export class NativeRuntimeSynth {
117
123
 
118
124
  /** Stop voices while keeping uploaded samples, patches, waveforms and clocks. */
119
125
  stopAllNative() {
120
- const {nes, snes, opm, adpcm, pce, psg} = this.primaryApis;
121
- if (nes?._stopVoices) nes._stopVoices();
126
+ const {nes, snes, opm, adpcm, pce, psg, dx7, opl} = this.primaryApis;
127
+ const pending=[],track=result=>{if(result?.then)pending.push(Promise.resolve(result));};
128
+ if (nes?._stopVoices) track(nes._stopVoices());
122
129
  else if (nes) for (let channel = 0; channel < 6; channel++) nes.keyOff(channel);
123
- if (snes) {
130
+ if (snes?._stopVoices) track(snes._stopVoices());
131
+ else if (snes) {
124
132
  try { snes.allNotesOff(); }
125
133
  catch (error) {
126
134
  if (!/timeline mode is exclusive/.test(String(error?.message))) throw error;
127
135
  // Timeline playback has a separate stop path and keeps its prepared data.
128
- snes.stopTimeline().catch(() => {});
136
+ track(snes.stopTimeline());
129
137
  }
130
138
  }
131
- if (opm?._stopVoices) opm._stopVoices();
139
+ if (opm?._stopVoices) track(opm._stopVoices());
132
140
  else opm?.allNotesOff();
133
- if (adpcm?._stopVoices) adpcm._stopVoices();
141
+ if (adpcm?._stopVoices) track(adpcm._stopVoices());
134
142
  else adpcm?.stop();
135
- if (pce?._stopVoices) pce._stopVoices();
143
+ if (pce?._stopVoices) track(pce._stopVoices());
136
144
  else pce?.allNotesOff();
137
- psg?.allNotesOff();
145
+ if (psg?._stopVoices) track(psg._stopVoices());
146
+ else psg?.allNotesOff();
147
+ if(dx7)track(dx7._stopVoices());
148
+ if(opl)track(opl._stopVoices());
149
+ if(!pending.length)return;
150
+ // A failed endpoint must not let a later audition overtake another chip's
151
+ // still-pending Stop. Preserve rejection after every endpoint has settled.
152
+ const stopped=Promise.allSettled(pending).then(results=>{
153
+ const failed=results.find(result=>result.status==='rejected');if(failed)throw failed.reason;
154
+ });
155
+ stopped.catch(()=>{});
156
+ return stopped;
138
157
  }
139
158
  reset() { this.stopAllNative(); }
140
159
 
@@ -165,6 +184,7 @@ export class NativeRuntimeSynth {
165
184
 
166
185
  #releasePrimaries() {
167
186
  for (const endpoint of this.primaryEndpoints.splice(0)) {
187
+ endpoint.detachObserver?.();
168
188
  endpoint.release?.();
169
189
  endpoint.client?.dispose();
170
190
  endpoint.dispose();
@@ -174,7 +194,7 @@ export class NativeRuntimeSynth {
174
194
  this.node = null;
175
195
  }
176
196
 
177
- #createAudioContext() { return new AudioContext(); }
197
+ #createAudioContext() { return this.chip === 'dx7' ? new AudioContext({sampleRate:44100}) : new AudioContext(); }
178
198
 
179
199
  async #initialize(signal) {
180
200
  if (!this.audioContext) this.audioContext = this.#createAudioContext();
@@ -191,6 +211,16 @@ export class NativeRuntimeSynth {
191
211
  } else if (this.chip === 'huc6280') {
192
212
  const endpoint = await this.#addPrimary('huc6280', 'pce', createHuc6280Audio, device => createHuc6280Client(device.port, {clock:device.clock}), options, signal);
193
213
  endpoint.client.initialize();
214
+ } else if (OPL_CHIPS.includes(this.chip)) {
215
+ const endpoint = await this.#addPrimary(this.chip, 'opl', createOplAudio, device => createOplClient(device.port,{chip:this.chip,clock:device.clock}), {...options,onError:()=>{if(this.initializationController?.signal!==signal||signal.aborted)return;this.#releasePrimaries();this.readyPromise=null;this.state='error';}}, signal);
216
+ endpoint.client.initialize();
217
+ } else if (this.chip === 'dx7') {
218
+ await this.#addPrimary('dx7', 'dx7', createDx7Audio, device => createDx7Client(device.port, device), {
219
+ ...options,onError:() => {
220
+ if (this.initializationController?.signal !== signal || signal.aborted) return;
221
+ this.#releasePrimaries();this.readyPromise=null;this.state='error';
222
+ },
223
+ }, signal);
194
224
  } else {
195
225
  const opm = await this.#addPrimary('ym2151', 'opm', createYm2151Audio, device => createYm2151Client(device.port), options, signal);
196
226
  await waitForInitialization(opm.client.setClock(4000000), signal);
@@ -204,6 +234,11 @@ export class NativeRuntimeSynth {
204
234
  const endpoint = {...device, name, globalName, client:null, release:null};
205
235
  this.primaryEndpoints.push(endpoint);
206
236
  endpoint.client = createClient(device);
237
+ if(OPL_CHIPS.includes(name)){
238
+ const observer=({data})=>{if(data?.type!=='opl-state')return;endpoint.client._adoptState(data.state);for(const listener of this.listeners)listener({type:'native-state',name:globalName,state:data.state});};
239
+ endpoint.node.port.addEventListener?.('message',observer);endpoint.node.port.start?.();
240
+ endpoint.detachObserver=()=>endpoint.node.port.removeEventListener?.('message',observer);
241
+ }
207
242
  Object.defineProperty(endpoint, 'clock', {enumerable:true, get:() => endpoint.client.clock});
208
243
  Object.defineProperty(endpoint, 'state', {enumerable:true, get:() => endpoint.client._getState?.()});
209
244
  endpoint.release = this.mixer.connect(name, name, device.node, this.audio.masterInputNode, this.audioContext);
@@ -5,10 +5,17 @@ import { GameboyDirectTransport } from '../gameboysynth.js';
5
5
  import { SegaPSGDirectTransport } from '../segapsgsynth.js';
6
6
  import { YM2151DirectTransport } from '../ym2151synth.js';
7
7
  import { NesApuDirectTransport } from '../nesapusynth.js';
8
+ import { OPLDirectTransport } from '../opl_synth.js';
9
+ import { YM3526DirectTransport } from '../ym3526synth.js';
10
+ import { YM3812DirectTransport } from '../ym3812synth.js';
11
+ import { Y8950DirectTransport } from '../y8950synth.js';
12
+ import { YMF262DirectTransport } from '../ymf262synth.js';
8
13
  export type AudifyTransportOptions = import('./megasynth.mjs').OutputConnectionOptions & {
9
14
  sampleRate?: number;
10
15
  gain?: number;
11
16
  queueBlocks?: number;
17
+ clock?: number;
18
+ chip?: import('../opl_synth.js').OPLChip;
12
19
  };
13
20
  export declare const YM2612AudifyTransport: new (chip: {
14
21
  writeRegister(register: number, value: number, port?: number): void;
@@ -95,3 +102,63 @@ export declare const NesApuAudifyTransport: new (chip: import("../nesapu.js").Ne
95
102
  output: Record<string, unknown> | null;
96
103
  };
97
104
  };
105
+ export declare const OPLAudifyTransport: new (chip: {
106
+ write(offset: number, value: number): void;
107
+ reset(): void;
108
+ sampleRate?: (clock?: number) => number;
109
+ generateStereo?: (frames: number) => {
110
+ left: Float32Array;
111
+ right: Float32Array;
112
+ };
113
+ readStatus?: () => number;
114
+ getIrq?: () => boolean;
115
+ }, options?: AudifyTransportOptions) => Omit<OPLDirectTransport, "getState"> & {
116
+ start(): Promise<void>;
117
+ stop(): Promise<void>;
118
+ close(): Promise<void>;
119
+ getState(): {
120
+ running: boolean;
121
+ error: string | null;
122
+ output: Record<string, unknown> | null;
123
+ };
124
+ };
125
+ export declare const YM3526AudifyTransport: new (chip: import("../ym3526.js").Ym3526, options?: AudifyTransportOptions) => Omit<YM3526DirectTransport, "getState"> & {
126
+ start(): Promise<void>;
127
+ stop(): Promise<void>;
128
+ close(): Promise<void>;
129
+ getState(): {
130
+ running: boolean;
131
+ error: string | null;
132
+ output: Record<string, unknown> | null;
133
+ };
134
+ };
135
+ export declare const YM3812AudifyTransport: new (chip: import("../ym3812.js").Ym3812, options?: AudifyTransportOptions) => Omit<YM3812DirectTransport, "getState"> & {
136
+ start(): Promise<void>;
137
+ stop(): Promise<void>;
138
+ close(): Promise<void>;
139
+ getState(): {
140
+ running: boolean;
141
+ error: string | null;
142
+ output: Record<string, unknown> | null;
143
+ };
144
+ };
145
+ export declare const Y8950AudifyTransport: new (chip: import("../y8950.js").Y8950, options?: AudifyTransportOptions) => Omit<Y8950DirectTransport, "getState"> & {
146
+ start(): Promise<void>;
147
+ stop(): Promise<void>;
148
+ close(): Promise<void>;
149
+ getState(): {
150
+ running: boolean;
151
+ error: string | null;
152
+ output: Record<string, unknown> | null;
153
+ };
154
+ };
155
+ export declare const YMF262AudifyTransport: new (chip: import("../ymf262.js").Ymf262, options?: AudifyTransportOptions) => Omit<YMF262DirectTransport, "getState"> & {
156
+ start(): Promise<void>;
157
+ stop(): Promise<void>;
158
+ close(): Promise<void>;
159
+ getState(): {
160
+ running: boolean;
161
+ error: string | null;
162
+ output: Record<string, unknown> | null;
163
+ };
164
+ };
@@ -5,6 +5,11 @@ import {GameboyDirectTransport} from '../gameboysynth.js';
5
5
  import {SegaPSGDirectTransport} from '../segapsgsynth.js';
6
6
  import {YM2151DirectTransport} from '../ym2151synth.js';
7
7
  import {NesApuDirectTransport} from '../nesapusynth.js';
8
+ import {OPLDirectTransport} from '../opl_synth.js';
9
+ import {YM3526DirectTransport} from '../ym3526synth.js';
10
+ import {YM3812DirectTransport} from '../ym3812synth.js';
11
+ import {Y8950DirectTransport} from '../y8950synth.js';
12
+ import {YMF262DirectTransport} from '../ymf262synth.js';
8
13
  import {ChipPCMRenderer} from '../chip_pcm_renderer.js';
9
14
  import {createThreadOutput} from './output_thread.mjs';
10
15
 
@@ -38,6 +43,7 @@ class Playback {
38
43
  gain: this.options.gain ?? .25, removeIdleOffset: this.name === 'ym2612',
39
44
  generate: typeof this.transport.generateStereo === 'function' ? this.transport.generateStereo.bind(this.transport) : undefined});
40
45
  }
46
+ if (this.transport instanceof OPLDirectTransport) this.renderer.rate=this.transport.sampleRate();
41
47
  this.running = true; this.ramp = 0; this.pump(); await this.output.start();
42
48
  if (this.error) throw this.error;
43
49
  }
@@ -73,7 +79,7 @@ class Playback {
73
79
  return this.closing;
74
80
  }
75
81
  }
76
- /** @typedef {import('./megasynth.mjs').OutputConnectionOptions & {sampleRate?: number, gain?: number, queueBlocks?: number}} AudifyTransportOptions */
82
+ /** @typedef {import('./megasynth.mjs').OutputConnectionOptions & {sampleRate?: number, gain?: number, queueBlocks?: number, clock?:number, chip?:import('../opl_synth.js').OPLChip}} AudifyTransportOptions */
77
83
  /** @template {new (...args: any[]) => any} T
78
84
  * @param {T} Direct @param {string} name
79
85
  * @returns {{new(chip: ConstructorParameters<T>[0], options?: AudifyTransportOptions): Omit<InstanceType<T>, 'getState'> & {
@@ -83,7 +89,7 @@ class Playback {
83
89
  */
84
90
  function audifyTransport(Direct, name) {
85
91
  return class extends Direct {
86
- constructor(chip, options = {}) {super(chip); this.playback = new Playback(this, options, name);}
92
+ constructor(chip, options = {}) {super(chip, options); this.playback = new Playback(this, options, name);}
87
93
  reset() {super.reset(); this.playback?.renderer?.resetHistory();}
88
94
  start() {return this.playback.start();}
89
95
  stop() {return this.playback.stop();}
@@ -100,3 +106,9 @@ export const YM2151AudifyTransport = audifyTransport(YM2151DirectTransport, 'ym2
100
106
  export const PWM32XAudifyTransport = audifyTransport(PWM32XDirectTransport, 'pwm');
101
107
 
102
108
  export const NesApuAudifyTransport = audifyTransport(NesApuDirectTransport, 'nes');
109
+
110
+ export const OPLAudifyTransport = audifyTransport(OPLDirectTransport, 'opl');
111
+ export const YM3526AudifyTransport = audifyTransport(YM3526DirectTransport, 'ym3526');
112
+ export const YM3812AudifyTransport = audifyTransport(YM3812DirectTransport, 'ym3812');
113
+ export const Y8950AudifyTransport = audifyTransport(Y8950DirectTransport, 'y8950');
114
+ export const YMF262AudifyTransport = audifyTransport(YMF262DirectTransport, 'ymf262');
package/opl_synth.d.ts ADDED
@@ -0,0 +1,200 @@
1
+ export type OPLChip = 'ym3526' | 'ym3812' | 'y8950' | 'ymf262';
2
+ export type OPLChipName = OPLChip;
3
+ export type OPLOperatorParams = {
4
+ am?: boolean | number;
5
+ vibrato?: boolean | number;
6
+ egt?: boolean | number;
7
+ sustain?: boolean | number;
8
+ ksr?: boolean | number;
9
+ multi?: number;
10
+ mul?: number;
11
+ ksl?: number;
12
+ tl?: number;
13
+ ar?: number;
14
+ dr?: number;
15
+ sl?: number;
16
+ rr?: number;
17
+ waveform?: number;
18
+ };
19
+ export type OPLPreset = {
20
+ label?: string;
21
+ name?: string;
22
+ algorithm?: number;
23
+ feedback?: number;
24
+ fourOp?: boolean;
25
+ pan?: {
26
+ left?: boolean;
27
+ right?: boolean;
28
+ };
29
+ operators?: OPLOperatorParams[];
30
+ };
31
+ export type OPLTransport = {
32
+ writeRegister(register: number, value: number, port?: number): void;
33
+ reset?: () => void;
34
+ dispose?: () => void;
35
+ };
36
+ export type RegisterChange = [number, number, number];
37
+ /** Convert Hz to the closest representable OPL BLOCK/F-NUM pair.
38
+ * OPL/OPL2 generate at clock/72; OPL3 uses clock/288. A multiplier of one
39
+ * advances the 20-bit phase by F-NUM * 2^BLOCK each native PCM sample.
40
+ * @param {number} frequency @param {number} [clock] @param {OPLChip} [chip]
41
+ * @returns {{fnum:number,block:number}} */
42
+ export declare function oplPitchFromHz(frequency: number, clock?: number, chip?: OPLChip): {
43
+ fnum: number;
44
+ block: number;
45
+ };
46
+ /** Borrow a synchronous chip. The transport does not own or dispose the chip. */
47
+ export declare class OPLDirectTransport {
48
+ chip: {
49
+ write(offset: number, value: number): void;
50
+ reset(): void;
51
+ sampleRate?: (clock?: number) => number;
52
+ generateStereo?: (frames: number) => {
53
+ left: Float32Array;
54
+ right: Float32Array;
55
+ };
56
+ readStatus?: () => number;
57
+ getIrq?: () => boolean;
58
+ };
59
+ kind: OPLChip;
60
+ clock: any;
61
+ /** @param {{write(offset:number,value:number):void,reset():void,sampleRate?:(clock?:number)=>number,
62
+ * generateStereo?:(frames:number)=>{left:Float32Array,right:Float32Array},readStatus?:()=>number,getIrq?:()=>boolean}} chip
63
+ * @param {{chip?:OPLChip,clock?:number}} [options] */
64
+ constructor(chip: {
65
+ write(offset: number, value: number): void;
66
+ reset(): void;
67
+ sampleRate?: (clock?: number) => number;
68
+ generateStereo?: (frames: number) => {
69
+ left: Float32Array;
70
+ right: Float32Array;
71
+ };
72
+ readStatus?: () => number;
73
+ getIrq?: () => boolean;
74
+ }, { chip: kind, clock }?: {
75
+ chip?: OPLChip;
76
+ clock?: number;
77
+ });
78
+ /** @param {number} offset @param {number} value */
79
+ write(offset: number, value: number): void;
80
+ /** Byte register plus port, or a flattened OPL3 register address (0..511).
81
+ * @param {number} register @param {number} value @param {number} [port] */
82
+ writeRegister(register: number, value: number, port?: number): void;
83
+ reset(): void;
84
+ sampleRate(): number | undefined;
85
+ /** @param {number} frames */
86
+ generateStereo(frames: number): {
87
+ left: Float32Array;
88
+ right: Float32Array;
89
+ };
90
+ readStatus(): number | undefined;
91
+ getIrq(): boolean;
92
+ }
93
+ /** Nine-channel OPL/OPL2 or eighteen-channel OPL3 instrument authoring.
94
+ * Channels are physical, zero-based. Logical operator order is modulator,
95
+ * carrier (0,1); a four-operator voice appends the paired channel's operators
96
+ * (2,3). OPL3 four-operator primaries are 0..2 and 9..11; each borrows CH+3.
97
+ *
98
+ * Construction never resets a borrowed chip. Call initialize() for silent,
99
+ * centered defaults, or adoptRegisterSnapshot() when attaching to live state.
100
+ * Raw writes made through this instance remain authoritative for partial edits.
101
+ */
102
+ export declare class OPLSynth {
103
+ #private;
104
+ transport: OPLTransport;
105
+ chip: OPLChip;
106
+ clock: any;
107
+ channels: any;
108
+ /** @type {((register:number,value:number,port:number)=>boolean|void)|undefined} */
109
+ _beforeWrite: ((register: number, value: number, port: number) => boolean | void) | undefined;
110
+ /** @type {((event:{method:string,args:number[],channel?:number})=>void)|undefined} */
111
+ _observe: ((event: {
112
+ method: string;
113
+ args: number[];
114
+ channel?: number;
115
+ }) => void) | undefined;
116
+ /** @param {{transport:OPLTransport,chip?:OPLChip,clock?:number}} options */
117
+ constructor({ transport, chip, clock }?: {
118
+ transport: OPLTransport;
119
+ chip?: OPLChip;
120
+ clock?: number;
121
+ });
122
+ /** Reset chip/core state without applying an instrument. */
123
+ reset(): void;
124
+ /** Reset and install silent two-operator voices; enable OPL2/OPL3 waveforms. */
125
+ initialize(): void;
126
+ /** Copy of hardware register authoring state, indexed by (port << 8)|register. */
127
+ getRegisterSnapshot(): Uint8Array<ArrayBuffer>;
128
+ /** Adopt an entire register image without writing or resetting the hardware.
129
+ * @param {Uint8Array|number[]} snapshot */
130
+ adoptRegisterSnapshot(snapshot: Uint8Array | number[]): void;
131
+ /** Raw byte register plus port, or flattened OPL3 register (0..511).
132
+ * @param {number} register @param {number} value @param {number} [port] */
133
+ writeRegister(register: number, value: number, port?: number): void;
134
+ /** Whether this channel is an enabled four-operator primary. @param {number} ch */
135
+ isFourOp(ch: number): boolean;
136
+ /** Enable/disable one YMF262 four-operator pair, stopping both keys first.
137
+ * @param {number} ch Primary channel (0..2 or 9..11). @param {boolean} enabled */
138
+ setFourOp(ch: number, enabled: boolean): void;
139
+ /** Partial operator updates preserve adjacent bits, including prior raw writes.
140
+ * KSL is the raw two-bit hardware code, not a sorted attenuation scale.
141
+ * @param {number} ch @param {number} op @param {OPLOperatorParams} params */
142
+ setOperator(ch: number, op: number, params: OPLOperatorParams): void;
143
+ /** Validate the complete patch before issuing any hardware writes.
144
+ * Algorithms: two-op 0=FM, 1=additive. Four-op: 0=1→2→3→4,
145
+ * 1=1+(2→3→4), 2=(1→2)+(3→4), 3=1+(2→3)+4.
146
+ * @param {number} ch @param {OPLPreset} preset */
147
+ setPreset(ch: number, preset: OPLPreset): void;
148
+ /** @param {number} ch @param {number} algorithm @param {number} [feedback] */
149
+ setAlgo(ch: number, algorithm: number, feedback?: number): void;
150
+ /** OPL3 stereo routing; preserves the additional hardware C/D output bits.
151
+ * @param {number} ch @param {boolean} left @param {boolean} right */
152
+ setPan(ch: number, left: boolean, right: boolean): void;
153
+ /** Raw F-NUM (0..1023) and BLOCK (0..7); preserves key-on and reserved bits.
154
+ * @param {number} ch @param {number} fnum @param {number} [block] */
155
+ setPitch(ch: number, fnum: number, block?: number): void;
156
+ /** @param {number} ch @param {number} frequency Frequency in Hz. */
157
+ setFrequency(ch: number, frequency: number): {
158
+ fnum: number;
159
+ block: number;
160
+ };
161
+ /** Note name or MIDI integer; A4 = MIDI 69 = 440 Hz.
162
+ * @param {number} ch @param {string|number} note */
163
+ setNote(ch: number, note: string | number): {
164
+ fnum: number;
165
+ block: number;
166
+ };
167
+ /** Key a melodic voice; rhythm mode reserves physical channels 6..8.
168
+ * @param {number} ch */
169
+ keyOn(ch: number): void;
170
+ /** @param {number} ch */
171
+ keyOff(ch: number): void;
172
+ /** @param {number} ch @param {string|number} note */
173
+ noteOn(ch: number, note: string | number): void;
174
+ /** @param {number} ch */
175
+ noteOff(ch: number): void;
176
+ /** Release every physical key and all percussion triggers, preserving patches. */
177
+ allNotesOff(): void;
178
+ /** Enable the hardware rhythm section and/or partially edit trigger bits.
179
+ * High-level melodic key-on is unavailable on CH6..8 while enabled.
180
+ * @param {{enabled?:boolean,bassDrum?:boolean,snare?:boolean,tom?:boolean,cymbal?:boolean,hiHat?:boolean}} settings */
181
+ setRhythm(settings: {
182
+ enabled?: boolean;
183
+ bassDrum?: boolean;
184
+ snare?: boolean;
185
+ tom?: boolean;
186
+ cymbal?: boolean;
187
+ hiHat?: boolean;
188
+ }): void;
189
+ /** Fixed-rate hardware LFO depth flags (false=shallow, true=deep).
190
+ * Enable AM/vibrato individually on operators to hear the LFO.
191
+ * @param {{amDepth?:boolean,pmDepth?:boolean}} settings */
192
+ setLFO(settings: {
193
+ amDepth?: boolean;
194
+ pmDepth?: boolean;
195
+ }): void;
196
+ }
197
+ /** Original two-operator patches shared by the OPL family, plus an OPL3 patch.
198
+ * The sine patch silences its modulator; every patch explicitly selects mode.
199
+ * @type {Readonly<Record<string,OPLPreset>>} */
200
+ export declare const OPL_PRESETS: Readonly<Record<string, OPLPreset>>;