@potatosalad775/eqcaps-device-bridge 0.4.3 → 0.5.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.
@@ -13,8 +13,17 @@
13
13
  // shows 10 bands and writes all 11. The app writes a config by selecting it (0x110E) and sending
14
14
  // every parameter as its own command: 0x9C01–0x9C04 for the pre-gain and its switch, left and
15
15
  // right, and 0x9000 | band << 8 | field for the bands (fields 1–5 left type, frequency, gain, Q
16
- // and switch; 6–10 right). It sends no save command after them. Both channels get the same
17
- // filters here (`channels: linked`); a pull returns the left channel's.
16
+ // and switch; 6–10 right). It sends no save command after them.
17
+ //
18
+ // A config holds both channels on every family, and the app's editor edits them apart. The DX5 II,
19
+ // which was seen to keep them apart (firmware 2.58), is driven so (`perChannel`): a push takes the
20
+ // right channel as `right` and a pull returns it. The other families get the same filters on both
21
+ // channels, and a pull returns the left channel's. The app keeps one bit per config in the
22
+ // L/R-mode mask (0x1112): set, its editor opens that config with the channels apart. The device
23
+ // sends the mask after a connect and echoes a write of it. After a push that names `right` the
24
+ // handler sets the config's bit where the channels differ and clears it where they are equal, as
25
+ // the app does after a save, when the device has sent the mask and the bit has to change; after a
26
+ // delete it moves the later configs' bits down, as the app does.
18
27
  //
19
28
  // Type codes are the editor's: 1 peak, 2 low pass, 3 high pass, 4 low shelf, 5 high shelf.
20
29
  // Gain is in tenths of a dB, Q in ten-thousandths, frequency in Hz. The pre-gain word is a linear
@@ -24,9 +33,17 @@
24
33
  // gain than the number says, as it does from Topping's app. A recording of the app with a DX5 II
25
34
  // showed why the table is kept: the app reads any other word as a neighbouring step.
26
35
  //
36
+ // The stored configs are a list, not fixed memories: 3 on the DX1 II and E50 II, up to 10 on the
37
+ // others, each with a name of up to 15 ASCII bytes. The app adds one to the end by sending a whole
38
+ // config (0x110B, 74 words: the name, the pre-gains and both channels' bands) and then its name
39
+ // again (0x110C the index, 0x110D the name's four words); it deletes one by index (0x110A), after
40
+ // which the later ones move down. A DX5 II makes a new config the active one by itself and falls
41
+ // back to config 0 after a delete. Creating, renaming and deleting are driven for the DX5 II (tried
42
+ // on firmware 2.58, and recorded from the app) and the DX9 Discrete (as the app does it,
43
+ // unchecked). The D900 stores a config in another layout, and the other two can't add or delete.
44
+ //
27
45
  // Left out, though the app does them: the heartbeat it sends every 1 or 5 s, the shutdown notice on
28
- // disconnect, the L/R-mode mask (0x1112) after a save, the preview state, creating, renaming and
29
- // deleting configs, and most settings besides the EQ.
46
+ // disconnect, the preview state, and most settings besides the EQ.
30
47
  //
31
48
  // The DX5 II's settings are controls: one read (0x710C) answers a block of words, one per setting,
32
49
  // and each setting is written with its own command, which the device takes at once. Tried on the
@@ -39,7 +56,12 @@ const READ = 0x10;
39
56
  const WRITE = 0x20;
40
57
  const CONNECT_STATE = 0x1101;
41
58
  const UPLOAD = 0x1106;
59
+ const DELETE_CONFIG = 0x110a;
60
+ const DOWNLOAD_CONFIG = 0x110b;
61
+ const RENAME_INDEX = 0x110c;
62
+ const RENAME_NAME = 0x110d;
42
63
  const SWITCH_CONFIG = 0x110e;
64
+ const LR_MODE = 0x1112;
43
65
  const AGREEMENT = 0x1120;
