File size: 19,980 Bytes
9bd3a64
fc78499
 
 
 
 
9bd3a64
 
fc78499
 
 
 
9bd3a64
 
 
 
 
 
 
 
 
 
 
 
 
fc78499
9bd3a64
 
 
 
 
 
6b5283a
9bd3a64
 
461c4ca
9bd3a64
 
 
 
 
 
 
 
4c51fc1
9bd3a64
 
 
 
 
 
 
 
 
 
fc78499
9bd3a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc78499
9bd3a64
 
fc78499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bd3a64
 
fc78499
 
 
 
 
9bd3a64
fc78499
 
 
 
 
 
 
 
 
9bd3a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c51fc1
9bd3a64
 
 
 
 
 
 
 
 
 
 
4c51fc1
9bd3a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c51fc1
9bd3a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
461c4ca
9bd3a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c51fc1
9bd3a64
 
 
 
 
 
4c51fc1
461c4ca
4c51fc1
461c4ca
4c51fc1
 
 
 
 
 
 
 
 
 
 
 
461c4ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c51fc1
 
 
9bd3a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc78499
9bd3a64
 
 
 
 
fc78499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bd3a64
 
fc78499
 
 
 
 
 
 
 
 
 
9bd3a64
 
 
 
 
 
 
 
 
 
 
461c4ca
9bd3a64
 
6b5283a
9bd3a64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// NanoMaestro Web Worker for isolated model inference and parsing
try {
  importScripts("https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/ort.min.js");
} catch (err) {
  console.error("Worker failed to import ONNX Runtime script:", err);
}

// ONNX runtime configuration
if (typeof ort !== "undefined") {
  ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/";
  ort.env.wasm.numThreads = Math.min(4, navigator.hardwareConcurrency || 1);
}

// State variables
let session = null;
let stoi = null;
let itos = null;
let vocabSize = 0;

let h = null;
let c = null;
let currentId = 0;
let activeModelName = "";

// Model parameters
let hiddenSize = 2048;
let numLayers = 2;

// Playback settings
let temperature = 0.85;
let topK = 40;
let currentBpm = 120;
let midiBpmMode = "lock";

// Seeding settings
let playingStyle = "default";
let midiTokens = [];
let midiStartBar = 0;
let isWarmingUp = false;

// Temporal queues for gathering parsed events
let tempQueue = [];
let parser = null;



function tokenId(token) {
  return stoi?.[token] ?? null;
}

function makeTensorId(id) {
  return new ort.Tensor("int64", BigInt64Array.from([BigInt(id)]), [1, 1]);
}

function zeroState() {
  return new ort.Tensor("float32", new Float32Array(numLayers * 1 * hiddenSize), [numLayers, 1, hiddenSize]);
}

function updateModelDimensions(session) {
  try {
    const inputNames = session.inputNames;
    if (inputNames.includes("h") && session.handler && session.handler._model) {
      const inputs = session.handler._model.graph.inputs;
      const hInput = inputs.find(i => i.name === "h");
      if (hInput && hInput.type && hInput.type.tensorType && hInput.type.tensorType.shape) {
        const shape = hInput.type.tensorType.shape.dim;
        const layers = Number(shape[0].dimValue);
        const hidden = Number(shape[2].dimValue);
        if (layers > 0 && hidden > 0) {
          numLayers = layers;
          hiddenSize = hidden;
          console.log(`Worker set model dimensions: layers=${numLayers}, hiddenSize=${hiddenSize}`);
        }
      }
    }
  } catch (e) {
    console.error("Worker failed to detect model dimensions:", e);
  }
}

// Model Step Execution with Memory Management
async function stepModel(record = true) {
  if (!session) return "?";
  
  const inputTensor = makeTensorId(currentId);
  const output = await session.run({ input: inputTensor, h, c });
  
  // Dispose input tensor to prevent memory leak
  inputTensor.dispose();
  
  // Dispose previous recurrent states to prevent memory leaks during continuous generation
  if (h) {
    try { h.dispose(); } catch (e) {}
  }
  if (c) {
    try { c.dispose(); } catch (e) {}
  }
  
  h = output.h_out;
  c = output.c_out;
  
  const logitsData = output.logits.data;
  currentId = sampleFromLogits(logitsData);
  output.logits.dispose();
  
  const token = itos[currentId] ?? "?";
  
  // Reset state on EOS
  if (token === "EOS") {
    if (h) { try { h.dispose(); } catch (e) {} }
    if (c) { try { c.dispose(); } catch (e) {} }
    h = zeroState();
    c = zeroState();
  }
  
  if (record && parser) {
    parser.feed(token);
  }
  return token;
}

