@potatosalad775/eqcaps-device-bridge 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -14
- package/dist/browser/webhid.d.ts +2 -2
- package/dist/browser/webhid.d.ts.map +1 -1
- package/dist/browser/webhid.js +23 -15
- package/dist/browser/webhid.js.map +1 -1
- package/dist/device.d.ts +49 -6
- package/dist/device.d.ts.map +1 -1
- package/dist/device.js +92 -18
- package/dist/device.js.map +1 -1
- package/dist/handler.d.ts +92 -4
- package/dist/handler.d.ts.map +1 -1
- package/dist/handler.js.map +1 -1
- package/dist/handlers/index.d.ts +3 -1
- package/dist/handlers/index.d.ts.map +1 -1
- package/dist/handlers/index.js +4 -0
- package/dist/handlers/index.js.map +1 -1
- package/dist/handlers/qudelix-usb-hid.d.ts +20 -13
- package/dist/handlers/qudelix-usb-hid.d.ts.map +1 -1
- package/dist/handlers/qudelix-usb-hid.js +234 -91
- package/dist/handlers/qudelix-usb-hid.js.map +1 -1
- package/dist/handlers/qudelix-v2-usb-hid.d.ts +31 -0
- package/dist/handlers/qudelix-v2-usb-hid.d.ts.map +1 -0
- package/dist/handlers/qudelix-v2-usb-hid.js +276 -0
- package/dist/handlers/qudelix-v2-usb-hid.js.map +1 -0
- package/dist/handlers/qudelix.d.ts +222 -0
- package/dist/handlers/qudelix.d.ts.map +1 -0
- package/dist/handlers/qudelix.js +646 -0
- package/dist/handlers/qudelix.js.map +1 -0
- package/dist/handlers/topping-usb-hid.d.ts +59 -0
- package/dist/handlers/topping-usb-hid.d.ts.map +1 -0
- package/dist/handlers/topping-usb-hid.js +892 -0
- package/dist/handlers/topping-usb-hid.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/protocols.d.ts +9 -0
- package/dist/protocols.d.ts.map +1 -1
- package/dist/protocols.js +82 -33
- package/dist/protocols.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,892 @@
|
|
|
1
|
+
// Topping's DACs on USB id 0x152a:0x8750 (DX1 II, DX5 II, E50 II, DX9 Discrete, D900), as
|
|
2
|
+
// Topping's web app (home.toppingaudio.com, "web v1.17.0", read 2026-11-20) drives them. Reads and
|
|
3
|
+
// writes were tried on a DX5 II (firmware 2.58); the other four families are unchecked.
|
|
4
|
+
//
|
|
5
|
+
// Every message is one 16-byte output report with id 0:
|
|
6
|
+
// [0x22, 0x33, kind, frames, frame, command hi, command lo, data (u32 BE), 0, 0, 0x66, 0x77, 0]
|
|
7
|
+
// `kind` is 0x10 for a read and 0x20 for a write. An answer is an input report in the same layout,
|
|
8
|
+
// with or without one byte in front. A long answer comes as `frames` reports, one 32-bit word each.
|
|
9
|
+
//
|
|
10
|
+
// The five families share the EQ commands and differ in how a session starts, how the list of
|
|
11
|
+
// stored configs is asked for, and where the firmware version, the active config and the EQ switch
|
|
12
|
+
// are read (`family`). A config holds a name, a pre-gain and 11 bands per channel; the app's editor
|
|
13
|
+
// shows 10 bands and writes all 11. The app writes a config by selecting it (0x110E) and sending
|
|
14
|
+
// every parameter as its own command: 0x9C01–0x9C04 for the pre-gain and its switch, left and
|
|
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.
|
|
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.
|
|
27
|
+
//
|
|
28
|
+
// Type codes are the editor's: 1 peak, 2 low pass, 3 high pass, 4 low shelf, 5 high shelf.
|
|
29
|
+
// Gain is in tenths of a dB, Q in ten-thousandths, frequency in Hz. The pre-gain word is a linear
|
|
30
|
+
// gain with 0 dB at 2^25. The app takes it from a table, except for the D900, where it computes
|
|
31
|
+
// 2^25 · 10^(dB/20); this handler does the same. The table is one 0.1 dB step off above +7.7 dB and
|
|
32
|
+
// below −10.3 dB and wrong at +3.5 and −4.9 dB, so there the device applies a slightly different
|
|
33
|
+
// gain than the number says, as it does from Topping's app. A recording of the app with a DX5 II
|
|
34
|
+
// showed why the table is kept: the app reads any other word as a neighbouring step.
|
|
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
|
+
//
|
|
45
|
+
// Left out, though the app does them: the heartbeat it sends every 1 or 5 s, the shutdown notice on
|
|
46
|
+
// disconnect, the preview state, and most settings besides the EQ.
|
|
47
|
+
//
|
|
48
|
+
// The DX5 II's settings are controls: one read (0x710C) answers a block of words, one per setting,
|
|
49
|
+
// and each setting is written with its own command, which the device takes at once. Tried on the
|
|
50
|
+
// DX5 II: each one read back as written. The other families' settings are not driven.
|
|
51
|
+
import { be32, field, grid, I32, U32 } from "../bytes.js";
|
|
52
|
+
import { BridgeError } from "../errors.js";
|
|
53
|
+
import { hidFrame, sendFrame, typeCodes } from "../io.js";
|
|
54
|
+
import { after } from "../platform.js";
|
|
55
|
+
const READ = 0x10;
|
|
56
|
+
const WRITE = 0x20;
|
|
57
|
+
const CONNECT_STATE = 0x1101;
|
|
58
|
+
const UPLOAD = 0x1106;
|
|
59
|
+
const DELETE_CONFIG = 0x110a;
|
|
60
|
+
const DOWNLOAD_CONFIG = 0x110b;
|
|
61
|
+
const RENAME_INDEX = 0x110c;
|
|
62
|
+
const RENAME_NAME = 0x110d;
|
|
63
|
+
const SWITCH_CONFIG = 0x110e;
|
|
64
|
+
const LR_MODE = 0x1112;
|
|
65
|
+
const AGREEMENT = 0x1120;
|
|
66
|
+
const SOFTWARE_VERSION = 0x1202;
|
|
67
|
+
const EQ_ENABLE_STATE = 0x1204;
|
|
68
|
+
const CURRENT_CONFIG = 0x1206;
|
|
69
|
+
const GET_SETTINGS = 0x710c;
|
|
70
|
+
const PEQ_STATE = 0x7132;
|
|
71
|
+
const D900_CAPABILITIES = 0x7144;
|
|
72
|
+
const D900_RESULT = 0x7145;
|
|
73
|
+
const PREAMP_LEFT = 0x9c01;
|
|
74
|
+
const PREAMP_RIGHT = 0x9c03;
|
|
75
|
+
const BAND = 0x9000;
|
|
76
|
+
/** `SWITCH_CONFIG` with this turns the EQ off on the DX1 II and E50 II. */
|
|
77
|
+
const NO_CONFIG = 0xffffffff;
|
|
78
|
+
const UNITY = 0x2000000;
|
|
79
|
+
const BANDS = 11;
|
|
80
|
+
/** Words of a config as the device sends it; the app accepts 70 or more. */
|
|
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;
|
|
86
|
+
const LIST_TIMEOUT = 10000;
|
|
87
|
+
const ANSWER_TIMEOUT = 2000;
|
|
88
|
+
const TYPES = typeCodes('topping', { PK: 1, LPQ: 2, HPQ: 3, LSC: 4, HSC: 5 }, [[0, 'PK']]);
|
|
89
|
+
const FAMILIES = {
|
|
90
|
+
dx1: { connect: 1, list: [UPLOAD, 0, READ], frame: 1, switchPause: 60, burst: 20, configs: 3 },
|
|
91
|
+
e50: {
|
|
92
|
+
connect: 1,
|
|
93
|
+
list: [UPLOAD, 0, READ],
|
|
94
|
+
frame: 1,
|
|
95
|
+
settings: { kind: READ, firmware: 31, slot: 14 },
|
|
96
|
+
switchPause: 60,
|
|
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
|
|
109
|
+
},
|
|
110
|
+
dx9d: {
|
|
111
|
+
connect: 2,
|
|
112
|
+
list: [AGREEMENT, 1, WRITE],
|
|
113
|
+
frame: 1,
|
|
114
|
+
settings: { kind: READ, firmware: 46, slot: 55, enabled: 54 },
|
|
115
|
+
switchPause: 60,
|
|
116
|
+
burst: 20,
|
|
117
|
+
configs: 10,
|
|
118
|
+
edits: true
|
|
119
|
+
},
|
|
120
|
+
d900: {
|
|
121
|
+
connect: 2,
|
|
122
|
+
list: [UPLOAD, 0, WRITE],
|
|
123
|
+
frame: 0,
|
|
124
|
+
settings: { kind: WRITE, firmware: 49, slot: 48, enabled: 47 },
|
|
125
|
+
switchPause: 60,
|
|
126
|
+
burst: 10,
|
|
127
|
+
configs: 10
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const familyOf = (o) => FAMILIES[o.family ?? 'dx1'];
|
|
131
|
+
/** Firmware the app refuses to control: the DX5 II below 2.37, the E50 II below 1.52. */
|
|
132
|
+
const MIN_FIRMWARE = { dx5: 0x237, e50: 0x152 };
|
|
133
|
+
export function toppingFrame(command, data, kind = WRITE, frames = 1, frame = 1) {
|
|
134
|
+
return Uint8Array.from([
|
|
135
|
+
0x22,
|
|
136
|
+
0x33,
|
|
137
|
+
kind,
|
|
138
|
+
frames,
|
|
139
|
+
frame,
|
|
140
|
+
command >> 8,
|
|
141
|
+
command & 0xff,
|
|
142
|
+
...be32(data >>> 0),
|
|
143
|
+
0,
|
|
144
|
+
0,
|
|
145
|
+
0x66,
|
|
146
|
+
0x77,
|
|
147
|
+
0
|
|
148
|
+
]);
|
|
149
|
+
}
|
|
150
|
+
/** A report in the frame layout, with or without one byte in front; null for anything else. */
|
|
151
|
+
export function parseToppingFrame(d) {
|
|
152
|
+
const at = d[0] === 0x22 && d[1] === 0x33 ? 0 : d[1] === 0x22 && d[2] === 0x33 ? 1 : -1;
|
|
153
|
+
if (at < 0 || d.length < at + 15 || d[at + 13] !== 0x66 || d[at + 14] !== 0x77)
|
|
154
|
+
return null;
|
|
155
|
+
return {
|
|
156
|
+
kind: d[at + 2],
|
|
157
|
+
frames: d[at + 3],
|
|
158
|
+
frame: d[at + 4],
|
|
159
|
+
command: (d[at + 5] << 8) | d[at + 6],
|
|
160
|
+
data: ((d[at + 7] << 24) | (d[at + 8] << 16) | (d[at + 9] << 8) | d[at + 10]) >>> 0
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The pre-gain words the app sends, per 0.1 dB from 0 to 12 dB of cut and of boost. They are not
|
|
165
|
+
* quite 2^25 · 10^(dB/20), and the app reads a word that is not in its table as the next step
|
|
166
|
+
* towards 0 dB, then writes that back when it connects: so a computed word would shift by 0.1 dB
|
|
167
|
+
* the first time Topping's app saw it.
|
|
168
|
+
*/
|
|
169
|
+
// prettier-ignore
|
|
170
|
+
const PREAMP_CUT = [33554432, 33170334, 32790632, 32415259, 32044214, 31677397, 31314807, 30956345, 30601977, 30251702, 29905387, 29563065, 29224668, 28890131, 28559452, 28232531, 27909335, 27589863, 27274048, 26961858, 26653224, 26348114, 26046527, 25748362, 25453620, 25162267, 24874235, 24589493, 24308004, 24029771, 23754692, 23482767, 23213962, 22948245, 22685547, 22425869, 22169178, 21915406, 21664553, 21416552, 21171403, 20929040, 20689494, 20452634, 20218524, 19987100, 19758292, 19532135, 19308528, 19308528, 18869032, 18653042, 18439502, 18228445, 18019770, 17813511, 17609600, 17408039, 17208759, 17011761, 16817045, 16624543, 16434222, 16246116, 16060157, 15876313, 15694582, 15514931, 15337328, 15161737, 14988194, 14816630, 14647012, 14479374, 14313616, 14149769, 13987802, 13827680, 13669404, 13512940, 13358254, 13205346, 13054183, 12904732, 12757025, 12610997, 12466645, 12323938, 12182876, 12043390, 11905548, 11769250, 11634529, 11501351, 11369717, 11239560, 11110912, 10983707, 10857979, 10733693, 10610817, 10489350, 10369292, 10250610, 10250610, 10133270, 10017273, 9902583, 9789237, 9677198, 9566402, 9456914, 9348667, 9241628, 9135865, 9031276, 8927894, 8825688, 8724655, 8624797, 8526080];
|
|
171
|
+
// prettier-ignore
|
|
172
|
+
const PREAMP_BOOST = [33554432, 33942958, 34335981, 34733601, 35135785, 35542632, 35954211, 36370521, 36791696, 37217703, 37648676, 38084615, 38525621, 38971727, 39423001, 39879509, 40341285, 40808430, 41280943, 41758960, 42242546, 42731669, 43226463, 43726994, 44233331, 44802347, 45263653, 45787807, 46318001, 46854335, 47396876, 47945693, 48500884, 49062518, 49630628, 50075157, 50786679, 51374754, 51969641, 52571439, 53180184, 53795975, 54418913, 55049032, 55686465, 56331314, 56983579, 57643427, 58310891, 58986107, 59669141, 60360061, 61058999, 61766058, 62481271, 63204772, 63936627, 64676972, 65425907, 66183499, 66949883, 67725124, 68509325, 69302652, 70105140, 70916922, 71738100, 72568774, 73409077, 74259112, 75119011, 75988843, 76868740, 77758839, 78659239, 79570074, 80491445, 81423520, 81423520, 82366332, 83320117, 84284907, 85260872, 86248144, 87246858, 88257115, 89279116, 90312894, 91358685, 92416556, 93486707, 94569207, 95664289, 96772022, 97892605, 99026141, 100172797, 101332740, 102506138, 103693093, 104893805, 106108408, 107337104, 108579994, 109837279, 111109159, 112395737, 113697213, 115013754, 116345563, 117692774, 119055587, 120434204, 121828760, 123239489, 124666526, 126110104, 127570360, 129047560, 130541873, 132053467];
|
|
173
|
+
/** A word some devices hold from the factory, which the app reads as -3 dB. */
|
|
174
|
+
const PREAMP_FACTORY_MINUS_3 = 0x16a77e5;
|
|
175
|
+
/** The pre-gain word for `db`: the app's table, or computed on the D900 as the app does there. */
|
|
176
|
+
export function toppingPreampWord(db, family) {
|
|
177
|
+
const tenths = field(db, 10, [-120, 120], 'preamp');
|
|
178
|
+
if (family === 'd900')
|
|
179
|
+
return Math.round(UNITY * 10 ** (tenths / 200));
|
|
180
|
+
const table = tenths < 0 ? PREAMP_CUT : PREAMP_BOOST;
|
|
181
|
+
const i = Math.abs(tenths);
|
|
182
|
+
// The table repeats the step before at -4.9, -10.4 and +7.8 dB, so the app can't hold those.
|
|
183
|
+
// A word halfway to the next step is one the app reads as the step meant.
|
|
184
|
+
if (i > 0 && table[i] === table[i - 1])
|
|
185
|
+
return Math.round((table[i] + table[i + 1]) / 2);
|
|
186
|
+
return table[i];
|
|
187
|
+
}
|
|
188
|
+
/** The pre-gain a word means, as the app reads it. */
|
|
189
|
+
function preampDb(word, family) {
|
|
190
|
+
if (family === 'd900')
|
|
191
|
+
return word > 0 ? Math.round(200 * Math.log10(word / UNITY)) / 10 || 0 : 0;
|
|
192
|
+
if (word === PREAMP_FACTORY_MINUS_3)
|
|
193
|
+
return -3;
|
|
194
|
+
if (word < UNITY) {
|
|
195
|
+
const exact = PREAMP_CUT.indexOf(word);
|
|
196
|
+
if (exact > 0)
|
|
197
|
+
return -exact / 10;
|
|
198
|
+
const i = PREAMP_CUT.findIndex((v, n) => n > 0 && word < v && word > (PREAMP_CUT[n + 1] ?? Infinity));
|
|
199
|
+
return i > 0 ? -i / 10 : 0;
|
|
200
|
+
}
|
|
201
|
+
const exact = PREAMP_BOOST.indexOf(word);
|
|
202
|
+
if (exact >= 0)
|
|
203
|
+
return exact / 10;
|
|
204
|
+
const i = PREAMP_BOOST.findIndex((v, n) => word > v && word < (PREAMP_BOOST[n + 1] ?? -Infinity));
|
|
205
|
+
return i > 0 ? i / 10 : 0;
|
|
206
|
+
}
|
|
207
|
+
const OFF = { type: 1, freq: 632, gain: 0, q: 7070, on: 0 };
|
|
208
|
+
/**
|
|
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).
|
|
242
|
+
*/
|
|
243
|
+
export function toppingWriteFrames(request, o, reserved = { left: OFF, right: OFF }) {
|
|
244
|
+
const family = familyOf(o);
|
|
245
|
+
const d900 = o.family === 'd900';
|
|
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');
|
|
263
|
+
const out = [];
|
|
264
|
+
const write = (command, data) => out.push(toppingFrame(command, data, WRITE, 1, family.frame));
|
|
265
|
+
if (slot !== undefined) {
|
|
266
|
+
out.push(toppingFrame(SWITCH_CONFIG, field(slot, 1, [0, 0xff], 'slot')));
|
|
267
|
+
}
|
|
268
|
+
const gain = toppingPreampWord(preamp, o.family);
|
|
269
|
+
const gainRight = toppingPreampWord(r?.preamp ?? preamp, o.family);
|
|
270
|
+
[gain, 1, gainRight, 1].forEach((v, i) => write(PREAMP_LEFT + i, v));
|
|
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));
|
|
272
|
+
if (d900) {
|
|
273
|
+
left.forEach((b, i) => side(i, b, 1));
|
|
274
|
+
right.forEach((b, i) => side(i, b, 6));
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
left.forEach((b, i) => {
|
|
278
|
+
side(i, b, 1);
|
|
279
|
+
side(i, right[i], 6);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
return out.map((d) => hidFrame(0, d));
|
|
283
|
+
}
|
|
284
|
+
export const toppingCodec = {
|
|
285
|
+
types: TYPES.types,
|
|
286
|
+
wire: () => ({
|
|
287
|
+
freq: { ...grid(1, U32), min: 1 },
|
|
288
|
+
q: { ...grid(10000, U32), min: 0.0001 },
|
|
289
|
+
gain: grid(10, I32),
|
|
290
|
+
preamp: grid(10, [-120, 120])
|
|
291
|
+
}),
|
|
292
|
+
encode: (request, o) => toppingWriteFrames(request, o),
|
|
293
|
+
decode(frames, o) {
|
|
294
|
+
const state = { filters: [] };
|
|
295
|
+
/** Each band's fields by number: 1–5 the left channel's, 6–10 the right's. */
|
|
296
|
+
const bands = [];
|
|
297
|
+
const gains = {};
|
|
298
|
+
for (const { data } of frames) {
|
|
299
|
+
const f = parseToppingFrame(data);
|
|
300
|
+
if (!f)
|
|
301
|
+
continue;
|
|
302
|
+
if (f.command === SWITCH_CONFIG)
|
|
303
|
+
state.slot = f.data;
|
|
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) {
|
|
307
|
+
(bands[((f.command >> 8) & 0xf) - 1] ??= {})[f.command & 0xff] = f.data;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
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);
|
|
329
|
+
}
|
|
330
|
+
return state;
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
/**
|
|
334
|
+
* Sends `request`, if any, and feeds every frame that arrives to `take` until it returns a value.
|
|
335
|
+
* Subscribes before sending, so a fast answer isn't missed.
|
|
336
|
+
*/
|
|
337
|
+
function gather(ctx, request, take, timeoutMs, what) {
|
|
338
|
+
const result = new Promise((resolve, reject) => {
|
|
339
|
+
const cancel = after(timeoutMs, () => {
|
|
340
|
+
unsubscribe();
|
|
341
|
+
reject(new BridgeError('timeout', `Topping: no answer to ${what} within ${timeoutMs} ms`));
|
|
342
|
+
});
|
|
343
|
+
const unsubscribe = ctx.transport.onInputReport((_id, data) => {
|
|
344
|
+
const frame = parseToppingFrame(data);
|
|
345
|
+
if (!frame)
|
|
346
|
+
return;
|
|
347
|
+
let value;
|
|
348
|
+
try {
|
|
349
|
+
value = take(frame);
|
|
350
|
+
}
|
|
351
|
+
catch (error) {
|
|
352
|
+
cancel();
|
|
353
|
+
unsubscribe();
|
|
354
|
+
reject(error);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (value === undefined)
|
|
358
|
+
return;
|
|
359
|
+
cancel();
|
|
360
|
+
unsubscribe();
|
|
361
|
+
resolve(value);
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
// Keeps a send failure from leaving the wait's rejection unhandled.
|
|
365
|
+
result.catch(() => { });
|
|
366
|
+
return request ? ctx.transport.sendReport(0, request).then(() => result) : result;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Collects the words of an answer to `command`. Its frames are numbered from 0 or from 1; the
|
|
370
|
+
* first one seen says which, as the app decides it.
|
|
371
|
+
*/
|
|
372
|
+
function words(command) {
|
|
373
|
+
const got = [];
|
|
374
|
+
let base;
|
|
375
|
+
let seen = 0;
|
|
376
|
+
return (f) => {
|
|
377
|
+
if (f.command !== command)
|
|
378
|
+
return undefined;
|
|
379
|
+
if (f.frames <= 1)
|
|
380
|
+
return [f.data];
|
|
381
|
+
base ??= f.frame === 1 ? 1 : 0;
|
|
382
|
+
const i = f.frame - base;
|
|
383
|
+
if (i < 0 || i >= f.frames)
|
|
384
|
+
return undefined;
|
|
385
|
+
if (got[i] === undefined)
|
|
386
|
+
seen++;
|
|
387
|
+
got[i] = f.data;
|
|
388
|
+
return seen === f.frames ? got : undefined;
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
function ask(ctx, command, kind, what, timeoutMs = ANSWER_TIMEOUT) {
|
|
392
|
+
const frame = familyOf(ctx.options).frame;
|
|
393
|
+
return gather(ctx, toppingFrame(command, 0, kind, 1, frame), words(command), timeoutMs, what);
|
|
394
|
+
}
|
|
395
|
+
const send = (ctx, command, data, frame = familyOf(ctx.options).frame) => ctx.transport.sendReport(0, toppingFrame(command, data, WRITE, 1, frame));
|
|
396
|
+
/** "2.39" from 0x239: the hex digits are the decimal ones. Null where they aren't. */
|
|
397
|
+
export function toppingFirmware(word) {
|
|
398
|
+
const digits = word.toString(16);
|
|
399
|
+
if (word === 0 || /[a-f]/.test(digits))
|
|
400
|
+
return null;
|
|
401
|
+
return `${Number(digits.slice(0, -2) || '0')}.${digits.slice(-2).padStart(2, '0')}`;
|
|
402
|
+
}
|
|
403
|
+
function refuseOldFirmware(o, word) {
|
|
404
|
+
const min = MIN_FIRMWARE[o.family ?? 'dx1'];
|
|
405
|
+
if (min !== undefined && word < min) {
|
|
406
|
+
throw new BridgeError('unsupported', `Topping: firmware ${toppingFirmware(word) ?? `0x${word.toString(16)}`} is older than ${toppingFirmware(min)}, the first Topping's app controls`);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
async function settings(ctx) {
|
|
410
|
+
const s = familyOf(ctx.options).settings;
|
|
411
|
+
if (!s)
|
|
412
|
+
return null;
|
|
413
|
+
return ask(ctx, GET_SETTINGS, s.kind, 'the settings request');
|
|
414
|
+
}
|
|
415
|
+
/** What the app requires of the D900's capabilities answer before it sends anything else. */
|
|
416
|
+
function d900Capable(c) {
|
|
417
|
+
const fixed = { 0: 2, 1: 255, 2: 7, 3: 10, 4: 10, 5: 30, 8: 74, 9: 78, 10: 74, 11: 10 };
|
|
418
|
+
return (c.length === 12 &&
|
|
419
|
+
Object.entries(fixed).every(([i, v]) => c[Number(i)] === v) &&
|
|
420
|
+
(c[6] & 0x1f0) === 0x1f0);
|
|
421
|
+
}
|
|
422
|
+
async function openSession(ctx, session) {
|
|
423
|
+
const o = ctx.options;
|
|
424
|
+
const family = familyOf(o);
|
|
425
|
+
if (o.family === 'd900') {
|
|
426
|
+
const c = await ask(ctx, D900_CAPABILITIES, WRITE, 'the capabilities request');
|
|
427
|
+
if (!d900Capable(c)) {
|
|
428
|
+
throw new BridgeError('unsupported', "Topping D900: this firmware doesn't speak the control protocol Topping's app requires");
|
|
429
|
+
}
|
|
430
|
+
// The device answers 0 until it grants the connection; the app asks again 200 ms later.
|
|
431
|
+
for (let i = 0;; i++) {
|
|
432
|
+
const [granted] = await gather(ctx, toppingFrame(CONNECT_STATE, family.connect, WRITE, 1, 0), words(CONNECT_STATE), ANSWER_TIMEOUT, 'the connection request');
|
|
433
|
+
if (granted === 1)
|
|
434
|
+
break;
|
|
435
|
+
if (i === 24)
|
|
436
|
+
throw new BridgeError('rejected', 'Topping D900: connection not granted');
|
|
437
|
+
await ctx.sleep(200);
|
|
438
|
+
}
|
|
439
|
+
await send(ctx, AGREEMENT, 1);
|
|
440
|
+
return session;
|
|
441
|
+
}
|
|
442
|
+
await send(ctx, CONNECT_STATE, family.connect);
|
|
443
|
+
await ctx.sleep(100);
|
|
444
|
+
if (o.family === 'dx5') {
|
|
445
|
+
const [word] = await ask(ctx, SOFTWARE_VERSION, READ, 'the firmware version request');
|
|
446
|
+
session.firmware = word & 0xfff;
|
|
447
|
+
refuseOldFirmware(o, session.firmware);
|
|
448
|
+
return session;
|
|
449
|
+
}
|
|
450
|
+
// The DX9 Discrete's agreement is its config list request, sent when the list is read.
|
|
451
|
+
if (o.family !== 'dx9d') {
|
|
452
|
+
await send(ctx, AGREEMENT, 1);
|
|
453
|
+
await ctx.sleep(100);
|
|
454
|
+
}
|
|
455
|
+
if (o.family === 'e50') {
|
|
456
|
+
const s = await settings(ctx);
|
|
457
|
+
session.firmware = s?.[family.settings.firmware] ?? 0;
|
|
458
|
+
refuseOldFirmware(o, session.firmware);
|
|
459
|
+
}
|
|
460
|
+
return session;
|
|
461
|
+
}
|
|
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
|
+
}
|
|
471
|
+
/** The transport's session, started on first use; one that fails to start is forgotten. */
|
|
472
|
+
function sessionOf(ctx) {
|
|
473
|
+
const known = sessions.get(ctx.transport);
|
|
474
|
+
if (known)
|
|
475
|
+
return known;
|
|
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);
|
|
482
|
+
sessions.set(ctx.transport, opening);
|
|
483
|
+
opening.catch(() => {
|
|
484
|
+
unwatch?.();
|
|
485
|
+
if (sessions.get(ctx.transport) === opening)
|
|
486
|
+
sessions.delete(ctx.transport);
|
|
487
|
+
});
|
|
488
|
+
return opening;
|
|
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
|
+
}
|
|
518
|
+
const band = (w, at) => ({
|
|
519
|
+
on: (w[at] & 0xff) === 1 ? 1 : 0,
|
|
520
|
+
type: (w[at] >> 8) & 0xff,
|
|
521
|
+
gain: ((w[at] >> 16) << 24) >> 24,
|
|
522
|
+
freq: w[at + 1],
|
|
523
|
+
q: w[at + 2]
|
|
524
|
+
});
|
|
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
|
+
*/
|
|
529
|
+
function decodeConfig(w, family) {
|
|
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)
|
|
538
|
+
});
|
|
539
|
+
return { left: channel(5, 8), right: channel(7, 41) };
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Reads the stored configs: a two-frame header (the count, then a word that is not the active
|
|
543
|
+
* config on a DX5 II), then each config as its own run of frames. The app takes a missing header
|
|
544
|
+
* as 3.
|
|
545
|
+
*/
|
|
546
|
+
async function readList(ctx, session) {
|
|
547
|
+
const [command, data, kind] = familyOf(ctx.options).list;
|
|
548
|
+
const configs = [];
|
|
549
|
+
let count;
|
|
550
|
+
let current = [];
|
|
551
|
+
const list = await gather(ctx, toppingFrame(command, data, kind), (f) => {
|
|
552
|
+
if (f.command !== UPLOAD)
|
|
553
|
+
return undefined;
|
|
554
|
+
if (f.frames === 2) {
|
|
555
|
+
if (f.frame === 0) {
|
|
556
|
+
count = f.data;
|
|
557
|
+
configs.length = 0;
|
|
558
|
+
}
|
|
559
|
+
else if (count === 0)
|
|
560
|
+
return { configs };
|
|
561
|
+
return undefined;
|
|
562
|
+
}
|
|
563
|
+
if (f.frames < CONFIG_WORDS)
|
|
564
|
+
return undefined;
|
|
565
|
+
if (f.frame === 0)
|
|
566
|
+
current = [];
|
|
567
|
+
current[f.frame] = f.data;
|
|
568
|
+
if (f.frame !== f.frames - 1)
|
|
569
|
+
return undefined;
|
|
570
|
+
configs.push(Array.from({ length: f.frames }, (_, i) => current[i] ?? 0));
|
|
571
|
+
return configs.length >= (count ?? 3) ? { configs } : undefined;
|
|
572
|
+
}, LIST_TIMEOUT, 'the config list request');
|
|
573
|
+
session.count = list.configs.length;
|
|
574
|
+
session.reserved = list.configs.map((w) => ({
|
|
575
|
+
left: band(w, 8 + 3 * (BANDS - 1)),
|
|
576
|
+
right: band(w, 41 + 3 * (BANDS - 1))
|
|
577
|
+
}));
|
|
578
|
+
return list;
|
|
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
|
+
});
|
|
602
|
+
/** The active config, from the settings where they hold it. */
|
|
603
|
+
async function activeSlot(ctx) {
|
|
604
|
+
const o = ctx.options;
|
|
605
|
+
const at = familyOf(o).settings?.slot;
|
|
606
|
+
if (at !== undefined)
|
|
607
|
+
return (await settings(ctx))?.[at] ?? null;
|
|
608
|
+
// The app only listens for the active config of the DX1 II and DX5 II. A DX5 II answers this
|
|
609
|
+
// read (firmware 2.58).
|
|
610
|
+
try {
|
|
611
|
+
const [slot] = await ask(ctx, CURRENT_CONFIG, READ, 'the active config request', 1500);
|
|
612
|
+
return slot === NO_CONFIG ? 0 : slot;
|
|
613
|
+
}
|
|
614
|
+
catch (error) {
|
|
615
|
+
ctx.log(`Topping: active config unknown (${error.message})`);
|
|
616
|
+
return null;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
async function setEnabled(ctx, enabled, slot) {
|
|
620
|
+
await sessionOf(ctx);
|
|
621
|
+
const o = ctx.options;
|
|
622
|
+
const family = familyOf(o);
|
|
623
|
+
if (o.family === 'dx1' || o.family === 'e50' || o.family === undefined) {
|
|
624
|
+
// These two have no switch: selecting no config turns the EQ off, selecting one turns it on.
|
|
625
|
+
const target = enabled ? (slot ?? (await activeSlot(ctx)) ?? 0) : NO_CONFIG;
|
|
626
|
+
await send(ctx, SWITCH_CONFIG, target, 1);
|
|
627
|
+
if (enabled && o.family !== 'e50') {
|
|
628
|
+
await ctx.sleep(140);
|
|
629
|
+
await send(ctx, SWITCH_CONFIG, target, 1);
|
|
630
|
+
}
|
|
631
|
+
await ctx.sleep(enabled ? 170 : 90);
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
if (enabled && slot !== undefined) {
|
|
635
|
+
await send(ctx, SWITCH_CONFIG, slot);
|
|
636
|
+
await ctx.sleep(o.family === 'd900' ? 100 : family.switchPause);
|
|
637
|
+
}
|
|
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
|
+
}
|
|
645
|
+
// The D900 reports each setting's outcome: the command, a status (0 for done) and the value.
|
|
646
|
+
const [, status] = await gather(ctx, toppingFrame(PEQ_STATE, enabled ? 1 : 0, WRITE, 1, 0), words(D900_RESULT), 5000, 'the EQ switch');
|
|
647
|
+
if (status !== 0)
|
|
648
|
+
throw new BridgeError('rejected', `Topping D900: EQ switch status ${status}`);
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* The DX5 II's one-word settings: the command that writes each, and its word in the settings
|
|
652
|
+
* answer. The value is the device's own number.
|
|
653
|
+
*/
|
|
654
|
+
export const TOPPING_DX5_SETTINGS = {
|
|
655
|
+
mute: [0x7103, 10],
|
|
656
|
+
input: [0x7104, 11],
|
|
657
|
+
output: [0x7105, 12],
|
|
658
|
+
gain: [0x7117, 13],
|
|
659
|
+
dacFilter: [0x710d, 18],
|
|
660
|
+
lineMode: [0x710e, 19],
|
|
661
|
+
brightness: [0x710a, 25],
|
|
662
|
+
polarity: [0x711d, 33],
|
|
663
|
+
crossfeed: [0x712b, 42]
|
|
664
|
+
};
|
|
665
|
+
const DX5_VOLUME = 0x7102;
|
|
666
|
+
const DX5_BALANCE = 0x7116;
|
|
667
|
+
/** Words of the DX5 II's settings answer that the volume depends on. */
|
|
668
|
+
const DX5 = { volume: 9, output: 12, balance: 17, lineMode: 19, volumeStep: 32 };
|
|
669
|
+
/** Outputs that are line outputs only: in DAC mode their volume is fixed. */
|
|
670
|
+
const DX5_LINE_OUTPUTS = [2, 5, 6];
|
|
671
|
+
const CONTROL_PAUSE = 60;
|
|
672
|
+
async function dx5Settings(ctx, fresh = false) {
|
|
673
|
+
if (ctx.options.family !== 'dx5') {
|
|
674
|
+
throw new BridgeError('unsupported', "Topping: only the DX5 II's settings are driven");
|
|
675
|
+
}
|
|
676
|
+
await sessionOf(ctx);
|
|
677
|
+
// Reading every control asks for each in turn: one answer serves them all. A write decides on
|
|
678
|
+
// a fresh one.
|
|
679
|
+
const held = dx5Blocks.get(ctx.transport);
|
|
680
|
+
if (!fresh && held && Date.now() - held.at < DX5_BLOCK_AGE)
|
|
681
|
+
return held.words;
|
|
682
|
+
const words = await ask(ctx, GET_SETTINGS, READ, 'the settings request');
|
|
683
|
+
dx5Blocks.set(ctx.transport, { at: Date.now(), words });
|
|
684
|
+
return words;
|
|
685
|
+
}
|
|
686
|
+
/** The settings answer last read, kept for `DX5_BLOCK_AGE` ms or until a setting is written. */
|
|
687
|
+
const dx5Blocks = new WeakMap();
|
|
688
|
+
const DX5_BLOCK_AGE = 250;
|
|
689
|
+
async function dx5Write(ctx, command, data) {
|
|
690
|
+
dx5Blocks.delete(ctx.transport);
|
|
691
|
+
await send(ctx, command, data >>> 0);
|
|
692
|
+
await ctx.sleep(CONTROL_PAUSE);
|
|
693
|
+
}
|
|
694
|
+
const dx5Word = (command, word) => ({
|
|
695
|
+
read: async (ctx) => (await dx5Settings(ctx))[word] ?? 0,
|
|
696
|
+
async write(ctx, value) {
|
|
697
|
+
await dx5Settings(ctx, true);
|
|
698
|
+
await dx5Write(ctx, command, value);
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
/** dB, held as attenuation steps of 0.5 dB, or of 1 dB when the device's volume step is so set. */
|
|
702
|
+
const dx5Volume = {
|
|
703
|
+
async read(ctx) {
|
|
704
|
+
const s = await dx5Settings(ctx);
|
|
705
|
+
const steps = s[DX5.volume] ?? 0;
|
|
706
|
+
return steps === 0 ? 0 : -steps / (s[DX5.volumeStep] === 1 ? 1 : 2);
|
|
707
|
+
},
|
|
708
|
+
async write(ctx, value) {
|
|
709
|
+
const s = await dx5Settings(ctx, true);
|
|
710
|
+
if (s[DX5.lineMode] === 1 && DX5_LINE_OUTPUTS.includes(s[DX5.output])) {
|
|
711
|
+
// The app sends nothing here.
|
|
712
|
+
throw new BridgeError('rejected', 'Topping DX5 II: the volume is fixed while a line output is in DAC mode');
|
|
713
|
+
}
|
|
714
|
+
await dx5Write(ctx, DX5_VOLUME, Math.round(-value * (s[DX5.volumeStep] === 1 ? 1 : 2)));
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
/** dB, negative towards the left; the device holds tenths, as a signed word. */
|
|
718
|
+
const dx5Balance = {
|
|
719
|
+
read: async (ctx) => ((await dx5Settings(ctx))[DX5.balance] | 0) / 10,
|
|
720
|
+
async write(ctx, value) {
|
|
721
|
+
await dx5Settings(ctx, true);
|
|
722
|
+
await dx5Write(ctx, DX5_BALANCE, Math.round(value * 10));
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
export const toppingUsbHid = {
|
|
726
|
+
id: 'topping-usb-hid',
|
|
727
|
+
transport: 'hid',
|
|
728
|
+
codec: toppingCodec,
|
|
729
|
+
capabilities: (_transport, o) => ({
|
|
730
|
+
canRead: true,
|
|
731
|
+
canWrite: true,
|
|
732
|
+
readsPreamp: true,
|
|
733
|
+
readsSlot: true,
|
|
734
|
+
writesPreamp: true,
|
|
735
|
+
writesSlot: true,
|
|
736
|
+
needsBandCount: false,
|
|
737
|
+
readsCurrentSlot: true,
|
|
738
|
+
canEnable: true,
|
|
739
|
+
editsSlots: familyOf(o).edits === true,
|
|
740
|
+
perChannel: familyOf(o).perChannel === true
|
|
741
|
+
}),
|
|
742
|
+
async pull(ctx, { slot, bands = BANDS - 1 }) {
|
|
743
|
+
const list = await readList(ctx, await sessionOf(ctx));
|
|
744
|
+
const index = slot ?? (await activeSlot(ctx)) ?? 0;
|
|
745
|
+
const config = list.configs[index];
|
|
746
|
+
if (!config) {
|
|
747
|
+
throw new BridgeError('invalid-request', `Topping: no config ${index}, the device holds ${list.configs.length}`);
|
|
748
|
+
}
|
|
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
|
+
};
|
|
756
|
+
},
|
|
757
|
+
async push(ctx, request) {
|
|
758
|
+
const session = await sessionOf(ctx);
|
|
759
|
+
const family = familyOf(ctx.options);
|
|
760
|
+
const count = session.count ?? (await readList(ctx, session)).configs.length;
|
|
761
|
+
const slot = request.slot ?? (await activeSlot(ctx)) ?? 0;
|
|
762
|
+
if (slot >= count) {
|
|
763
|
+
throw new BridgeError('invalid-request', `Topping: no config ${slot}, the device holds ${count}; create it first`);
|
|
764
|
+
}
|
|
765
|
+
const frames = toppingWriteFrames({ ...request, slot }, ctx.options, session.reserved[slot]);
|
|
766
|
+
const [select, ...parameters] = frames;
|
|
767
|
+
await sendFrame(ctx.transport, select);
|
|
768
|
+
await ctx.sleep(family.switchPause);
|
|
769
|
+
for (const [i, frame] of parameters.entries()) {
|
|
770
|
+
await sendFrame(ctx.transport, frame);
|
|
771
|
+
// The DX5 II also pauses after the pre-gain; the D900 counts the pre-gain's four in.
|
|
772
|
+
const sent = ctx.options.family === 'd900' ? i + 1 : i - 3;
|
|
773
|
+
if (sent > 0 ? sent % family.burst === 0 : sent === 0 && ctx.options.family === 'dx5') {
|
|
774
|
+
await ctx.sleep(10);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
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
|
+
}
|
|
783
|
+
return { reconnect: false };
|
|
784
|
+
},
|
|
785
|
+
async currentSlot(ctx) {
|
|
786
|
+
await sessionOf(ctx);
|
|
787
|
+
return activeSlot(ctx);
|
|
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
|
+
},
|
|
858
|
+
async eqEnabled(ctx) {
|
|
859
|
+
await sessionOf(ctx);
|
|
860
|
+
const o = ctx.options;
|
|
861
|
+
const at = familyOf(o).settings?.enabled;
|
|
862
|
+
if (at !== undefined) {
|
|
863
|
+
const s = await settings(ctx);
|
|
864
|
+
return s?.[at] === undefined ? null : s[at] !== 0;
|
|
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.
|
|
869
|
+
const [state] = await ask(ctx, EQ_ENABLE_STATE, READ, 'the EQ state request');
|
|
870
|
+
return (state & 1) === 1;
|
|
871
|
+
},
|
|
872
|
+
setEnabled,
|
|
873
|
+
async firmware(ctx) {
|
|
874
|
+
const session = await sessionOf(ctx);
|
|
875
|
+
const at = familyOf(ctx.options).settings?.firmware;
|
|
876
|
+
if (session.firmware !== undefined)
|
|
877
|
+
return toppingFirmware(session.firmware);
|
|
878
|
+
if (at !== undefined)
|
|
879
|
+
return toppingFirmware((await settings(ctx))?.[at] ?? 0);
|
|
880
|
+
const [word] = await ask(ctx, SOFTWARE_VERSION, READ, 'the firmware version request');
|
|
881
|
+
return toppingFirmware(word);
|
|
882
|
+
},
|
|
883
|
+
controls: {
|
|
884
|
+
volume: dx5Volume,
|
|
885
|
+
balance: dx5Balance,
|
|
886
|
+
...Object.fromEntries(Object.entries(TOPPING_DX5_SETTINGS).map(([id, [command, word]]) => [
|
|
887
|
+
id,
|
|
888
|
+
dx5Word(command, word)
|
|
889
|
+
]))
|
|
890
|
+
}
|
|
891
|
+
};
|
|
892
|
+
//# sourceMappingURL=topping-usb-hid.js.map
|