-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource.cpp
More file actions
1507 lines (1238 loc) · 42.2 KB
/
Copy pathsource.cpp
File metadata and controls
1507 lines (1238 loc) · 42.2 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
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
glitchds source
// Prepare audio files using:
// SOX: sox -V s17.wav -c 1 -2 -r 22050 snd17.raw
// Includes
#include <PA9.h>
#include <fat.h>
#include <sys/dir.h>
#include "gfx/all_gfx.h"
#include "gfx/all_gfx.c"
// Defines
#define GRID_WIDTH 15
#define GRID_HEIGHT 11
#define FREQ_MOD_FADER_HEIGHT 120
#define FREQ_MOD_FADER_CENTER 40
// Modes
#define NUMBER_OF_MODES 7
#define DRAW_CA 1
#define SET_TRIGGERS 2
#define PALLET_MODE 3
#define SET_CLOCK_SOURCE 4
#define FREQ_MOD_MODE 5
#define DISTORTION_MODE 6
#define SAVE_LOAD_MODE 7
// Sequencer Status
#define STOPPED 0
#define CLOCKED 1
#define MANUAL 2
// strumming positions
#define STRUM_TOP_HALF 0
#define STRUM_BOTTOM_HALF 1
#define FREQ_MOD_MULTIPLIER 1000
#define DISTORTION_RANGE 300
#define NUMBER_OF_SOUNDS 30
// Function prototypes
void choose_swatch(int swatch_number);
void draw8bitRectEx(int x, int y, int width, int height, bool screen, int colour);
void draw_manual_tap_control();
void draw_bpm();
void draw_screen_ca_mode();
void draw_screen_set_clock_source_mode();
void initialize_sounds();
void load_sound_from_file(int slot_id, char *filename);
int find_position_of_filename_in_directory(char *filename);
void update_screen_pallet_mode();
SoundInfo sound_slot[32];
char *filenames[500];
int number_of_files = 0;
int file_offset = 0;
// int slot_assignments[6]; // slot_assignments[slot] = sound_id (for saving snapshots)
char swatch_0_file_assignment[100];
char swatch_1_file_assignment[100];
char swatch_2_file_assignment[100];
char swatch_3_file_assignment[100];
char swatch_4_file_assignment[100];
char swatch_5_file_assignment[100];
int m;
int n;
int neighbors;
int sequencer_step = 0;
int step_rate = 6;
int redraw_flag = 0;
int selected_swatch = 1;
int sequencer_status = STOPPED;
int strum_quadrant;
int distortion_level = 0;
int distortion = 0;
int distortion_y = 0;
int file_id = 0;
int keyboard_visible = 0;
int channel_frequencies[7];
int bpm = 90;
int freq_mod[6][32];
int triggers[GRID_WIDTH][GRID_HEIGHT];
bool now[GRID_WIDTH][GRID_HEIGHT], next[GRID_WIDTH][GRID_HEIGHT];
bool seed[GRID_WIDTH][GRID_HEIGHT];
#define pixel_width 16
void clear_ca(bool mat[][GRID_HEIGHT]) // Sets matrix to all dead
{
int x;
int y;
for (y = 0; y < GRID_HEIGHT; y++)
{
for (x = 0; x < GRID_WIDTH; x++)
mat[x][y] = 0;
}
}
void clear_triggers(int triggers[][GRID_HEIGHT]) // Sets matrix to all dead
{
int x;
int y;
for (y = 0; y < GRID_HEIGHT; y++)
{
for (x = 0; x < GRID_WIDTH; x++)
triggers[x][y] = 0;
}
}
void draw8bitRect(int x, int y, int width, int height, bool screen, int colour)
{
x = x * pixel_width;
y = y * pixel_width;
draw8bitRectEx(x, y, width, height, screen, colour);
}
void draw8bitRectEx(int x, int y, int width, int height, bool screen, int colour)
{
// Draw top line
u16 lastX = x + width;
u16 i;
for (i = x; i < lastX; i++)
{
PA_Put8bitPixel(screen, i, y, colour);
}
// Calculate DMA copy values - we need to deal with even values only
u16 dmaX = (x + 1) & ~1;
s32 dmaWidth = width;
bool drawRight = false;
// Did we convert to an even x value?
if (dmaX != x)
{
// Reduce the width to compensate
dmaWidth--;
// Remember that we need to draw an extra pixel
drawRight = true;
}
// Pre-emptive bitshift to save calculations
dmaX = dmaX >> 1;
dmaWidth = (dmaWidth & 1 ? dmaWidth - 1 : dmaWidth) >> 1;
// Calculate last line to draw
u16 lastY = y + height;
lastX = x + width - 1;
// Precalculate line values for loop
u16 *line0 = PA_DrawBg[screen] + dmaX + (y << 7);
u16 *linei = line0 + (1 << 7);
// Loop through all lines
for (i = y + 1; i < lastY; i++)
{
// Perform DMA copy
if (dmaWidth > 0)
DMA_Copy(line0, linei, dmaWidth, DMA_16NOW);
// Fill left gap
if (x & 1)
PA_Put8bitPixel(screen, x, i, colour);
// Fill right gap
if ((width & 1) || (drawRight))
PA_Put8bitPixel(screen, lastX, i, colour);
// Move to next line
linei += (1 << 7);
}
}
void drawBlock(int x, int y, bool screen, int colour)
{
draw8bitRect(x, y, pixel_width, pixel_width, screen, colour);
}
void calculate_ca(bool mata[][GRID_HEIGHT], bool matb[][GRID_HEIGHT])
{
int x;
int y;
for (y = 1; y < GRID_HEIGHT - 1; y++)
{
for (x = 1; x < GRID_WIDTH - 1; x++)
{
neighbors = 0;
// Begin counting number of neighbors:
// Begin counting number of neighbors:
if (mata[x - 1][y - 1] == 1)
neighbors += 1;
if (mata[x - 1][y] == 1)
neighbors += 1;
if (mata[x - 1][y + 1] == 1)
neighbors += 1;
if (mata[x][y - 1] == 1)
neighbors += 1;
if (mata[x][y + 1] == 1)
neighbors += 1;
if (mata[x + 1][y - 1] == 1)
neighbors += 1;
if (mata[x + 1][y] == 1)
neighbors += 1;
if (mata[x + 1][y + 1] == 1)
neighbors += 1;
// Apply rules to the cell:
if (mata[x][y] == 1 && neighbors < 2)
matb[x][y] = 0;
else if (mata[x][y] == 1 && neighbors > 3)
matb[x][y] = 0;
else if (mata[x][y] == 1 && (neighbors == 2 || neighbors == 3))
matb[x][y] = 1;
else if (mata[x][y] == 0 && neighbors == 3)
matb[x][y] = 1;
}
}
}
void swap_ca(bool mata[][GRID_HEIGHT], bool matb[][GRID_HEIGHT]) // Replaces first matrix with second
{
int x;
int y;
for (y = 0; y < GRID_HEIGHT; y++)
{
for (x = 0; x < GRID_WIDTH; x++)
mata[x][y] = matb[x][y];
}
}
void seq_play_audio(bool mata[][GRID_HEIGHT], bool matb[][GRID_HEIGHT]) // Replaces first matrix with second
{
// Play sounds
int x;
int y;
int channel_number;
int sound_id;
// int step_increase = 0;
// int step_decrease = 0;
for (y = 0; y < GRID_HEIGHT; y++)
{
for (x = 0; x < GRID_WIDTH; x++)
{
if ((!mata[x][y]) && matb[x][y])
{
if ((triggers[x][y] > 0) && (triggers[x][y] < 7))
{
sound_id = triggers[x][y] - 1;
sound_slot[sound_id].rate = (s32)22050 + ((freq_mod[sound_id][sequencer_step] - FREQ_MOD_FADER_CENTER) * FREQ_MOD_MULTIPLIER);
channel_number = AS_SoundPlay(sound_slot[sound_id]);
channel_frequencies[channel_number] = sound_slot[sound_id].rate;
}
}
}
}
}
void draw_ca(bool mat[][GRID_HEIGHT], bool screen) // Prints matrix to screen
{
int x;
int y;
PA_Clear8bitBg(screen);
for (y = 1; y < GRID_HEIGHT; y++)
{
for (x = 1; x < GRID_WIDTH; x++)
{
if (mat[x][y])
{
// draw8bitRect(x, y, pixel_width, pixel_width, screen, 1); // int x, int y, int w, int h, bool screen, int color
drawBlock(x, y, screen, 1);
}
}
}
}
void draw_matrix(int mat[][GRID_HEIGHT], bool screen)
{
int x;
int y;
for (y = 1; y < GRID_HEIGHT; y++)
{
for (x = 1; x < GRID_WIDTH; x++)
{
if (mat[x][y])
draw8bitRect(x, y, pixel_width, pixel_width, screen, mat[x][y]); // int x, int y, int w, int h, bool screen, int color
}
}
}
void draw_swatches()
{
draw8bitRect(7, 0, pixel_width - 2, pixel_width - 2, 0, 1);
draw8bitRect(8, 0, pixel_width - 2, pixel_width - 2, 0, 2);
draw8bitRect(9, 0, pixel_width - 2, pixel_width - 2, 0, 3);
draw8bitRect(10, 0, pixel_width - 2, pixel_width - 2, 0, 4);
draw8bitRect(11, 0, pixel_width - 2, pixel_width - 2, 0, 5);
draw8bitRect(12, 0, pixel_width - 2, pixel_width - 2, 0, 6);
}
int poll_swatches()
{
int x;
int y;
if (Stylus.Newpress)
{
x = Stylus.X;
y = Stylus.Y;
if (y < 17)
{
if (x >= 112 && x <= 127)
selected_swatch = 1;
if (x >= 128 && x <= 143)
selected_swatch = 2;
if (x >= 144 && x <= 159)
selected_swatch = 3;
if (x >= 160 && x <= 175)
selected_swatch = 4;
if (x >= 176 && x <= 191)
selected_swatch = 5;
if (x >= 191 && x <= 208)
selected_swatch = 6;
draw_swatches();
choose_swatch(selected_swatch);
return (1);
}
}
if (Pad.Newpress.Left)
{
selected_swatch--;
if (selected_swatch < 1)
selected_swatch = 6;
draw_swatches();
choose_swatch(selected_swatch);
return (1);
}
if (Pad.Newpress.Right)
{
selected_swatch++;
if (selected_swatch > 6)
selected_swatch = 1;
draw_swatches();
choose_swatch(selected_swatch);
return (1);
}
return (0);
}
void choose_swatch(int swatch_number)
{
switch (swatch_number)
{
case 1:
draw8bitRect(7, 0, pixel_width - 2, pixel_width - 2, 0, 0); // clear out old swatch
draw8bitRect(7, 0, pixel_width - 5, pixel_width - 5, 0, 1); // draw smaller version
break;
case 2:
draw8bitRect(8, 0, pixel_width - 2, pixel_width - 2, 0, 0); // clear out old swatch
draw8bitRect(8, 0, pixel_width - 5, pixel_width - 5, 0, 2); // draw smaller version
break;
case 3:
draw8bitRect(9, 0, pixel_width - 2, pixel_width - 2, 0, 0); // clear out old swatch
draw8bitRect(9, 0, pixel_width - 5, pixel_width - 5, 0, 3); // draw smaller version
break;
case 4:
draw8bitRect(10, 0, pixel_width - 2, pixel_width - 2, 0, 0); // clear out old swatch
draw8bitRect(10, 0, pixel_width - 5, pixel_width - 5, 0, 4); // draw smaller version
break;
case 5:
draw8bitRect(11, 0, pixel_width - 2, pixel_width - 2, 0, 0); // clear out old swatch
draw8bitRect(11, 0, pixel_width - 5, pixel_width - 5, 0, 5); // draw smaller version
break;
case 6:
draw8bitRect(12, 0, pixel_width - 2, pixel_width - 2, 0, 0); // clear out old swatch
draw8bitRect(12, 0, pixel_width - 5, pixel_width - 5, 0, 6); // draw smaller version
break;
}
}
void draw_screen_ca_mode()
{
PA_8bitText(0, 3, 3, 255, 40, "sequencer control - press START to toggle on/off", 1, 1, 0, 100);
}
void draw_screen_set_clock_source_mode()
{
if (sequencer_status == MANUAL)
{
draw_manual_tap_control();
}
else
{
draw_bpm();
}
}
void draw_bpm()
{
char buffer[10];
// PA_8bitText (u8 screen, s16 basex, s16 basey, s16 maxx, s16 maxy, char *text, u8 color, u8 size, u8 transp, s32 limit)
// example: PA_8bitText(0, 10, 20, 255, 40, text, 1, 3, 0, 100);
PA_Clear8bitBg(0);
PA_8bitText(0, 3, 3, 255, 40, "clock control:BPM - press Y to switch to STRUM", 1, 1, 0, 100);
sprintf(buffer, "%d BPM", bpm);
PA_8bitText(0, 90, 80, 255, 40, buffer, 1, 4, 0, 130);
PA_8bitText(0, 30, 105, 255, 40, "Use up/down keypad for +/- 1 BPM", 1, 2, 0, 130);
PA_8bitText(0, 25, 120, 255, 40, "Use right/left keypad for +/- 10 BPM", 1, 2, 0, 130);
PA_8bitText(0, 65, 180, 255, 40, "BPM is not 100% accurate", 1, 1, 0, 130);
}
void draw_manual_tap_control()
{
PA_Clear8bitBg(0);
PA_8bitText(0, 3, 3, 255, 40, "clock control:STRUM - press Y to switch to BPM", 1, 1, 0, 100);
draw8bitRectEx(0, 95, 255, 2, 0, 2); // x,y,w,h,screen,color
PA_8bitText(0, 60, 180, 255, 40, "Press left keypad to rewind", 1, 1, 0, 130);
}
// Draw the entire freq_mod page
void draw_freq_mod_faders()
{
int i;
PA_Clear8bitBg(0);
PA_8bitText(0, 3, 3, 255, 40, "frequency modulation", 1, 1, 0, 100);
PA_8bitText(0, 45, 140, 255, 40, "Press Y to reset modulation", 1, 2, 0, 130);
draw_swatches();
choose_swatch(selected_swatch);
for (i = 0; i < 32; i++)
{
draw8bitRectEx(i * 8, FREQ_MOD_FADER_HEIGHT - freq_mod[selected_swatch - 1][i], 7, freq_mod[selected_swatch - 1][i], 0, selected_swatch);
}
}
void draw_freq_mod_fader(int i)
{
draw8bitRectEx(i * 8, 40, 7, 100, 0, 0);
draw8bitRectEx(i * 8, FREQ_MOD_FADER_HEIGHT - freq_mod[selected_swatch - 1][i], 7, freq_mod[selected_swatch - 1][i], 0, selected_swatch);
}
void draw_freq_mod_position()
{
int old_sequencer_step = sequencer_step - 1;
if (old_sequencer_step == -1)
old_sequencer_step = 31;
draw8bitRectEx(old_sequencer_step * 8, FREQ_MOD_FADER_HEIGHT + 2, 7, 7, 0, 0); // clear out old position indicator
draw8bitRectEx(sequencer_step * 8, FREQ_MOD_FADER_HEIGHT + 2, 7, 7, 0, selected_swatch); // draw little position indicator
}
void draw_sequencer_step_position()
{
int old_sequencer_step = sequencer_step - 1;
if (old_sequencer_step == -1)
old_sequencer_step = 31;
draw8bitRectEx(old_sequencer_step * 8, 183, 7, 7, 1, 0); // clear out old position indicator
draw8bitRectEx(sequencer_step * 8, 183, 7, 7, 1, 1); // draw little position indicator
}
void draw_screen_trigger_mode()
{
PA_Clear8bitBg(0);
PA_8bitText(0, 3, 3, 255, 40, "trigger configuration", 1, 1, 0, 100);
PA_8bitText(0, 65, 180, 255, 40, "Press Y to reset triggers", 1, 1, 0, 130);
draw_matrix(triggers, 0);
draw_swatches();
choose_swatch(selected_swatch);
}
void update_screen_pallet_mode()
{
int i;
int y_pos = 0;
PA_Clear8bitBg(0);
PA_8bitText(0, 3, 3, 255, 40, "choose sounds", 1, 1, 0, 100);
PA_8bitText(0, 35, 180, 255, 40, "Press up/down to select sound, A to load", 1, 1, 0, 130);
draw_swatches();
choose_swatch(selected_swatch);
for (i = file_offset; i < (file_offset + 9); i++)
{
if ((y_pos < 4) && (i >= 0) && (i <= number_of_files))
{
// PA_8bitText (u8 screen, s16 basex, s16 basey, s16 maxx, s16 maxy, char *text, u8 color, u8 size, u8 transp, s32 limit)
PA_8bitText(0, 3, (y_pos * 12) + 35, 255, 40, filenames[i], 1, 0, 0, 100);
}
if (y_pos == 4)
{
PA_8bitText(0, 3, 90, 255, 40, filenames[i], 1, 3, 0, 100);
}
if ((y_pos > 4) && (i >= 0) && (i <= number_of_files))
{
// PA_8bitText (u8 screen, s16 basex, s16 basey, s16 maxx, s16 maxy, char *text, u8 color, u8 size, u8 transp, s32 limit)
PA_8bitText(0, 3, (y_pos * 12) + 55, 255, 40, filenames[i], 1, 0, 0, 100);
}
y_pos++;
}
}
void draw_screen_pallet_mode()
{
int i;
int y_pos = 0;
PA_Clear8bitBg(0);
PA_8bitText(0, 3, 3, 255, 40, "choose sounds", 1, 1, 0, 100);
PA_8bitText(0, 35, 180, 255, 40, "Press up/down to select sound, A to load", 1, 1, 0, 130);
draw_swatches();
choose_swatch(selected_swatch);
if (selected_swatch == 1)
file_offset = find_position_of_filename_in_directory(swatch_0_file_assignment) - 4;
if (selected_swatch == 2)
file_offset = find_position_of_filename_in_directory(swatch_1_file_assignment) - 4;
if (selected_swatch == 3)
file_offset = find_position_of_filename_in_directory(swatch_2_file_assignment) - 4;
if (selected_swatch == 4)
file_offset = find_position_of_filename_in_directory(swatch_3_file_assignment) - 4;
if (selected_swatch == 5)
file_offset = find_position_of_filename_in_directory(swatch_4_file_assignment) - 4;
if (selected_swatch == 6)
file_offset = find_position_of_filename_in_directory(swatch_5_file_assignment) - 4;
for (i = file_offset; i < (file_offset + 9); i++)
{
if ((y_pos < 4) && (i >= 0) && (i <= number_of_files))
{
// PA_8bitText (u8 screen, s16 basex, s16 basey, s16 maxx, s16 maxy, char *text, u8 color, u8 size, u8 transp, s32 limit)
PA_8bitText(0, 3, (y_pos * 12) + 35, 255, 40, filenames[i], 1, 0, 0, 100);
}
if (y_pos == 4)
{
PA_8bitText(0, 3, 90, 255, 40, filenames[i], 1, 3, 0, 100);
}
if ((y_pos > 4) && (i >= 0) && (i <= number_of_files))
{
// PA_8bitText (u8 screen, s16 basex, s16 basey, s16 maxx, s16 maxy, char *text, u8 color, u8 size, u8 transp, s32 limit)
PA_8bitText(0, 3, (y_pos * 12) + 55, 255, 40, filenames[i], 1, 0, 0, 100);
}
y_pos++;
}
}
void get_sound_file_directory_listing()
{
int file_number = 0;
struct stat st;
char filename[256]; // to hold a full filename and string terminator
DIR_ITER *dir = diropen("/glitchDS/sounds");
if (dir == NULL)
{
PA_OutputText(0, 2, 2, "Unable to open the directory.");
PA_OutputText(0, 2, 3, "Please make sure that /glitchDS/sounds exists");
}
else
{
dirnext(dir, filename, &st); // eat up .
dirnext(dir, filename, &st); // eat up ..
while (dirnext(dir, filename, &st) == 0)
{
filenames[file_number] = (char *)malloc(strlen(filename) + 1);
strcpy(filenames[file_number], filename);
file_number++;
}
}
number_of_files = file_number - 1;
}
void draw_screen_save_load_mode()
{
char buffer[50];
// PA_8bitText (u8 screen, s16 basex, s16 basey, s16 maxx, s16 maxy, char *text, u8 color, u8 size, u8 transp, s32 limit)
// example: PA_8bitText(0, 10, 20, 255, 40, text, 1, 3, 0, 100);
sprintf(buffer, "Slot %d", file_id);
PA_Clear8bitBg(0);
PA_8bitText(0, 3, 3, 255, 40, "save/load", 1, 1, 0, 100);
PA_8bitText(0, 40, 80, 255, 40, buffer, 1, 4, 0, 130);
PA_8bitText(0, 50, 105, 255, 40, "Press A to save", 1, 2, 0, 130);
PA_8bitText(0, 50, 120, 255, 40, "Press B to load", 1, 2, 0, 130);
PA_8bitText(0, 55, 180, 255, 40, "Press up/down to select slot", 1, 1, 0, 130);
}
void draw_screen_distortion_mode()
{
PA_Clear8bitBg(0);
PA_8bitText(0, 3, 3, 255, 40, "distortion settings", 1, 1, 0, 100);
if ((distortion_y > 20) && (distortion_y < 185))
draw8bitRectEx(0, distortion_y, 255, 2, 0, 2);
}
void init_freq_mod_array()
{
int i, j;
for (j = 0; j < 6; j++)
{
for (i = 0; i < 32; i++)
{
freq_mod[j][i] = FREQ_MOD_FADER_CENTER; // FREQ_MOD_FADER_CENTER is considered the "0" point, which is arbitrary
}
}
}
void reset_freq_mod()
{
int i;
for (i = 0; i < 32; i++)
{
freq_mod[selected_swatch - 1][i] = FREQ_MOD_FADER_CENTER; // FREQ_MOD_FADER_CENTER is considered the "0" point, which is arbitrary
}
}
void next_state()
{
sequencer_step++;
if (sequencer_step == 32)
{
sequencer_step = 0;
// reset automaton
seq_play_audio(now, seed);
swap_ca(now, seed);
}
else
{
clear_ca(next);
calculate_ca(now, next);
seq_play_audio(now, next);
swap_ca(now, next);
}
}
void stop_sequencer()
{
sequencer_step = 0;
sequencer_status = STOPPED;
PA_VBLCounterPause(0);
PA_VBLCounterPause(1);
}
void start_sequencer()
{
sequencer_status = CLOCKED;
seq_play_audio(now, seed);
swap_ca(now, seed);
PA_VBLCounterStart(1);
}
void do_interrupt()
{
if (sequencer_status == CLOCKED)
redraw_flag = 1;
}
int main(int argc, char **argv)
{
bpm = 90;
char filename[30];
char large_buffer[100];
int key_hold_time = 0;
int i;
// PALib Initialization
PA_Init();
PA_InitVBL();
PA_SetBgPalCol(0, 1, PA_RGB(31, 31, 31));
PA_SetBgPalCol(0, 2, PA_RGB(0, 0, 25));
PA_SetBgPalCol(0, 3, PA_RGB(30, 5, 10));
PA_SetBgPalCol(0, 4, PA_RGB(15, 0, 31));
PA_SetBgPalCol(0, 5, PA_RGB(0, 15, 15));
PA_SetBgPalCol(0, 6, PA_RGB(15, 15, 0));
PA_SetBgPalCol(1, 1, PA_RGB(31, 31, 31));
PA_Init8bitBg(0, 3);
PA_Init8bitBg(1, 3);
PA_InitText(1, 0);
PA_InitText(0, 0);
PA_SetTextCol(1, 31, 31, 31);
PA_SetTextCol(0, 0, 0, 31);
int channel;
// int slot_id;
int stylus_x = 0;
int stylus_y = 0;
PA_8bitText(0, 3, 3, 255, 40, "Loading...", 1, 1, 0, 100); // PA_OutputText(bool screen, u16 x, u16 y, const char *text, arguments...)
PA_WaitForVBL();
PA_WaitForVBL();
PA_WaitForVBL(); // wait a few VBLs
fatInitDefault(); // Initialise fat library
// Init AS_Lib for normal sound playback only
// PA_InitASLibForSounds(AS_MODE_SURROUND | AS_MODE_16CH);
// AS_SetDefaultSettings(AS_PCM_16BIT, 22050, AS_NO_DELAY);
PA_InitSound();
get_sound_file_directory_listing();
initialize_sounds();
clear_ca(now);
clear_ca(seed);
clear_triggers(triggers);
int old_stylus_x = 10;
int old_stylus_y = 10;
int old_bpm = 0;
int drew_freq_mod = 0;
int mode = DRAW_CA;
// 15 x 11
triggers[7][7] = 1;
triggers[7][9] = 1;
triggers[4][4] = 1;
triggers[8][8] = 2;
triggers[3][3] = 2;
triggers[8][7] = 4;
triggers[6][6] = 5;
triggers[3][3] = 6;
triggers[6][4] = 4;
triggers[5][6] = 3;
triggers[7][8] = 3;
// Set interrupt
TIMER_CR(1) = TIMER_ENABLE | TIMER_DIV_1024 | TIMER_IRQ_REQ;
TIMER_DATA(1) = (int)(-(0x2000000 >> 10) / (float)((bpm * 4.0) / (float)60));
irqSet(IRQ_TIMER1, do_interrupt);
// Initialize frequency mod
init_freq_mod_array();
// PA_8bitText (u8 screen, s16 basex, s16 basey, s16 maxx, s16 maxy, char *text, u8 color, u8 size, u8 transp, s32 limit)
// example: PA_8bitText(0, 10, 20, 255, 40, text, 1, 3, 0, 100);
PA_Clear8bitBg(0);
PA_Clear8bitBg(1);
draw_screen_ca_mode();
// Infinite loop to keep the program
while (1)
{
// :: Switch Modes
if (Pad.Newpress.R)
{
mode++;
if (mode > NUMBER_OF_MODES)
mode = 1;
}
if (Pad.Newpress.L)
{
mode--;
if (mode < 1)
mode = NUMBER_OF_MODES;
}
if (Pad.Newpress.L || Pad.Newpress.R)
{
PA_Clear8bitBg(0);
// PA_Clear8bitBg(1);
switch (mode)
{
case DRAW_CA:
draw_ca(seed, 0);
draw_screen_ca_mode(); // was draw_sequencer_control
break;
case SET_TRIGGERS:
draw_screen_trigger_mode();
break;
case PALLET_MODE:
draw_screen_pallet_mode();
break;
case SET_CLOCK_SOURCE:
draw_screen_set_clock_source_mode();
break;
case FREQ_MOD_MODE:
draw_freq_mod_faders();
draw_freq_mod_position();
break;
case DISTORTION_MODE:
draw_screen_distortion_mode();
break;
case SAVE_LOAD_MODE:
draw_screen_save_load_mode();
break;
}
}
// :: Mode Actions ::
if (mode == DRAW_CA)
{
if (Pad.Newpress.Y)
{
PA_Clear8bitBg(0);
clear_ca(seed);
draw_ca(seed, 0);
draw_screen_ca_mode(); // was draw_sequencer_control
}
if (Stylus.Held)
{
stylus_x = Stylus.X / pixel_width;
stylus_y = Stylus.Y / pixel_width;
if ((stylus_x > 0) && (stylus_x < GRID_WIDTH) && (stylus_y > 0) && (stylus_y < GRID_HEIGHT))
{
if ((stylus_x != old_stylus_x) || (stylus_y != old_stylus_y) || Stylus.Newpress)
{
if (seed[stylus_x][stylus_y] == 0)
{
seed[stylus_x][stylus_y] = 1;
draw8bitRect(stylus_x, stylus_y, pixel_width, pixel_width, 0, 1); // int x, int y, int w, int h, bool screen, int color
}
else
{
seed[stylus_x][stylus_y] = 0;
draw8bitRect(stylus_x, stylus_y, pixel_width, pixel_width, 0, 0); // int x, int y, int w, int h, bool screen, int color
}
old_stylus_x = stylus_x;
old_stylus_y = stylus_y;
}
}
}
if (redraw_flag)
draw_ca(now, 1);
}
// User is setting trigger points
if (mode == SET_TRIGGERS)
{
// Handle the case where the user sets or erases a trigger
stylus_x = Stylus.X / pixel_width;
stylus_y = Stylus.Y / pixel_width;
if ((stylus_x > 0) && (stylus_x < GRID_WIDTH) && (stylus_y > 0) && (stylus_y < GRID_HEIGHT))
{
if (Stylus.Newpress)
{
if (triggers[stylus_x][stylus_y] == 0)
{
triggers[stylus_x][stylus_y] = selected_swatch; // change to use selected color
draw8bitRect(stylus_x, stylus_y, pixel_width, pixel_width, 0, selected_swatch); // int x, int y, int w, int h, bool screen, int color
}
else
{
triggers[stylus_x][stylus_y] = 0;
draw8bitRect(stylus_x, stylus_y, pixel_width, pixel_width, 0, 0); // int x, int y, int w, int h, bool screen, int color
}
old_stylus_x = stylus_x;
old_stylus_y = stylus_y;
}
}
// Handle reset of all triggers
if (Pad.Newpress.Y)
{
clear_triggers(triggers);
draw_screen_trigger_mode();
}
// Handle selection of swatches
poll_swatches();
// Continue to draw the sequencer state on the top screen
if (redraw_flag)
draw_ca(now, 1);
}
if (mode == SET_CLOCK_SOURCE)
{
if (Pad.Newpress.Y)
{
if (sequencer_status != MANUAL)
{
stop_sequencer();
sequencer_status = MANUAL;
draw_sequencer_step_position();
}
else
{
start_sequencer();
}
draw_screen_set_clock_source_mode();
}
if (sequencer_status == CLOCKED)
{
old_bpm = bpm;
if (Pad.Newpress.Up)
if (bpm < 300)
bpm += 1;
if (Pad.Newpress.Down)
if (bpm > 49)
bpm -= 1;
if (Pad.Newpress.Right)
if (bpm < 291)
bpm += 10;
if (Pad.Newpress.Left)
if (bpm > 60)
bpm -= 10;
if (old_bpm != bpm)
{
old_bpm = bpm;
TIMER_DATA(1) = (int)(-(0x2000000 >> 10) / ((bpm * 4) / (float)60));
draw_screen_set_clock_source_mode();
}
}