function sampleFromLogits(logits) {
  const temp = Math.max(0.05, temperature);
  const k = Math.max(1, topK);
  const scored = [];
  for (let i = 0; i < logits.length; i += 1) {
    const token = itos[i];
    if (token === undefined) continue;
    let score = logits[i] / temp;
    if (token === "<EOP>" || token === "EOS") score -= 1.0;
    scored.push([i, score]);
  }
  scored.sort((a, b) => b[1] - a[1]);
  const picked = scored.slice(0, Math.min(k, scored.length));
  const maxScore = picked[0]?.[1] ?? 0;
  let sum = 0;
  for (const item of picked) {
    item[2] = Math.exp(item[1] - maxScore);
    sum += item[2];
  }
  let r = Math.random() * sum;
  for (const item of picked) {
    r -= item[2];
    if (r <= 0) return item[0];
  }
  return picked[picked.length - 1][0];
}

// Duration converters


function durationToSecondsFromEventSteps(steps, grid) {
  const quarterSeconds = 60 / currentBpm;
  return Math.max(0.035, (Math.max(1, steps) * 4 * quarterSeconds) / Math.max(1, grid));
}

function midiToTonePitch(midi) {
  const names = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
  return `${names[((midi % 12) + 12) % 12]}${Math.floor(midi / 12) - 1}`;
}



function pushEvent(event) {
  if (isWarmingUp) return; // Discard prompt history events
  tempQueue.push(event);
}

// Token Slicers
function getTokensUpToBar(tokens, targetBar) {
  let barCount = 0;
  const sliced = [];
  for (const t of tokens) {
    if (t === "BAR") {
      barCount += 1;
      if (barCount > targetBar) {
        break;
      }
    }
    sliced.push(t);
  }
  return sliced;
}



// Event Parser
function makeEventParser() {
  return {
    grid: 64,
    bar: -1,
    pos: 0,
    pendingPosition: null,
    pendingNotes: [],
    pendingNote: null,
    lastQ: 0,
    reset() {
      this.grid = 64;
      this.bar = -1;
      this.pos = 0;
      this.pendingPosition = null;
      this.pendingNotes = [];
      this.pendingNote = null;
      this.lastQ = 0;
    },
    feed(token) {
      if (token.startsWith("BPM_")) {
        const bpm = Number(token.slice(4));
        if (Number.isFinite(bpm) && bpm >= 40 && bpm <= 220) {
          if (playingStyle !== "midi" || isWarmingUp || midiBpmMode === "model") {
            currentBpm = bpm;
            self.postMessage({ action: "tempo", bpm: bpm });
          }
        }
        return;
      }
      if (token.startsWith("GRID_")) {
        const grid = Number(token.slice(5));
        if (Number.isFinite(grid) && grid > 0) this.grid = grid;
        return;
      }
      if (token === "BAR") {
        this.flushTo(this.absoluteQFor(this.bar + 1, 0));
        this.bar += 1;
        this.pos = 0;
        return;
      }
      if (token.startsWith("POS_")) {
        const pos = Number(token.slice(4));
        if (!Number.isFinite(pos)) return;
        this.flushTo(this.absoluteQFor(this.bar, pos));
        this.pos = pos;
        return;
      }
      if (token.startsWith("NOTE_")) {
        const midi = Number(token.slice(5));
        if (Number.isFinite(midi) && midi >= 0 && midi <= 127) {
          this.pendingNote = { midi, durationSteps: 1, velocity: 0.72 };
        }
        return;
      }
      if (token.startsWith("DUR_") && this.pendingNote) {
        const steps = Number(token.slice(4));
        if (Number.isFinite(steps)) this.pendingNote.durationSteps = Math.max(1, steps);
        return;
      }
      if (token.startsWith("VEL_") && this.pendingNote) {
        const bucket = Number(token.slice(4));
        if (Number.isFinite(bucket)) this.pendingNote.velocity = Math.max(0.2, Math.min(0.95, bucket / 8));
        const q = this.absoluteQFor(this.bar, this.pos);
        this.pendingPosition ??= q;
        this.pendingNotes.push(this.pendingNote);
        this.pendingNote = null;
      }
    },
    absoluteQFor(bar, pos) {
      return Math.max(0, bar) * 4 + (Math.max(0, pos) * 4) / Math.max(1, this.grid);
    },
    flushTo(nextQ) {
      if (this.pendingNote) {
        const q = this.absoluteQFor(this.bar, this.pos);
        this.pendingPosition ??= q;
        this.pendingNotes.push(this.pendingNote);
        this.pendingNote = null;
      }
      if (this.pendingNotes.length && this.pendingPosition !== null) {
        const gap = Math.max(0, this.pendingPosition - this.lastQ);
        if (gap > 0) pushEvent({ type: "rest", duration: this.quartersToSeconds(gap) });
        pushEvent({
          type: "note",
          notes: this.pendingNotes.map((note) => midiToTonePitch(note.midi)),
          perNoteDurations: this.pendingNotes.map((note) => durationToSecondsFromEventSteps(note.durationSteps, this.grid)),
          velocities: this.pendingNotes.map((note) => note.velocity),
          duration: 0,
          advance: 0,
        });
        this.lastQ = this.pendingPosition;
      }
      const finalGap = Math.max(0, nextQ - this.lastQ);
      if (finalGap > 0) pushEvent({ type: "rest", duration: this.quartersToSeconds(finalGap) });
      this.lastQ = Math.max(this.lastQ, nextQ);
      this.pendingPosition = null;
      this.pendingNotes = [];
    },
    quartersToSeconds(quarters) {
      return (quarters * 60) / currentBpm;
    },
  };
}

