Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions include/osc.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#pragma once
#include "config.h"

typedef struct {
double phase; // current phase (in table index units)
double phase_inc; // phase increment per sample
int wt_index; // index into the shared wavetable array
} Osc;

// Create an oscillator (here, allocation is done in State_create)
Osc Osc_create(int wt_index, double freq);

// Update an oscillator's frequency.
void Osc_set_freq(Osc *osc, double freq);

// (No Osc_destroy is needed since the array is owned by State.)
1 change: 1 addition & 0 deletions src/osc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "osc.h"
#include "config.h"

Osc Osc_create(int wt_index, double freq) {
Osc osc;
Expand Down
1 change: 0 additions & 1 deletion src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ float State_mix_sample(State *state) {
if (osc->phase >= len)
osc->phase -= len;
}
// Average the oscillators in this voice.
voice_sum /= NUM_OSCS;
mix += voice_sum;
}
Expand Down
9 changes: 2 additions & 7 deletions tests/test_vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Test(vec, int) {
cr_assert_eq(vec->size, (size_t)(i + 1), "After push %d, vec size should be %d", i + 1, i + 1);
}

// Verify that each stored int matches its expected value.
// Verify each stored int matches its expected value.
for (size_t i = 0; i < vec->size; i++) {
int value = 0;
Vec_get(vec, i, &value);
Expand All @@ -24,28 +24,23 @@ Test(vec, int) {
}

Test(vec, osc) {
// Create a vector for Osc pointers.
// We are storing Osc* elements, so the element size is sizeof(Osc*).
// The destroy function is set to Osc_destroy so that each Osc is cleaned up.
Vec *vec = Vec_create(sizeof(Osc*), (ElemDestroyFunc)Osc_destroy);
cr_assert_not_null(vec, "Vec_create returned NULL for Osc vector");
cr_assert_eq(vec->size, 0, "Initial Osc vector size should be 0");

const int numOsc = 1;
for (int i = 0; i < numOsc; i++) {
// Create an oscillator with varying parameters.
Osc *osc = Osc_create(WAVEFORM_SINE + (i % 2), 256 * (i + 1), 220.0 + (i * 10));
Vec_push_back(vec, &osc);
cr_assert_eq(vec->size, (size_t)(i + 1), "After push %d, Osc vector size should be %d", i + 1, i + 1);
}


// Verify that each stored Osc pointer is not NULL.
// Verify each stored Osc pointer is not NULL.
for (size_t i = 0; i < vec->size; i++) {
Osc *osc_ptr = NULL;
Vec_get(vec, i, &osc_ptr);
cr_assert_not_null(osc_ptr, "Osc element at index %zu should not be NULL", i);
// Optionally, you can add more detailed tests of the Osc's properties here.
}

Vec_destroy(vec);
Expand Down
5 changes: 0 additions & 5 deletions tests/test_vec_macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,27 @@ Test(VecOscPtr, create_push_get_destroy) {
cr_assert_not_null(ov, "Vec_OscPtr_create returned NULL");
cr_assert_eq(ov->vec->size, 0, "Initial vector size should be 0");

// Create two oscillators with different properties.
Osc *osc1 = Osc_create(WAVEFORM_SINE, 256, 440.0);
cr_assert_not_null(osc1, "Osc_create returned NULL for osc1");
Osc *osc2 = Osc_create(WAVEFORM_SAW, 512, 220.0);
cr_assert_not_null(osc2, "Osc_create returned NULL for osc2");

// Push the oscillators into the typed vector.
Vec_OscPtr_push_back(ov, osc1);
cr_assert_eq(ov->vec->size, 1, "After pushing osc1, size should be 1");
Vec_OscPtr_push_back(ov, osc2);
cr_assert_eq(ov->vec->size, 2, "After pushing osc2, size should be 2");

// Retrieve the stored oscillators.
Osc *retrieved1 = Vec_OscPtr_get(ov, 0);
Osc *retrieved2 = Vec_OscPtr_get(ov, 1);
cr_assert_not_null(retrieved1, "Retrieved oscillator 1 should not be NULL");
cr_assert_not_null(retrieved2, "Retrieved oscillator 2 should not be NULL");

// Check that the retrieved oscillators have the expected properties.
cr_assert_eq(retrieved1->wt->type, WAVEFORM_SINE, "Expected osc1 to have WAVEFORM_SINE");
cr_assert_eq(retrieved1->wt->length, 256, "Expected osc1 wavetable length to be 256");

cr_assert_eq(retrieved2->wt->type, WAVEFORM_SAW, "Expected osc2 to have WAVEFORM_SAW");
cr_assert_eq(retrieved2->wt->length, 512, "Expected osc2 wavetable length to be 512");

// Verify phase and phase_inc for osc1.
cr_assert_float_eq(retrieved1->phase, 0.0, 0.0001, "Initial phase for osc1 should be 0");
double expected_phase_inc1 = (TABLE_SIZE * 440.0) / SAMPLE_RATE;
cr_assert_float_eq(retrieved1->phase_inc, expected_phase_inc1, 0.0001,
Expand Down