-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gui.c
1071 lines (985 loc) · 23.9 KB
/
gui.c
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ncurses.h"
#include <math.h>
#include "lib/events/events.h"
#include "lib/chiptune/chiptune.h"
#include "fatfs/ff.h"
// TODO:
// FEATURES:
// * ` on a track, if on a note, goes to edit that note's instrument
// * Home/End - sends you to tracky songy = 0 type of thing, depending on tab
// * PgUp/PgDn - sends you up/down the list by 16 or so lines
// * shift+space -> play song from beginning
// * ctrl+space -> loop song?
// * shift+tab -> move backwards through tabs
// ALGORITHMS
// * make numtracks go up when adding a note to a track, not just when moving to a new one
// * efficiently clear out instrument column when changing instrument when next one has less length
#include "stuff.h"
// choose the way to write your scale here:
#ifdef GERMAN_KEYS
static const char * const notenames[] = {"C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "H-"};
#else
static const char * const notenames[] = {"C-", "C#", "D-", "Eb", "E-", "F-", "F#", "G-", "Ab", "A-", "Bb", "B-"};
#endif
uint8_t text_color;
static const char *keymap[2] = { // how to layout two octaves chromatically on the keyboard
"zsxdcvgbhnjm,l.;/", // first octave
"q2w3er5t6y7ui9o0p" // second octave
};
static const char *validcmds = "0bdfijlmtvw~+=";
#define SETLO(v,x) v = ((v) & 0xf0) | (x)
#define SETHI(v,x) v = ((v) & 0x0f) | ((x) << 4)
int songx, songy, songoffs;
u16 songlen=1;
int trackx=0, tracky=0, trackoffs=0;
int instrx=0, instry=0, instroffs=0;
int currtrack=1, currinstr=1;
int currtab=0;
int octave=4;
char cmd[16] = {0, 0}; // we do this to avoid undefined behavior in setcmd
char cmdinput[8];
u8 cmdinputpos;
u8 cmdinputnumeric;
enum {
MODE_NONE = 0,
MODE_EDIT = 1
};
int mode = MODE_NONE;
struct instrument iclip;
struct trackline tclip[256];
static void drawgui();
static void resetgui()
{
mode = MODE_NONE;
currtab = 0;
songx = 0;
songy = 0;
currtrack = 1;
tracky = 0;
trackx = 0;
currinstr = 1;
instry = 0;
instrx = 0;
}
void setalert(const char *alerto)
{
int i = print_at(0, LINES - 1, text_color, alerto);
char* alert = &vram[LINES - 1][0];
for(; i < 32; i++)
alert[i] = ' ';
}
static void setcmd(const char *cmdo)
{
if(!cmd[0]) // if cmd had nothing in it
{
if (cmd[1] != cmdo[1]) // check if the previous command was the desired command
{ // if not, then we need to remove previous input.
cmdinput[0] = 0; // kill previous input
cmdinputpos = 0;
}
strcpy(cmd, cmdo);
}
else if (cmd[0] != cmdo[0]) // command did not start with cmdo[0]
{
strcpy(cmd, cmdo);
cmdinput[0] = 0; // zero the input string, we just changed input.
cmdinputpos = 0;
}
else // cmd[0] was cmdo[0], so we want to remove command.
{
cmd[0] = 0;
return;
}
switch (cmd[0])
{
case 'b': // base filename
case 'f': // file to open
cmdinputnumeric = 0;
break;
case 't': // tracklength
case 's': // songspeed
cmdinputnumeric = 1;
break;
}
}
void evalcmd()
{
if (cmdinput[0])
{
int result;
switch (cmd[0])
{
case 'b': // basename of file
sprintf(filename, "%s.song", cmdinput);
break;
case 'f': // file to open
silence();
char newfile[13];
sprintf(newfile, "%s.song", cmdinput);
if (loadfile(newfile)) {
cmd[0] = 0;
resetgui();
redrawgui();
}
break;
case 't': // tracklength
if (1 == sscanf(cmdinput, "%d", &result))
{
if (result > 0 && result < 257)
{
if (realign_tracks(result))
{
setalert("lost some higher tracks due to memory constraints!");
if (currtrack*tracklen > NTRACKLINE)
currtrack = NTRACKLINE/tracklen;
}
drawgui();
}
else
setalert("need tracklength > 0 and < 257");
}
else
setalert("unknown input!!");
break;
case 's': // songspeed
if (1 == sscanf(cmdinput, "%d", &result))
{
if (result > 0 && result < 256)
songspeed = result;
else
setalert("need songspeed > 0 and < 256");
}
else
setalert("unknown input!!");
break;
}
}
cmd[0] = 0;
cmdinput[0] = 0;
cmdinputpos = 0;
}
static void listfiles(void)
{
DIR dir;
FRESULT res;
FILINFO fno;
res = f_opendir(&dir, "");
if (res != FR_OK)
return;
clear();
res = f_readdir(&dir, &fno);
int line = 6;
for (;;)
{
res = f_readdir(&dir, &fno);
// End of dir?
if (res != FR_OK || fno.fname[0] == 0)
break;
char* pos = strchr(fno.fname, '.');
if (pos == NULL) {
continue;
}
if (!strncmp(".SON", pos, 4) || !strncmp(".son", pos, 4))
{
print_at(2, line++, text_color, fno.fname);
}
}
print_at(2, line++, text_color, "-------------");
f_closedir(&dir);
}
int hexdigit(char c) {
if(c >= '0' && c <= '9') return c - '0';
if(c >= 'a' && c <= 'f') return c - 'a' + 10;
return -1;
}
int freqkey(int c) {
char *s;
int f = -1;
if(c == '-' /*|| c == KEY_DC*/) return 0;
if(c == '=') return 12 * 9 + 2; // Stop oscillator (in instruments)
if(c > 0 && c < 256) {
s = strchr(keymap[0], c);
if(s) {
f = (s - (keymap[0])) + octave * 12 + 1;
} else {
s = strchr(keymap[1], c);
if(s) {
f = (s - (keymap[1])) + octave * 12 + 12 + 1;
}
}
}
if(f > 12 * 9 + 1) return -1;
return f;
}
//void exitgui() {
// endwin();
//}
void initgui() {
initscr();
//noecho();
//keypad(stdscr, TRUE);
//raw();
redrawgui();
//atexit(exitgui);
}
static void hexstr(char* target, uint8_t value)
{
static const char hex[] = "0123456789ABCDEF";
target[0] = hex[value >> 4];
target[1] = hex[value & 0xF];
}
static void scrollbar(int x, int y, int height, int pos, int range)
{
int start = pos * height / range;
int end = start + height * height / range;
for (int i = 0; i < height; i++)
{
uint8_t* tgt = &vram_attr[y+i][x];
if (i >= start && i <= end)
*tgt = 6;
else
*tgt = 0;
}
}
static void drawsonged(int x, int y, int height) {
int i, j, k = 0;
char buf[6];
int showline;
if (playsong)
showline = (songpos - 1 + songlen) % songlen;
else
showline = songy;
if(showline < songoffs) songoffs = showline;
if(showline >= songoffs + height) songoffs = showline - height + 1;
buf[5] = 0;
buf[2] = ':';
for(i = 0; i < songlen; i++) {
if(i >= songoffs && i - songoffs < height) {
move(y + k, x + 0);
if(i == songy)
attrset(A_BOLD);
else
attrset(A_NORMAL);
hexstr(buf, i);
buf[3] = 0;
addstr(buf);
for(j = 0; j < 4; j++) {
addch(' ');
hexstr(buf, song[i].track[j]);
hexstr(buf + 3, song[i].transp[j]);
addstr(buf);
}
if(playsong && songpos == (i + 1))
*(uint16_t*)(&vram_attr[y+k][x]) = 0x0505;
k++;
}
}
// blank lines at song end
while (k < height) {
move(y + k, x + 0);
addstr(" ");
k++;
}
if (songlen > height) {
scrollbar(x-1, y, height, songoffs, songlen);
}
}
static void drawtracked(int x, int y, int height) {
int i, j;
char buf[6];
if (height > tracklen)
height = tracklen;
int position;
if (playtrack)
position = (trackpos - 1 + tracklen) % tracklen ;
else
position = tracky;
if(position < trackoffs) trackoffs = position;
if(position >= trackoffs + height) trackoffs = position - height + 1;
for(i = 0; i < tracklen; i++) {
if(i >= trackoffs && i - trackoffs < height) {
move(y + i - trackoffs, x + 0);
if(i == tracky)
attrset(A_BOLD);
else
attrset(A_NORMAL);
buf[2] = ':';
buf[3] = ' ';
buf[4] = 0;
hexstr(buf, i);
addstr(buf);
if(track(currtrack,i)->note) {
sprintf(buf, "%s%d ",
notenames[(track(currtrack,i)->note - 1) % 12],
(track(currtrack,i)->note - 1) / 12);
} else {
buf[0] = buf[1] = buf[2] = '-';
}
addstr(buf);
buf[2] = 0;
hexstr(buf, track(currtrack, i)->instr);
addstr(buf);
for(j = 0; j < 2; j++) {
buf[0] = ' ';
if(track(currtrack,i)->cmd[j]) {
buf[1] = track(currtrack,i)->cmd[j];
hexstr(buf + 2, track(currtrack,i)->param[j]);
} else {
buf[1] = buf[2] = buf[3] = '.';
buf[4] = 0;
}
addstr(buf);
}
if(playtrack && ((i + 1) % tracklen) == trackpos)
*(uint16_t*)(&vram_attr[y+i-trackoffs][x]) = 0x0505;
}
}
if (tracklen > height) {
scrollbar(x-1, y, height, trackoffs, tracklen);
}
}
static void drawinstred(int x, int y, int height) {
int i, j = 0;
char buf[8];
// TODO:
// what is modifying instry/instroffs to be -1?
// this should not be required!
if(instroffs <0) instroffs=0;
if(instry <0) instry=0;
// but for some reason somehow these guys get turned to -1 somewhere
if(instry >= instrument[currinstr].length) instry = instrument[currinstr].length - 1;
if(instry < instroffs) instroffs = instry;
if(instry >= instroffs + height) instroffs = instry - height + 1;
for(i = 0; i < instrument[currinstr].length; i++) {
if(i >= instroffs && i - instroffs < height) {
move(y + j, x + 0);
if(i == instry) attrset(A_BOLD);
sprintf(buf, "%02x: %c ", i, instrument[currinstr].line[i].cmd);
addstr(buf);
if(instrument[currinstr].line[i].cmd == '+' || instrument[currinstr].line[i].cmd == '=') {
if(instrument[currinstr].line[i].param > 83)
sprintf(buf, "---");
else if(instrument[currinstr].line[i].param) {
sprintf(buf, "%s%d",
notenames[(instrument[currinstr].line[i].param - 1) % 12],
(instrument[currinstr].line[i].param - 1) / 12);
} else {
buf[0] = buf[1] = buf[2] = '-';
buf[3] = 0;
}
} else {
hexstr(buf, instrument[currinstr].line[i].param);
buf[2] = '-';
buf[3] = 0;
}
addstr(buf);
sprintf(buf, " %c ", instrument[currinstr].line[i].cmd2);
addstr(buf);
if(instrument[currinstr].line[i].cmd2 == '+' || instrument[currinstr].line[i].cmd2 == '=') {
if(instrument[currinstr].line[i].param2 > 83)
sprintf(buf, "---");
else if(instrument[currinstr].line[i].param2) {
sprintf(buf, "%s%d",
notenames[(instrument[currinstr].line[i].param2 - 1) % 12],
(instrument[currinstr].line[i].param2 - 1) / 12);
} else {
buf[0] = buf[1] = buf[2] = '-';
buf[3] = 0;
}
} else {
hexstr(buf, instrument[currinstr].line[i].param2);
buf[2] = '-';
buf[3] = 0;
}
addstr(buf);
attrset(A_NORMAL);
j++;
}
}
while (j < height)
{
move(y + j, x + 0);
addstr(" ");
j++;
}
}
void handleinput() {
int c, x;
if ((c = getch()) != KEY_ERR)
{
if (cmd[0])
{
// numbers uppercase letters lowercase letters _ or -
if ( (c>=48 && c<=57) || (c>=65 && c<=90) || (c>=97 && c<=122) || (c == 95 || c == 45) )
{
if (cmdinputnumeric)
{
// only allow inputting numbers
if (c>=48 && c<=57)
{
if (cmdinputpos < 2)
{
cmdinput[cmdinputpos] = c;
cmdinput[++cmdinputpos] = 0;
}
else
{
cmdinput[cmdinputpos] = c;
cmdinput[cmdinputpos+1] = 0;
}
}
}
else
{
if (cmdinputpos < 7)
{
cmdinput[cmdinputpos] = c;
cmdinput[++cmdinputpos] = 0;
}
else
cmdinput[cmdinputpos] = c;
}
}
else switch (c)
{
case 8: // backspace
if (cmdinputpos > 0)
{
if (cmdinputnumeric)
{
if (cmdinputpos == 2 && cmdinput[cmdinputpos])
cmdinput[cmdinputpos] = 0;
else
cmdinput[--cmdinputpos] = 0;
}
else
{
if (cmdinputpos == 7 && cmdinput[cmdinputpos])
cmdinput[cmdinputpos] = 0;
else
cmdinput[--cmdinputpos] = 0;
}
}
break;
case '\n':
evalcmd();
break;
case 'T' - '@':
setcmd("tracklength");
break;
case 'F' - '@':
setcmd("base filename");
break;
case 'O' - '@':
setcmd("file to open");
listfiles();
break;
case 'S' - '@':
setcmd("songspeed");
break;
}
}
else
switch(c) {
case ' ':
if (playsong || playtrack)
{
// stop play
silence();
}
else
{
// start play
if(currtab == 1) {
startplaytrack(currtrack);
} else if (currtab == 0) {
startplaysong(songy);
} else {
startplaysong(0);
}
}
break;
case 10: // enter: toggle edit mode
case 13: // enter on other systems...
if (mode & MODE_EDIT)
{
mode -= MODE_EDIT; // lock it down
}
else
{
mode += MODE_EDIT; // allow editing
}
break;
case 'N' - '@':
case 'N':
case 9:
currtab++;
if (mode & MODE_EDIT)
currtab %= 3;
else
currtab %= 2; // this glosses over a bug
break;
case 'P' - '@':
case 'P':
currtab--;
if (mode & MODE_EDIT)
currtab %= 3;
else
currtab %= 2;
break;
case 'W' - '@':
if (mode & MODE_EDIT)
savefile(filename);
break;
case 'T' - '@':
if (mode & MODE_EDIT)
setcmd("tracklength");
break;
case 'F' - '@':
setcmd("base filename");
break;
case 'O' - '@':
setcmd("file to open");
listfiles();
break;
case 'S' - '@':
if (mode & MODE_EDIT)
setcmd("songspeed");
break;
case '<':
if(octave) octave--;
break;
case '>':
if(octave < 8) octave++;
break;
case '[':
if(currinstr > 1) {
currinstr--;
if (instrument[currinstr].length < instrument[currinstr+1].length)
redrawgui();
}
break;
case ']':
if(currinstr < NINST-1) {
currinstr++;
if (instrument[currinstr].length < instrument[currinstr-1].length)
redrawgui();
}
break;
case '{':
if(currtrack > 1) currtrack--;
break;
case '}':
if((currtrack+1)*tracklen <= NTRACKLINE && currtrack < 255) {
currtrack++;
// update the number of tracks -- TODO: probably should wait to put
// something in them, but nevertheless keep track of it.
if (numtracks < currtrack)
numtracks = currtrack;
} else {
setalert("can't fit more tracks in memory");
}
break;
case '`':
if(currtab == 0) {
int t = song[songy].track[songx / 4];
if(t) currtrack = t;
currtab = 1;
} else if(currtab == 1) {
currtab = 0;
}
break;
// case '#': // or '\\' ?
// optimize();
// break;
// case '%': // or '=' ?
// export();
// break;
case KEY_LEFT:
switch(currtab) {
case 0:
if(songx) songx--;
break;
case 1:
if(trackx) trackx--;
break;
case 2:
if((mode&MODE_EDIT) && instrx) instrx--;
break;
}
break;
case KEY_RIGHT:
switch(currtab) {
case 0:
if(songx < 15) songx++;
break;
case 1:
if(trackx < 8) trackx++;
break;
case 2:
if((mode&MODE_EDIT) && instrx < 5) instrx++;
break;
}
break;
case KEY_UP:
case KEY_PAGEUP:
{
int off = (c == KEY_PAGEUP) ? 16 : 1;
switch(currtab) {
case 0:
if(songy >= off) songy-=off;
break;
case 1:
if(tracky >= off) {
tracky-=off;
} else {
tracky = tracklen - 1;
}
break;
case 2:
if(instry >= off) instry-=off;
break;
}
break;
}
case KEY_DOWN:
case KEY_PAGEDOWN:
{
int off = (c == KEY_PAGEDOWN) ? 16 : 1;
switch(currtab) {
case 0:
if(songy < songlen - off) songy+=off;
break;
case 1:
if(tracky < tracklen - off) {
tracky+=off;
} else {
tracky = 0;
}
break;
case 2:
if(instry < instrument[currinstr].length - off) instry+=off;
break;
}
break;
}
case 'C': // copy
if (mode & MODE_EDIT)
{
if(currtab == 2) {
memcpy(&iclip, &instrument[currinstr], sizeof(struct instrument));
} else if(currtab == 1) {
memcpy(&tclip, &tracking[tracklen*currtrack], tracklen*sizeof(struct trackline));
}
}
break;
case 'V': // paste
if (mode & MODE_EDIT)
{
if(currtab == 2) {
memcpy(&instrument[currinstr], &iclip, sizeof(struct instrument));
} else if(currtab == 1) {
memcpy(&tracking[tracklen*currtrack], &tclip, tracklen*sizeof(struct trackline));
}
}
break;
case 8: // backspace
case 127: // delete
if(mode & MODE_EDIT) switch (currtab) {
case 0:
song[songy].track[songx/4] = 0;
song[songy].transp[songx/4] = 0;
break;
case 1:
memset(track(currtrack,tracky), 0, sizeof(struct trackline));
break;
case 2:
instrument[currinstr].line[instry].cmd = '0';
instrument[currinstr].line[instry].param = 0;
}
break;
default:
if(mode & MODE_EDIT) {
x = hexdigit(c);
// helpful for debugging:
// sprintf(alert, "%d %x %c", (int) c, x, c);
if(x >= 0) {
if(currtab == 2
&& instrx > 0
&& instrument[currinstr].line[instry].cmd != '+'
&& instrument[currinstr].line[instry].cmd != '=') {
switch(instrx) {
case 1: SETHI(instrument[currinstr].line[instry].param, x); break;
case 2: SETLO(instrument[currinstr].line[instry].param, x); break;
case 4: SETHI(instrument[currinstr].line[instry].param2, x); break;
case 5: SETLO(instrument[currinstr].line[instry].param2, x); break;
}
}
if(currtab == 1 && trackx > 0) {
switch(trackx) {
case 1: SETHI(track(currtrack,tracky)->instr, x); break;
case 2: SETLO(track(currtrack,tracky)->instr, x); break;
case 4: if(track(currtrack,tracky)->cmd[0])
SETHI(track(currtrack,tracky)->param[0], x); break;
case 5: if(track(currtrack,tracky)->cmd[0])
SETLO(track(currtrack,tracky)->param[0], x); break;
case 7: if(track(currtrack,tracky)->cmd[1])
SETHI(track(currtrack,tracky)->param[1], x); break;
case 8: if(track(currtrack,tracky)->cmd[1])
SETLO(track(currtrack,tracky)->param[1], x); break;
}
}
if(currtab == 0) {
switch(songx & 3) {
case 0: SETHI(song[songy].track[songx / 4], x); break;
case 1: SETLO(song[songy].track[songx / 4], x); break;
case 2: SETHI(song[songy].transp[songx / 4], x); break;
case 3: SETLO(song[songy].transp[songx / 4], x); break;
}
}
}
x = freqkey(c);
if(x >= 0) {
if(currtab == 2
&& instrx && instrx < 3
&& (instrument[currinstr].line[instry].cmd == '+' || instrument[currinstr].line[instry].cmd == '=')) {
instrument[currinstr].line[instry].param = x;
}
if(currtab == 2
&& instrx > 3
&& (instrument[currinstr].line[instry].cmd2 == '+' || instrument[currinstr].line[instry].cmd2 == '=')) {
instrument[currinstr].line[instry].param2 = x;
}
if(currtab == 1 && !trackx) {
track(currtrack,tracky)->note = x;
if(x) {
track(currtrack,tracky)->instr = currinstr;
} else {
track(currtrack,tracky)->instr = 0;
}
tracky++;
tracky %= tracklen;
if(x) iedplonk(x, currinstr);
}
}
if(currtab == 2 && instrx == 0) {
if(strchr(validcmds, c)) {
instrument[currinstr].line[instry].cmd = c;
}
}
if(currtab == 2 && instrx == 3) {
if(strchr(validcmds, c)) {
instrument[currinstr].line[instry].cmd2 = c;
}
}
if(currtab == 1 && (trackx == 3 || trackx == 6 || trackx == 9)) {
if(strchr(validcmds, c)) {
if(c == '.' || c == '0') c = 0;
track(currtrack,tracky)->cmd[(trackx - 3) / 3] = c;
}
}
if(c == 'A') {
if(currtab == 2) {
struct instrument *in = &instrument[currinstr];
if(in->length < NINSTLINE) {
memmove(&in->line[instry + 2], &in->line[instry + 1], sizeof(struct instrline) * (in->length - instry - 1));
instry++;
in->length++;
in->line[instry].cmd = '0';
in->line[instry].param = 0;
}
} else if(currtab == 0) {
if(songlen < 256) {
memmove(&song[songy + 2], &song[songy + 1], sizeof(struct songline) * (songlen - songy - 1));
songy++;
songlen++;
memset(&song[songy], 0, sizeof(struct songline));
}
}
} else if(c == 'I') {
if(currtab == 2) {
struct instrument *in = &instrument[currinstr];
if(in->length < NINSTLINE) {
memmove(&in->line[instry + 1], &in->line[instry + 0], sizeof(struct instrline) * (in->length - instry));
in->length++;
in->line[instry].cmd = '0';
in->line[instry].param = 0;
}
} else if(currtab == 0) {
if(songlen < 256) {
memmove(&song[songy + 1], &song[songy + 0], sizeof(struct songline) * (songlen - songy));
songlen++;
memset(&song[songy], 0, sizeof(struct songline));
}
}
} else if(c == 'D') {
if(currtab == 2) {
struct instrument *in = &instrument[currinstr];
if(in->length > 1) {
memmove(&in->line[instry + 0], &in->line[instry + 1], sizeof(struct instrline) * (in->length - instry - 1));
in->length--;
if(instry >= in->length) instry = in->length - 1;
redrawgui();
}
} else if(currtab == 0) {
if(songlen > 1) {
memmove(&song[songy + 0], &song[songy + 1], sizeof(struct songline) * (songlen - songy - 1));
songlen--;
if(songy >= songlen) songy = songlen - 1;
redrawgui();
}
}
}
}
else { //
x = freqkey(c);
if(x > 0) {
iedplonk(x, currinstr);
}
}
break;
}
}
}
static void drawgui() {
char buf[32];
int songcols[] = {0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19, 21, 22};
int trackcols[] = {0, 4, 5, 7, 8, 9, 11, 12, 13};
int instrcols[] = {0, 2, 3, 6, 8, 9};
if (mode & MODE_EDIT)
{
mvaddstr(0, COLUMNS-22, "lock");
}
else
{
mvaddstr(0, COLUMNS-22, "edit");
}
if (playsong||playtrack)
{
mvaddstr(1, COLUMNS-22, "stop");
}
else
{
mvaddstr(1, COLUMNS-22, "play");
}
mvaddstr(2, 14, " ");
mvaddstr(2, 14, filename);
text_color = 3;
if (mode & MODE_EDIT)
text_color = 3;
else
text_color = A_REVERSE;
sprintf(buf, "^S)ongspeed: %d ", songspeed);
mvaddstr(3, 1, buf);
sprintf(buf, "^T)racklength: %d ", tracklen);
mvaddstr(3, 30, buf);
mvaddstr(3, COLUMNS - 23, "^W)rite");
text_color = 0;
if (cmd[0] != 'f') {
drawsonged(2, 6, LINES - 8);
}
buf[2] = 0;
hexstr(buf, currtrack);
mvaddstr(5, 37, buf);
drawtracked(31, 6, LINES - 8);
hexstr(buf, currinstr);
mvaddstr(5, 58, buf);
drawinstred(50, 6, LINES - 12);
hexstr(buf, octave);
mvaddstr(5, COLUMNS - 7, buf);
// Draw channel volumes
int p = 5;
for (int o = 0; o < 4; o++)
{
int v = osc[o].volume >> 4;
int k;
for (k = 0; k < v; k++)
{
vram_attr[6 + k][p] = 3 + osc[o].waveform;
}
for (; k < 16; k++)
{
vram_attr[6 + k][p] = 0;
}
p += 6;
}
// Update cursor position
int cx, cy;
if (cmd[0])
{
int off = sprintf(buf, "new %s? %s", cmd, cmdinput);
setalert(buf);
cy = LINES - 1;
cx = off;
} else {
switch(currtab) {
case 0:
default:
cy = 6 + songy - songoffs;
cx = 0 + 6 + songcols[songx];
break;
case 1:
cy = 6 +tracky - trackoffs;
cx = 29 + 6 + trackcols[trackx];
break;