function makeParser() {
  return makeEventParser();
}

async function warmPrompt() {
  h = zeroState();
  c = zeroState();
  
  let allTokensStr;
  if (playingStyle === "midi" && midiTokens.length > 0) {
    allTokensStr = getTokensUpToBar(midiTokens, midiStartBar);
  } else if (playingStyle === "bach") {
    allTokensStr = [
      "BOS", "BPM_100", "GRID_64",
      "BAR",
      "POS_0", "NOTE_81", "DUR_4", "VEL_6",
      "POS_4", "NOTE_79", "DUR_4", "VEL_6",
      "POS_8", "NOTE_77", "DUR_4", "VEL_6",
      "POS_12", "NOTE_76", "DUR_4", "VEL_6",
      "POS_16", "NOTE_74", "DUR_4", "VEL_6",
      "POS_20", "NOTE_73", "DUR_4", "VEL_6",
      "POS_24", "NOTE_74", "DUR_16", "VEL_6",
      "BAR"
    ];
  } else if (playingStyle === "mozart") {
    allTokensStr = [
      "BOS", "BPM_132", "GRID_64",
      "BAR",
      // Bright G-major chamber-style opening inspired by Mozart's serenades.
      "POS_0", "NOTE_55", "DUR_8", "VEL_6",
      "NOTE_67", "DUR_8", "VEL_6",
      "POS_8", "NOTE_74", "DUR_4", "VEL_7",
      "POS_12", "NOTE_71", "DUR_4", "VEL_6",
      "POS_16", "NOTE_79", "DUR_8", "VEL_7",
      "POS_24", "NOTE_78", "DUR_4", "VEL_6",
      "POS_28", "NOTE_76", "DUR_4", "VEL_6",
      "POS_32", "NOTE_74", "DUR_8", "VEL_7",
      "POS_40", "NOTE_72", "DUR_4", "VEL_6",
      "POS_44", "NOTE_71", "DUR_4", "VEL_6",
      "POS_48", "NOTE_69", "DUR_8", "VEL_6",
      "POS_56", "NOTE_67", "DUR_8", "VEL_7",
      "BAR",
      "POS_0", "NOTE_60", "DUR_16", "VEL_5",
      "NOTE_64", "DUR_16", "VEL_5",
      "NOTE_69", "DUR_16", "VEL_5",
      "POS_16", "NOTE_72", "DUR_4", "VEL_6",
      "POS_20", "NOTE_76", "DUR_4", "VEL_6",
      "POS_24", "NOTE_81", "DUR_8", "VEL_7",
      "POS_32", "NOTE_79", "DUR_4", "VEL_6",
      "POS_36", "NOTE_78", "DUR_4", "VEL_6",
      "POS_40", "NOTE_76", "DUR_4", "VEL_6",
      "POS_44", "NOTE_74", "DUR_4", "VEL_6",
      "POS_48", "NOTE_71", "DUR_8", "VEL_6",
      "POS_56", "NOTE_67", "DUR_8", "VEL_7",
      "BAR"
    ];
  } else if (playingStyle === "chopin") {
    allTokensStr = [
      "BOS", "BPM_68", "GRID_64",
      "BAR",
      // Original C-minor nocturne texture: rolling bass beneath a lyrical upper line.
      "POS_0", "NOTE_36", "DUR_16", "VEL_4", "NOTE_72", "DUR_12", "VEL_6",
      "POS_8", "NOTE_43", "DUR_8", "VEL_4",
      "POS_12", "NOTE_75", "DUR_8", "VEL_6",
      "POS_16", "NOTE_48", "DUR_8", "VEL_4",
      "POS_24", "NOTE_51", "DUR_8", "VEL_4", "NOTE_77", "DUR_12", "VEL_6",
      "POS_32", "NOTE_39", "DUR_16", "VEL_4", "NOTE_79", "DUR_16", "VEL_7",
      "POS_40", "NOTE_46", "DUR_8", "VEL_4",
      "POS_48", "NOTE_51", "DUR_8", "VEL_4", "NOTE_77", "DUR_8", "VEL_6",
      "POS_56", "NOTE_55", "DUR_8", "VEL_4", "NOTE_75", "DUR_8", "VEL_5",
      "BAR",
      "POS_0", "NOTE_41", "DUR_16", "VEL_4", "NOTE_72", "DUR_16", "VEL_6",
      "POS_8", "NOTE_48", "DUR_8", "VEL_4",
      "POS_16", "NOTE_53", "DUR_8", "VEL_4", "NOTE_74", "DUR_8", "VEL_6",
      "POS_24", "NOTE_56", "DUR_8", "VEL_4", "NOTE_75", "DUR_8", "VEL_6",
      "POS_32", "NOTE_43", "DUR_16", "VEL_4", "NOTE_79", "DUR_12", "VEL_7",
      "POS_40", "NOTE_50", "DUR_8", "VEL_4",
      "POS_44", "NOTE_77", "DUR_8", "VEL_6",
      "POS_48", "NOTE_55", "DUR_8", "VEL_4",
      "POS_56", "NOTE_60", "DUR_8", "VEL_4", "NOTE_75", "DUR_8", "VEL_5",
      "BAR"
    ];
  } else if (playingStyle === "debussy") {
    allTokensStr = [
      "BOS", "BPM_74", "GRID_64",
      "BAR",
      // Original impressionist sketch: open fifths and slowly shifting parallel colors.
      "POS_0", "NOTE_38", "DUR_32", "VEL_3", "NOTE_57", "DUR_16", "VEL_4", "NOTE_62", "DUR_16", "VEL_4", "NOTE_69", "DUR_16", "VEL_5",
      "POS_16", "NOTE_59", "DUR_16", "VEL_4", "NOTE_64", "DUR_16", "VEL_4", "NOTE_71", "DUR_16", "VEL_5",
      "POS_32", "NOTE_45", "DUR_32", "VEL_3", "NOTE_61", "DUR_16", "VEL_4", "NOTE_66", "DUR_16", "VEL_4", "NOTE_73", "DUR_16", "VEL_5",
      "POS_48", "NOTE_64", "DUR_16", "VEL_4", "NOTE_69", "DUR_16", "VEL_4", "NOTE_76", "DUR_16", "VEL_5",
      "BAR",
      "POS_0", "NOTE_40", "DUR_32", "VEL_3", "NOTE_59", "DUR_16", "VEL_4", "NOTE_64", "DUR_16", "VEL_4", "NOTE_71", "DUR_16", "VEL_5",
      "POS_16", "NOTE_61", "DUR_16", "VEL_4", "NOTE_66", "DUR_16", "VEL_4", "NOTE_73", "DUR_16", "VEL_5",
      "POS_32", "NOTE_43", "DUR_32", "VEL_3", "NOTE_62", "DUR_16", "VEL_4", "NOTE_67", "DUR_16", "VEL_4", "NOTE_74", "DUR_16", "VEL_5",
      "POS_48", "NOTE_66", "DUR_16", "VEL_4", "NOTE_71", "DUR_16", "VEL_4", "NOTE_78", "DUR_16", "VEL_5",
      "BAR"
    ];
  } else if (playingStyle === "beethoven") {
    allTokensStr = [
      "BOS", "BPM_126", "GRID_64",
      "BAR",
      // Original dramatic sonata gesture: compact motif, octave bass, accented response.
      "POS_0", "NOTE_36", "DUR_8", "VEL_7", "NOTE_48", "DUR_8", "VEL_7", "NOTE_67", "DUR_4", "VEL_7",
      "POS_4", "NOTE_70", "DUR_4", "VEL_7",
      "POS_8", "NOTE_68", "DUR_8", "VEL_8",
      "POS_16", "NOTE_43", "DUR_8", "VEL_6", "NOTE_55", "DUR_8", "VEL_6", "NOTE_65", "DUR_4", "VEL_7",
      "POS_20", "NOTE_68", "DUR_4", "VEL_7",
      "POS_24", "NOTE_67", "DUR_8", "VEL_8",
      "POS_32", "NOTE_39", "DUR_8", "VEL_7", "NOTE_51", "DUR_8", "VEL_7", "NOTE_63", "DUR_4", "VEL_7",
      "POS_36", "NOTE_67", "DUR_4", "VEL_7",
      "POS_40", "NOTE_72", "DUR_8", "VEL_8",
      "POS_48", "NOTE_44", "DUR_8", "VEL_6", "NOTE_56", "DUR_8", "VEL_6", "NOTE_70", "DUR_4", "VEL_7",
      "POS_52", "NOTE_68", "DUR_4", "VEL_7",
      "POS_56", "NOTE_67", "DUR_8", "VEL_8",
      "BAR",
      "POS_0", "NOTE_41", "DUR_16", "VEL_7", "NOTE_53", "DUR_16", "VEL_7", "NOTE_65", "DUR_8", "VEL_7",
      "POS_8", "NOTE_68", "DUR_4", "VEL_7",
      "POS_12", "NOTE_72", "DUR_4", "VEL_8",
      "POS_16", "NOTE_77", "DUR_8", "VEL_8",
      "POS_24", "NOTE_75", "DUR_8", "VEL_7",
      "POS_32", "NOTE_43", "DUR_16", "VEL_7", "NOTE_55", "DUR_16", "VEL_7", "NOTE_74", "DUR_8", "VEL_7",
      "POS_40", "NOTE_72", "DUR_8", "VEL_7",
      "POS_48", "NOTE_67", "DUR_16", "VEL_8", "NOTE_72", "DUR_16", "VEL_8", "NOTE_75", "DUR_16", "VEL_8",
      "BAR"
    ];
  } else if (playingStyle === "satie") {
    allTokensStr = [
      "BOS", "BPM_62", "GRID_64",
      "BAR",
      // Original sparse meditation: low bass, suspended chords, unhurried melody.
      "POS_0", "NOTE_38", "DUR_32", "VEL_3", "NOTE_69", "DUR_20", "VEL_5",
      "POS_16", "NOTE_57", "DUR_24", "VEL_3", "NOTE_62", "DUR_24", "VEL_3", "NOTE_65", "DUR_24", "VEL_3",
      "POS_32", "NOTE_45", "DUR_32", "VEL_3", "NOTE_72", "DUR_16", "VEL_5",
      "POS_48", "NOTE_55", "DUR_24", "VEL_3", "NOTE_60", "DUR_24", "VEL_3", "NOTE_64", "DUR_24", "VEL_3",
      "BAR",
      "POS_0", "NOTE_40", "DUR_32", "VEL_3", "NOTE_71", "DUR_20", "VEL_5",
      "POS_16", "NOTE_59", "DUR_24", "VEL_3", "NOTE_64", "DUR_24", "VEL_3", "NOTE_67", "DUR_24", "VEL_3",
      "POS_32", "NOTE_43", "DUR_32", "VEL_3", "NOTE_74", "DUR_16", "VEL_5",
      "POS_48", "NOTE_57", "DUR_24", "VEL_3", "NOTE_62", "DUR_24", "VEL_3", "NOTE_66", "DUR_24", "VEL_3",
      "BAR"
    ];
  } else {
    allTokensStr = ["BOS", "BPM_120", "GRID_64", "BAR", "POS_0"];
  }
  
  isWarmingUp = true;
  
  // 1. Feed all prompt tokens to parser to advance its clock
  if (parser) {
    for (const token of allTokensStr) {
      parser.feed(token);
    }
  }
  
  // 2. Slice model warm-up tokens to last 256 (matching model's context window)
  const modelTokensStr = allTokensStr.slice(-256);
  const ids = modelTokensStr.map(tokenId).filter((id) => id !== null);
  
  for (const id of ids) {
    currentId = id;
    await stepModel(false);
  }
  
  isWarmingUp = false;
}

