summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-05-01 11:13:26 +0200
committerBad Diode <bd@badd10de.dev>2021-05-01 11:13:26 +0200
commit9c00fb0f37c20d61343bbfe9163530d458251545 (patch)
treebb5427d820bd13c703042eae55e9b286662ffbce
parentc2edce15ca79b8a841c187fcacd184ee6c713b82 (diff)
downloadgba-experiments-9c00fb0f37c20d61343bbfe9163530d458251545.tar.gz
gba-experiments-9c00fb0f37c20d61343bbfe9163530d458251545.zip
Start prototyping a sound sequencer
-rw-r--r--src/common.h2
-rw-r--r--src/main.c117
2 files changed, 66 insertions, 53 deletions
diff --git a/src/common.h b/src/common.h
index e864a71..4151d18 100644
--- a/src/common.h
+++ b/src/common.h
@@ -655,7 +655,7 @@ wait_vsync(void) {
655 while(DISP_VCOUNT < 160); 655 while(DISP_VCOUNT < 160);
656} 656}
657 657
658// General utility functions. Min/Max/Clamp 658// General utility macros.
659#define MIN(A, B) ((A) <= (B) ? (A) : (B)) 659#define MIN(A, B) ((A) <= (B) ? (A) : (B))
660#define MAX(A, B) ((A) >= (B) ? (A) : (B)) 660#define MAX(A, B) ((A) >= (B) ? (A) : (B))
661#define CLAMP(X, MIN, MAX) ((X) <= (MIN) ? (MIN) : (X) > (MAX) ? (MAX): (X)) 661#define CLAMP(X, MIN, MAX) ((X) <= (MIN) ? (MIN) : (X) > (MAX) ? (MAX): (X))
diff --git a/src/main.c b/src/main.c
index 8b36226..ac6bf66 100644
--- a/src/main.c
+++ b/src/main.c
@@ -35,78 +35,91 @@ int main(void) {
35 SOUND_DMG_MASTER = sound_volume(SOUND_SQUARE1 | SOUND_SQUARE2, 7); 35 SOUND_DMG_MASTER = sound_volume(SOUND_SQUARE1 | SOUND_SQUARE2, 7);
36 SOUND_DSOUND_MASTER = SOUND_DMG100; 36 SOUND_DSOUND_MASTER = SOUND_DMG100;
37 37
38 int frame_counter = 0;
39 Note active_note = NOTE_C_4; 38 Note active_note = NOTE_C_4;
40 int octave_diff = 0; 39 int octave_diff = 0;
41 bool playing = false; 40 bool playing = false;
42 bool new_note = false; 41 bool new_note = false;
43 u8 interval = 4; 42 u8 interval = 7;
43 u32 sound_sequence[16] = {
44 NOTE_C_4,
45 NOTE_A_4,
46 NOTE_D_5,
47 NOTE_D_4,
48 NOTE_F_4,
49 NOTE_G_4,
50 NOTE_C_4,
51 NOTE_A_4,
52 NOTE_D_5,
53 NOTE_D_4,
54 NOTE_F_4,
55 NOTE_G_4,
56 NOTE_A_4,
57 NOTE_G_4,
58 NOTE_A_4,
59 NOTE_F_4,
60 };
61 int step_counter = 0;
62 int step_counter_2 = 0;
63 int frame_counter = 0;
64 bool trigger_1 = true;
65 bool trigger_2 = true;
44 while(true) { 66 while(true) {
45 bios_vblank_wait(); 67 bios_vblank_wait();
46 poll_keys(); 68 poll_keys();
47 69
48 txt_position(0, 1); 70 txt_position(0, 1);
49 txt_clear_line();
50 71
51 if (key_pressed(KEY_B)) { 72 if (frame_counter >= 7 * 4) {
52 active_note = NOTE_C_4 + 12 * octave_diff; 73 if ((step_counter + 1) % 4 == 0) {
53 new_note = true; 74 step_counter_2 = (step_counter_2 + 1) % 16;
54 } else if (key_pressed(KEY_A)) { 75 trigger_2 = true;
55 active_note = NOTE_D_4 + 12 * octave_diff; 76 }
56 new_note = true; 77 trigger_1 = true;
57 } else if (key_pressed(KEY_LEFT)) { 78 frame_counter = 0;
58 active_note = NOTE_E_4 + 12 * octave_diff; 79 step_counter = (step_counter + 1) % 16;
59 new_note = true;
60 } else if (key_pressed(KEY_DOWN)) {
61 active_note = NOTE_F_4 + 12 * octave_diff;
62 new_note = true;
63 } else if (key_pressed(KEY_RIGHT)) {
64 active_note = NOTE_G_4 + 12 * octave_diff;
65 new_note = true;
66 } else if (key_pressed(KEY_UP)) {
67 active_note = NOTE_A_4 + 12 * octave_diff;
68 new_note = true;
69 } else if (key_pressed(KEY_L)) {
70 active_note = NOTE_B_4 + 12 * octave_diff;
71 new_note = true;
72 } else if (key_pressed(KEY_R)) {
73 active_note = NOTE_C_5 + 12 * octave_diff;
74 new_note = true;
75 } 80 }
76 if (key_pressed(KEY_START)) { 81
77 octave_diff = CLAMP(octave_diff + 1, -2, 3); 82 if (trigger_1 && playing) {
83 active_note = sound_sequence[step_counter];
84 txt_clear_line();
85 txt_printf("SYNTH 1\n\n");
86 txt_clear_line();
87 txt_printf("Step: %d\n", step_counter);
88 txt_clear_line();
89 txt_printf("Playing: %s\n\n\n\n", note_names[active_note]);
90 SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(13) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_DUTY(1);
91 SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note];
92 trigger_1 = false;
78 } 93 }
79 if (key_pressed(KEY_SELECT)) { 94
80 octave_diff = CLAMP(octave_diff - 1, -2, 3); 95 if (trigger_2 && playing) {
96 active_note = sound_sequence[step_counter_2];
97 txt_clear_line();
98 txt_printf("SYNTH 2\n\n");
99 txt_clear_line();
100 txt_printf("Step: %d\n", step_counter_2);
101 txt_clear_line();
102 txt_printf("Playing: %s\n", note_names[active_note]);
103 SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(12) | SOUND_SQUARE_ENV_TIME(3) | SOUND_SQUARE_DUTY(3);
104 SOUND_SQUARE2_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note];
105 trigger_2 = false;
81 } 106 }
82 107
83 if (key_curr > 0) { 108 if (key_pressed(KEY_B)) {
84 txt_printf(" Playing: %s\n", note_names[active_note]);
85 playing = true; 109 playing = true;
86 } else { 110 step_counter = 0;
87 playing = false; 111 step_counter_2 = 8;
112 frame_counter = 0;
113 trigger_1 = true;
114 trigger_2 = true;
88 } 115 }
89 116
90 if (playing) { 117 if (key_pressed(KEY_A)) {
91 if (new_note) { 118 playing = false;
92 // SOUND_SQUARE1_SWEEP = 3 | (1 << 3) | (2 << 4);
93 // SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(0x7);
94 // SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(0);
95 SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_ENV_INC | SOUND_SQUARE_DUTY(3);
96 // SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_ENV_INC;
97 SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note];
98 // SOUND_SQUARE2_FREQ = SOUND_SQUARE_RESET | sound_rate((active_note + interval) % 12, octave);
99 }
100 new_note = false;
101 } else {
102 SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(0) | SOUND_SQUARE_ENV_TIME(0);
103 SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(0) | SOUND_SQUARE_ENV_TIME(0);
104 SOUND_SQUARE1_FREQ = 0;
105 SOUND_SQUARE2_FREQ = 0;
106 } 119 }
107 120
108 frame_counter++; 121 frame_counter++;
109 update_button_sprites(); 122 // update_button_sprites();
110 }; 123 };
111 124
112 return 0; 125 return 0;