44
66
  const SOFTWARE_VERSION = 0x1202;
45
67
  const EQ_ENABLE_STATE = 0x1204;
@@ -49,6 +71,7 @@ const PEQ_STATE = 0x7132;
49
71
  const D900_CAPABILITIES = 0x7144;
50
72
  const D900_RESULT = 0x7145;
51
73
  const PREAMP_LEFT = 0x9c01;
74
+ const PREAMP_RIGHT = 0x9c03;
52
75
  const BAND = 0x9000;
53
76
  /** `SWITCH_CONFIG` with this turns the EQ off on the DX1 II and E50 II. */
54
77
  const NO_CONFIG = 0xffffffff;
@@ -56,27 +79,43 @@ const UNITY = 0x2000000;
56
79
  const BANDS = 11;
57
80
  /** Words of a config as the device sends it; the app accepts 70 or more. */
58
81
  const CONFIG_WORDS = 70;
82
+ /** Words of a config as the app sends a new one: the 78 the device answers, less four spare. */
83
+ const NEW_CONFIG_WORDS = 74;
84
+ /** A name's bytes; the 16th byte of its four words is the config's band count. */
85
+ const NAME_BYTES = 15;
59
86
  const LIST_TIMEOUT = 10000;
60
87
  const ANSWER_TIMEOUT = 2000;
61
88
  const TYPES = typeCodes('topping', { PK: 1, LPQ: 2, HPQ: 3, LSC: 4, HSC: 5 }, [[0, 'PK']]);
