diff --git a/main.c b/main.c index 8eb1e74..1f5e54c 100644 --- a/main.c +++ b/main.c @@ -93,6 +93,8 @@ static OscillatorArray init_osc_array() { }; Oscillator* oscArray = malloc(sizeof(Oscillator*) * 1); + assert(oscArray); + oscArray[0] = first; OscillatorArray oscillators = { @@ -160,6 +162,10 @@ size_t detect_note_pressed(Note* note) { // GUI //------------------------------------------------------------------------------------ +void note_on(Synth *synth, Note *note) { + +} + void DrawUi(Synth *synth) { const int panel_x_start = 0; const int panel_y_start = 0; @@ -201,7 +207,11 @@ void DrawUi(Synth *synth) { for (int ui_osc_i = 0; ui_osc_i < synth->oscillators.count; ui_osc_i++) { OscillatorUI* ui_osc = &synth->ui_oscillators[ui_osc_i]; + assert(ui_osc); + Oscillator* osc = &synth->oscillators.array[ui_osc_i]; + assert(osc); + const bool has_shape_param = (ui_osc->waveshape == Square); // Draw Oscillator Panel @@ -268,7 +278,11 @@ void DrawUi(Synth *synth) { for (int ui_osc_i = 0; ui_osc_i < synth->oscillators.count; ui_osc_i += 1) { OscillatorUI* ui_osc = &synth->ui_oscillators[ui_osc_i]; + assert(ui_osc); + Oscillator* osc = &synth->oscillators.array[ui_osc_i]; + assert(osc); + // Shape select int shape_index = (int)(ui_osc->waveshape); bool is_dropdown_click = GuiDropdownBox(ui_osc->shape_dropdown_rect, @@ -329,6 +343,8 @@ int main(int argc, char **argv) { for (size_t i = 0; i < synth.oscillators.count; i++) { OscillatorUI* ui = &synth.ui_oscillators[i]; + assert(ui); + ui->freq = synth.oscillators.array[i].freq; ui->waveshape = synth.oscillators.array[i].osc; ui->volume = synth.oscillators.array[i].volume; @@ -353,9 +369,11 @@ int main(int argc, char **argv) { //---------------------------------------------------------------------------------- // Fill ring buffer from current sound SynthSound* sound = synth.out_signal; + assert(sound); + size_t size_for_buffer = 0; if (!ring_buffer.is_full && sound->sample_count != sound_played_count) { - write_log("[INFO] IsFull:%d Samples:%zu Played:%zu\n", + write_log("[INFO] IsFull:%d Samples:%zu Played:%d\n", ring_buffer.is_full, sound->sample_count, sound_played_count); @@ -396,11 +414,11 @@ int main(int argc, char **argv) { // Update On Input //---------------------------------------------------------------------------------- - Note current_note = synth.current_note; - if (detect_note_pressed(¤t_note)) { - *sound = get_note_sound(&synth, current_note); + Note* current_note = &synth.current_note; + if (detect_note_pressed(current_note)) { + *sound = get_note_sound(&synth, *current_note); sound_played_count = 0; - write_log("Note played: %s\n", current_note.name); + write_log("Note played: %s\n", current_note->name); } //---------------------------------------------------------------------------------- @@ -424,6 +442,8 @@ int main(int argc, char **argv) { NoteArray note_array = parse_notes(buf, strlen(buf)); SynthSound* sounds = malloc(sizeof(SynthSound) * note_array.count); + assert(sounds); + for (size_t i = 0; i < note_array.count; i++) { Note note = note_array.notes[i]; sounds[i] = get_note_sound(&synth, note); @@ -431,6 +451,8 @@ int main(int argc, char **argv) { SynthSound song = concat_sounds(sounds, note_array.count); uint16_t* song_pcm = malloc(sizeof(uint16_t) * song.sample_count); + assert(song_pcm); + for (size_t i = 0; i < song.sample_count; i++) { song_pcm[i] = toInt16Sample(song.samples[i]); } diff --git a/oscillator.c b/oscillator.c index 9e6bd42..8cb58e3 100644 --- a/oscillator.c +++ b/oscillator.c @@ -103,6 +103,8 @@ float multiosc(OscillatorGenerationParameter param) { float osc_sample = 0.f; for (size_t i = 0; i < param.oscillators.count; i++) { Oscillator* osc = ¶m.oscillators.array[i]; + assert(osc); + switch (osc->osc) { case Sine: osc_sample += sineosc(osc) * osc->volume; @@ -123,21 +125,19 @@ float multiosc(OscillatorGenerationParameter param) { } SynthSound freq(float duration, OscillatorArray osc) { - SynthSound samples = get_init_samples(duration); + size_t sample_count = (size_t)(duration * SAMPLE_RATE); - float* output = malloc(sizeof(float) * samples.sample_count); - for (int i = 0; i < samples.sample_count; i++) { - float sample = samples.samples[i]; + float* output = malloc(sizeof(float) * sample_count); + for (size_t i = 0; i < sample_count; i++) { OscillatorGenerationParameter param = { - .oscillators = osc, - .sample = sample + .oscillators = osc }; output[i] = multiosc(param); } SynthSound res = { .samples = output, - .sample_count = samples.sample_count + .sample_count = sample_count }; return res; diff --git a/oscillator.h b/oscillator.h index 410994b..8ac30be 100644 --- a/oscillator.h +++ b/oscillator.h @@ -25,7 +25,6 @@ typedef struct OscillatorArray { typedef struct OscillatorGenerationParameter { OscillatorArray oscillators; - float sample; } OscillatorGenerationParameter; void osc_set_freq(Oscillator* osc, float freq); diff --git a/utils.h b/utils.h index 287b8bc..ac30032 100644 --- a/utils.h +++ b/utils.h @@ -2,6 +2,7 @@ #define UTILS_H #include "stdio.h" +#include "assert.h" #define write_log(format,args...) do { \ printf(format, ## args); \