fix: asserts && current note pointer
This commit is contained in:
32
main.c
32
main.c
@@ -93,6 +93,8 @@ static OscillatorArray init_osc_array() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Oscillator* oscArray = malloc(sizeof(Oscillator*) * 1);
|
Oscillator* oscArray = malloc(sizeof(Oscillator*) * 1);
|
||||||
|
assert(oscArray);
|
||||||
|
|
||||||
oscArray[0] = first;
|
oscArray[0] = first;
|
||||||
|
|
||||||
OscillatorArray oscillators = {
|
OscillatorArray oscillators = {
|
||||||
@@ -160,6 +162,10 @@ size_t detect_note_pressed(Note* note) {
|
|||||||
// GUI
|
// GUI
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void note_on(Synth *synth, Note *note) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void DrawUi(Synth *synth) {
|
void DrawUi(Synth *synth) {
|
||||||
const int panel_x_start = 0;
|
const int panel_x_start = 0;
|
||||||
const int panel_y_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++)
|
for (int ui_osc_i = 0; ui_osc_i < synth->oscillators.count; ui_osc_i++)
|
||||||
{
|
{
|
||||||
OscillatorUI* ui_osc = &synth->ui_oscillators[ui_osc_i];
|
OscillatorUI* ui_osc = &synth->ui_oscillators[ui_osc_i];
|
||||||
|
assert(ui_osc);
|
||||||
|
|
||||||
Oscillator* osc = &synth->oscillators.array[ui_osc_i];
|
Oscillator* osc = &synth->oscillators.array[ui_osc_i];
|
||||||
|
assert(osc);
|
||||||
|
|
||||||
const bool has_shape_param = (ui_osc->waveshape == Square);
|
const bool has_shape_param = (ui_osc->waveshape == Square);
|
||||||
|
|
||||||
// Draw Oscillator Panel
|
// 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)
|
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];
|
OscillatorUI* ui_osc = &synth->ui_oscillators[ui_osc_i];
|
||||||
|
assert(ui_osc);
|
||||||
|
|
||||||
Oscillator* osc = &synth->oscillators.array[ui_osc_i];
|
Oscillator* osc = &synth->oscillators.array[ui_osc_i];
|
||||||
|
assert(osc);
|
||||||
|
|
||||||
// Shape select
|
// Shape select
|
||||||
int shape_index = (int)(ui_osc->waveshape);
|
int shape_index = (int)(ui_osc->waveshape);
|
||||||
bool is_dropdown_click = GuiDropdownBox(ui_osc->shape_dropdown_rect,
|
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++)
|
for (size_t i = 0; i < synth.oscillators.count; i++)
|
||||||
{
|
{
|
||||||
OscillatorUI* ui = &synth.ui_oscillators[i];
|
OscillatorUI* ui = &synth.ui_oscillators[i];
|
||||||
|
assert(ui);
|
||||||
|
|
||||||
ui->freq = synth.oscillators.array[i].freq;
|
ui->freq = synth.oscillators.array[i].freq;
|
||||||
ui->waveshape = synth.oscillators.array[i].osc;
|
ui->waveshape = synth.oscillators.array[i].osc;
|
||||||
ui->volume = synth.oscillators.array[i].volume;
|
ui->volume = synth.oscillators.array[i].volume;
|
||||||
@@ -353,9 +369,11 @@ int main(int argc, char **argv) {
|
|||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Fill ring buffer from current sound
|
// Fill ring buffer from current sound
|
||||||
SynthSound* sound = synth.out_signal;
|
SynthSound* sound = synth.out_signal;
|
||||||
|
assert(sound);
|
||||||
|
|
||||||
size_t size_for_buffer = 0;
|
size_t size_for_buffer = 0;
|
||||||
if (!ring_buffer.is_full && sound->sample_count != sound_played_count) {
|
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,
|
ring_buffer.is_full,
|
||||||
sound->sample_count,
|
sound->sample_count,
|
||||||
sound_played_count);
|
sound_played_count);
|
||||||
@@ -396,11 +414,11 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
// Update On Input
|
// Update On Input
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
Note current_note = synth.current_note;
|
Note* current_note = &synth.current_note;
|
||||||
if (detect_note_pressed(¤t_note)) {
|
if (detect_note_pressed(current_note)) {
|
||||||
*sound = get_note_sound(&synth, current_note);
|
*sound = get_note_sound(&synth, *current_note);
|
||||||
sound_played_count = 0;
|
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));
|
NoteArray note_array = parse_notes(buf, strlen(buf));
|
||||||
SynthSound* sounds = malloc(sizeof(SynthSound) * note_array.count);
|
SynthSound* sounds = malloc(sizeof(SynthSound) * note_array.count);
|
||||||
|
assert(sounds);
|
||||||
|
|
||||||
for (size_t i = 0; i < note_array.count; i++) {
|
for (size_t i = 0; i < note_array.count; i++) {
|
||||||
Note note = note_array.notes[i];
|
Note note = note_array.notes[i];
|
||||||
sounds[i] = get_note_sound(&synth, note);
|
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);
|
SynthSound song = concat_sounds(sounds, note_array.count);
|
||||||
uint16_t* song_pcm = malloc(sizeof(uint16_t) * song.sample_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++) {
|
for (size_t i = 0; i < song.sample_count; i++) {
|
||||||
song_pcm[i] = toInt16Sample(song.samples[i]);
|
song_pcm[i] = toInt16Sample(song.samples[i]);
|
||||||
}
|
}
|
||||||
|
|||||||
14
oscillator.c
14
oscillator.c
@@ -103,6 +103,8 @@ float multiosc(OscillatorGenerationParameter param) {
|
|||||||
float osc_sample = 0.f;
|
float osc_sample = 0.f;
|
||||||
for (size_t i = 0; i < param.oscillators.count; i++) {
|
for (size_t i = 0; i < param.oscillators.count; i++) {
|
||||||
Oscillator* osc = ¶m.oscillators.array[i];
|
Oscillator* osc = ¶m.oscillators.array[i];
|
||||||
|
assert(osc);
|
||||||
|
|
||||||
switch (osc->osc) {
|
switch (osc->osc) {
|
||||||
case Sine:
|
case Sine:
|
||||||
osc_sample += sineosc(osc) * osc->volume;
|
osc_sample += sineosc(osc) * osc->volume;
|
||||||
@@ -123,21 +125,19 @@ float multiosc(OscillatorGenerationParameter param) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SynthSound freq(float duration, OscillatorArray osc) {
|
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);
|
float* output = malloc(sizeof(float) * sample_count);
|
||||||
for (int i = 0; i < samples.sample_count; i++) {
|
for (size_t i = 0; i < sample_count; i++) {
|
||||||
float sample = samples.samples[i];
|
|
||||||
OscillatorGenerationParameter param = {
|
OscillatorGenerationParameter param = {
|
||||||
.oscillators = osc,
|
.oscillators = osc
|
||||||
.sample = sample
|
|
||||||
};
|
};
|
||||||
output[i] = multiosc(param);
|
output[i] = multiosc(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
SynthSound res = {
|
SynthSound res = {
|
||||||
.samples = output,
|
.samples = output,
|
||||||
.sample_count = samples.sample_count
|
.sample_count = sample_count
|
||||||
};
|
};
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ typedef struct OscillatorArray {
|
|||||||
|
|
||||||
typedef struct OscillatorGenerationParameter {
|
typedef struct OscillatorGenerationParameter {
|
||||||
OscillatorArray oscillators;
|
OscillatorArray oscillators;
|
||||||
float sample;
|
|
||||||
} OscillatorGenerationParameter;
|
} OscillatorGenerationParameter;
|
||||||
|
|
||||||
void osc_set_freq(Oscillator* osc, float freq);
|
void osc_set_freq(Oscillator* osc, float freq);
|
||||||
|
|||||||
Reference in New Issue
Block a user