2024-06-13 11:43:53 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2024-06-14 08:18:00 +00:00
|
|
|
#include <string.h>
|
2024-06-13 11:43:53 +00:00
|
|
|
|
|
|
|
#define SOKOL_IMPL
|
|
|
|
#include "sokol_app.h"
|
|
|
|
#include "sokol_audio.h"
|
2024-06-29 15:15:06 +00:00
|
|
|
#include "sokol_gfx.h"
|
|
|
|
#include "sokol_gl.h"
|
|
|
|
#include "sokol_glue.h"
|
2024-06-13 11:43:53 +00:00
|
|
|
#include "sokol_log.h"
|
|
|
|
|
2024-06-24 07:58:31 +00:00
|
|
|
#define STFU_MAIN
|
2024-06-13 11:43:53 +00:00
|
|
|
#include "../config.h"
|
2024-06-13 12:47:09 +00:00
|
|
|
#include "../envelope.c"
|
2024-06-13 11:43:53 +00:00
|
|
|
#include "../oscillators.c"
|
|
|
|
#include "../phaser.c"
|
2024-06-29 15:15:06 +00:00
|
|
|
#include "config.h"
|
2024-06-13 11:43:53 +00:00
|
|
|
|
2024-06-24 07:58:31 +00:00
|
|
|
#include "../Instruments/Trojka/trojka.c"
|
2024-06-29 15:15:06 +00:00
|
|
|
#include "Widgets/slider.c"
|
2024-06-13 11:43:53 +00:00
|
|
|
|
2024-06-24 07:58:31 +00:00
|
|
|
static struct stfu_trojka synth = {0};
|
2024-06-13 11:43:53 +00:00
|
|
|
|
2024-06-24 07:58:31 +00:00
|
|
|
// todo: Separate to playback.c, generic for Player and Maker
|
2024-06-14 08:18:00 +00:00
|
|
|
#define CROSSFADE_RING_RANGE \
|
|
|
|
(STFU_CROSSFADE_BUFFER_FRAMES * STFU_AUDIO_CHANNEL_COUNT)
|
|
|
|
static float crossfade_ring[CROSSFADE_RING_RANGE] = {0};
|
|
|
|
static size_t crossfade_ring_needle = 0;
|
|
|
|
static unsigned int crossfade_frames_left = 0;
|
|
|
|
|
2024-06-24 13:28:19 +00:00
|
|
|
// 1.0 is with A at 440
|
|
|
|
static float octave = 0.5;
|
|
|
|
|
2024-06-29 15:33:59 +00:00
|
|
|
static struct stfu_slider feedback_slider = {.x = 16,
|
|
|
|
.y = 16,
|
|
|
|
.width = 120,
|
|
|
|
.height = 16,
|
|
|
|
.min = 0,
|
|
|
|
.max = 1,
|
|
|
|
.cur = 0.25};
|
|
|
|
|
2024-06-13 11:43:53 +00:00
|
|
|
static void stream(float *buffer, int num_frames, int num_channels) {
|
2024-06-14 08:18:00 +00:00
|
|
|
/* Fade away */
|
|
|
|
for (size_t i = 0; crossfade_frames_left > 0;
|
|
|
|
crossfade_frames_left--, i += 2) {
|
|
|
|
crossfade_ring[(crossfade_ring_needle + i) % CROSSFADE_RING_RANGE] *=
|
|
|
|
(float)crossfade_frames_left / STFU_CROSSFADE_BUFFER_FRAMES;
|
|
|
|
crossfade_ring[(crossfade_ring_needle + i + 1) % CROSSFADE_RING_RANGE] *=
|
|
|
|
(float)crossfade_frames_left / STFU_CROSSFADE_BUFFER_FRAMES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Drive the ring buffer */
|
|
|
|
// todo: Use memcpy for left and right side of the ring.
|
|
|
|
while (num_frames--) {
|
|
|
|
buffer[0] = crossfade_ring[crossfade_ring_needle % CROSSFADE_RING_RANGE];
|
|
|
|
buffer[1] =
|
|
|
|
crossfade_ring[(crossfade_ring_needle + 1) % CROSSFADE_RING_RANGE];
|
2024-06-29 15:15:06 +00:00
|
|
|
synth = stfu_sample_trojka(
|
|
|
|
synth, &crossfade_ring[crossfade_ring_needle % CROSSFADE_RING_RANGE]);
|
2024-06-14 08:18:00 +00:00
|
|
|
buffer = &buffer[2];
|
|
|
|
crossfade_ring_needle += 2;
|
2024-06-13 11:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init(void) {
|
|
|
|
saudio_setup(&(saudio_desc){
|
|
|
|
.sample_rate = STFU_AUDIO_FRAME_RATE,
|
|
|
|
.num_channels = STFU_AUDIO_CHANNEL_COUNT,
|
|
|
|
// .stream_cb = stream,
|
2024-06-29 15:15:06 +00:00
|
|
|
.logger.func = slog_func,
|
|
|
|
});
|
|
|
|
|
|
|
|
sg_setup(&(sg_desc){
|
|
|
|
.environment = sglue_environment(),
|
|
|
|
.logger.func = slog_func,
|
|
|
|
});
|
|
|
|
|
|
|
|
sgl_setup(&(sgl_desc_t){
|
|
|
|
.logger.func = slog_func,
|
2024-06-13 11:43:53 +00:00
|
|
|
});
|
|
|
|
|
2024-06-24 13:28:19 +00:00
|
|
|
synth.freq_scales[0] = 1.0;
|
|
|
|
synth.freq_scales[1] = 0.25;
|
|
|
|
synth.freq_scales[2] = 2.0;
|
|
|
|
|
|
|
|
synth.indices[0] = 2.0;
|
|
|
|
synth.indices[1] = 10.0;
|
|
|
|
|
2024-06-24 07:58:31 +00:00
|
|
|
synth.feedback_gain = 0.25;
|
2024-06-24 13:28:19 +00:00
|
|
|
|
2024-06-24 07:58:31 +00:00
|
|
|
for (int i = 0; i < STFU_TROJKA_OP_COUNT; ++i) {
|
|
|
|
synth.envelopes[i] = stfu_init_envelope(
|
|
|
|
(struct stfu_envelope_point[]){[0] = {.dur = 0.02, .vol = 1.0},
|
|
|
|
[1] = {.dur = 0.5, .vol = 0.33},
|
|
|
|
[2] = {.dur = 1.0, .vol = 0}},
|
|
|
|
3);
|
|
|
|
synth.gains[i] = 1.0;
|
|
|
|
}
|
2024-06-13 11:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup(void) { saudio_shutdown(); }
|
|
|
|
|
|
|
|
static void event(const sapp_event *e) {
|
|
|
|
switch (e->type) {
|
2024-06-29 15:33:59 +00:00
|
|
|
case SAPP_EVENTTYPE_MOUSE_DOWN: {
|
|
|
|
if (e->mouse_button != SAPP_MOUSEBUTTON_LEFT)
|
|
|
|
break;
|
|
|
|
|
|
|
|
feedback_slider =
|
|
|
|
stfu_press_slider(feedback_slider, e->mouse_x, e->mouse_y);
|
|
|
|
synth.feedback_gain = feedback_slider.cur;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2024-06-13 11:43:53 +00:00
|
|
|
case SAPP_EVENTTYPE_KEY_DOWN: {
|
|
|
|
if (e->key_repeat)
|
|
|
|
break;
|
|
|
|
|
2024-06-24 13:28:19 +00:00
|
|
|
bool key_was_pressed = false;
|
|
|
|
|
2024-06-13 11:43:53 +00:00
|
|
|
switch (e->key_code) {
|
|
|
|
case SAPP_KEYCODE_Z:
|
2024-06-24 13:28:19 +00:00
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[0] * octave);
|
|
|
|
break;
|
2024-06-13 11:43:53 +00:00
|
|
|
case SAPP_KEYCODE_X:
|
2024-06-24 13:28:19 +00:00
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[1] * octave);
|
2024-06-13 11:43:53 +00:00
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_C:
|
2024-06-24 13:28:19 +00:00
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[2] * octave);
|
2024-06-13 11:43:53 +00:00
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_V:
|
2024-06-24 13:28:19 +00:00
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[3] * octave);
|
2024-06-13 11:43:53 +00:00
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_B:
|
2024-06-24 13:28:19 +00:00
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[4] * octave);
|
2024-06-13 11:43:53 +00:00
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_N:
|
2024-06-24 13:28:19 +00:00
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[5] * octave);
|
2024-06-13 11:43:53 +00:00
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_M:
|
2024-06-24 13:28:19 +00:00
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[6] * octave);
|
2024-06-13 11:43:53 +00:00
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_COMMA:
|
2024-06-24 13:28:19 +00:00
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[7] * octave);
|
2024-06-13 11:43:53 +00:00
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_PERIOD:
|
2024-06-24 13:28:19 +00:00
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[8] * octave);
|
2024-06-13 11:43:53 +00:00
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_SLASH:
|
2024-06-24 13:28:19 +00:00
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[9] * octave);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAPP_KEYCODE_A:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[0] * (octave * 2));
|
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_S:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[1] * (octave * 2));
|
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_D:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[2] * (octave * 2));
|
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_F:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[3] * (octave * 2));
|
2024-06-13 11:43:53 +00:00
|
|
|
break;
|
2024-06-24 13:28:19 +00:00
|
|
|
case SAPP_KEYCODE_G:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[4] * (octave * 2));
|
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_H:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[5] * (octave * 2));
|
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_J:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[6] * (octave * 2));
|
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_K:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[7] * (octave * 2));
|
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_L:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[8] * (octave * 2));
|
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_SEMICOLON:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[9] * (octave * 2));
|
|
|
|
break;
|
|
|
|
case SAPP_KEYCODE_APOSTROPHE:
|
|
|
|
key_was_pressed = true;
|
|
|
|
synth = stfu_press_trojka(synth, STFU_SCALE[10] * (octave * 2));
|
|
|
|
break;
|
|
|
|
|
2024-06-13 11:43:53 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-06-24 13:28:19 +00:00
|
|
|
if (key_was_pressed) {
|
|
|
|
crossfade_frames_left = STFU_CROSSFADE_BUFFER_FRAMES;
|
|
|
|
}
|
|
|
|
|
2024-06-13 11:43:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void frame(void) {
|
|
|
|
int frames = (int)(sapp_frame_duration() * (float)STFU_AUDIO_FRAME_RATE);
|
|
|
|
int expected = saudio_expect();
|
|
|
|
frames = frames > expected ? expected : frames;
|
|
|
|
// todo: Prevent drifting apart.
|
|
|
|
static float buffer[STFU_AUDIO_FRAME_RATE * STFU_AUDIO_CHANNEL_COUNT];
|
|
|
|
while (frames > 0) {
|
|
|
|
int now = frames > STFU_AUDIO_FRAME_RATE ? STFU_AUDIO_FRAME_RATE : frames;
|
|
|
|
stream(buffer, now, STFU_AUDIO_CHANNEL_COUNT);
|
|
|
|
saudio_push(buffer, now);
|
|
|
|
frames -= now;
|
|
|
|
}
|
2024-06-29 15:15:06 +00:00
|
|
|
|
|
|
|
sg_pass pass = {.swapchain = sglue_swapchain()};
|
|
|
|
sg_begin_pass(&pass);
|
|
|
|
sgl_load_default_pipeline();
|
|
|
|
sgl_load_identity();
|
|
|
|
sgl_ortho(0, STFU_MAKER_WINDOW_WIDTH, STFU_MAKER_WINDOW_HEIGHT, 0, -1, 1);
|
2024-06-29 15:33:59 +00:00
|
|
|
stfu_draw_slider(feedback_slider);
|
2024-06-29 15:15:06 +00:00
|
|
|
// sgl_pop_matrix();
|
|
|
|
sgl_draw();
|
|
|
|
sg_end_pass();
|
|
|
|
sg_commit();
|
2024-06-13 11:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sapp_desc sokol_main(int argc, char *argv[]) {
|
|
|
|
return (sapp_desc){
|
2024-06-29 15:15:06 +00:00
|
|
|
.width = STFU_MAKER_WINDOW_WIDTH,
|
|
|
|
.height = STFU_MAKER_WINDOW_HEIGHT,
|
2024-06-13 11:43:53 +00:00
|
|
|
.init_cb = init,
|
|
|
|
.frame_cb = frame,
|
|
|
|
.cleanup_cb = cleanup,
|
|
|
|
.event_cb = event,
|
|
|
|
.logger.func = slog_func,
|
2024-06-26 12:37:05 +00:00
|
|
|
// .gl_major_version = 2,
|
|
|
|
// .gl_minor_version = 0,
|
2024-06-13 11:43:53 +00:00
|
|
|
// .swap_interval = 0,
|
2024-06-24 07:58:31 +00:00
|
|
|
.window_title = "stfu-maker",
|
2024-06-13 11:43:53 +00:00
|
|
|
};
|
|
|
|
}
|