This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
616 lines (536 loc) · 17.9 KB
/
Copy pathmain.js
File metadata and controls
616 lines (536 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
const { app, BrowserWindow, ipcMain, Menu, dialog } = require("electron");
const path = require("path");
const shell = require('electron').shell
const isMac = process.platform === 'darwin'
const midi = require('midi')
const logger = require('electron-log');
const userLog = logger.scope('user');
//make midi in and out global
var midiOutput
var midiInput
var tools = require('./app/main/tools')
var midiTools = require('./app/main/midi')
var mainWindow;
const loadMainWindow = () => {
mainWindow = new BrowserWindow({
width : 1240,
height: 900,
webPreferences: {
nodeIntegration: false, // is default value after Electron v5
contextIsolation: true, // protect against prototype pollution
enableRemoteModule: false, // turn off remote
preload: path.join(__dirname, 'app/preload/preload.js')
}
});
mainWindow.loadFile(path.join(__dirname, "index.html"));
}
//for the Mac OS About menu
app.setAboutPanelOptions({
applicationName: "Wavsyn",
applicationVersion: "Version",
version: app.getVersion(),
credits: "Developed by Erich izdepski",
copyright: "Copyright 2021"
});
//for the help menu item 'about'
function aboutWavsyn() {
var aboutMessage = `Wavsyn, Version (${app.getVersion()}) Developed by Erich izdepski, Copyright 2021
Chrome Version ${process.versions['chrome']}
Node Version ${process.versions['node']}
Electron Version ${process.versions['electron']}`
let options = {
title : "Wavsyn Environment Details",
message : aboutMessage
}
dialog.showMessageBoxSync(mainWindow, options)
}
//on ready event call loadMainWindow
app.whenReady().then(() => {
loadMainWindow();
})
// clean up properly after shutdown after event
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
//start app if not already running (single instance)
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
loadMainWindow();
}
});
//set up for communications with renderer process
ipcMain.on("toMain", (event, args) => {
var toolName = args.tool;
var source = args.source
var destination = args.destination
// run tool given by name and send result back to renderer process
mainWindow.webContents.send("fromMain", tools.allTools[toolName](source, destination));
});
ipcMain.on("getPlatform", (event, args) => {
let platform = process.platform
mainWindow.webContents.send("getPlatform", platform);
});
function showWarning(args) {
let options = {
title : "I'm sorry, Dave, I'm afraid I can't do that.",
message : args
}
dialog.showMessageBoxSync(mainWindow, options)
}
ipcMain.on("showWarning", (event, args) => {
showWarning(args)
})
ipcMain.on("saveLogs", (event, args) => {
//the args variable contains the latest text area output. save to a file. it appends.
userLog.info(args)
});
//proxy to midi.js
ipcMain.on("getMidiPorts", (event, args) => {
//read all input and output ports and send to render process
mainWindow.webContents.send("midiPorts", midiTools.getMidiPorts())
});
ipcMain.on("getProgramDump", (event, args) => {
// Set up a new output.
midiOutput = new midi.Output();
midiInput = new midi.Input();
// Configure a callback.
midiInput.on('message', (deltaTime, message) => {
if (message.length == 1255) {
// The message is an array of numbers corresponding to the bytes in the dump (i hope!)
mainWindow.webContents.send("programDump", Buffer.from(message));
console.log(`program dump: ${message} d: ${deltaTime}`);
}
else {
//error
console.log(`data does not contain a program dump fo 1255 bytes; has ${message.length} bytes`);
}
midiInput.closePort()
});
midiInput.openPort(parseInt(args["midiIn"]));
//turn on listening for sysex messages (ignores timing and active sensing)
midiInput.ignoreTypes(false, true, true);
midiOutput.openPort(parseInt(args["midiOut"]));
if (args["isLower"]) {
//get lower dump
midiOutput.sendMessage([240, 15, 1, 3, 247])
}
else {
//get upper dump
midiOutput.sendMessage([240, 15, 1, 19, 247])
}
// Close the port when done.
midiOutput.closePort();
})
/**
* Execute mirage command to save to floppy
*/
ipcMain.on("saveSound", (event, args) => {
/**data["midiIn"] = midiIn
data["midiOut"] = midiOut
data["isLower"] = isLower
data["sound"] = sound */
let isLower = args["isLower"]
let sound = args["sound"]
var sysex = []
//11 is lower, 12 is upper, then sound , then enter (10)
if (isLower) {
//save to lower #sound (1, 2, 3)
sysex = [1, 1, sound, 10]
}
else {
//save to upper #sound
sysex = [1, 2, sound, 10]
}
sendSysex(args["midiIn"], args["midiOut"], sysex)
})
/**
* Execute mirage command to load from floppy soudn 1, 2, 3. This also automatically sends
* back a program dump and an extra 12 bytes of systex (why I don't know).
*/
ipcMain.on("loadSound", (event, args) => {
let midiIn = args["midiIn"]
let midiOut = args["midiOut"]
let isLower = args["isLower"]
let isLowerProgram = args["isLowerProgram"]
let sound = args["sound"]
midiOutput = new midi.Output();
midiInput = new midi.Input();
midiInput.openPort(parseInt(midiIn));
//turn on listening for sysex messages (ignores timing and active sensing)
midiInput.ignoreTypes(false, true, true);
midiOutput.openPort(parseInt(midiOut));
// Configure a callback.
midiInput.on('message', (deltaTime, message) => {
if (message.length == 1255) {
console.log(`program dump: ${message} d: ${deltaTime}`);
// The message is an array of numbers corresponding to the bytes in the dump (i hope!)
mainWindow.webContents.send("programDump", Buffer.from(message));
}
else {
//other midi received.
console.log(`data received: ${message}`);
}
});
var sysex = []
//17 is lower, 16 is upper. The 10 is for enter.
if (isLower) {
sysex = [17, sound, 10]
}
else {
sysex = [16, sound, 10]
}
var prefix = [240, 15, 1, 1]
var suffix = [127, 247]
var fullSysex = [...prefix, ...sysex, ...suffix]
console.log(`sending sysex command loadSound ${fullSysex}`)
midiOutput.sendMessage(fullSysex)
//now change the program bank to match. Always setting to program 1 after a loadsound
if (isLowerProgram == isLower) {
//no bank change
sysex = [0, 1 ]
}
else {
//bank change
sysex = [0, 0, 1]
}
fullSysex = [...prefix, ...sysex, ...suffix]
console.log(`sending sysex command changeProgram ${fullSysex}`)
midiOutput.sendMessage(fullSysex)
midiOutput.closePort()
})
ipcMain.on("readParameter", (event, args) => {
/** data["midiIn"] = midiIn
data["midiOut"] = midiOut
data["parameter"] = parameter
send syex to Mirage to read a parameter*/
//need parameters as two digits- decimal integers
let d1 = parseInt(args["parameter"].charAt(0))
let d2 = parseInt(args["parameter"].charAt(1))
// Set up a new output.
midiOutput = new midi.Output();
midiInput = new midi.Input();
// Configure a callback.
midiInput.on('message', (deltaTime, message) => {
mainWindow.webContents.send("parameterValue", Buffer.from(message));
console.log(`readParameter received: ${message} d: ${deltaTime}`);
midiInput.closePort()
});
midiInput.openPort(parseInt(args["midiIn"]));
//turn on listening for sysex messages (ignores timing and active sensing)
midiInput.ignoreTypes(false, true, true);
midiOutput.openPort(parseInt(args["midiOut"]));
//the command means (12) for parameter d1d2 (13) read the value (since none is given)
//f0 0f 01 01 0c 03 07 0d 7f f7
let sx = [240, 15, 1, 1, 12, d1, d2, 13, 127, 247]
console.log(`sending readParameter sysex ${sx}`)
midiOutput.sendMessage(sx)
// Close the port when done.
midiOutput.closePort();
})
/**
* since setting the program bank in the loadSound command, this command NEVER has to change the bank,
* simplifying the logic.
*/
ipcMain.on("changeProgram", (event, args) => {
/** data["midiIn"] = midiIn
data["midiOut"] = midiOut
data["program"] = program
*/
console.log(`sending changeProgram sysex; changing to program ${args["program"]}`)
sendSysex(args["midiIn"], args["midiOut"], [0, args["program"] ] )
})
/**
* There is no single write command. You send an up or down arrow. (1 for each value change)
*/
ipcMain.on("writeParameter", (event, args) => {
/**data["midiIn"] = midiIn
data["midiOut"] = midiOut
//treat parameter as two character string. parse out the integers. simplest.
data["parameter"] = parameter
data["delta"] = up or down arrows to send via sysex
down arrow: F0 0F 01 01 0f 7f F7
up arrow: F0 0F 01 01 0e 7f F7
*/
let parameter = args["parameter"]
let midiIn = args["midiIn"]
let midiOut = args["midiOut"]
let d1 = parseInt(parameter.charAt(0))
let d2 = parseInt(parameter.charAt(1))
let delta = parseInt(args["delta"])
let selectSysex = [12, d1, d2, 13]
midiOutput = new midi.Output();
midiInput = new midi.Input();
midiInput.openPort(parseInt(midiIn));
//turn on listening for sysex messages (ignores timing and active sensing)
midiInput.ignoreTypes(false, true, true);
midiOutput.openPort(parseInt(midiOut));
// Configure a callback.
midiInput.on('message', (deltaTime, message) => {
// The message is an array of numbers corresponding to the MIDI bytes:
// [status, data1, data2]
// https://www.cs.cf.ac.uk/Dave/Multimedia/node158.html has some helpful
// information interpreting the messages.
console.log(`m: ${message} d: ${deltaTime}`);
});
//make command to select parameter to change f0 0f 01 01 0c 03 07 7f f7
var prefix = [240, 15, 1, 1]
var suffix = [127, 247]
var fullSysex = [...prefix, ...selectSysex, ...suffix]
//select the parameter to change
midiOutput.sendMessage(fullSysex)
//now send the right number of up and down arrows.
if (delta > 0) {
//uparrow
fullSysex = [...prefix, [14], ...suffix]
}
else {
//down arrow
fullSysex = [...prefix, [15], ...suffix]
}
//now send it. we only send a single up or down arrow at a time
midiOutput.sendMessage(fullSysex)
console.log(`send writeParameter ${fullSysex}`)
//close output and input
midiOutput.closePort()
//closing here since multiple messages were sent and there may be several callbacks
midiInput.closePort()
})
ipcMain.on("selectDirectory", (event, args) => {
console.log("in select directory in main process")
let options = {
title : "Open location for processing",
//just show all files so user is not confued by empty directory
filters : [{name : 'All Files', extensions: ['*']}],
buttonLabel : "Choose Location",
properties: ['openDirectory']
}
dialog.showOpenDialog(mainWindow, options).then(result => {
if (result.canceled) {
//cancelled so no directory selected
let msg = "directory selection cancelled"
console.log(msg)
mainWindow.webContents.send("selectDirectory", msg);
}
else {
let msg = new Array(2)
if (args =="source")
{
msg[0] = "source"
}
else {
msg[0] = "destination"
}
msg[1] = result.filePaths[0]
console.log(`selected ${msg}`)
mainWindow.webContents.send("selectDirectory", msg);
}
}).catch(err => {
console.log(err)
mainWindow.webContents.send("selectDirectory", err);
})
});
//would like it midi.js but concerned callbacks won't work
function sendSysex(midiIn, midiOut, sysex) {
midiOutput = new midi.Output();
midiInput = new midi.Input();
midiInput.openPort(parseInt(midiIn));
//turn on listening for sysex messages (ignores timing and active sensing)
midiInput.ignoreTypes(false, true, true);
midiOutput.openPort(parseInt(midiOut));
// Configure a callback.
midiInput.on('message', (deltaTime, message) => {
// The message is an array of numbers corresponding to the MIDI bytes:
// [status, data1, data2]
// https://www.cs.cf.ac.uk/Dave/Multimedia/node158.html has some helpful
// information interpreting the messages.
console.log(`m: ${message} d: ${deltaTime}`);
midiInput.closePort()
});
//send command and return value from a param: f0 0f 01 01 0c 03 07 0d 7f f7
var prefix = [240, 15, 1, 1]
var suffix = [127, 247]
var fullSysex = [...prefix, ...sysex, ...suffix]
console.log(`sending sysex ${fullSysex}`)
midiOutput.sendMessage(fullSysex)
//close output
midiOutput.closePort()
}
//set up custom menus
const template = [
// { role: 'appMenu' }
...(isMac ? [{
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
}] : []),
// { role: 'fileMenu' }
{
label: 'File',
submenu: [
(isMac ? { role: 'close' } : { role: 'quit' }),
]
},
// { role: 'editMenu' }
/**
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
...(isMac ? [
{ role: 'pasteAndMatchStyle' },
{ role: 'delete' },
{ role: 'selectAll' },
{ type: 'separator' },
{
label: 'Speech',
submenu: [
{ role: 'startSpeaking' },
{ role: 'stopSpeaking' }
]
}
] : [
{ role: 'delete' },
{ type: 'separator' },
{ role: 'selectAll' }
])
]
},
*/
// { role: 'viewMenu' }
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{
label: 'Edit',
submenu: [
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" }
]
},
// { role: 'windowMenu' }
{
label: 'Window',
submenu: [
{ label: 'Audio Processor',
click() {
//load main view (which is also default)
mainWindow.loadFile(path.join(__dirname, "/app/pages/audio.html"));
}
},
{ label: 'Editor',
click() {
//load editor html file
mainWindow.loadFile(path.join(__dirname, "index.html"));
}},
{ role: 'minimize' },
{ role: 'zoom' },
...(isMac ? [
{ type: 'separator' },
{ role: 'front' },
{ type: 'separator' },
{ role: 'window' }
] : [
{ role: 'close' }
])
]
},
{
label: 'Software',
submenu: [
{ label: 'Omniflop',
click() {
shell.openExternal('http://www.shlock.co.uk/Utils/OmniFlop/OmniFlop.htm')
}
},
{ label: 'HxC Floppy Emulator',
click() {
shell.openExternal('https://hxc2001.com/download/floppy_drive_emulator/')
}
}
]
},
{
role: 'help',
submenu: [
{
label: 'Wavsyn on Github',
click() {
shell.openExternal('https://github.com/mogrifier/wavsyn')
}
},
{
label: 'Manual',
click() {
shell.openExternal('https://github.com/mogrifier/wavsyn/wiki')
}
},
{
label: "Erich's Blog",
click() {
shell.openExternal('https://erichizdepski.wordpress.com')
}
},
{
label: 'About',
click() {
aboutWavsyn()
}
}
]
}
]
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
//get a config dump: F0 0F 01 00 F7
/**
* F0 0F 01 02
* 00 00 02 03 02 00 0E 01 00 04 00 00 02 02 00 0A 00 00 00 03 00 00 01 00 00 00 00 00
* 01 00 00 00 00 00 0F 0F 0F 0E 00 00 00 00 01 00 00 00 0F 0F 01 00 00 00 00 00 00 02 00 00 F7
*
* made changes and saved the config to a disk
* F0 0F 01 02
* 00 00 02 03 0C 00 02 01 00 04 00 00 07 03 0E 00 00 00 00 03 00 00 01 00 00 00 00 00
* 01 00 00 00 00 00 0F 0F 0F 0E 00 00 00 00 01 00 00 00 0F 0F 01 00 00 00 00 00 00 02 00 00 F7
*/
//load a sound: F0 0F 01 01 10 02 0a 7f F7
/** read a parameter: f0 0f 01 01 0c 03 07 0d 7f f7 - crazy. 0c means parameter number follows. Comes as two bytes. 03 07 = 37
0d means return the value of the parameter.
returned F0 0F 01 0D 00 25 00 0A F7 . 00 25 - this is hex for 37. So decimal one way, hex the other. Value is 00 0A.
Mirage reads 40 (max resonance. Internal value max is 160, by the way.)
These are nibbles sent in LS MS order. Reverse them to create a byte. value is 160, which corresponds to a 40 on mirage display.
*/
//show value on mirage display: f0 0f 01 01 0c 03 07 0d 7f f7 (shows value of 37.)
//change a parameter: F0 0F 01 01 0c 25 0d 09 7f F7
//set up arrow: F0 0F 01 01 0e 7f F7 and got this: F0 0F 01 0D 03 25 04 00 F7
//I pressed up arrao to change value of param 37 and giot this back: F0 0F 01 0D 02 25 0C 00 F7
// od 02 means value was 2. 25 0c looks like 37 (hex 25 is 37) and 0c is parameter number.
//240, 15, 1, 19, 247 F0 0F 01 13 F7
//tells mirage to switch to param 37 : f0 0f 01 01 0c 03 07 7f f7
//return value from a param: f0 0f 01 01 0c 03 07 0d 7f f7