-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
495 lines (361 loc) · 17.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
const { dirname, join } = require("path");
const Registry = require("winreg");
const koffi = require("koffi");
const { VoicemeeterDefaultConfig, VoicemeeterType, RunVoicemeeterType, InterfaceType, LevelType, DeviceType, MacroButtonState, MacroButtonTrigger, MacroButtonColor } = require("./voicemeeterEnums");
const getDLLPath = () => {
const regKey = new Registry({
hive: Registry.HKLM,
key: "\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\VB:Voicemeeter {17359A74-1236-5467}"
});
return new Promise((resolve) => {
regKey.values((error, items) => {
const uninstallerPath = items.find((item) => item.name === "UninstallString").value;
resolve(join(dirname(uninstallerPath), "VoicemeeterRemote64.dll"));
});
});
}
let libvoicemeeter;
const voicemeeter = {
isInitialised: false,
isConnected: false,
outputDevices: [],
inputDevices: [],
type: 0,
version: null,
voicemeeterConfig: null,
async init() {
const dll = koffi.load(await getDLLPath());
libvoicemeeter = {
// Login
VBVMR_Login: dll.func("long __stdcall VBVMR_Login(void)"),
VBVMR_Logout: dll.func("long __stdcall VBVMR_Logout(void)"),
VBVMR_RunVoicemeeter: dll.func("long __stdcall VBVMR_RunVoicemeeter(long vType)"),
// General informations
VBVMR_GetVoicemeeterType: dll.func("long __stdcall VBVMR_GetVoicemeeterType(_Out_ long * pType)"),
VBVMR_GetVoicemeeterVersion: dll.func("long __stdcall VBVMR_GetVoicemeeterVersion(_Out_ long * pVersion)"),
// Get parameters
VBVMR_IsParametersDirty: dll.func("long __stdcall VBVMR_IsParametersDirty(void)"),
VBVMR_GetParameterFloat: dll.func("long __stdcall VBVMR_GetParameterFloat(char * szParamName, _Out_ float * pValue)"),
VBVMR_GetParameterStringA: dll.func("long __stdcall VBVMR_GetParameterStringA(char * szParamName, _Out_ void * szString)"),
VBVMR_GetParameterStringW: dll.func("long __stdcall VBVMR_GetParameterStringW(char * szParamName, _Out_ unsigned short * wszString)"), // Not tested
// Get levels
VBVMR_GetLevel: dll.func("long __stdcall VBVMR_GetLevel(long nType, long nuChannel, _Out_ float * pValue)"),
VBVMR_GetMidiMessage: dll.func("long __stdcall VBVMR_GetMidiMessage(_Out_ unsigned char *pMIDIBuffer, long nbByteMax)"), // Not tested
VBVMR_SendMidiMessage: dll.func("long __stdcall VBVMR_SendMidiMessage(_Out_ unsigned char *pMIDIBuffer, long nbByte)"), // Not tested
// Set parameters
VBVMR_SetParameterFloat: dll.func("long __stdcall VBVMR_SetParameterFloat(char * szParamName, float Value)"),
VBVMR_SetParameterStringA: dll.func("long __stdcall VBVMR_SetParameterStringA(char * szParamName, char * szString)"), // Not tested
VBVMR_SetParameterStringW: dll.func("long __stdcall VBVMR_SetParameterStringW(char * szParamName, unsigned short * wszString)"), // Not tested
VBVMR_SetParameters: dll.func("long __stdcall VBVMR_SetParameters(char * szParamScript)"),
VBVMR_SetParametersW: dll.func("long __stdcall VBVMR_SetParametersW(unsigned short * szParamScript)"), // Not tested
// Devices enumerator
VBVMR_Output_GetDeviceNumber: dll.func("long __stdcall VBVMR_Output_GetDeviceNumber(void)"),
VBVMR_Output_GetDeviceDescA: dll.func("long __stdcall VBVMR_Output_GetDeviceDescA(long zindex, _Out_ long * nType, _Out_ void * szDeviceName, _Out_ void * szHardwareId)"),
VBVMR_Output_GetDeviceDescW: dll.func("long __stdcall VBVMR_Output_GetDeviceDescW(long zindex, _Out_ long * nType, _Out_ unsigned short * wszDeviceName, _Out_ unsigned short * wszHardwareId)"), // Not tested
VBVMR_Input_GetDeviceNumber: dll.func("long __stdcall VBVMR_Input_GetDeviceNumber(void)"),
VBVMR_Input_GetDeviceDescA: dll.func("long __stdcall VBVMR_Input_GetDeviceDescA(long zindex, _Out_ long * nType, _Out_ void * szDeviceName, _Out_ void * szHardwareId)"),
VBVMR_Input_GetDeviceDescW: dll.func("long __stdcall VBVMR_Input_GetDeviceDescW(long zindex, _Out_ long * nType, _Out_ unsigned short * wszDeviceName, _Out_ unsigned short * wszHardwareId)"), // Not tested
// TODO Implement callback
// Macro buttons
VBVMR_MacroButton_IsDirty: dll.func("long __stdcall VBVMR_MacroButton_IsDirty(void)"),
VBVMR_MacroButton_GetStatus: dll.func("long __stdcall VBVMR_MacroButton_GetStatus(long nuLogicalButton, _Out_ float * pValue, long bitmode)"),
VBVMR_MacroButton_SetStatus: dll.func("long __stdcall VBVMR_MacroButton_SetStatus(long nuLogicalButton, float fValue, long bitmode)")
};
this.isInitialised = true;
},
runVoicemeeter(runVoicemeeterType) {
if (libvoicemeeter.VBVMR_RunVoicemeeter(runVoicemeeterType) !== 0)
throw "Running failed";
},
isParametersDirty() {
return libvoicemeeter.VBVMR_IsParametersDirty();
},
/**
* @deprecated Use getRawParameterFloat
*/
getParameter(parameterName) {
return getRawParameterFloat(parameterName);
},
getRawParameterFloat(parameter) {
if (!this.isConnected)
throw "Not connected";
const value = [0];
if (libvoicemeeter.VBVMR_GetParameterFloat(parameter, value) !== 0)
throw "Running failed";
return value[0];
},
getRawParameterString(parameter) {
if (!this.isConnected)
throw "Not connected";
const value = Buffer.alloc(512);
if (libvoicemeeter.VBVMR_GetParameterStringA(parameter, value) !== 0)
throw "Running failed";
return value.toString().replace(/\x00+$/, "");
},
setRawParameterFloat(parameter, value) {
if (!this.isConnected)
throw "Not connected";
if (libvoicemeeter.VBVMR_SetParameterFloat(parameter, value) !== 0)
throw "Running failed";
},
setRawParameterString(parameter, value) {
if (!this.isConnected)
throw "Not connected";
if (libvoicemeeter.VBVMR_SetParameterStringA(parameter, value) !== 0)
throw "Running failed";
},
setRawParameters(parameters) {
if (!this.isConnected)
throw "Not connected";
if (libvoicemeeter.VBVMR_SetParameters(parameters) !== 0)
throw "Running failed";
},
login() {
if (!this.isInitialised)
throw "Await the initialization before login";
if (this.isConnected)
throw "Already connected";
if (libvoicemeeter.VBVMR_Login() !== 0)
throw "Login failed";
this.type = this._getVoicemeeterType();
this.version = this._getVoicemeeterVersion();
this.voicemeeterConfig = VoicemeeterDefaultConfig[this.type];
this.isConnected = true;
},
logout() {
if (!this.isConnected)
throw "Not connected";
if (libvoicemeeter.VBVMR_Logout() !== 0)
throw "Logout failed";
this.isConnected = false;
},
getOutputDeviceNumber() {
return libvoicemeeter.VBVMR_Output_GetDeviceNumber();
},
getInputDeviceNumber() {
return libvoicemeeter.VBVMR_Input_GetDeviceNumber();
},
updateDeviceList() {
if (!this.isConnected)
throw "Not connected";
this.outputDevices = [];
this.inputDevices = [];
const outputDeviceNumber = this.getOutputDeviceNumber();
for (let i = 0; i < outputDeviceNumber; i++) {
const type = [0];
const deviceName = Buffer.alloc(256);
const hardwareId = Buffer.alloc(256);
if (libvoicemeeter.VBVMR_Output_GetDeviceDescA(i, type, deviceName, hardwareId) !== 0)
throw "Running failed";
this.outputDevices.push({
type: type[0],
name: deviceName.toString().replace(/\x00+$/, ""),
hardwareId: hardwareId.toString().replace(/\x00+$/, "")
});
}
const inputDeviceNumber = this.getInputDeviceNumber();
for (let i = 0; i < inputDeviceNumber; i++) {
const type = [0];
const deviceName = Buffer.alloc(256);
const hardwareId = Buffer.alloc(256);
if (libvoicemeeter.VBVMR_Input_GetDeviceDescA(i, type, deviceName, hardwareId) !== 0)
throw "Running failed";
this.inputDevices.push({
type: type[0],
name: deviceName.toString().replace(/\x00+$/, ""),
hardwareId: hardwareId.toString().replace(/\x00+$/, "")
});
}
},
showVoicemeeter() {
this.setRawParameters("Command.Show=1;");
},
shutdownVoicemeeter() {
this.setRawParameters("Command.Shutdown=1;");
},
restartVoicemeeterAudioEngine() {
this.setRawParameters("Command.Restart=1;");
},
ejectVoicemeeterCassette() {
this.setRawParameters("Command.Eject=1;");
},
resetVoicemeeterConfiguration() {
this.setRawParameters("Command.Reset=1;");
},
saveVoicemeeterConfiguration(filename) {
this.setRawParameters("Command.Save=" + filename + ";");
},
loadVoicemeeterConfiguration(filename) {
this.setRawParameters("Command.Load=" + filename + ";");
},
lockVoicemeeterGui(lock) {
this.setRawParameters("Command.Lock=" + (lock ? 1 : 0) + ";");
},
setMacroButtonState(button, state) {
if (!Object.values(MacroButtonState).includes(state))
throw "Invalid state";
this.setRawParameters("Command.Button[" + button + "].State=" + state + ";");
},
setMacroButtonStateOnly(button, state) {
if (!Object.values(MacroButtonState).includes(state))
throw "Invalid state";
this.setRawParameters("Command.Button[" + button + "].StateOnly=" + state + ";");
},
setMacroButtonTrigger(button, trigger) {
if (!Object.values(MacroButtonTrigger).includes(trigger))
throw "Invalid trigger";
this.setRawParameters("Command.Button[" + button + "].Trigger=" + trigger + ";");
},
/**
* Seems to be broken in the Voicemeeter API
*/
setMacroButtonColor(button, color) {
if (!Object.values(MacroButtonColor).includes(color))
throw "Invalid color";
this.setRawParameters("Command.Button[" + button + "].Color=" + color + ";");
},
showVbanChatDialog() {
this.setRawParameters("Command.DialogShow.VBANCHAT=1;");
},
getLevel(type, channel) {
if (!this.isConnected)
throw "Not connected";
const value = [0];
if (libvoicemeeter.VBVMR_GetLevel(type, channel, value) !== 0)
throw "Running failed";
return value[0];
},
_getVoicemeeterType() {
const voicemeeterType = [0];
if (libvoicemeeter.VBVMR_GetVoicemeeterType(voicemeeterType) !== 0)
throw "Running failed";
switch (voicemeeterType[0]) {
case 1:
return VoicemeeterType.voicemeeter;
case 2:
return VoicemeeterType.voicemeeterBanana;
case 3:
return VoicemeeterType.voicemeeterPotato;
default:
return VoicemeeterType.unknown;
}
},
_getVoicemeeterVersion() {
const voicemeeterVersion = [0];
if (libvoicemeeter.VBVMR_GetVoicemeeterVersion(voicemeeterVersion) !== 0)
throw "Running failed";
const v1 = (voicemeeterVersion[0] & 0xFF000000) >> 24;
const v2 = (voicemeeterVersion[0] & 0x00FF0000) >> 16;
const v3 = (voicemeeterVersion[0] & 0x0000FF00) >> 8;
const v4 = voicemeeterVersion[0] & 0x000000FF;
return `${v1}.${v2}.${v3}.${v4}`;
},
_getParameterFloat(type, name, id) {
if (!this.isConnected)
throw "Not connected";
if (!this.voicemeeterConfig)
throw "Configuration error";
if (!Object.values(InterfaceType).includes(type))
throw "Invalid type";
const interfaceType = type === InterfaceType.strip ? "Strip" : "Bus";
if (!this.voicemeeterConfig[type === InterfaceType.strip ? "strips" : "buses"].some((strip) => strip.id === id))
throw `${interfaceType} ${id} not found`;
const parameter = `${interfaceType}[${id}].${name}`;
return this.getRawParameterFloat(parameter);
},
_getParameterString(type, name, id) {
if (!this.isConnected)
throw "Not connected";
if (!this.voicemeeterConfig)
throw "Configuration error";
if (!Object.values(InterfaceType).includes(type))
throw "Invalid type";
const interfaceType = type === InterfaceType.strip ? "Strip" : "Bus";
if (!this.voicemeeterConfig[type === InterfaceType.strip ? "strips" : "buses"].some((strip) => strip.id === id))
throw `${interfaceType} ${id} not found`;
const parameter = `${interfaceType}[${id}].${name}`;
return this.getRawParameterString(parameter);
},
_setParameterFloat(type, name, id, value) {
if (!this.isConnected)
throw "Not connected";
if (!this.voicemeeterConfig)
throw "Configuration error";
if (!Object.values(InterfaceType).includes(type))
throw "Invalid type";
const interfaceType = type === InterfaceType.strip ? "Strip" : "Bus";
if (!this.voicemeeterConfig[type === InterfaceType.strip ? "strips" : "buses"].some((strip) => strip.id === id))
throw `${interfaceType} ${id} not found`;
const parameter = `${interfaceType}[${id}].${name}`;
this.setRawParameterFloat(parameter, value);
},
_setParameterString(type, name, id, value) {
if (!this.isConnected)
throw "Not connected";
if (!this.voicemeeterConfig)
throw "Configuration error";
if (!Object.values(InterfaceType).includes(type))
throw "Invalid type";
const interfaceType = type === InterfaceType.strip ? "Strip" : "Bus";
if (!this.voicemeeterConfig[type === InterfaceType.strip ? "strips" : "buses"].some((strip) => strip.id === id))
throw `${interfaceType} ${id} not found`;
const parameter = `${interfaceType}[${id}].${name}`;
this.setRawParameterString(parameter, value);
},
_setParameters(parameters) {
if (!this.isConnected)
throw "Not connected";
if (!this.voicemeeterConfig)
throw "Configuration error";
if (!Array.isArray(parameters))
throw "Parameters must be an array";
const script = parameters.map((parameter) => {
if (!Object.values(InterfaceType).includes(parameter.type))
throw "Invalid type";
const interfaceType = parameter.type === InterfaceType.strip ? "Strip" : "Bus";
if (!this.voicemeeterConfig[parameter.type === InterfaceType.strip ? "strips" : "buses"].some((strip) => strip.id === parameter.id))
throw `${interfaceType} ${parameter.id} not found`;
return `${interfaceType}[${parameter.id}].${parameter.name}=${parameter.value};`;
}).join("");
this.setRawParameters(script);
},
/**
* @deprecated Use setRawParameters
*/
_sendRawParameterScript(script) {
this.setRawParameters(script);
}
}
const busesParametersNames = ["Mono", "Mute", "Gain"];
const stripParametersNames = ["Mono", "Mute", "Solo", "MC", "Gain", "Pan_x", "Pan_y", "Color_x", "Color_y", "fx_x", "fx_y", "Audibility", "Gate", "Comp", "A1", "A2", "A3", "A4", "A5", "B1", "B2", "B3"];
busesParametersNames.forEach((name) => {
voicemeeter[`setBus${name}`] = (busNumber, value) => {
if (typeof value === "boolean")
voicemeeter._setParameterFloat(InterfaceType.bus, name, busNumber, value ? 1 : 0);
else
voicemeeter._setParameterFloat(InterfaceType.bus, name, busNumber, value);
}
voicemeeter[`getBus${name}`] = (busNumber) => {
return voicemeeter._getParameterFloat(InterfaceType.bus, name, busNumber);
}
});
stripParametersNames.forEach((name) => {
voicemeeter[`setStrip${name}`] = (stripNumber, value) => {
if (typeof value === "boolean")
voicemeeter._setParameterFloat(InterfaceType.strip, name, stripNumber, value ? 1 : 0);
else
voicemeeter._setParameterFloat(InterfaceType.strip, name, stripNumber, value);
}
voicemeeter[`getStrip${name}`] = (stripNumber) => {
return voicemeeter._getParameterFloat(InterfaceType.strip, name, stripNumber);
}
});
module.exports = voicemeeter;
module.exports.VoicemeeterType = VoicemeeterType;
module.exports.RunVoicemeeterType = RunVoicemeeterType;
module.exports.InterfaceType = InterfaceType;
module.exports.LevelType = LevelType;
module.exports.DeviceType = DeviceType;
module.exports.MacroButtonState = MacroButtonState;
module.exports.MacroButtonTrigger = MacroButtonTrigger;
module.exports.MacroButtonColor = MacroButtonColor;