62
89
  const FAMILIES = {
63
- dx1: { connect: 1, list: [UPLOAD, 0, READ], frame: 1, switchPause: 60, burst: 20 },
90
+ dx1: { connect: 1, list: [UPLOAD, 0, READ], frame: 1, switchPause: 60, burst: 20, configs: 3 },
64
91
  e50: {
65
92
  connect: 1,
66
93
  list: [UPLOAD, 0, READ],
67
94
  frame: 1,
68
95
  settings: { kind: READ, firmware: 31, slot: 14 },
69
96
  switchPause: 60,
70
- burst: 20
97
+ burst: 20,
98
+ configs: 3
99
+ },
100
+ dx5: {
101
+ connect: 2,
102
+ list: [AGREEMENT, 1, WRITE],
103
+ frame: 1,
104
+ switchPause: 120,
105
+ burst: 10,
106
+ configs: 10,
107
+ edits: true,
108
+ perChannel: true
71
109
  },
72
- dx5: { connect: 2, list: [AGREEMENT, 1, WRITE], frame: 1, switchPause: 120, burst: 10 },
73
110
  dx9d: {
74
111
  connect: 2,
75
112
  list: [AGREEMENT, 1, WRITE],
76
113
  frame: 1,
77
114
  settings: { kind: READ, firmware: 46, slot: 55, enabled: 54 },
78
115
  switchPause: 60,
79
- burst: 20
116
+ burst: 20,
117
+ configs: 10,
118
+ edits: true
80
119
  },
81
120
  d900: {
82
121
  connect: 2,
@@ -84,7 +123,8 @@ const FAMILIES = {
84
123
  frame: 0,
85
124
  settings: { kind: WRITE, firmware: 49, slot: 48, enabled: 47 },
86
125
  switchPause: 60,
87
- burst: 10
126
+ burst: 10,
127
+ configs: 10
88
128
  }
89
129
  };
90
130
  const familyOf = (o) => FAMILIES[o.family ?? 'dx1'];
@@ -166,38 +206,68 @@ function preampDb(word, family) {
166
206
  }
167
207
  const OFF = { type: 1, freq: 632, gain: 0, q: 7070, on: 0 };
168
208
  /**
169
- * The frames that select `slot` and write the request to both channels: the pre-gain, then the 11
170
- * bands, unused ones switched off at the app's defaults. The D900 gets all left bands before the
171
- * right ones, as the app sends them, and its 11th band as `reserved` (the app writes back what it
172
- * read there).
209
+ * A config's name as its four words: up to 15 bytes of printable ASCII, four to a word with the
210
+ * first in the low byte. The app drops anything else from a name; this refuses it.
211
+ */
212
+ export function toppingNameWords(name) {
213
+ if (!/^[\x20-\x7e]+$/.test(name) || name !== name.trim() || name.length > NAME_BYTES) {
214
+ throw new BridgeError('invalid-request', `Topping: a config's name is 1 to ${NAME_BYTES} ASCII characters, not ${JSON.stringify(name)}`);
215
+ }
216
+ return [0, 4, 8, 12].map((at) => [3, 2, 1, 0].reduce((w, i) => ((w << 8) | (name.charCodeAt(at + i) || 0)) >>> 0, 0));
217
+ }
218
+ /** The name in a stored config's first four words. */
219
+ export function toppingConfigName(w) {
220
+ const bytes = [0, 1, 2, 3].flatMap((i) => [0, 8, 16, 24].map((s) => ((w[i] ?? 0) >>> s) & 0xff));
221
+ const end = bytes.indexOf(0);
222
+ return String.fromCharCode(...bytes.slice(0, end < 0 ? NAME_BYTES : Math.min(end, NAME_BYTES)));
223
+ }
224
+ /**
225
+ * The words of a new config as the app sends one: the name with the band count in its 16th byte,
226
+ * each channel's switch and pre-gain, then 11 bands a channel of three words (switch, type and
227
+ * gain; frequency; Q). This one is empty: 0 dB and every band off at the app's defaults.
228
+ */
229
+ export function toppingNewConfigWords(name) {
230
+ const words = toppingNameWords(name);
231
+ words[3] = (words[3] | (BANDS << 24)) >>> 0;
232
+ words.push(1, UNITY, 1, UNITY);
233
+ for (let i = 0; i < 2 * BANDS; i++)
234
+ words.push(OFF.on | (OFF.type << 8), OFF.freq, OFF.q);
235
+ return words;
236
+ }
237
+ /**
238
+ * The frames that select `slot` and write the request: the pre-gains, then the 11 bands of each
239
+ * channel, unused ones switched off at the app's defaults. Without `right` both channels get
240
+ * `filters` and `preamp`. The D900 gets all left bands before the right ones, as the app sends
241
+ * them, and its 11th band as `reserved` (the app writes back what it read there).
173
242
  */
174
243
  export function toppingWriteFrames(request, o, reserved = { left: OFF, right: OFF }) {
175
244
  const family = familyOf(o);
176
245
  const d900 = o.family === 'd900';
177
- const { filters, preamp = 0, slot } = request;
178
- if (filters.length > BANDS - (d900 ? 1 : 0)) {
179
- throw new BridgeError('invalid-request', `Topping: ${filters.length} filters, a config holds ${BANDS - (d900 ? 1 : 0)}`);
180
- }
181
- const bands = filters.map((f) => ({
182
- type: TYPES.encode(f.type),
183
- freq: field(f.freq, 1, U32, 'freq'),
184
- gain: field(f.gain, 10, I32, 'gain'),
185
- q: field(f.q, 10000, U32, 'q'),
186
- on: 1
187
- }));
188
- const left = Array.from({ length: BANDS }, (_, i) => bands[i] ?? OFF);
189
- const right = [...left];
190
- if (d900) {
191
- left[BANDS - 1] = reserved.left;
192
- right[BANDS - 1] = reserved.right;
193
- }
246
+ const { filters, preamp = 0, right: r, slot } = request;
247
+ const most = BANDS - (d900 ? 1 : 0);
248
+ const channel = (filters, kept, side) => {
249
+ if (filters.length > most) {
250
+ throw new BridgeError('invalid-request', `Topping: ${filters.length} filters${side}, a config holds ${most}`);
251
+ }
252
+ const bands = filters.map((f) => ({
253
+ type: TYPES.encode(f.type),
254
+ freq: field(f.freq, 1, U32, 'freq'),
255
+ gain: field(f.gain, 10, I32, 'gain'),
256
+ q: field(f.q, 10000, U32, 'q'),
257
+ on: 1
258
+ }));
259
+ return Array.from({ length: BANDS }, (_, i) => (d900 && i === most ? kept : (bands[i] ?? OFF)));
260
+ };
261
+ const left = channel(filters, reserved.left, '');
262
+ const right = channel(r?.filters ?? filters, reserved.right, ' on the right');
194
263
  const out = [];
195
264
  const write = (command, data) => out.push(toppingFrame(command, data, WRITE, 1, family.frame));
196
265
  if (slot !== undefined) {
197
266
  out.push(toppingFrame(SWITCH_CONFIG, field(slot, 1, [0, 0xff], 'slot')));
198
267
  }
199
268
  const gain = toppingPreampWord(preamp, o.family);
200
- [gain, 1, gain, 1].forEach((v, i) => write(PREAMP_LEFT + i, v));
269
+ const gainRight = toppingPreampWord(r?.preamp ?? preamp, o.family);
270
+ [gain, 1, gainRight, 1].forEach((v, i) => write(PREAMP_LEFT + i, v));
201
271
  const side = (band, b, first) => [b.type, b.freq, b.gain, b.q, b.on].forEach((v, i) => write(BAND | ((band + 1) << 8) | (first + i), v));
202
272
  if (d900) {
203
273
  left.forEach((b, i) => side(i, b, 1));
@@ -222,28 +292,40 @@ export const toppingCodec = {
222
292
  encode: (request, o) => toppingWriteFrames(request, o),
223
293
  decode(frames, o) {
224
294
  const state = { filters: [] };
295
+ /** Each band's fields by number: 1–5 the left channel's, 6–10 the right's. */
225
296
  const bands = [];
297
+ const gains = {};
226
298
  for (const { data } of frames) {
227
299
  const f = parseToppingFrame(data);
228
300
  if (!f)
229
301
  continue;
230
302
  if (f.command === SWITCH_CONFIG)
231
303
  state.slot = f.data;
232
- else if (f.command === PREAMP_LEFT)
233
- state.preamp = preampDb(f.data, o.family);
234
- else if (f.command >> 12 === 9 && (f.command & 0xff) <= 5 && f.command >> 8 < 0x9c) {
304
+ else if (f.command === PREAMP_LEFT || f.command === PREAMP_RIGHT)
305
+ gains[f.command] = f.data;
306
+ else if (f.command >> 12 === 9 && (f.command & 0xff) <= 10 && f.command >> 8 < 0x9c) {
235
307
  (bands[((f.command >> 8) & 0xf) - 1] ??= {})[f.command & 0xff] = f.data;
236
308
  }
237
309
  }
238
- for (const b of bands) {
239
- if (!b || b[5] !== 1)
240
- continue;
241
- state.filters.push({
242
- type: TYPES.decode(b[1]),
243
- freq: b[2],
244
- gain: (b[3] | 0) / 10,
245
- q: b[4] / 10000
246
- });
310
+ const channel = (first) => bands.flatMap((b) => b[first + 4] === 1
311
+ ? [
312
+ {
313
+ type: TYPES.decode(b[first]),
314
+ freq: b[first + 1],
315
+ gain: (b[first + 2] | 0) / 10,
316
+ q: b[first + 3] / 10000
317
+ }
318
+ ]
319
+ : []);
320
+ const [gainLeft, gainRight] = [gains[PREAMP_LEFT], gains[PREAMP_RIGHT]];
321
+ state.filters = channel(1);
322
+ if (gainLeft !== undefined)
323
+ state.preamp = preampDb(gainLeft, o.family);
324
+ const apart = gainLeft !== gainRight || bands.some((b) => [1, 2, 3, 4, 5].some((i) => b[i] !== b[i + 5]));
325
+ if (apart) {
326
+ state.right = { filters: channel(6) };
327
+ if (gainRight !== undefined)
328
+ state.right.preamp = preampDb(gainRight, o.family);
247
329
  }
248
330
  return state;
249
331
  }
@@ -337,10 +419,9 @@ function d900Capable(c) {
337
419
  Object.entries(fixed).every(([i, v]) => c[Number(i)] === v) &&
338
420
  (c[6] & 0x1f0) === 0x1f0);
339
421
  }
340
- async function openSession(ctx) {
422
+ async function openSession(ctx, session) {
341
423
  const o = ctx.options;
342
424
  const family = familyOf(o);
343
- const session = { reserved: [] };
344
425
  if (o.family === 'd900') {
345
426
  const c = await ask(ctx, D900_CAPABILITIES, WRITE, 'the capabilities request');
346
427
  if (!d900Capable(c)) {
@@ -379,19 +460,61 @@ async function openSession(ctx) {
379
460
  return session;
380
461
  }
381
462
  const sessions = new WeakMap();
463
+ /** Keeps the L/R-mode mask the device sends: after a connect, and echoing a write. */
464
+ function watchLrMode(transport, session) {
465
+ return transport.onInputReport((_id, data) => {
466
+ const f = parseToppingFrame(data);
467
+ if (f?.command === LR_MODE)
468
+ session.lrMode = f.data & 0xffff;
469
+ });
470
+ }
382
471
  /** The transport's session, started on first use; one that fails to start is forgotten. */
383
472
  function sessionOf(ctx) {
384
473
  const known = sessions.get(ctx.transport);
385
474
  if (known)
386
475
  return known;
387
- const opening = openSession(ctx);
476
+ const session = { reserved: [] };
477
+ // Listening from before the connection request, after which the device sends the mask.
478
+ const unwatch = familyOf(ctx.options).perChannel
479
+ ? watchLrMode(ctx.transport, session)
480
+ : undefined;
481
+ const opening = openSession(ctx, session);
388
482
  sessions.set(ctx.transport, opening);
389
483
  opening.catch(() => {
484
+ unwatch?.();
390
485
  if (sessions.get(ctx.transport) === opening)
391
486
  sessions.delete(ctx.transport);
392
487
  });
393
488
  return opening;
394
489
  }
490
+ /**
491
+ * The mask once config `slot` is deleted, as the app reckons it until the device sends one: the
492
+ * later configs' bits move down with them.
493
+ */
494
+ function dropLrModeBit(session, slot) {
495
+ const m = session.lrMode;
496
+ if (m !== undefined)
497
+ session.lrMode = ((m & ((1 << slot) - 1)) | ((m >>> (slot + 1)) << slot)) & 0xffff;
498
+ }
499
+ /**
500
+ * Makes `slot`'s bit in the L/R-mode mask say whether its channels differ, as the app does after
501
+ * a save, so that its editor opens the config that way. Only a mask the device sent is written
502
+ * (the app starts from 0 without one, which would clear the other configs' bits), and only when
503
+ * the bit changes.
504
+ */
505
+ async function markChannels(ctx, session, slot, apart) {
506
+ const mask = session.lrMode;
507
+ if (mask === undefined) {
508
+ ctx.log(`Topping: the L/R-mode mask is unknown; config ${slot}'s bit is left as it is`);
509
+ return;
510
+ }
511
+ const next = (apart ? mask | (1 << slot) : mask & ~(1 << slot)) >>> 0;
512
+ if (next === mask)
513
+ return;
514
+ session.lrMode = next;
515
+ await send(ctx, LR_MODE, session.lrMode);
516
+ await ctx.sleep(35);
517
+ }
395
518
  const band = (w, at) => ({
396
519
  on: (w[at] & 0xff) === 1 ? 1 : 0,
397
520
  type: (w[at] >> 8) & 0xff,
@@ -399,15 +522,21 @@ const band = (w, at) => ({
399
522
  freq: w[at + 1],
400
523
  q: w[at + 2]
401
524
  });
402
- /** A stored config: words 5 the left pre-gain, 8… the left bands and 41… the right, 3 words each. */
525
+ /**
526
+ * A stored config: words 5 and 7 the left and right pre-gains, 8… the left bands and 41… the
527
+ * right, 3 words each.
528
+ */
403
529
  function decodeConfig(w, family) {
404
- const filters = Array.from({ length: BANDS }, (_, i) => {
405
- const b = band(w, 8 + 3 * i);
406
- if (!b.on)
407
- return null;
408
- return { type: TYPES.decode(b.type), freq: b.freq, gain: b.gain / 10, q: b.q / 10000 };
530
+ const channel = (gain, first) => ({
531
+ filters: Array.from({ length: BANDS }, (_, i) => {
532
+ const b = band(w, first + 3 * i);
533
+ if (!b.on)
534
+ return null;
535
+ return { type: TYPES.decode(b.type), freq: b.freq, gain: b.gain / 10, q: b.q / 10000 };
536
+ }),
537
+ preamp: preampDb(w[gain], family)
409
538
  });
410
- return { filters, preamp: preampDb(w[5], family) };
539
+ return { left: channel(5, 8), right: channel(7, 41) };
411
540
  }
412
541
  /**
413
542
  * Reads the stored configs: a two-frame header (the count, then a word that is not the active
@@ -448,6 +577,28 @@ async function readList(ctx, session) {
448
577
  }));
449
578
  return list;
450
579
  }
580
+ /** Sends the index and then the name's words, as the app renames a config. */
581
+ async function sendName(ctx, slot, name) {
582
+ const words = toppingNameWords(name);
583
+ await send(ctx, RENAME_INDEX, slot);
584
+ await ctx.sleep(50);
585
+ for (const [i, w] of words.entries()) {
586
+ await ctx.transport.sendReport(0, toppingFrame(RENAME_NAME, w, WRITE, words.length, i));
587
+ }
588
+ await ctx.sleep(80);
589
+ }
590
+ /** The session, for a family whose configs are created, renamed and deleted. */
591
+ async function editing(ctx) {
592
+ if (!familyOf(ctx.options).edits) {
593
+ throw new BridgeError('unsupported', 'Topping: configs are created, renamed and deleted on the DX5 II and DX9 Discrete only');
594
+ }
595
+ return sessionOf(ctx);
596
+ }
597
+ const slotOf = (w, id) => ({
598
+ id,
599
+ name: toppingConfigName(w) || `Config ${id + 1}`,
600
+ writable: true
601
+ });
451
602
  /** The active config, from the settings where they hold it. */
452
603
  async function activeSlot(ctx) {
453
604
  const o = ctx.options;
@@ -484,8 +635,13 @@ async function setEnabled(ctx, enabled, slot) {
484
635
  await send(ctx, SWITCH_CONFIG, slot);
485
636
  await ctx.sleep(o.family === 'd900' ? 100 : family.switchPause);
486
637
  }
487
- if (o.family !== 'd900')
488
- return send(ctx, PEQ_STATE, enabled ? 1 : 0);
638
+ if (o.family !== 'd900') {
639
+ await send(ctx, PEQ_STATE, enabled ? 1 : 0);
640
+ // A DX5 II takes up to 45 ms to switch, and answers a read of its EQ state with the old one
641
+ // until then.
642
+ await ctx.sleep(100);
643
+ return;
644
+ }
489
645
  // The D900 reports each setting's outcome: the command, a status (0 for done) and the value.
490
646
  const [, status] = await gather(ctx, toppingFrame(PEQ_STATE, enabled ? 1 : 0, WRITE, 1, 0), words(D900_RESULT), 5000, 'the EQ switch');
491
647
  if (status !== 0)
@@ -570,7 +726,7 @@ export const toppingUsbHid = {
570
726
  id: 'topping-usb-hid',
571
727
  transport: 'hid',
572
728
  codec: toppingCodec,
573
- capabilities: () => ({
729
+ capabilities: (_transport, o) => ({
574
730
  canRead: true,
575
731
  canWrite: true,
576
732
  readsPreamp: true,
@@ -579,7 +735,9 @@ export const toppingUsbHid = {
579
735
  writesSlot: true,
580
736
  needsBandCount: false,
581
737
  readsCurrentSlot: true,
582
- canEnable: true
738
+ canEnable: true,
739
+ editsSlots: familyOf(o).edits === true,
740
+ perChannel: familyOf(o).perChannel === true
583
741
  }),
584
742
  async pull(ctx, { slot, bands = BANDS - 1 }) {
585
743
  const list = await readList(ctx, await sessionOf(ctx));
@@ -588,8 +746,13 @@ export const toppingUsbHid = {
588
746
  if (!config) {
589
747
  throw new BridgeError('invalid-request', `Topping: no config ${index}, the device holds ${list.configs.length}`);
590
748
  }
591
- const { filters, preamp } = decodeConfig(config, ctx.options.family);
592
- return { filters: filters.slice(0, bands), preamp, slot: index };
749
+ const { left, right } = decodeConfig(config, ctx.options.family);
750
+ const cut = (c) => ({ filters: c.filters.slice(0, bands), preamp: c.preamp });
751
+ return {
752
+ ...cut(left),
753
+ ...(familyOf(ctx.options).perChannel ? { right: cut(right) } : {}),
754
+ slot: index
755
+ };
593
756
  },
594
757
  async push(ctx, request) {
595
758
  const session = await sessionOf(ctx);
@@ -597,7 +760,7 @@ export const toppingUsbHid = {
597
760
  const count = session.count ?? (await readList(ctx, session)).configs.length;
598
761
  const slot = request.slot ?? (await activeSlot(ctx)) ?? 0;
599
762
  if (slot >= count) {
600
- throw new BridgeError('invalid-request', `Topping: no config ${slot}, the device holds ${count}; Topping's app creates them`);
763
+ throw new BridgeError('invalid-request', `Topping: no config ${slot}, the device holds ${count}; create it first`);
601
764
  }
602
765
  const frames = toppingWriteFrames({ ...request, slot }, ctx.options, session.reserved[slot]);
603
766
  const [select, ...parameters] = frames;
@@ -612,26 +775,97 @@ export const toppingUsbHid = {
612
775
  }
613
776
  }
614
777
  await ctx.sleep(85);
778
+ // A push without `right` leaves the mask alone, as before the channels were driven apart.
779
+ if (request.right) {
780
+ const apart = toppingCodec.decode(frames, ctx.options).right !== undefined;
781
+ await markChannels(ctx, session, slot, apart);
782
+ }
615
783
  return { reconnect: false };
616
784
  },
617
785
  async currentSlot(ctx) {
618
786
  await sessionOf(ctx);
619
787
  return activeSlot(ctx);
620
788
  },
789
+ async slots(ctx) {
790
+ const list = await readList(ctx, await sessionOf(ctx));
791
+ return list.configs.map(slotOf);
792
+ },
793
+ async createSlot(ctx, { name }) {
794
+ const session = await editing(ctx);
795
+ const family = familyOf(ctx.options);
796
+ // The app reads the list again first: the new config goes after the last one.
797
+ const held = (await readList(ctx, session)).configs.map(toppingConfigName);
798
+ const slot = held.length;
799
+ if (slot >= family.configs) {
800
+ throw new BridgeError('invalid-request', `Topping: the device holds ${slot} configs, the most it stores`);
801
+ }
802
+ let named = name;
803
+ for (let n = slot + 1; named === undefined; n++) {
804
+ if (!held.includes(`Config ${n}`))
805
+ named = `Config ${n}`;
806
+ }
807
+ const words = toppingNewConfigWords(named);
808
+ for (const [i, w] of words.entries()) {
809
+ await ctx.transport.sendReport(0, toppingFrame(DOWNLOAD_CONFIG, w, WRITE, NEW_CONFIG_WORDS, i));
810
+ if (i % 10 === 9)
811
+ await ctx.sleep(20);
812
+ }
813
+ await ctx.sleep(120);
814
+ // A DX5 II plays the new config by itself; the app selects it on the others.
815
+ if (ctx.options.family !== 'dx5') {
816
+ await send(ctx, SWITCH_CONFIG, slot);
817
+ await ctx.sleep(120);
818
+ }
819
+ await sendName(ctx, slot, named);
820
+ const now = (await readList(ctx, session)).configs;
821
+ if (now.length !== slot + 1 || toppingConfigName(now[slot]) !== named) {
822
+ throw new BridgeError('rejected', `Topping: the device holds ${now.length} configs after creating one, not ${slot + 1} ending in "${named}"`);
823
+ }
824
+ return slotOf(now[slot], slot);
825
+ },
826
+ async renameSlot(ctx, slot, name) {
827
+ const session = await editing(ctx);
828
+ const count = session.count ?? (await readList(ctx, session)).configs.length;
829
+ if (!Number.isInteger(slot) || slot < 0 || slot >= count) {
830
+ throw new BridgeError('invalid-request', `Topping: no config ${slot}, the device holds ${count}`);
831
+ }
832
+ await sendName(ctx, slot, name);
833
+ },
834
+ async deleteSlot(ctx, slot) {
835
+ const session = await editing(ctx);
836
+ const count = (await readList(ctx, session)).configs.length;
837
+ if (!Number.isInteger(slot) || slot < 0 || slot >= count) {
838
+ throw new BridgeError('invalid-request', `Topping: no config ${slot}, the device holds ${count}`);
839
+ }
840
+ if (count <= 1) {
841
+ // The app refuses this too.
842
+ throw new BridgeError('invalid-request', "Topping: the device's last config stays");
843
+ }
844
+ // Before the delete: a mask the device sends after it replaces this.
845
+ dropLrModeBit(session, slot);
846
+ await send(ctx, DELETE_CONFIG, slot);
847
+ await ctx.sleep(140);
848
+ // A DX5 II falls back to its first config; the app selects that one to match.
849
+ if (ctx.options.family === 'dx5') {
850
+ await send(ctx, SWITCH_CONFIG, 0);
851
+ await ctx.sleep(120);
852
+ }
853
+ const now = (await readList(ctx, session)).configs.length;
854
+ if (now !== count - 1) {
855
+ throw new BridgeError('rejected', `Topping: the device holds ${now} configs after deleting one of ${count}`);
856
+ }
857
+ },
621
858
  async eqEnabled(ctx) {
622
859
  await sessionOf(ctx);
623
860
  const o = ctx.options;
624
- if (o.family === 'dx5') {
625
- // The app waits for the DX5 II to announce its switch; the device answers a read of it
626
- // (firmware 2.58).
627
- const [on] = await ask(ctx, PEQ_STATE, READ, 'the EQ switch request');
628
- return on !== 0;
629
- }
630
861
  const at = familyOf(o).settings?.enabled;
631
862
  if (at !== undefined) {
632
863
  const s = await settings(ctx);
633
864
  return s?.[at] === undefined ? null : s[at] !== 0;
634
865
  }
866
+ // The DX5 II too, whose app only waits for the device to announce its switch (0x7132). That
867
+ // command can't be asked: a DX5 II (firmware 2.58) takes a read of it as switching the EQ
868
+ // off. This one it answers and leaves the switch alone.
635
869
  const [state] = await ask(ctx, EQ_ENABLE_STATE, READ, 'the EQ state request');
636
870
  return (state & 1) === 1;
637
871
  },