@potatosalad775/eqcaps-device-bridge 0.4.2 → 0.4.3
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 +30 -2
- package/dist/device.d.ts +14 -6
- package/dist/device.d.ts.map +1 -1
- package/dist/device.js +40 -18
- package/dist/device.js.map +1 -1
- package/dist/handler.d.ts +25 -1
- 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 +219 -87
- 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 +266 -0
- package/dist/handlers/qudelix-v2-usb-hid.js.map +1 -0
- package/dist/handlers/qudelix.d.ts +185 -0
- package/dist/handlers/qudelix.d.ts.map +1 -0
- package/dist/handlers/qudelix.js +585 -0
- package/dist/handlers/qudelix.js.map +1 -0
- package/dist/handlers/topping-usb-hid.d.ts +51 -0
- package/dist/handlers/topping-usb-hid.d.ts.map +1 -0
- package/dist/handlers/topping-usb-hid.js +658 -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 +80 -33
- package/dist/protocols.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,658 @@
|
|
|
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. Both channels get the same
|
|
17
|
+
// filters here (`channels: linked`); a pull returns the left channel's.
|
|
18
|
+
//
|
|
19
|
+
// Type codes are the editor's: 1 peak, 2 low pass, 3 high pass, 4 low shelf, 5 high shelf.
|
|
20
|
+
// Gain is in tenths of a dB, Q in ten-thousandths, frequency in Hz. The pre-gain word is a linear
|
|
21
|
+
// gain with 0 dB at 2^25. The app takes it from a table, except for the D900, where it computes
|
|
22
|
+
// 2^25 · 10^(dB/20); this handler does the same. The table is one 0.1 dB step off above +7.7 dB and
|
|
23
|
+
// below −10.3 dB and wrong at +3.5 and −4.9 dB, so there the device applies a slightly different
|
|
24
|
+
// gain than the number says, as it does from Topping's app. A recording of the app with a DX5 II
|
|
25
|
+
// showed why the table is kept: the app reads any other word as a neighbouring step.
|
|
26
|
+
//
|
|
27
|
+
// 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.
|
|
30
|
+
//
|
|
31
|
+
// The DX5 II's settings are controls: one read (0x710C) answers a block of words, one per setting,
|
|
32
|
+
// and each setting is written with its own command, which the device takes at once. Tried on the
|
|
33
|
+
// DX5 II: each one read back as written. The other families' settings are not driven.
|
|
34
|
+
import { be32, field, grid, I32, U32 } from "../bytes.js";
|
|
35
|
+
import { BridgeError } from "../errors.js";
|
|
36
|
+
import { hidFrame, sendFrame, typeCodes } from "../io.js";
|
|
37
|
+
import { after } from "../platform.js";
|
|
38
|
+
const READ = 0x10;
|
|
39
|
+
const WRITE = 0x20;
|
|
40
|
+
const CONNECT_STATE = 0x1101;
|
|
41
|
+
const UPLOAD = 0x1106;
|
|
42
|
+
const SWITCH_CONFIG = 0x110e;
|
|
43
|
+
const AGREEMENT = 0x1120;
|
|
44
|
+
const SOFTWARE_VERSION = 0x1202;
|
|
45
|
+
const EQ_ENABLE_STATE = 0x1204;
|
|
46
|
+
const CURRENT_CONFIG = 0x1206;
|
|
47
|
+
const GET_SETTINGS = 0x710c;
|
|
48
|
+
const PEQ_STATE = 0x7132;
|
|
49
|
+
const D900_CAPABILITIES = 0x7144;
|
|
50
|
+
const D900_RESULT = 0x7145;
|
|
51
|
+
const PREAMP_LEFT = 0x9c01;
|
|
52
|
+
const BAND = 0x9000;
|
|
53
|
+
/** `SWITCH_CONFIG` with this turns the EQ off on the DX1 II and E50 II. */
|
|
54
|
+
const NO_CONFIG = 0xffffffff;
|
|
55
|
+
const UNITY = 0x2000000;
|
|
56
|
+
const BANDS = 11;
|
|
57
|
+
/** Words of a config as the device sends it; the app accepts 70 or more. */
|
|
58
|
+
const CONFIG_WORDS = 70;
|
|
59
|
+
const LIST_TIMEOUT = 10000;
|
|
60
|
+
const ANSWER_TIMEOUT = 2000;
|
|
61
|
+
const TYPES = typeCodes('topping', { PK: 1, LPQ: 2, HPQ: 3, LSC: 4, HSC: 5 }, [[0, 'PK']]);
|
|
62
|
+
const FAMILIES = {
|
|
63
|
+
dx1: { connect: 1, list: [UPLOAD, 0, READ], frame: 1, switchPause: 60, burst: 20 },
|
|
64
|
+
e50: {
|
|
65
|
+
connect: 1,
|
|
66
|
+
list: [UPLOAD, 0, READ],
|
|
67
|
+
frame: 1,
|
|
68
|
+
settings: { kind: READ, firmware: 31, slot: 14 },
|
|
69
|
+
switchPause: 60,
|
|
70
|
+
burst: 20
|
|
71
|
+
},
|
|
72
|
+
dx5: { connect: 2, list: [AGREEMENT, 1, WRITE], frame: 1, switchPause: 120, burst: 10 },
|
|
73
|
+
dx9d: {
|
|
74
|
+
connect: 2,
|
|
75
|
+
list: [AGREEMENT, 1, WRITE],
|
|
76
|
+
frame: 1,
|
|
77
|
+
settings: { kind: READ, firmware: 46, slot: 55, enabled: 54 },
|
|
78
|
+
switchPause: 60,
|
|
79
|
+
burst: 20
|
|
80
|
+
},
|
|
81
|
+
d900: {
|
|
82
|
+
connect: 2,
|
|
83
|
+
list: [UPLOAD, 0, WRITE],
|
|
84
|
+
frame: 0,
|
|
85
|
+
settings: { kind: WRITE, firmware: 49, slot: 48, enabled: 47 },
|
|
86
|
+
switchPause: 60,
|
|
87
|
+
burst: 10
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const familyOf = (o) => FAMILIES[o.family ?? 'dx1'];
|
|
91
|
+
/** Firmware the app refuses to control: the DX5 II below 2.37, the E50 II below 1.52. */
|
|
92
|
+
const MIN_FIRMWARE = { dx5: 0x237, e50: 0x152 };
|
|
93
|
+
export function toppingFrame(command, data, kind = WRITE, frames = 1, frame = 1) {
|
|
94
|
+
return Uint8Array.from([
|
|
95
|
+
0x22,
|
|
96
|
+
0x33,
|
|
97
|
+
kind,
|
|
98
|
+
frames,
|
|
99
|
+
frame,
|
|
100
|
+
command >> 8,
|
|
101
|
+
command & 0xff,
|
|
102
|
+
...be32(data >>> 0),
|
|
103
|
+
0,
|
|
104
|
+
0,
|
|
105
|
+
0x66,
|
|
106
|
+
0x77,
|
|
107
|
+
0
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
/** A report in the frame layout, with or without one byte in front; null for anything else. */
|
|
111
|
+
export function parseToppingFrame(d) {
|
|
112
|
+
const at = d[0] === 0x22 && d[1] === 0x33 ? 0 : d[1] === 0x22 && d[2] === 0x33 ? 1 : -1;
|
|
113
|
+
if (at < 0 || d.length < at + 15 || d[at + 13] !== 0x66 || d[at + 14] !== 0x77)
|
|
114
|
+
return null;
|
|
115
|
+
return {
|
|
116
|
+
kind: d[at + 2],
|
|
117
|
+
frames: d[at + 3],
|
|
118
|
+
frame: d[at + 4],
|
|
119
|
+
command: (d[at + 5] << 8) | d[at + 6],
|
|
120
|
+
data: ((d[at + 7] << 24) | (d[at + 8] << 16) | (d[at + 9] << 8) | d[at + 10]) >>> 0
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The pre-gain words the app sends, per 0.1 dB from 0 to 12 dB of cut and of boost. They are not
|
|
125
|
+
* quite 2^25 · 10^(dB/20), and the app reads a word that is not in its table as the next step
|
|
126
|
+
* towards 0 dB, then writes that back when it connects: so a computed word would shift by 0.1 dB
|
|
127
|
+
* the first time Topping's app saw it.
|
|
128
|
+
*/
|
|
129
|
+
// prettier-ignore
|
|
130
|
+
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];
|
|
131
|
+
// prettier-ignore
|
|
132
|
+
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];
|
|
133
|
+
/** A word some devices hold from the factory, which the app reads as -3 dB. */
|
|
134
|
+
const PREAMP_FACTORY_MINUS_3 = 0x16a77e5;
|
|
135
|
+
/** The pre-gain word for `db`: the app's table, or computed on the D900 as the app does there. */
|
|
136
|
+
export function toppingPreampWord(db, family) {
|
|
137
|
+
const tenths = field(db, 10, [-120, 120], 'preamp');
|
|
138
|
+
if (family === 'd900')
|
|
139
|
+
return Math.round(UNITY * 10 ** (tenths / 200));
|
|
140
|
+
const table = tenths < 0 ? PREAMP_CUT : PREAMP_BOOST;
|
|
141
|
+
const i = Math.abs(tenths);
|
|
142
|
+
// The table repeats the step before at -4.9, -10.4 and +7.8 dB, so the app can't hold those.
|
|
143
|
+
// A word halfway to the next step is one the app reads as the step meant.
|
|
144
|
+
if (i > 0 && table[i] === table[i - 1])
|
|
145
|
+
return Math.round((table[i] + table[i + 1]) / 2);
|
|
146
|
+
return table[i];
|
|
147
|
+
}
|
|
148
|
+
/** The pre-gain a word means, as the app reads it. */
|
|
149
|
+
function preampDb(word, family) {
|
|
150
|
+
if (family === 'd900')
|
|
151
|
+
return word > 0 ? Math.round(200 * Math.log10(word / UNITY)) / 10 || 0 : 0;
|
|
152
|
+
if (word === PREAMP_FACTORY_MINUS_3)
|
|
153
|
+
return -3;
|
|
154
|
+
if (word < UNITY) {
|
|
155
|
+
const exact = PREAMP_CUT.indexOf(word);
|
|
156
|
+
if (exact > 0)
|
|
157
|
+
return -exact / 10;
|
|
158
|
+
const i = PREAMP_CUT.findIndex((v, n) => n > 0 && word < v && word > (PREAMP_CUT[n + 1] ?? Infinity));
|
|
159
|
+
return i > 0 ? -i / 10 : 0;
|
|
160
|
+
}
|
|
161
|
+
const exact = PREAMP_BOOST.indexOf(word);
|
|
162
|
+
if (exact >= 0)
|
|
163
|
+
return exact / 10;
|
|
164
|
+
const i = PREAMP_BOOST.findIndex((v, n) => word > v && word < (PREAMP_BOOST[n + 1] ?? -Infinity));
|
|
165
|
+
return i > 0 ? i / 10 : 0;
|
|
166
|
+
}
|
|
167
|
+
const OFF = { type: 1, freq: 632, gain: 0, q: 7070, on: 0 };
|
|
168
|
+
/**
|
|
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).
|
|
173
|
+
*/
|
|
174
|
+
export function toppingWriteFrames(request, o, reserved = { left: OFF, right: OFF }) {
|
|
175
|
+
const family = familyOf(o);
|
|
176
|
+
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
|
+
}
|
|
194
|
+
const out = [];
|
|
195
|
+
const write = (command, data) => out.push(toppingFrame(command, data, WRITE, 1, family.frame));
|
|
196
|
+
if (slot !== undefined) {
|
|
197
|
+
out.push(toppingFrame(SWITCH_CONFIG, field(slot, 1, [0, 0xff], 'slot')));
|
|
198
|
+
}
|
|
199
|
+
const gain = toppingPreampWord(preamp, o.family);
|
|
200
|
+
[gain, 1, gain, 1].forEach((v, i) => write(PREAMP_LEFT + i, v));
|
|
201
|
+
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
|
+
if (d900) {
|
|
203
|
+
left.forEach((b, i) => side(i, b, 1));
|
|
204
|
+
right.forEach((b, i) => side(i, b, 6));
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
left.forEach((b, i) => {
|
|
208
|
+
side(i, b, 1);
|
|
209
|
+
side(i, right[i], 6);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return out.map((d) => hidFrame(0, d));
|
|
213
|
+
}
|
|
214
|
+
export const toppingCodec = {
|
|
215
|
+
types: TYPES.types,
|
|
216
|
+
wire: () => ({
|
|
217
|
+
freq: { ...grid(1, U32), min: 1 },
|
|
218
|
+
q: { ...grid(10000, U32), min: 0.0001 },
|
|
219
|
+
gain: grid(10, I32),
|
|
220
|
+
preamp: grid(10, [-120, 120])
|
|
221
|
+
}),
|
|
222
|
+
encode: (request, o) => toppingWriteFrames(request, o),
|
|
223
|
+
decode(frames, o) {
|
|
224
|
+
const state = { filters: [] };
|
|
225
|
+
const bands = [];
|
|
226
|
+
for (const { data } of frames) {
|
|
227
|
+
const f = parseToppingFrame(data);
|
|
228
|
+
if (!f)
|
|
229
|
+
continue;
|
|
230
|
+
if (f.command === SWITCH_CONFIG)
|
|
231
|
+
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) {
|
|
235
|
+
(bands[((f.command >> 8) & 0xf) - 1] ??= {})[f.command & 0xff] = f.data;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
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
|
+
});
|
|
247
|
+
}
|
|
248
|
+
return state;
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* Sends `request`, if any, and feeds every frame that arrives to `take` until it returns a value.
|
|
253
|
+
* Subscribes before sending, so a fast answer isn't missed.
|
|
254
|
+
*/
|
|
255
|
+
function gather(ctx, request, take, timeoutMs, what) {
|
|
256
|
+
const result = new Promise((resolve, reject) => {
|
|
257
|
+
const cancel = after(timeoutMs, () => {
|
|
258
|
+
unsubscribe();
|
|
259
|
+
reject(new BridgeError('timeout', `Topping: no answer to ${what} within ${timeoutMs} ms`));
|
|
260
|
+
});
|
|
261
|
+
const unsubscribe = ctx.transport.onInputReport((_id, data) => {
|
|
262
|
+
const frame = parseToppingFrame(data);
|
|
263
|
+
if (!frame)
|
|
264
|
+
return;
|
|
265
|
+
let value;
|
|
266
|
+
try {
|
|
267
|
+
value = take(frame);
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
cancel();
|
|
271
|
+
unsubscribe();
|
|
272
|
+
reject(error);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (value === undefined)
|
|
276
|
+
return;
|
|
277
|
+
cancel();
|
|
278
|
+
unsubscribe();
|
|
279
|
+
resolve(value);
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
// Keeps a send failure from leaving the wait's rejection unhandled.
|
|
283
|
+
result.catch(() => { });
|
|
284
|
+
return request ? ctx.transport.sendReport(0, request).then(() => result) : result;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Collects the words of an answer to `command`. Its frames are numbered from 0 or from 1; the
|
|
288
|
+
* first one seen says which, as the app decides it.
|
|
289
|
+
*/
|
|
290
|
+
function words(command) {
|
|
291
|
+
const got = [];
|
|
292
|
+
let base;
|
|
293
|
+
let seen = 0;
|
|
294
|
+
return (f) => {
|
|
295
|
+
if (f.command !== command)
|
|
296
|
+
return undefined;
|
|
297
|
+
if (f.frames <= 1)
|
|
298
|
+
return [f.data];
|
|
299
|
+
base ??= f.frame === 1 ? 1 : 0;
|
|
300
|
+
const i = f.frame - base;
|
|
301
|
+
if (i < 0 || i >= f.frames)
|
|
302
|
+
return undefined;
|
|
303
|
+
if (got[i] === undefined)
|
|
304
|
+
seen++;
|
|
305
|
+
got[i] = f.data;
|
|
306
|
+
return seen === f.frames ? got : undefined;
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
function ask(ctx, command, kind, what, timeoutMs = ANSWER_TIMEOUT) {
|
|
310
|
+
const frame = familyOf(ctx.options).frame;
|
|
311
|
+
return gather(ctx, toppingFrame(command, 0, kind, 1, frame), words(command), timeoutMs, what);
|
|
312
|
+
}
|
|
313
|
+
const send = (ctx, command, data, frame = familyOf(ctx.options).frame) => ctx.transport.sendReport(0, toppingFrame(command, data, WRITE, 1, frame));
|
|
314
|
+
/** "2.39" from 0x239: the hex digits are the decimal ones. Null where they aren't. */
|
|
315
|
+
export function toppingFirmware(word) {
|
|
316
|
+
const digits = word.toString(16);
|
|
317
|
+
if (word === 0 || /[a-f]/.test(digits))
|
|
318
|
+
return null;
|
|
319
|
+
return `${Number(digits.slice(0, -2) || '0')}.${digits.slice(-2).padStart(2, '0')}`;
|
|
320
|
+
}
|
|
321
|
+
function refuseOldFirmware(o, word) {
|
|
322
|
+
const min = MIN_FIRMWARE[o.family ?? 'dx1'];
|
|
323
|
+
if (min !== undefined && word < min) {
|
|
324
|
+
throw new BridgeError('unsupported', `Topping: firmware ${toppingFirmware(word) ?? `0x${word.toString(16)}`} is older than ${toppingFirmware(min)}, the first Topping's app controls`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
async function settings(ctx) {
|
|
328
|
+
const s = familyOf(ctx.options).settings;
|
|
329
|
+
if (!s)
|
|
330
|
+
return null;
|
|
331
|
+
return ask(ctx, GET_SETTINGS, s.kind, 'the settings request');
|
|
332
|
+
}
|
|
333
|
+
/** What the app requires of the D900's capabilities answer before it sends anything else. */
|
|
334
|
+
function d900Capable(c) {
|
|
335
|
+
const fixed = { 0: 2, 1: 255, 2: 7, 3: 10, 4: 10, 5: 30, 8: 74, 9: 78, 10: 74, 11: 10 };
|
|
336
|
+
return (c.length === 12 &&
|
|
337
|
+
Object.entries(fixed).every(([i, v]) => c[Number(i)] === v) &&
|
|
338
|
+
(c[6] & 0x1f0) === 0x1f0);
|
|
339
|
+
}
|
|
340
|
+
async function openSession(ctx) {
|
|
341
|
+
const o = ctx.options;
|
|
342
|
+
const family = familyOf(o);
|
|
343
|
+
const session = { reserved: [] };
|
|
344
|
+
if (o.family === 'd900') {
|
|
345
|
+
const c = await ask(ctx, D900_CAPABILITIES, WRITE, 'the capabilities request');
|
|
346
|
+
if (!d900Capable(c)) {
|
|
347
|
+
throw new BridgeError('unsupported', "Topping D900: this firmware doesn't speak the control protocol Topping's app requires");
|
|
348
|
+
}
|
|
349
|
+
// The device answers 0 until it grants the connection; the app asks again 200 ms later.
|
|
350
|
+
for (let i = 0;; i++) {
|
|
351
|
+
const [granted] = await gather(ctx, toppingFrame(CONNECT_STATE, family.connect, WRITE, 1, 0), words(CONNECT_STATE), ANSWER_TIMEOUT, 'the connection request');
|
|
352
|
+
if (granted === 1)
|
|
353
|
+
break;
|
|
354
|
+
if (i === 24)
|
|
355
|
+
throw new BridgeError('rejected', 'Topping D900: connection not granted');
|
|
356
|
+
await ctx.sleep(200);
|
|
357
|
+
}
|
|
358
|
+
await send(ctx, AGREEMENT, 1);
|
|
359
|
+
return session;
|
|
360
|
+
}
|
|
361
|
+
await send(ctx, CONNECT_STATE, family.connect);
|
|
362
|
+
await ctx.sleep(100);
|
|
363
|
+
if (o.family === 'dx5') {
|
|
364
|
+
const [word] = await ask(ctx, SOFTWARE_VERSION, READ, 'the firmware version request');
|
|
365
|
+
session.firmware = word & 0xfff;
|
|
366
|
+
refuseOldFirmware(o, session.firmware);
|
|
367
|
+
return session;
|
|
368
|
+
}
|
|
369
|
+
// The DX9 Discrete's agreement is its config list request, sent when the list is read.
|
|
370
|
+
if (o.family !== 'dx9d') {
|
|
371
|
+
await send(ctx, AGREEMENT, 1);
|
|
372
|
+
await ctx.sleep(100);
|
|
373
|
+
}
|
|
374
|
+
if (o.family === 'e50') {
|
|
375
|
+
const s = await settings(ctx);
|
|
376
|
+
session.firmware = s?.[family.settings.firmware] ?? 0;
|
|
377
|
+
refuseOldFirmware(o, session.firmware);
|
|
378
|
+
}
|
|
379
|
+
return session;
|
|
380
|
+
}
|
|
381
|
+
const sessions = new WeakMap();
|
|
382
|
+
/** The transport's session, started on first use; one that fails to start is forgotten. */
|
|
383
|
+
function sessionOf(ctx) {
|
|
384
|
+
const known = sessions.get(ctx.transport);
|
|
385
|
+
if (known)
|
|
386
|
+
return known;
|
|
387
|
+
const opening = openSession(ctx);
|
|
388
|
+
sessions.set(ctx.transport, opening);
|
|
389
|
+
opening.catch(() => {
|
|
390
|
+
if (sessions.get(ctx.transport) === opening)
|
|
391
|
+
sessions.delete(ctx.transport);
|
|
392
|
+
});
|
|
393
|
+
return opening;
|
|
394
|
+
}
|
|
395
|
+
const band = (w, at) => ({
|
|
396
|
+
on: (w[at] & 0xff) === 1 ? 1 : 0,
|
|
397
|
+
type: (w[at] >> 8) & 0xff,
|
|
398
|
+
gain: ((w[at] >> 16) << 24) >> 24,
|
|
399
|
+
freq: w[at + 1],
|
|
400
|
+
q: w[at + 2]
|
|
401
|
+
});
|
|
402
|
+
/** A stored config: words 5 the left pre-gain, 8… the left bands and 41… the right, 3 words each. */
|
|
403
|
+
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 };
|
|
409
|
+
});
|
|
410
|
+
return { filters, preamp: preampDb(w[5], family) };
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Reads the stored configs: a two-frame header (the count, then a word that is not the active
|
|
414
|
+
* config on a DX5 II), then each config as its own run of frames. The app takes a missing header
|
|
415
|
+
* as 3.
|
|
416
|
+
*/
|
|
417
|
+
async function readList(ctx, session) {
|
|
418
|
+
const [command, data, kind] = familyOf(ctx.options).list;
|
|
419
|
+
const configs = [];
|
|
420
|
+
let count;
|
|
421
|
+
let current = [];
|
|
422
|
+
const list = await gather(ctx, toppingFrame(command, data, kind), (f) => {
|
|
423
|
+
if (f.command !== UPLOAD)
|
|
424
|
+
return undefined;
|
|
425
|
+
if (f.frames === 2) {
|
|
426
|
+
if (f.frame === 0) {
|
|
427
|
+
count = f.data;
|
|
428
|
+
configs.length = 0;
|
|
429
|
+
}
|
|
430
|
+
else if (count === 0)
|
|
431
|
+
return { configs };
|
|
432
|
+
return undefined;
|
|
433
|
+
}
|
|
434
|
+
if (f.frames < CONFIG_WORDS)
|
|
435
|
+
return undefined;
|
|
436
|
+
if (f.frame === 0)
|
|
437
|
+
current = [];
|
|
438
|
+
current[f.frame] = f.data;
|
|
439
|
+
if (f.frame !== f.frames - 1)
|
|
440
|
+
return undefined;
|
|
441
|
+
configs.push(Array.from({ length: f.frames }, (_, i) => current[i] ?? 0));
|
|
442
|
+
return configs.length >= (count ?? 3) ? { configs } : undefined;
|
|
443
|
+
}, LIST_TIMEOUT, 'the config list request');
|
|
444
|
+
session.count = list.configs.length;
|
|
445
|
+
session.reserved = list.configs.map((w) => ({
|
|
446
|
+
left: band(w, 8 + 3 * (BANDS - 1)),
|
|
447
|
+
right: band(w, 41 + 3 * (BANDS - 1))
|
|
448
|
+
}));
|
|
449
|
+
return list;
|
|
450
|
+
}
|
|
451
|
+
/** The active config, from the settings where they hold it. */
|
|
452
|
+
async function activeSlot(ctx) {
|
|
453
|
+
const o = ctx.options;
|
|
454
|
+
const at = familyOf(o).settings?.slot;
|
|
455
|
+
if (at !== undefined)
|
|
456
|
+
return (await settings(ctx))?.[at] ?? null;
|
|
457
|
+
// The app only listens for the active config of the DX1 II and DX5 II. A DX5 II answers this
|
|
458
|
+
// read (firmware 2.58).
|
|
459
|
+
try {
|
|
460
|
+
const [slot] = await ask(ctx, CURRENT_CONFIG, READ, 'the active config request', 1500);
|
|
461
|
+
return slot === NO_CONFIG ? 0 : slot;
|
|
462
|
+
}
|
|
463
|
+
catch (error) {
|
|
464
|
+
ctx.log(`Topping: active config unknown (${error.message})`);
|
|
465
|
+
return null;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
async function setEnabled(ctx, enabled, slot) {
|
|
469
|
+
await sessionOf(ctx);
|
|
470
|
+
const o = ctx.options;
|
|
471
|
+
const family = familyOf(o);
|
|
472
|
+
if (o.family === 'dx1' || o.family === 'e50' || o.family === undefined) {
|
|
473
|
+
// These two have no switch: selecting no config turns the EQ off, selecting one turns it on.
|
|
474
|
+
const target = enabled ? (slot ?? (await activeSlot(ctx)) ?? 0) : NO_CONFIG;
|
|
475
|
+
await send(ctx, SWITCH_CONFIG, target, 1);
|
|
476
|
+
if (enabled && o.family !== 'e50') {
|
|
477
|
+
await ctx.sleep(140);
|
|
478
|
+
await send(ctx, SWITCH_CONFIG, target, 1);
|
|
479
|
+
}
|
|
480
|
+
await ctx.sleep(enabled ? 170 : 90);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
if (enabled && slot !== undefined) {
|
|
484
|
+
await send(ctx, SWITCH_CONFIG, slot);
|
|
485
|
+
await ctx.sleep(o.family === 'd900' ? 100 : family.switchPause);
|
|
486
|
+
}
|
|
487
|
+
if (o.family !== 'd900')
|
|
488
|
+
return send(ctx, PEQ_STATE, enabled ? 1 : 0);
|
|
489
|
+
// The D900 reports each setting's outcome: the command, a status (0 for done) and the value.
|
|
490
|
+
const [, status] = await gather(ctx, toppingFrame(PEQ_STATE, enabled ? 1 : 0, WRITE, 1, 0), words(D900_RESULT), 5000, 'the EQ switch');
|
|
491
|
+
if (status !== 0)
|
|
492
|
+
throw new BridgeError('rejected', `Topping D900: EQ switch status ${status}`);
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* The DX5 II's one-word settings: the command that writes each, and its word in the settings
|
|
496
|
+
* answer. The value is the device's own number.
|
|
497
|
+
*/
|
|
498
|
+
export const TOPPING_DX5_SETTINGS = {
|
|
499
|
+
mute: [0x7103, 10],
|
|
500
|
+
input: [0x7104, 11],
|
|
501
|
+
output: [0x7105, 12],
|
|
502
|
+
gain: [0x7117, 13],
|
|
503
|
+
dacFilter: [0x710d, 18],
|
|
504
|
+
lineMode: [0x710e, 19],
|
|
505
|
+
brightness: [0x710a, 25],
|
|
506
|
+
polarity: [0x711d, 33],
|
|
507
|
+
crossfeed: [0x712b, 42]
|
|
508
|
+
};
|
|
509
|
+
const DX5_VOLUME = 0x7102;
|
|
510
|
+
const DX5_BALANCE = 0x7116;
|
|
511
|
+
/** Words of the DX5 II's settings answer that the volume depends on. */
|
|
512
|
+
const DX5 = { volume: 9, output: 12, balance: 17, lineMode: 19, volumeStep: 32 };
|
|
513
|
+
/** Outputs that are line outputs only: in DAC mode their volume is fixed. */
|
|
514
|
+
const DX5_LINE_OUTPUTS = [2, 5, 6];
|
|
515
|
+
const CONTROL_PAUSE = 60;
|
|
516
|
+
async function dx5Settings(ctx, fresh = false) {
|
|
517
|
+
if (ctx.options.family !== 'dx5') {
|
|
518
|
+
throw new BridgeError('unsupported', "Topping: only the DX5 II's settings are driven");
|
|
519
|
+
}
|
|
520
|
+
await sessionOf(ctx);
|
|
521
|
+
// Reading every control asks for each in turn: one answer serves them all. A write decides on
|
|
522
|
+
// a fresh one.
|
|
523
|
+
const held = dx5Blocks.get(ctx.transport);
|
|
524
|
+
if (!fresh && held && Date.now() - held.at < DX5_BLOCK_AGE)
|
|
525
|
+
return held.words;
|
|
526
|
+
const words = await ask(ctx, GET_SETTINGS, READ, 'the settings request');
|
|
527
|
+
dx5Blocks.set(ctx.transport, { at: Date.now(), words });
|
|
528
|
+
return words;
|
|
529
|
+
}
|
|
530
|
+
/** The settings answer last read, kept for `DX5_BLOCK_AGE` ms or until a setting is written. */
|
|
531
|
+
const dx5Blocks = new WeakMap();
|
|
532
|
+
const DX5_BLOCK_AGE = 250;
|
|
533
|
+
async function dx5Write(ctx, command, data) {
|
|
534
|
+
dx5Blocks.delete(ctx.transport);
|
|
535
|
+
await send(ctx, command, data >>> 0);
|
|
536
|
+
await ctx.sleep(CONTROL_PAUSE);
|
|
537
|
+
}
|
|
538
|
+
const dx5Word = (command, word) => ({
|
|
539
|
+
read: async (ctx) => (await dx5Settings(ctx))[word] ?? 0,
|
|
540
|
+
async write(ctx, value) {
|
|
541
|
+
await dx5Settings(ctx, true);
|
|
542
|
+
await dx5Write(ctx, command, value);
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
/** dB, held as attenuation steps of 0.5 dB, or of 1 dB when the device's volume step is so set. */
|
|
546
|
+
const dx5Volume = {
|
|
547
|
+
async read(ctx) {
|
|
548
|
+
const s = await dx5Settings(ctx);
|
|
549
|
+
const steps = s[DX5.volume] ?? 0;
|
|
550
|
+
return steps === 0 ? 0 : -steps / (s[DX5.volumeStep] === 1 ? 1 : 2);
|
|
551
|
+
},
|
|
552
|
+
async write(ctx, value) {
|
|
553
|
+
const s = await dx5Settings(ctx, true);
|
|
554
|
+
if (s[DX5.lineMode] === 1 && DX5_LINE_OUTPUTS.includes(s[DX5.output])) {
|
|
555
|
+
// The app sends nothing here.
|
|
556
|
+
throw new BridgeError('rejected', 'Topping DX5 II: the volume is fixed while a line output is in DAC mode');
|
|
557
|
+
}
|
|
558
|
+
await dx5Write(ctx, DX5_VOLUME, Math.round(-value * (s[DX5.volumeStep] === 1 ? 1 : 2)));
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
/** dB, negative towards the left; the device holds tenths, as a signed word. */
|
|
562
|
+
const dx5Balance = {
|
|
563
|
+
read: async (ctx) => ((await dx5Settings(ctx))[DX5.balance] | 0) / 10,
|
|
564
|
+
async write(ctx, value) {
|
|
565
|
+
await dx5Settings(ctx, true);
|
|
566
|
+
await dx5Write(ctx, DX5_BALANCE, Math.round(value * 10));
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
export const toppingUsbHid = {
|
|
570
|
+
id: 'topping-usb-hid',
|
|
571
|
+
transport: 'hid',
|
|
572
|
+
codec: toppingCodec,
|
|
573
|
+
capabilities: () => ({
|
|
574
|
+
canRead: true,
|
|
575
|
+
canWrite: true,
|
|
576
|
+
readsPreamp: true,
|
|
577
|
+
readsSlot: true,
|
|
578
|
+
writesPreamp: true,
|
|
579
|
+
writesSlot: true,
|
|
580
|
+
needsBandCount: false,
|
|
581
|
+
readsCurrentSlot: true,
|
|
582
|
+
canEnable: true
|
|
583
|
+
}),
|
|
584
|
+
async pull(ctx, { slot, bands = BANDS - 1 }) {
|
|
585
|
+
const list = await readList(ctx, await sessionOf(ctx));
|
|
586
|
+
const index = slot ?? (await activeSlot(ctx)) ?? 0;
|
|
587
|
+
const config = list.configs[index];
|
|
588
|
+
if (!config) {
|
|
589
|
+
throw new BridgeError('invalid-request', `Topping: no config ${index}, the device holds ${list.configs.length}`);
|
|
590
|
+
}
|
|
591
|
+
const { filters, preamp } = decodeConfig(config, ctx.options.family);
|
|
592
|
+
return { filters: filters.slice(0, bands), preamp, slot: index };
|
|
593
|
+
},
|
|
594
|
+
async push(ctx, request) {
|
|
595
|
+
const session = await sessionOf(ctx);
|
|
596
|
+
const family = familyOf(ctx.options);
|
|
597
|
+
const count = session.count ?? (await readList(ctx, session)).configs.length;
|
|
598
|
+
const slot = request.slot ?? (await activeSlot(ctx)) ?? 0;
|
|
599
|
+
if (slot >= count) {
|
|
600
|
+
throw new BridgeError('invalid-request', `Topping: no config ${slot}, the device holds ${count}; Topping's app creates them`);
|
|
601
|
+
}
|
|
602
|
+
const frames = toppingWriteFrames({ ...request, slot }, ctx.options, session.reserved[slot]);
|
|
603
|
+
const [select, ...parameters] = frames;
|
|
604
|
+
await sendFrame(ctx.transport, select);
|
|
605
|
+
await ctx.sleep(family.switchPause);
|
|
606
|
+
for (const [i, frame] of parameters.entries()) {
|
|
607
|
+
await sendFrame(ctx.transport, frame);
|
|
608
|
+
// The DX5 II also pauses after the pre-gain; the D900 counts the pre-gain's four in.
|
|
609
|
+
const sent = ctx.options.family === 'd900' ? i + 1 : i - 3;
|
|
610
|
+
if (sent > 0 ? sent % family.burst === 0 : sent === 0 && ctx.options.family === 'dx5') {
|
|
611
|
+
await ctx.sleep(10);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
await ctx.sleep(85);
|
|
615
|
+
return { reconnect: false };
|
|
616
|
+
},
|
|
617
|
+
async currentSlot(ctx) {
|
|
618
|
+
await sessionOf(ctx);
|
|
619
|
+
return activeSlot(ctx);
|
|
620
|
+
},
|
|
621
|
+
async eqEnabled(ctx) {
|
|
622
|
+
await sessionOf(ctx);
|
|
623
|
+
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
|
+
const at = familyOf(o).settings?.enabled;
|
|
631
|
+
if (at !== undefined) {
|
|
632
|
+
const s = await settings(ctx);
|
|
633
|
+
return s?.[at] === undefined ? null : s[at] !== 0;
|
|
634
|
+
}
|
|
635
|
+
const [state] = await ask(ctx, EQ_ENABLE_STATE, READ, 'the EQ state request');
|
|
636
|
+
return (state & 1) === 1;
|
|
637
|
+
},
|
|
638
|
+
setEnabled,
|
|
639
|
+
async firmware(ctx) {
|
|
640
|
+
const session = await sessionOf(ctx);
|
|
641
|
+
const at = familyOf(ctx.options).settings?.firmware;
|
|
642
|
+
if (session.firmware !== undefined)
|
|
643
|
+
return toppingFirmware(session.firmware);
|
|
644
|
+
if (at !== undefined)
|
|
645
|
+
return toppingFirmware((await settings(ctx))?.[at] ?? 0);
|
|
646
|
+
const [word] = await ask(ctx, SOFTWARE_VERSION, READ, 'the firmware version request');
|
|
647
|
+
return toppingFirmware(word);
|
|
648
|
+
},
|
|
649
|
+
controls: {
|
|
650
|
+
volume: dx5Volume,
|
|
651
|
+
balance: dx5Balance,
|
|
652
|
+
...Object.fromEntries(Object.entries(TOPPING_DX5_SETTINGS).map(([id, [command, word]]) => [
|
|
653
|
+
id,
|
|
654
|
+
dx5Word(command, word)
|
|
655
|
+
]))
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
//# sourceMappingURL=topping-usb-hid.js.map
|