aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: b2d7a23130a245379bb43ce616adf3b3acea946e (plain)
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
/*
Copyright (c) 2023 Bad Diode

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

// TODO: A list of features I would like to get to implement in the near future.
//
// UI tweaks.
//  - Notification support for feedback when doing some operations
//    (copying/pasting)
//  - Animations for cursor movement/current step highlight. (A fade out maybe?)
//  - Theming support, with a number of pre-configured themes and custom colors.
//  - Add panning support. We could send ch1-3 to the left and ch4 to the right
//    to act as metronome and achieve analog sync.
//
// Quality of life improvements.
//  + Add a settings page to change some configuration parameters.
//  - Change the cursor, the line is difficult to see.
//  - When not on play mode, adjusting a note or a parameter triggers the sound.
//    This could get annoying, so maybe it should be a configuration option to
//    enable it?
//  - Pattern chaining for more than 1 queue and/or song mode.
//  - Undo/Redo.
//  - Select + up/down to queue the next pattern as we move to it?
//
//  Advanced
//  + Sync via CV by using the link cable.
//  - Audio sync by panning left the sound and using right as a click (or
//    viceversa, needs to check the standard.)
//  - Sync via MIDI via arduinoboy or something similar.
//  - Add an FM channel using Direct Sound.
//  - Per trig note probability.
//  - Add an envelope to ch3, would need to work with a timer in order to make
//    it work I think.
//  - Allow "marking" several trigs to be able to copy/paste them and/or adjust
//    their parameters.
//
//  Bugfixes
//  - Sound can get hung up sometimes, but I can't reproduce when this happens.
//    Not sure if this is an emulator thing or happens also in hardware.
//  - Cursor can stay in position instead of dissapering, again I can't
//    reproduce this right now, just happened randomly. Needs investigation.
//  + Pattern chaining seems off, it plays the first note of the pattern before
//    switching
//  + Memory corruption when trying to load a save file. Regression due to
//  removal of filesystem.c
//

#include "gba/gba.h"

#include "renderer_m0.c"
#include "sequencer.c"

#define PROF_ENABLE 0
#include "profiling.c"

void
render_sequencer(void) {
    PROF(draw_rect(0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, 1), clear_cycles);
    if (redraw_trigs) {
        PROF(draw_triggers(), draw_trigs_cycles);
        redraw_trigs = false;
    }
    if (redraw_channels) {
        PROF(draw_channels(), draw_btn_cycles);
        redraw_channels = false;
    }
    if (redraw_pattern_buttons) {
        PROF(draw_pattern_buttons(), draw_btn_cycles);
        redraw_pattern_buttons = false;
    }
    if (redraw_bank_buttons) {
        PROF(draw_bank_buttons(), draw_btn_cycles);
        redraw_bank_buttons = false;
    }
    if (redraw_bpm) {
        PROF(draw_bpm(), draw_btn_cycles);
        redraw_bpm = false;
    }
    if (redraw_play_pause) {
        PROF(draw_play(), draw_btn_cycles);
        PROF(draw_stop(), draw_btn_cycles);
        redraw_play_pause = false;
    }
    if (redraw_piano_note) {
        PROF(draw_piano_notes(), draw_piano_cycles);
        redraw_piano_note = false;
    }
    if (redraw_params) {
        PROF(draw_parameters(), draw_param_cycles);
        redraw_params = false;
    }
    PROF(draw_cursors(), draw_cursor_cycles);
}

bool clear_screen = false;

typedef enum Scene {
    SCENE_SEQUENCER = 0,
    SCENE_SETTINGS,
} Scene;

static scene = SCENE_SETTINGS;
static next_scene = SCENE_SETTINGS;

typedef enum SyncSetting {
    SYNC_NONE = 0,
    SYNC_OUT_LINK_16,
    SYNC_OUT_LINK_8,
    SYNC_OUT_LINK_4,
    SYNC_NUM,
} SyncSetting;

char * sync_setting_str[] = {
    "NONE",
    "LINK OUT (16)",
    "LINK OUT (8)",
    "LINK OUT (4)",
};

typedef enum  ThemeSetting {
    THEME_DEFAULT = 0,
} ThemeSetting;

char * theme_setting_str[] = {
    "DEFAULT",
};

typedef enum  CursorSetting {
    CURSOR_DEFAULT = 0,
} CursorSetting;

char * cursor_setting_str[] = {
    "LINE",
};

typedef struct Settings {
    SyncSetting sync;
    ThemeSetting theme;
    CursorSetting cursor;
} Settings;

static Settings settings = {0};
static settings_cursor_loc = 0;

void
draw_settings_cursor(void) {
    int x = 6;
    int y = 17 + settings_cursor_loc * 12;
    draw_line(x + 1, y + 3, x + 1, y + 7, COL_CYAN);
    draw_line(x + 2, y + 4, x + 2, y + 6, COL_CYAN);
    draw_line(x + 3, y + 5, x + 3, y + 5, COL_CYAN);
}

void
render_settings(void) {
    PROF(draw_rect(0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, 1), clear_cycles);
    txt_drawf_small("settings", 11, 5, COL_FG)
    txt_printf("\n\n\n");
    txt_printf("  SYNC:   %s\n\n", sync_setting_str[settings.sync]);
    txt_printf("  THEME:  %s\n\n", theme_setting_str[settings.theme]);
    txt_printf("  CURSOR: %s\n\n", cursor_setting_str[settings.cursor]);
    txt_render();
    txt_clear();
    draw_settings_cursor();
}

#define N_SETTINGS 3

void
handle_settings_input(void) {
    if (key_tap(KEY_DOWN)) {
        if (settings_cursor_loc == (N_SETTINGS - 1)) {
            settings_cursor_loc = 0;
        } else {
            settings_cursor_loc++;
        }
        clear_screen = true;
    }
    if (key_tap(KEY_UP)) {
        if (settings_cursor_loc == 0) {
            settings_cursor_loc = N_SETTINGS - 1;
        } else {
            settings_cursor_loc--;
        }
        clear_screen = true;
    }
    if (key_tap(KEY_R)) {
        switch (settings_cursor_loc) {
            case 0: {
                if ((settings.sync + 1) >= SYNC_NUM) {
                    settings.sync = 0;
                } else {
                    settings.sync++;
                }
            } break;
        }
        clear_screen = true;
    }
    if (key_tap(KEY_L)) {
        switch (settings_cursor_loc) {
            case 0: {
                if (settings.sync == 0) {
                    settings.sync = SYNC_NUM - 1;
                } else {
                    settings.sync--;
                }
            } break;
        }
        clear_screen = true;
    }
    if (key_tap(KEY_B)) {
        next_scene = SCENE_SEQUENCER;
    }
}

void
render(void) {
    if (clear_screen) {
        screen_fill(COL_BG);
        clear_screen = false;
    }
    switch (scene) {
        case SCENE_SETTINGS: {
            render_settings();
        } break;
        case SCENE_SEQUENCER: {
            render_sequencer();
        } break;
    }
}

void
handle_input(void) {
    switch (scene) {
        case SCENE_SETTINGS: {
            handle_settings_input();
        } break;
        case SCENE_SEQUENCER: {
            handle_sequencer_input();
        } break;
    }
}

void
update(void) {
    if (next_scene != scene) {
        scene = next_scene;
        clear_screen = true;
    }
    last_trig_loc = trig_selection_loc;
    last_channel_loc = channel_selection_loc;
    last_pattern_loc = pattern_selection_loc;
    last_right_col_loc = right_col_selection_loc;
    if (update_bpm) {
        set_time(patterns[current_pattern].bpm);
        update_bpm = false;
    }
}

int
main(void) {
    // Adjust system wait times.
    SYSTEM_WAIT = SYSTEM_WAIT_CARTRIDGE;

    // Initialize renderer.
    renderer_init();

    // Register interrupts.
    irq_init();
    irs_set(IRQ_VBLANK, irs_stub);

    // Initialize sequencer.
    sequencer_init();
    txt_spacing(6);

    // Main loop.
    while (true) {
        poll_keys();
        bios_vblank_wait();
        PROF_SHOW();
        FRAME_START();
        PROF(flip_buffer(), flip_cycles);
        PROF(update(), update_cycles);
        PROF(handle_input(), input_cycles);
        PROF(render(), render_cycles);
        FRAME_END();
    }

    return 0;
}