From 9c00fb0f37c20d61343bbfe9163530d458251545 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sat, 1 May 2021 11:13:26 +0200 Subject: Start prototyping a sound sequencer --- src/common.h | 2 +- src/main.c | 117 +++++++++++++++++++++++++++++++++-------------------------- 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) { while(DISP_VCOUNT < 160); } -// General utility functions. Min/Max/Clamp +// General utility macros. #define MIN(A, B) ((A) <= (B) ? (A) : (B)) #define MAX(A, B) ((A) >= (B) ? (A) : (B)) #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) { SOUND_DMG_MASTER = sound_volume(SOUND_SQUARE1 | SOUND_SQUARE2, 7); SOUND_DSOUND_MASTER = SOUND_DMG100; - int frame_counter = 0; Note active_note = NOTE_C_4; int octave_diff = 0; bool playing = false; bool new_note = false; - u8 interval = 4; + u8 interval = 7; + u32 sound_sequence[16] = { + NOTE_C_4, + NOTE_A_4, + NOTE_D_5, + NOTE_D_4, + NOTE_F_4, + NOTE_G_4, + NOTE_C_4, + NOTE_A_4, + NOTE_D_5, + NOTE_D_4, + NOTE_F_4, + NOTE_G_4, + NOTE_A_4, + NOTE_G_4, + NOTE_A_4, + NOTE_F_4, + }; + int step_counter = 0; + int step_counter_2 = 0; + int frame_counter = 0; + bool trigger_1 = true; + bool trigger_2 = true; while(true) { bios_vblank_wait(); poll_keys(); txt_position(0, 1); - txt_clear_line(); - if (key_pressed(KEY_B)) { - active_note = NOTE_C_4 + 12 * octave_diff; - new_note = true; - } else if (key_pressed(KEY_A)) { - active_note = NOTE_D_4 + 12 * octave_diff; - new_note = true; - } else if (key_pressed(KEY_LEFT)) { - active_note = NOTE_E_4 + 12 * octave_diff; - new_note = true; - } else if (key_pressed(KEY_DOWN)) { - active_note = NOTE_F_4 + 12 * octave_diff; - new_note = true; - } else if (key_pressed(KEY_RIGHT)) { - active_note = NOTE_G_4 + 12 * octave_diff; - new_note = true; - } else if (key_pressed(KEY_UP)) { - active_note = NOTE_A_4 + 12 * octave_diff; - new_note = true; - } else if (key_pressed(KEY_L)) { - active_note = NOTE_B_4 + 12 * octave_diff; - new_note = true; - } else if (key_pressed(KEY_R)) { - active_note = NOTE_C_5 + 12 * octave_diff; - new_note = true; + if (frame_counter >= 7 * 4) { + if ((step_counter + 1) % 4 == 0) { + step_counter_2 = (step_counter_2 + 1) % 16; + trigger_2 = true; + } + trigger_1 = true; + frame_counter = 0; + step_counter = (step_counter + 1) % 16; } - if (key_pressed(KEY_START)) { - octave_diff = CLAMP(octave_diff + 1, -2, 3); + + if (trigger_1 && playing) { + active_note = sound_sequence[step_counter]; + txt_clear_line(); + txt_printf("SYNTH 1\n\n"); + txt_clear_line(); + txt_printf("Step: %d\n", step_counter); + txt_clear_line(); + txt_printf("Playing: %s\n\n\n\n", note_names[active_note]); + SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(13) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_DUTY(1); + SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note]; + trigger_1 = false; } - if (key_pressed(KEY_SELECT)) { - octave_diff = CLAMP(octave_diff - 1, -2, 3); + + if (trigger_2 && playing) { + active_note = sound_sequence[step_counter_2]; + txt_clear_line(); + txt_printf("SYNTH 2\n\n"); + txt_clear_line(); + txt_printf("Step: %d\n", step_counter_2); + txt_clear_line(); + txt_printf("Playing: %s\n", note_names[active_note]); + SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(12) | SOUND_SQUARE_ENV_TIME(3) | SOUND_SQUARE_DUTY(3); + SOUND_SQUARE2_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note]; + trigger_2 = false; } - if (key_curr > 0) { - txt_printf(" Playing: %s\n", note_names[active_note]); + if (key_pressed(KEY_B)) { playing = true; - } else { - playing = false; + step_counter = 0; + step_counter_2 = 8; + frame_counter = 0; + trigger_1 = true; + trigger_2 = true; } - if (playing) { - if (new_note) { - // SOUND_SQUARE1_SWEEP = 3 | (1 << 3) | (2 << 4); - // SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(0x7); - // SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(0); - SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_ENV_INC | SOUND_SQUARE_DUTY(3); - // SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_ENV_INC; - SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note]; - // SOUND_SQUARE2_FREQ = SOUND_SQUARE_RESET | sound_rate((active_note + interval) % 12, octave); - } - new_note = false; - } else { - SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(0) | SOUND_SQUARE_ENV_TIME(0); - SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(0) | SOUND_SQUARE_ENV_TIME(0); - SOUND_SQUARE1_FREQ = 0; - SOUND_SQUARE2_FREQ = 0; + if (key_pressed(KEY_A)) { + playing = false; } frame_counter++; - update_button_sprites(); + // update_button_sprites(); }; return 0; -- cgit v1.2.1