diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 14 | ||||
| -rw-r--r-- | src/messages.c | 8 | ||||
| -rw-r--r-- | src/messages.h | 22 | ||||
| -rw-r--r-- | src/sounds.c | 44 | ||||
| -rw-r--r-- | src/sounds.h | 30 | ||||
| -rw-r--r-- | src/ui.c | 10 | ||||
| -rw-r--r-- | src/ui.h | 4 |
7 files changed, 66 insertions, 66 deletions
@@ -40,7 +40,7 @@ typedef struct { KnobSettings knob_settings; snd_pcm_t *sound_device; - message_queue msg_queue; + MessageQueue msg_queue; WaveData wave_data; pthread_t sound_thread; } app_state; @@ -63,7 +63,7 @@ int init_sounds(app_state *state) { mqueue_init(&state->msg_queue); - sound_thread_meta *sound_thread_params = malloc(sizeof(sound_thread_meta)); + SoundThreadMeta *sound_thread_params = malloc(sizeof(SoundThreadMeta)); sound_thread_params->pcm = state->sound_device; sound_thread_params->queue = &state->msg_queue; sound_thread_params->wave_data = &state->wave_data; @@ -105,7 +105,7 @@ int init_sounds(app_state *state) { }, }; - synth_message param_messages[6] = { + SynthMessage param_messages[6] = { { .type = MSG_PARAM_CHANGE, .param_change = @@ -339,7 +339,7 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { } state->keys[i].keyboard_pressed = true; - mqueue_push(&state->msg_queue, (synth_message){ + mqueue_push(&state->msg_queue, (SynthMessage){ .type = MSG_NOTE_ON, .note = { .note_id = i }, }); @@ -355,7 +355,7 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { break; } state->keys[i].keyboard_pressed = false; - mqueue_push(&state->msg_queue, (synth_message){ + mqueue_push(&state->msg_queue, (SynthMessage){ .type = MSG_NOTE_OFF, .note = { .note_id = i }, }); @@ -403,9 +403,9 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) { app_state *state = appstate; - message_queue *queue = &state->msg_queue; + MessageQueue *queue = &state->msg_queue; - synth_message stop_msg = {.type = MSG_STOP}; + SynthMessage stop_msg = {.type = MSG_STOP}; mqueue_push(queue, stop_msg); pthread_join(state->sound_thread, NULL); diff --git a/src/messages.c b/src/messages.c index 80998a7..1c1378e 100644 --- a/src/messages.c +++ b/src/messages.c @@ -1,6 +1,6 @@ #include "messages.h" -int mqueue_get(message_queue *q, synth_message *msg) { +int mqueue_get(MessageQueue *q, SynthMessage *msg) { pthread_mutex_lock(&(q)->lock); if ((q)->tail == (q)->head) { pthread_mutex_unlock(&(q)->lock); @@ -14,7 +14,7 @@ int mqueue_get(message_queue *q, synth_message *msg) { return 0; } -int mqueue__push_no_lock(message_queue *q, synth_message msg) { +int mqueue__push_no_lock(MessageQueue *q, SynthMessage msg) { size_t next = ((q)->head + 1) % MESSAGE_QUEUE_SIZE; if ((q)->tail == next) { @@ -26,14 +26,14 @@ int mqueue__push_no_lock(message_queue *q, synth_message msg) { return 0; } -int mqueue_push(message_queue *q, synth_message msg) { +int mqueue_push(MessageQueue *q, SynthMessage msg) { pthread_mutex_lock(&(q)->lock); int ret = mqueue__push_no_lock(q, msg); pthread_mutex_unlock(&(q)->lock); return ret; } -int mqueue_push_many(message_queue *q, synth_message *msg, size_t count) { +int mqueue_push_many(MessageQueue *q, SynthMessage *msg, size_t count) { pthread_mutex_lock(&(q)->lock); int ret = 0; for (size_t i = 0; i < count; i++) { diff --git a/src/messages.h b/src/messages.h index edbc32e..ff9784c 100644 --- a/src/messages.h +++ b/src/messages.h @@ -14,14 +14,14 @@ typedef enum { PARAM_DECAY, PARAM_SUSTAIN, PARAM_RELEASE, -} param_type; +} ParamType; typedef enum { OSC_SINE, OSC_SAW, OSC_SQUARE, OSC_TRIANGLE, -} oscilator_type; +} OscilatorType; typedef enum { MSG_NOTE_ON, @@ -29,10 +29,10 @@ typedef enum { MSG_ALL_NOTES_OFF, MSG_PARAM_CHANGE, MSG_STOP, -} synth_message_type; +} SynthMessageType; typedef struct { - synth_message_type type; + SynthMessageType type; union { // NOTE_ON / NOTE_OFF @@ -42,18 +42,18 @@ typedef struct { // SET_PARAM; struct { - param_type param_type; + ParamType param_type; float value; } param_change; }; -} synth_message; +} SynthMessage; typedef struct { - synth_message buffer[MESSAGE_QUEUE_SIZE]; + SynthMessage buffer[MESSAGE_QUEUE_SIZE]; size_t head; size_t tail; pthread_mutex_t lock; -} message_queue; +} MessageQueue; typedef struct { float freq; @@ -61,9 +61,9 @@ typedef struct { atomic_int write_index; } WaveData; -int mqueue_get(message_queue *q, synth_message *msg); -int mqueue_push(message_queue *q, synth_message msg); -int mqueue_push_many(message_queue *q, synth_message *msg, size_t count); +int mqueue_get(MessageQueue *q, SynthMessage *msg); +int mqueue_push(MessageQueue *q, SynthMessage msg); +int mqueue_push_many(MessageQueue *q, SynthMessage *msg, size_t count); #define mqueue_init(q) \ do { \ diff --git a/src/sounds.c b/src/sounds.c index d1eb875..904b76b 100644 --- a/src/sounds.c +++ b/src/sounds.c @@ -1,7 +1,7 @@ #define MIDI_FREQS_LIST #include "sounds.h" -float envelope_next(envelope *env) { +float envelope_next(Envelope *env) { float value; bool next_state = false; @@ -63,13 +63,13 @@ float envelope_next(envelope *env) { return value; } -void envelope_note_on(envelope_params params, envelope *env) { +void envelope_note_on(EnvelopeParams params, Envelope *env) { env->state = ENV_ATTACK; env->counter = 0; env->params = params; } -void envelope_note_off(envelope *env) { +void envelope_note_off(Envelope *env) { env->state = ENV_RELEASE; env->counter = 0; } @@ -86,7 +86,7 @@ float osc_saw_get(float phase) { return (phase / M_PI) - 1; } -oscilator_func osc_get(oscilator_type type) { +OscilatorFunc osc_get(OscilatorType type) { switch (type) { case OSC_SINE: return osc_sine_get; @@ -99,7 +99,7 @@ oscilator_func osc_get(oscilator_type type) { } } -void set_note_on(synth_params *params, synth_voices *voices, size_t note_id) { +void set_note_on(SynthParams *params, SynthVoices *voices, size_t note_id) { if (note_id >= voices->size) { return; } @@ -108,14 +108,14 @@ void set_note_on(synth_params *params, synth_voices *voices, size_t note_id) { params->last_freq = voices->buffer[note_id].freq; } -void set_note_off(synth_voices *voices, size_t note_id) { +void set_note_off(SynthVoices *voices, size_t note_id) { if (note_id >= voices->size) { return; } envelope_note_off(&voices->buffer[note_id].envelope); } -void set_all_notes_off(synth_voices *voices) { +void set_all_notes_off(SynthVoices *voices) { for (size_t i = 0; i < voices->size; i++) { if (voices->buffer[i].active) { voices->buffer[i].active = false; @@ -123,7 +123,7 @@ void set_all_notes_off(synth_voices *voices) { } } -void set_param(synth_params *params, param_type type, float value) { +void set_param(SynthParams *params, ParamType type, float value) { switch (type) { case PARAM_OSC: { params->oscilator_type = (int)value; @@ -152,10 +152,10 @@ void set_param(synth_params *params, param_type type, float value) { } } -void generate_voices(synth_voices *voices, synth_params *params, float *buffer, size_t buffer_size) { - oscilator_func oscilator = osc_get(params->oscilator_type); +void generate_voices(SynthVoices *voices, SynthParams *params, float *buffer, size_t buffer_size) { + OscilatorFunc oscilator = osc_get(params->oscilator_type); for (size_t i = 0; i < voices->size; i++) { - synth_voice *voice = &voices->buffer[i]; + SynthVoice *voice = &voices->buffer[i]; if (!voice->active) { continue; } @@ -180,7 +180,7 @@ void generate_voices(synth_voices *voices, synth_params *params, float *buffer, } } -void print_wave(oscilator_func osc, float freq, size_t size) { +void print_wave(OscilatorFunc osc, float freq, size_t size) { float phase = 0; float phase_inc = 2 * M_PI * freq / SAMPLE_RATE; @@ -196,7 +196,7 @@ void print_wave(oscilator_func osc, float freq, size_t size) { printf("}\n"); } -void post_process(synth_params *params, float *scratch_buffer, +void post_process(SynthParams *params, float *scratch_buffer, size_t buffer_size) { for (size_t i = 0; i < buffer_size; i++) { scratch_buffer[i] *= params->master_volume; @@ -210,15 +210,15 @@ void prepare_output(float *scratch_buffer, short *output_buffer, } } -void sound_loop_start(snd_pcm_t *pcm, message_queue *queue, WaveData *wave_data, synth_voices *voices) { - synth_params params; +void sound_loop_start(snd_pcm_t *pcm, MessageQueue *queue, WaveData *wave_data, SynthVoices *voices) { + SynthParams params; short output_buffer[PERIOD_SIZE]; float scratch_buffer[PERIOD_SIZE]; float display_buffer[DISPLAY_SAMPLES]; size_t display_write_pos = 0; while (true) { - synth_message msg; + SynthMessage msg; while (mqueue_get(queue, &msg) == 0) { switch (msg.type) { case MSG_NOTE_ON: { @@ -236,7 +236,7 @@ void sound_loop_start(snd_pcm_t *pcm, message_queue *queue, WaveData *wave_data, break; } case MSG_PARAM_CHANGE: { - param_type type = msg.param_change.param_type; + ParamType type = msg.param_change.param_type; float value = msg.param_change.value; set_param(¶ms, type, value); break; @@ -287,9 +287,9 @@ stop: return; } -void fill_voices(synth_voice *voices, MidiNote *freqs, size_t freqs_amount) { +void fill_voices(SynthVoice *voices, MidiNote *freqs, size_t freqs_amount) { for (size_t i = 0; i < freqs_amount; i++) { - voices[i] = (synth_voice){ + voices[i] = (SynthVoice){ .active = false, .freq = freqs[i].freq, .phase = 0, @@ -300,12 +300,12 @@ void fill_voices(synth_voice *voices, MidiNote *freqs, size_t freqs_amount) { } void *sound_thread_start(void *ptr) { - sound_thread_meta *meta = ptr; + SoundThreadMeta *meta = ptr; - synth_voice buffer[12]; + SynthVoice buffer[12]; fill_voices(buffer, &midi_freqs[49], 12); - synth_voices voices = { + SynthVoices voices = { .buffer = buffer, .size = 12, }; diff --git a/src/sounds.h b/src/sounds.h index 2d185d6..639ce80 100644 --- a/src/sounds.h +++ b/src/sounds.h @@ -23,9 +23,9 @@ typedef struct { snd_pcm_t *pcm; - message_queue *queue; + MessageQueue *queue; WaveData *wave_data; -} sound_thread_meta; +} SoundThreadMeta; typedef enum { ENV_OFF, @@ -33,44 +33,44 @@ typedef enum { ENV_DECAY, ENV_SUSTAIN, ENV_RELEASE, -} envelope_state; +} EnvelopeState; typedef struct { int attack_time; int decay_time; float sustain_level; int release_time; -} envelope_params; +} EnvelopeParams; typedef struct { - envelope_state state; + EnvelopeState state; int counter; float current_inc; float release_value; - envelope_params params; -} envelope; + EnvelopeParams params; +} Envelope; typedef struct { bool active; float freq; float phase; float phase_inc; - envelope envelope; -} synth_voice; + Envelope envelope; +} SynthVoice; typedef struct { - synth_voice *buffer; + SynthVoice *buffer; size_t size; -} synth_voices; +} SynthVoices; typedef struct { - oscilator_type oscilator_type; + OscilatorType oscilator_type; float master_volume; - envelope_params envelope_params; + EnvelopeParams envelope_params; float last_freq; -} synth_params; +} SynthParams; -typedef float (*oscilator_func)(float phase); +typedef float (*OscilatorFunc)(float phase); void *sound_thread_start(void *ptr); int set_hw_params(snd_pcm_t *pcm); @@ -76,7 +76,7 @@ void handle_knob_press(Clay_ElementId element_id, Clay_PointerData pointer_info, float value = denormalize_value(normalized_value, start, end); knob->info->value = value; - mqueue_push(ui_data->msg_queue, (synth_message){ + mqueue_push(ui_data->msg_queue, (SynthMessage){ .type = MSG_PARAM_CHANGE, .param_change = { .param_type = knob->info->param_type, @@ -94,7 +94,7 @@ void handle_key_press(Clay_ElementId element_id, Clay_PointerData pointer_info, if (!pressed && (pointer_info.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME || pointer_info.state == CLAY_POINTER_DATA_PRESSED)) { - mqueue_push(ui_data->msg_queue, (synth_message){ + mqueue_push(ui_data->msg_queue, (SynthMessage){ .type = MSG_NOTE_ON, .note = { .note_id = idx, @@ -106,7 +106,7 @@ void handle_key_press(Clay_ElementId element_id, Clay_PointerData pointer_info, if (pressed && (pointer_info.state == CLAY_POINTER_DATA_RELEASED_THIS_FRAME || pointer_info.state == CLAY_POINTER_DATA_RELEASED)) { if (!ui_data->keys[idx].keyboard_pressed) { - mqueue_push(ui_data->msg_queue, (synth_message){ + mqueue_push(ui_data->msg_queue, (SynthMessage){ .type = MSG_NOTE_OFF, .note = { .note_id = idx, @@ -124,7 +124,7 @@ void draw_white_key(size_t idx, UIData *ui_data) { bool hovered = Clay_Hovered(); if (!hovered && mouse_pressed && !keyboard_pressed) { - mqueue_push(ui_data->msg_queue, (synth_message){ + mqueue_push(ui_data->msg_queue, (SynthMessage){ .type = MSG_NOTE_OFF, .note = { .note_id = idx, @@ -180,7 +180,7 @@ void draw_black_key(size_t idx, UIData *ui_data) { bool hovered = Clay_Hovered(); if (!hovered && mouse_pressed && !keyboard_pressed) { - mqueue_push(ui_data->msg_queue, (synth_message){ + mqueue_push(ui_data->msg_queue, (SynthMessage){ .type = MSG_NOTE_OFF, .note = { .note_id = idx, @@ -31,7 +31,7 @@ typedef struct { typedef struct { float value; - param_type param_type; + ParamType param_type; float range_start; float range_end; } KnobInfo; @@ -46,7 +46,7 @@ typedef struct { typedef struct { Arena *arena; - message_queue *msg_queue; + MessageQueue *msg_queue; float *wave_buffer; size_t wave_buffer_size; |