async function pumpTokens(targetCount = 96) {
  tempQueue = [];
  let steps = 0;
  // Keep stepping the model until we collect enough parsed events or hit loop bounds
  while (tempQueue.length < targetCount && steps < 300) {
    await stepModel(true);
    steps += 1;
  }
  return {
    events: tempQueue,
    lastToken: itos[currentId] ?? "?"
  };
}

// Message Router
self.onmessage = async function (e) {
  const data = e.data;
  
  switch (data.action) {
    case "init":
      try {
        console.log(`Worker loading model: ${data.activeModelName}, requested EP: ${data.ep || "wasm"}`);
        activeModelName = data.activeModelName;
        stoi = data.vocab.stoi;
        itos = Object.fromEntries(Object.entries(data.vocab.itos).map(([key, value]) => [Number(key), value]));
        vocabSize = data.vocab.vocab_size;
        
        if (data.modelConfig) {
          numLayers = data.modelConfig.numLayers || 2;
          hiddenSize = data.modelConfig.hiddenSize || 2048;
        }
        
        if (session) {
          try { session.dispose(); } catch (e) {}
          session = null;
        }
        
        let epUsed = data.ep || "wasm";
        if (data.ep === "webgpu") {
          try {
            console.log("Attempting WebGPU execution provider...");
            session = await ort.InferenceSession.create(data.modelBuffer, {
              executionProviders: ["webgpu"],
            });
            console.log("WebGPU session created successfully!");
            epUsed = "webgpu";
          } catch (webgpuErr) {
            console.warn("WebGPU EP failed or unsupported by browser, falling back to CPU WASM:", webgpuErr);
            session = await ort.InferenceSession.create(data.modelBuffer, {
              executionProviders: ["wasm"],
              graphOptimizationLevel: "all",
            });
            epUsed = "wasm (CPU fallback)";
          }
        } else {
          session = await ort.InferenceSession.create(data.modelBuffer, {
            executionProviders: ["wasm"],
            graphOptimizationLevel: "all",
          });
          epUsed = "wasm";
        }
        
        updateModelDimensions(session);
        
        if (h) { try { h.dispose(); } catch (e) {} h = null; }
        if (c) { try { c.dispose(); } catch (e) {} c = null; }
        
        self.postMessage({
          action: "initialized",
          activeModelName: activeModelName,
          vocabSize: vocabSize,
          epUsed: epUsed
        });
      } catch (err) {
        console.error("Worker initialization failed:", err);
        self.postMessage({ action: "error", message: `Init failed: ${err.message}` });
      }
      break;
      
    case "start":
      try {
        temperature = data.temperature;
        topK = data.topK;
        currentBpm = data.bpm;
        playingStyle = data.playingStyle || "default";
        midiTokens = data.midiTokens;
        midiStartBar = data.midiStartBar;
        midiBpmMode = data.midiBpmMode || "lock";
        
        parser = makeParser();
        parser.reset();
        
        await warmPrompt();
        
        const initialBatch = await pumpTokens(96);
        self.postMessage({
          action: "started",
          events: initialBatch.events,
          lastToken: initialBatch.lastToken
        });
      } catch (err) {
        console.error("Worker start failed:", err);
        self.postMessage({ action: "error", message: `Start failed: ${err.message}` });
      }
      break;
      
    case "pump":
      try {
        temperature = data.temperature;
        topK = data.topK;
        const batch = await pumpTokens(96);
        self.postMessage({
          action: "events",
          events: batch.events,
          lastToken: batch.lastToken
        });
      } catch (err) {
        console.error("Worker pump failed:", err);
        self.postMessage({ action: "error", message: `Pump failed: ${err.message}` });
      }
      break;
      
    case "stop":
      // Halts ongoing actions, resets states if needed
      break;
  }
};