diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2023-12-07 17:54:04 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2023-12-07 17:54:04 +0100 |
commit | 820b171245f2f14766f3accdb0246a4e2c0d596a (patch) | |
tree | 9e2df3027dab99956371ea9ed86cd67ef81a9917 /source | |
parent | aa5c9be52f2d41b8efb240ad96f808cf4f3e79c1 (diff) | |
download | saw-820b171245f2f14766f3accdb0246a4e2c0d596a.zip |
Refactor
Diffstat (limited to 'source')
-rw-r--r-- | source/saw/_exe.c | 1 | ||||
-rw-r--r-- | source/saw/main.c | 147 | ||||
-rw-r--r-- | source/saw/profiler.c | 30 | ||||
-rw-r--r-- | source/saw/profiler.h | 6 | ||||
-rw-r--r-- | source/saw/version.h | 9 |
5 files changed, 69 insertions, 124 deletions
diff --git a/source/saw/_exe.c b/source/saw/_exe.c index 7b6c1f7..721afb9 100644 --- a/source/saw/_exe.c +++ b/source/saw/_exe.c @@ -1,3 +1,2 @@ #include "_lib.c" #include "main.c" -#include "profiler.c" diff --git a/source/saw/main.c b/source/saw/main.c index 1cfaaa5..77a8ec4 100644 --- a/source/saw/main.c +++ b/source/saw/main.c @@ -1,6 +1,3 @@ -#include "profiler.h" -#include "version.h" - #include "../kit/math.h" #include "../kit/time.h" #include "../kit/string_ref.h" @@ -28,9 +25,14 @@ #include "font.inl.h" +#define VERSION_MAJOR 0 +#define VERSION_MINOR 0 +#define VERSION_BABY 0 +#define VERSION_DEV 1 + enum { - SAW_CHANNEL_COUNT = 2, - SAW_SAMPLE_RATE = 44100, + CHANNEL_COUNT = 2, + SAMPLE_RATE = 44100, TRACK_COUNT = 16, ROLL_COUNT = 32, @@ -231,7 +233,7 @@ static void saw_play_voice(saw_track_t *track, saw_roll_t *roll, saw_voices[0] = (saw_voice_t) { .enabled = 1, .time = 0, - .duration = (f64) duration / (f64) SAW_SAMPLE_RATE, + .duration = (f64) duration / (f64) SAMPLE_RATE, .frequency = saw_pitch_frequency(pitch), .amplitude = saw_pitch_amplitude(pitch) * track->volume, .phase = { @@ -335,7 +337,7 @@ static void saw_audio(ma_device *device, void *void_out_, f64 release = track->envelope.release; for (i64 i = 0; i < frame_count; i++) { - f64 t = (f64) saw_voices[n].time / (f64) SAW_SAMPLE_RATE; + f64 t = (f64) saw_voices[n].time / (f64) SAMPLE_RATE; f64 a = amplitude * saw_envelope(t, attack, decay, sustain, duration, release); out[i * 2] += (f32) (saw_oscillator(wave_type, frequency, @@ -393,8 +395,8 @@ static void saw_ui_compose(i64 x0, i64 y0, i64 width, i64 height) { i64 top = frame_height - y0 - height + track_height; i64 bottom = frame_height - y0; i64 dx = x0 + saw_compose.offset_x; - i64 l = dx + (roll->time * grid_scale) / SAW_SAMPLE_RATE; - i64 r = l + (roll->duration * grid_scale) / SAW_SAMPLE_RATE; + i64 l = dx + (roll->time * grid_scale) / SAMPLE_RATE; + i64 r = l + (roll->duration * grid_scale) / SAMPLE_RATE; i64 u = frame_height - y0 - height + track_height + saw_compose.offset_y + roll->track * track_height; i64 d = u + track_height; @@ -449,18 +451,17 @@ static void saw_ui_compose(i64 x0, i64 y0, i64 width, i64 height) { i64 cell = ((saw_mouse_x - saw_compose.offset_x) * grid_rate) / grid_scale; - i64 c0 = (roll->time * grid_rate) / SAW_SAMPLE_RATE; - i64 c1 = c0 + - (roll->duration * grid_rate) / SAW_SAMPLE_RATE; + i64 c0 = (roll->time * grid_rate) / SAMPLE_RATE; + i64 c1 = c0 + (roll->duration * grid_rate) / SAMPLE_RATE; saw_compose.grid_input = 1; saw_compose.grid_roll = saw_current_roll; if (cell - c0 > c1 - cell) { saw_compose.grid_cell = c0; - roll->duration = ((cell - c0 + 1) * SAW_SAMPLE_RATE) / + roll->duration = ((cell - c0 + 1) * SAMPLE_RATE) / grid_rate; } else { saw_compose.grid_cell = c1 - 1; - roll->duration = ((c1 - cell + 1) * SAW_SAMPLE_RATE) / + roll->duration = ((c1 - cell + 1) * SAMPLE_RATE) / grid_rate; } } else { @@ -483,7 +484,7 @@ static void saw_ui_compose(i64 x0, i64 y0, i64 width, i64 height) { saw_mouse_y >= frame_height - y0 - height && saw_mouse_y < frame_height - y0 - height + track_height) saw_playback_frame = ((saw_mouse_x - saw_compose.offset_x) * - SAW_SAMPLE_RATE) / + SAMPLE_RATE) / grid_scale; else if (saw_lbutton_click && saw_mouse_y >= @@ -495,7 +496,7 @@ static void saw_ui_compose(i64 x0, i64 y0, i64 width, i64 height) { 1; i64 cell = ((saw_mouse_x - saw_compose.offset_x) * grid_rate) / grid_scale; - i64 frame = (cell * SAW_SAMPLE_RATE) / grid_rate; + i64 frame = (cell * SAMPLE_RATE) / grid_rate; i64 n = -1; @@ -506,7 +507,7 @@ static void saw_ui_compose(i64 x0, i64 y0, i64 width, i64 height) { } i64 x = x0 + saw_compose.offset_x + - (frame * grid_scale) / SAW_SAMPLE_RATE; + (frame * grid_scale) / SAMPLE_RATE; i64 y = frame_height - y0 - height + track_height + saw_compose.offset_y + track * track_height; @@ -532,8 +533,7 @@ static void saw_ui_compose(i64 x0, i64 y0, i64 width, i64 height) { .rate = ROLL_DEFAULT_RATE, .notes = { 0 }, .time = frame, - .duration = (ROLL_DEFAULT_RATE * SAW_SAMPLE_RATE) / - grid_rate, + .duration = (ROLL_DEFAULT_RATE * SAMPLE_RATE) / grid_rate, .loop_duration = 0, .offset_x = 0, .offset_y = ROLL_DEFAULT_OFFSET_Y, @@ -573,7 +573,7 @@ static void saw_ui_compose(i64 x0, i64 y0, i64 width, i64 height) { if (cell < 0 || track < 0 || track >= TRACK_COUNT) break; - i64 frame = (cell * SAW_SAMPLE_RATE) / grid_rate; + i64 frame = (cell * SAMPLE_RATE) / grid_rate; i64 n = -1; @@ -640,15 +640,14 @@ static void saw_ui_compose(i64 x0, i64 y0, i64 width, i64 height) { if (cell >= 0) { if (saw_compose.grid_cell <= cell) { - p->time = (saw_compose.grid_cell * SAW_SAMPLE_RATE) / - grid_rate; + p->time = (saw_compose.grid_cell * SAMPLE_RATE) / grid_rate; p->duration = ((1 + cell - saw_compose.grid_cell) * - SAW_SAMPLE_RATE) / + SAMPLE_RATE) / grid_rate; } else { - p->time = (cell * SAW_SAMPLE_RATE) / grid_rate; + p->time = (cell * SAMPLE_RATE) / grid_rate; p->duration = ((1 + saw_compose.grid_cell - cell) * - SAW_SAMPLE_RATE) / + SAMPLE_RATE) / grid_rate; } } @@ -659,29 +658,28 @@ static void saw_ui_compose(i64 x0, i64 y0, i64 width, i64 height) { saw_roll_t *q = saw_rolls + i; if (!q->enabled || p->track != q->track) continue; - i64 q_cell = (q->time * grid_rate) / SAW_SAMPLE_RATE; - i64 q_size = (q->duration * grid_rate) / SAW_SAMPLE_RATE; + i64 q_cell = (q->time * grid_rate) / SAMPLE_RATE; + i64 q_size = (q->duration * grid_rate) / SAMPLE_RATE; if (saw_compose.grid_cell < q_cell && cell >= q_cell) { cell = q_cell - 1; - p->time = (saw_compose.grid_cell * SAW_SAMPLE_RATE) / - grid_rate; + p->time = (saw_compose.grid_cell * SAMPLE_RATE) / grid_rate; p->duration = ((q_cell - saw_compose.grid_cell) * - SAW_SAMPLE_RATE) / + SAMPLE_RATE) / grid_rate; } if (saw_compose.grid_cell > q_cell && cell < q_cell + q_size) { - cell = q_cell + q_size; - p->time = ((q_cell + q_size) * SAW_SAMPLE_RATE) / grid_rate; + cell = q_cell + q_size; + p->time = ((q_cell + q_size) * SAMPLE_RATE) / grid_rate; p->duration = ((1 + saw_compose.grid_cell - q_cell - q_size) * - SAW_SAMPLE_RATE) / + SAMPLE_RATE) / grid_rate; } } if (p->duration <= 0) - p->duration = SAW_SAMPLE_RATE / grid_rate; + p->duration = SAMPLE_RATE / grid_rate; } else saw_compose.grid_input = 0; } @@ -690,8 +688,8 @@ static void saw_ui_compose(i64 x0, i64 y0, i64 width, i64 height) { // i32 x = x0 + saw_compose.offset_x - border * 2 + - (saw_playback_frame * grid_scale + SAW_SAMPLE_RATE / 2) / - SAW_SAMPLE_RATE; + (saw_playback_frame * grid_scale + SAMPLE_RATE / 2) / + SAMPLE_RATE; i32 w = border * 4; if (x >= x0 - border * 2 && x < x0 + width) { @@ -990,7 +988,7 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, i32 border = 2; i32 sheet_offset = 40; - i32 sheet_scale = (40 * SAW_SAMPLE_RATE) / (10000 * roll->rate); + i32 sheet_scale = (40 * SAMPLE_RATE) / (10000 * roll->rate); // Title text // @@ -1037,7 +1035,7 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, if (t <= 0) roll->loop_duration = 0; else - roll->loop_duration = (t * SAW_SAMPLE_RATE + roll->rate / 2) / + roll->loop_duration = (t * SAMPLE_RATE + roll->rate / 2) / roll->rate; } } @@ -1084,7 +1082,7 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, (saw_lbutton_click || (saw_lbutton_down && roll->last_index != pitch))) saw_play_voice(saw_tracks + roll->track, roll, pitch, - SAW_SAMPLE_RATE / roll->rate); + SAMPLE_RATE / roll->rate); if (saw_rbutton_click) roll->pitch_turned_off[pitch] = @@ -1130,8 +1128,8 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, i32 h = pianokey_height; - for (i32 t = 0; - t < (roll->duration * roll->rate) / SAW_SAMPLE_RATE; t++) { + for (i32 t = 0; t < (roll->duration * roll->rate) / SAMPLE_RATE; + t++) { i32 x = x0 + pianokey_width + sheet_offset + t * sheet_scale + roll->offset_x; @@ -1143,11 +1141,11 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, for (i32 n = 0; n < SHEET_SIZE; n++) { saw_roll_note_t *p = roll->notes + n; if (p->enabled && p->pitch == pitch && - t >= (p->time * roll->rate + SAW_SAMPLE_RATE / 2) / - SAW_SAMPLE_RATE && + t >= (p->time * roll->rate + SAMPLE_RATE / 2) / + SAMPLE_RATE && t < ((p->time + p->duration) * roll->rate + - SAW_SAMPLE_RATE / 2) / - SAW_SAMPLE_RATE) { + SAMPLE_RATE / 2) / + SAMPLE_RATE) { note = n; break; } @@ -1171,7 +1169,7 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, i8 turned_off = roll->pitch_turned_off[pitch] || (roll->loop_duration > 0 && t >= (roll->loop_duration * roll->rate) / - SAW_SAMPLE_RATE); + SAMPLE_RATE); i8 has_cursor = !roll->grid_input && saw_mouse_x >= x && saw_mouse_x < x + w && saw_mouse_y >= y && @@ -1192,15 +1190,15 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, if (!roll->notes[n].enabled) { roll->notes[n] = (saw_roll_note_t) { .enabled = 1, - .time = (t * SAW_SAMPLE_RATE) / roll->rate, - .duration = SAW_SAMPLE_RATE / roll->rate, + .time = (t * SAMPLE_RATE) / roll->rate, + .duration = SAMPLE_RATE / roll->rate, .pitch = pitch }; roll->grid_input = 1; roll->grid_note = n; roll->grid_time = t; saw_play_voice(saw_tracks + roll->track, roll, pitch, - SAW_SAMPLE_RATE / roll->rate); + SAMPLE_RATE / roll->rate); break; } } @@ -1224,11 +1222,11 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, i64 x = x0 + pianokey_width + sheet_offset + roll->offset_x + (note->time * roll->rate * sheet_scale + - SAW_SAMPLE_RATE / 2) / - SAW_SAMPLE_RATE; + SAMPLE_RATE / 2) / + SAMPLE_RATE; i64 w = (note->duration * roll->rate * sheet_scale + - SAW_SAMPLE_RATE / 2) / - SAW_SAMPLE_RATE; + SAMPLE_RATE / 2) / + SAMPLE_RATE; i32 h = pianokey_height; i8 has_cursor = (roll->grid_input && roll->grid_note == n) || @@ -1297,14 +1295,12 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, if (t >= 0) { if (roll->grid_time <= t) { - p->time = (roll->grid_time * SAW_SAMPLE_RATE) / roll->rate; - p->duration = ((1 + t - roll->grid_time) * - SAW_SAMPLE_RATE) / + p->time = (roll->grid_time * SAMPLE_RATE) / roll->rate; + p->duration = ((1 + t - roll->grid_time) * SAMPLE_RATE) / roll->rate; } else { - p->time = (t * SAW_SAMPLE_RATE) / roll->rate; - p->duration = ((1 + roll->grid_time - t) * - SAW_SAMPLE_RATE) / + p->time = (t * SAMPLE_RATE) / roll->rate; + p->duration = ((1 + roll->grid_time - t) * SAMPLE_RATE) / roll->rate; } } @@ -1315,26 +1311,21 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, saw_roll_note_t *q = roll->notes + n; if (!q->enabled || q->pitch != p->pitch) continue; - if (q->time < - (roll->grid_time * SAW_SAMPLE_RATE) / roll->rate && + if (q->time < (roll->grid_time * SAMPLE_RATE) / roll->rate && q->time + q->duration > p->time) { p->time = q->time + q->duration; - p->duration = (roll->grid_time * SAW_SAMPLE_RATE) / - roll->rate > + p->duration = (roll->grid_time * SAMPLE_RATE) / roll->rate > p->time - ? ((roll->grid_time + 1) * - SAW_SAMPLE_RATE) / + ? ((roll->grid_time + 1) * SAMPLE_RATE) / roll->rate - p->time - : SAW_SAMPLE_RATE / roll->rate; + : SAMPLE_RATE / roll->rate; } - if (q->time > - (roll->grid_time * SAW_SAMPLE_RATE) / roll->rate && + if (q->time > (roll->grid_time * SAMPLE_RATE) / roll->rate && q->time < p->time + p->duration) { - p->time = (roll->grid_time * SAW_SAMPLE_RATE) / roll->rate; + p->time = (roll->grid_time * SAMPLE_RATE) / roll->rate; p->duration = q->time - - (roll->grid_time * SAW_SAMPLE_RATE) / - roll->rate; + (roll->grid_time * SAMPLE_RATE) / roll->rate; assert(p->duration > 0); } } @@ -1355,8 +1346,8 @@ static void saw_ui_roll(saw_roll_t *roll, i64 x0, i64 y0, i64 width, i32 x = x0 + pianokey_width + sheet_offset + roll->offset_x - border * 2 + ((frame - roll->time) * roll->rate * sheet_scale + - SAW_SAMPLE_RATE / 2) / - SAW_SAMPLE_RATE; + SAMPLE_RATE / 2) / + SAMPLE_RATE; i32 w = border * 4; if (x >= x0 + pianokey_width + sheet_offset - border * 2 && @@ -1406,8 +1397,8 @@ static void saw_init(void) { ma_device_type_playback); config.playback.format = ma_format_f32; - config.playback.channels = SAW_CHANNEL_COUNT; - config.sampleRate = SAW_SAMPLE_RATE; + config.playback.channels = CHANNEL_COUNT; + config.sampleRate = SAMPLE_RATE; config.dataCallback = saw_audio; config.pUserData = NULL; @@ -1435,7 +1426,7 @@ static void saw_init(void) { .rate = ROLL_DEFAULT_RATE, .notes = { 0 }, .time = 0, - .duration = (48 * SAW_SAMPLE_RATE) / ROLL_DEFAULT_RATE, + .duration = (48 * SAMPLE_RATE) / ROLL_DEFAULT_RATE, .loop_duration = 0, .offset_x = 0, .offset_y = ROLL_DEFAULT_OFFSET_Y, @@ -1982,14 +1973,14 @@ sapp_desc sokol_main(i32 argc, char **argv) { if (print_version) { printf("Saw v%d.%d.%d" -#if SAW_VERSION_DEV +#if VERSION_DEV "_dev" #endif #ifndef NDEBUG " (Debug)" #endif " - Music sequencer standalone application.\n", - SAW_VERSION_MAJOR, SAW_VERSION_MINOR, SAW_VERSION_BABY); + VERSION_MAJOR, VERSION_MINOR, VERSION_BABY); exit(0); } diff --git a/source/saw/profiler.c b/source/saw/profiler.c deleted file mode 100644 index 2f8e23b..0000000 --- a/source/saw/profiler.c +++ /dev/null @@ -1,30 +0,0 @@ -#include "../kit/time.h" -#include "../kit/types.h" - -#include <stdio.h> - -i64 profiler_time_; - -#ifdef __GNUC__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-function" -# pragma GCC diagnostic ignored "-Wunknown-pragmas" -# pragma GCC push_options -# pragma GCC optimize("O3") -#endif - -void profile_frame(char const *s) { - struct timespec ts; - timespec_get(&ts, TIME_UTC); - i64 t = ((i64) ts.tv_sec) * 1000 + ((i64) ts.tv_nsec) / 1000000; - if (profiler_time_ == 0) - printf("%s\n", s); - else - printf("%-19s+%lld\n", s, t - profiler_time_); - profiler_time_ = t; -} - -#ifdef __GNUC__ -# pragma GCC pop_options -# pragma GCC diagnostic pop -#endif diff --git a/source/saw/profiler.h b/source/saw/profiler.h deleted file mode 100644 index 850b2e8..0000000 --- a/source/saw/profiler.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef SAW_PROFILER_H -#define SAW_PROFILER_H - -void profile_frame(char const *s); - -#endif diff --git a/source/saw/version.h b/source/saw/version.h deleted file mode 100644 index 85d89fc..0000000 --- a/source/saw/version.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef SAW_VERSION_H -#define SAW_VERSION_H - -#define SAW_VERSION_MAJOR 0 -#define SAW_VERSION_MINOR 0 -#define SAW_VERSION_BABY 0 -#define SAW_VERSION_DEV 1 - -#endif |