-
Notifications
You must be signed in to change notification settings - Fork 0
/
P3synth.pde
678 lines (551 loc) · 19.9 KB
/
P3synth.pde
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import drop.*;
import java.io.*;
import java.util.Map.*;
import java.awt.*;
import processing.awt.PSurfaceAWT;
final processing.core.PApplet PARENT = this;
final float VERCODE = 23.36;
final float OVERALL_VOL = 0.8;
final float HIRES_MULT = 2;
String process_id = "p3synth2332";
Frame frame;
String osname;
Player player;
LabsModule win_labs;
PlaylistModule win_plist;
DnDMidListener dnd_mid;
DnDSfListener dnd_sf;
PImage[] logo_anim;
PImage[] osc_type_textures;
PImage midi_program_icon;
PImage logo_icon;
PFont[] fonts;
SoundFile[] samples;
ThemeEngine t;
boolean is_newbie;
ButtonToolbar media_buttons;
ButtonToolbar setting_buttons;
Button b_labs;
Button curr_mid_pressed;
WaitingDialog dialog_meta_msgs;
Form dialog_settings;
boolean show_key_hints = false;
boolean hi_res = false;
boolean low_frate = false;
int frame_height;
void settings() {
if (setup_process_lock()) {
System.exit(0);
return;
}
size(724, 460);
// i think this only works on windows lol
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
ui.showException("I'm sorry to announce that something has gone wrong and P3synth seems to have crashed. Please restart the program if frozen. Details:", "An error occured", new Exception(e));
}
});
}
void setup() {
new Sound(PARENT).volume(1); // fixes crackling? (sometimes??)
t = new ThemeEngine();
surface.setTitle("vlco_o P3synth");
frame = ( (PSurfaceAWT.SmoothCanvas)surface.getNative() ).getFrame();
frame.setSize(new Dimension(724, 460));
frame_height = frame.getSize().height;
setup_images();
setup_buttons();
setup_config();
setup_fonts();
setup_alt_resources();
player = new Player();
SDrop drop = new SDrop(PARENT);
dnd_mid = new DnDMidListener();
dnd_sf = new DnDSfListener();
drop.addDropListener(dnd_mid);
drop.addDropListener(dnd_sf);
if (prefs.getBoolean("media keys", false)) {
try {
GlobalScreen.registerNativeHook();
GlobalScreen.addNativeKeyListener(new MediaKeysListener());
}
catch (NativeHookException nhe) {
println("nhe on media keys");
}
}
if (is_newbie) {
ui.showInfoDialog(
"Welcome! Please check your audio levels.\n\n" +
"Feel free to check the usage guide via the HELP button.\n",
"Hello");
if (new File("P3synth config").exists()) ui.showWarningDialog(
"This version uses a new settings backend, and the old config file will be ignored!\n" +
"It's recommended to delete the 'P3synth config' file from the executable's directory.",
"Settings conflict");
prefs.putBoolean("new user", false);
}
request_media_buttons_refresh();
redraw_all();
if (args != null && args.length > 0) {
player.vu_anim_val = -1.0;
try_play_from_args(args.length > 1 ? args[1] : "", args[0]);
}
beginDiscordActivity();
}
void exit() {
DiscordRPC.discordShutdown();
store_control_memory();
super.exit();
}
double div = 4;
void draw() {
updateDiscordActivity();
if (!frame.isVisible()) return;
redraw_all();
if (player.playing_state == 1) {
int n = (int) (player.seq.getTickPosition() / (player.midi_resolution/div)) % 8;
image(logo_anim[abs(n)], 311, 10);
}
else {
image(logo_anim[0], 311, 10);
if (player.vu_anim_val >= 0.0) {
player.vu_anim_step();
}
}
if (dnd_mid.draggedOnto) player.custom_info_msg = "OK! (MID file)";
else if (dnd_sf.draggedOnto) player.custom_info_msg = "OK! (Soundfont)";
else player.custom_info_msg = "";
}
void redraw_all() {
if (t.is_extended_theme) gradientRect(0, 0, width, frame_height, (int) t.theme[2], t.theme[5], 0, this);
else background(t.theme[2]);
media_buttons.redraw();
setting_buttons.redraw();
b_labs.redraw_at_pos(353, player.disp.POS_Y_POSBAR + 7);
player.redraw();
}
boolean setup_process_lock() {
try {
JUnique.acquireLock(process_id, new MessageHandler() {
public String handle(String message) {
if (!message.equals("")) {
String[] split_msg = message.split("\n");
try_play_from_args(split_msg.length > 1 ? split_msg[1] : "", split_msg[0]);
}
else {
frame.toFront();
frame.setState(Frame.NORMAL);
}
return "gotcha";
}
});
}
catch (AlreadyLockedException ale) {
String msg_to_send = "";
if (args != null && args.length > 0) msg_to_send = args[0] + "\n" + (args.length > 1 ? args[1] : "");
if (JUnique.sendMessage(process_id, msg_to_send).equals("gotcha")) {
return true;
}
else JUnique.releaseLock(process_id);
}
return false;
}
void setup_alt_resources() {
samples = new SoundFile[4];
for (int i = 1; i <= samples.length; i++) {
samples[i-1] = new SoundFile(PARENT, "samples/" + i + ".wav");
}
key_transforms.put("Major", new int[] {-1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1});
key_transforms.put("Minor", new int[] {-1, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0});
key_transforms.put("Natural minor (Aeolian)", new int[] {-1, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0});
key_transforms.put("Lydian", new int[] {0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1});
key_transforms.put("Mixolydian", new int[] {0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 0, 1});
key_transforms.put("Dorian", new int[] {0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 1});
key_transforms.put("Phrygian", new int[] {-1, 0, -1, 0, 0, -1, 0, -1, 0, 0, 0, 0});
key_transforms.put("Locrian", new int[] {-1, 0, -1, 0, 0, -1, 0, -1, 0, 0, -1, 0});
key_transforms.put("Monotonic", new int[] {6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5});
key_transforms.put("** Restore original", new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
}
void setup_images() {
logo_anim = new PImage[8];
for (int i = 0; i < 8; i++) {
PImage img = loadImage("graphics/logo" + i + ".png");
logo_anim[i] = img;
}
osc_type_textures = new PImage[6];
for (int i = -1; i < 5; i++) {
PImage img = loadImage("graphics/osc_" + i + ".png");
osc_type_textures[i+1] = img;
}
midi_program_icon = loadImage("graphics/midi_program.png");
logo_icon = loadImage("graphics/icon.png");
surface.setIcon(logo_icon);
}
void setup_fonts() {
fonts = new PFont[6];
fonts[0] = loadFont("TerminusTTF-12.vlw");
fonts[1] = loadFont("TerminusTTF-14.vlw");
fonts[2] = loadFont("TerminusTTF-Bold-14.vlw");
fonts[3] = loadFont("TerminusTTF-Bold_Italic-14.vlw");
fonts[4] = loadFont("AtariST8x16SystemFont-16.vlw");
fonts[5] = fonts[4];
}
void setup_buttons() {
Button b1 = new Button(12, 376, "reload", "Replay");
b1.set_key_hint("Home");
Button b2 = new Button("stop", "Stop");
b2.set_key_hint("End");
Button b3 = new Button("pause", "Pause");
b3.set_key_hint("Space");
Button[] buttons_ctrl = {b1, b3, b2};
media_buttons = new ButtonToolbar(160, 16, 1.3, 0, buttons_ctrl);
b1 = new Button("info", "Help");
//b1.set_key_hint("F1");
b2 = new Button("conf", "Config");
b2.set_key_hint("F2");
b3 = new Button("update", "Update");
Button[] buttons_set = {b2, b1, b3};
setting_buttons = new ButtonToolbar(456, 16, 1.3, 0, buttons_set);
b_labs = new Button(353, 390, "expand", "Labs");
b_labs.set_key_hint("F3");
}
void setup_media_keys() {
ui.showInfoDialog("Global media keys have been enabled.\nPlease follow this wizard to assign the correct keys.", "Setting up media keys");
mk_setup = "play";
ui.showInfoDialog("PRESS PLAY KEY NOW, then click OK.\nDon't worry if the PLAY and PAUSE is the same key for you.", "Setting up media keys");
mk_setup = "pause";
ui.showInfoDialog("PRESS PAUSE KEY NOW, then click OK.\nDon't worry if the PLAY and PAUSE is the same key for you.", "Setting up media keys");
mk_setup = "back";
ui.showInfoDialog("PRESS BACK/PREVIOUS SONG KEY NOW, then click OK.", "Setting up media keys");
mk_setup = "forward";
ui.showInfoDialog("PRESS FORWARD/NEXT SONG KEY NOW, then click OK.", "Setting up media keys");
mk_setup = "stop";
ui.showInfoDialog("PRESS STOP KEY NOW, then click OK.", "Setting up media keys");
ui.showInfoDialog("Done. This will apply after a program restart.", "Finished setting");
mk_setup = "";
}
void request_media_buttons_refresh() {
if (player == null) return;
media_buttons.get_button("Pause").set_pressed(player.playing_state == 0);
media_buttons.get_button("Stop").set_pressed(player.playing_state == -1);
}
void toggle_labs_win() {
if (win_labs == null) {
cursor(WAIT);
win_labs = new LabsModule(frame);
String[] args = {""};
runSketch(args, win_labs);
cursor(ARROW);
}
else {
if (win_labs.isLooping()) {
win_labs.selfFrame.setVisible(false);
win_labs.noLoop();
}
else {
win_labs.selfFrame.setVisible(true);
win_labs.loop();
}
}
win_labs.reposition();
b_labs.set_pressed(!b_labs.pressed);
}
void toggle_playlist_win() {
if (win_plist == null) {
cursor(WAIT);
win_plist = new PlaylistModule(frame, this);
String[] args = {""};
runSketch(args, win_plist);
cursor(ARROW);
}
else {
if (win_plist.isLooping()) {
win_plist.selfFrame.setVisible(false);
win_plist.noLoop();
}
else {
win_plist.selfFrame.setVisible(true);
win_plist.loop();
}
}
win_plist.reposition();
if (player != null && win_plist.isLooping()) {
player.seq.setLoopCount(0);
player.disp.b_loop.set_pressed(false);
}
}
void set_small_height(boolean how) {
frame.setSize(new Dimension(724, how ? 180 : 460));
frame_height = frame.getSize().height;
}
void keyPressed() {
if (keyCode == 18) { // ALT
show_key_hints = true;
}
if (key == ' ') {
if (player.playing_state == -1) return;
Button b = media_buttons.get_button("Pause");
player.set_playing_state( b.pressed ? 1 : 0 );
}
else if (key == 'o') {
File file = ui.showFileSelection("MIDI files", "mid", "midi");
try_play_file(file);
}
else if (key == 'l') {
if (player.seq == null) return;
int n = player.disp.b_loop.pressed ? 0 : 64;
player.seq.setLoopCount(n);
player.disp.b_loop.set_pressed(!player.disp.b_loop.pressed);
}
else if (key == 'm') {
ui.showTableImmutable(player.get_metadata_table(), Arrays.asList("Parameter", "Value"), "Files' metadata");
}
else if (key == 'n') {
if (dialog_meta_msgs != null) dialog_meta_msgs.close();
dialog_meta_msgs = ui.showWaitingDialog(
"These are lyrics, comments, or other text in the MIDI file.",
"Meta message history", player.history_text_messages
);
}
else if (key == 's') {
File file = ui.showFileSelection("Instrument patch files", "sf2", "dls");
cursor(WAIT);
try_load_sf(file);
cursor(ARROW);
}
else if (keyCode == 112) { // F1
}
else if (keyCode == 113) {
open_config_dialog();
}
else if (keyCode == 114) { // F3
toggle_labs_win();
}
else if (keyCode == 115) {
cursor(WAIT);
player.set_seq_synth(!player.system_synth);
cursor(ARROW);
}
else if (keyCode == 116) { // F5
toggle_playlist_win();
}
else if (keyCode == 36) { // HOME
player.reload_curr_file(false);
}
else if (keyCode == 35) { // END
if (player.playing_state == -1) return;
player.set_playing_state(-1);
if (win_plist != null) win_plist.set_current_item(-1);
}
if (win_plist != null) {
if (keyCode == 33) { // PGUP
win_plist.previous();
}
else if (keyCode == 34) { // PGDOWN
win_plist.next();
}
}
if (player.playing_state != -1) {
if (keyCode == LEFT) {
player.seq.setMicrosecondPosition(player.seq.getMicrosecondPosition() - 2500000);
}
else if (keyCode == RIGHT) {
player.seq.setMicrosecondPosition(player.seq.getMicrosecondPosition() + 2500000);
}
}
if (Character.isDigit(key)) {
int chan = Integer.parseInt(String.valueOf(key)) - 1;
if (chan == -1) chan = 9;
player.set_channel_muted(!player.channels[chan].silenced, chan);
}
}
void keyReleased() {
show_key_hints = false;
}
void mousePressed() {
for (Button b : media_buttons.buttons.values()) {
if (b.icon_filename.equals("pause") || b.icon_filename.equals("stop")) continue;
if (b.collided()) curr_mid_pressed = b;
}
for (Button b : setting_buttons.buttons.values()) {
if (b.collided()) curr_mid_pressed = b;
}
if (player.disp.b_metadata.collided()) curr_mid_pressed = player.disp.b_metadata;
else if (player.disp.b_prev.collided()) curr_mid_pressed = player.disp.b_prev;
else if (player.disp.b_next.collided()) curr_mid_pressed = player.disp.b_next;
if (curr_mid_pressed != null) curr_mid_pressed.set_pressed(true);
else if (player != null) player.vgm_disp.check_buttons(mouseButton);
}
void mouseReleased() {
show_key_hints = false;
if (curr_mid_pressed != null) {
curr_mid_pressed.set_pressed(false);
curr_mid_pressed = null;
}
if (mouseButton == LEFT) {
if(media_buttons.collided("Stop")) {
if (player.playing_state == -1) return;
player.set_playing_state(-1);
if (win_plist != null) win_plist.set_current_item(-1);
}
else if(media_buttons.collided("Pause")) {
if (player.playing_state == -1) return;
Button b = media_buttons.get_button("Pause");
player.set_playing_state( b.pressed ? 1 : 0 );
}
else if(media_buttons.collided("Replay")) {
player.reload_curr_file(false);
}
else if(setting_buttons.collided("Config")) {
open_config_dialog();
}
else if(setting_buttons.collided("Help")) {
boolean go = ui.showConfirmDialog(
"Thanks for using P3synth (v" + VERCODE + ")!\n"+
"Drag and drop a MIDI file in the main window or hold 'ALT' to see available key shortcuts.\n"+
"vlcoo.net | github.com/vlcoo/p3synth\n" +
"\nThe guide is available online. Open?",
"Help"
);
if (go) open_web_url("https://github.com/vlcoo/P3synth/wiki");
}
else if(player.disp.collided_metamsg_rect() && player != null) {
if (dialog_meta_msgs != null) dialog_meta_msgs.close();
dialog_meta_msgs = ui.showWaitingDialog(
"These are lyrics, comments, or other text in the MIDI file.",
"Meta message history", player.history_text_messages
);
}
else if(setting_buttons.collided("Update")) {
cursor(WAIT);
//WaitingDialog wd = ui.showWaitingDialog("Checking for updates...", "Please wait");
float v = check_if_newer_ver();
cursor(ARROW);
//wd.close();
if (v > 0) {
ui.showConfirmDialog(
"There is a newer release available. Download it?", "Update",
new Runnable() { public void run() { download_latest_ver(); } },
new Runnable() { public void run() {} }
);
}
else if (v == 0) ui.showInfoDialog("You're running the latest release of P3synth.", "Update");
else if (v == -1000) ui.showErrorDialog("GitHub could not be reached. Try again later.", "Update");
else ui.showInfoDialog(
"You're running a version that's ahead of the latest release,\n" +
"so this may be the nightly source or a fork of P3synth.",
"Update"
);
}
else if(b_labs.collided()) {
toggle_labs_win();
}
else if (player.disp.collided_sfload_rect()) {
if (!player.system_synth && player.sf_filename.equals("Default") && !prefs.getBoolean("dismiss sf tip", false)) {
ui.showInfoDialog(
"Bonus: drag and drop SF2/DLS file in that box to load it!\n",
"Switching modes"
);
prefs.putBoolean("dismiss sf tip", true);
}
cursor(WAIT);
player.set_seq_synth(!player.system_synth);
cursor(ARROW);
}
else if (player.disp.collided_queue_rect()) {
toggle_playlist_win();
}
}
else if (mouseButton == RIGHT) {
if(media_buttons.collided("Replay")) {
player.reload_curr_file(true);
}
}
player.disp.check_buttons(mouseButton);
player.check_chan_disp_buttons(mouseButton); // check for any presses on the channel display
}
void mouseDragged() {
if (mouseButton == LEFT) {
player.disp.check_buttons(mouseButton, true);
}
}
void mouseMoved() {
if (player.disp.collided_sfload_rect() ||
player.disp.collided_metamsg_rect() ||
player.disp.collided_queue_rect() ||
player.disp.collided_posbar()
) cursor(HAND);
else cursor(ARROW);
}
// this doesn't stand for dungeons and dragons
class DnDMidListener extends DropListener {
boolean draggedOnto = false;
int PADDING = 64;
DnDMidListener() {
setTargetRect(PADDING, PADDING, width-PADDING*2, height-PADDING*2);
}
void dropEnter() {
draggedOnto = true;
}
void dropLeave() {
draggedOnto = false;
}
void dropEvent(DropEvent e) {
cursor(WAIT);
if (try_play_file(e.file())) {
media_buttons.get_button("Pause").set_pressed(false);
}
cursor(ARROW);
}
}
class DnDSfListener extends DropListener {
boolean draggedOnto = false;
int PADDING = 64;
DnDSfListener() {
setTargetRect(width-138, 8, 106, 48);
}
void dropEnter() {
draggedOnto = true;
}
void dropLeave() {
draggedOnto = false;
}
void dropEvent(DropEvent e) {
cursor(WAIT);
try_load_sf(e.file());
cursor(ARROW);
}
}
boolean try_play_file(File selection, boolean invoked_from_playlist) {
if (selection != null) {
String s = selection.getAbsolutePath();
String response = player.play_file(s);
if (!response.equals("")) {
ui.showErrorDialog(response, "Can't play");
return false;
}
if (player.vu_anim_val >= 0.0) player.vu_anim_val = -1.0;
if (!invoked_from_playlist && win_plist != null) win_plist.set_current_item(-1);
return true;
}
return false;
}
boolean try_play_file(File selection) {
return try_play_file(selection, false);
}
boolean try_load_sf(File selection) {
if (selection != null) {
String response = player.load_soundfont(selection);
if (!response.equals("")) {
ui.showErrorDialog(response, "Can't load");
return false;
}
return true;
}
return false;
}
void try_play_from_args(String sf, String mid) {
if (!sf.equals("")) try_load_sf(new File(sf));
try_play_file(new File(mid));
}