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
9 changes: 3 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ else()
endif()

# List your source files
set(SOURCES
src/main.c
src/osc.c
src/vec.c
src/wavetable.c
file(GLOB SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/src/*"
)

# Create the executable target
Expand All @@ -39,7 +36,7 @@ enable_testing()

# Define unit test sources
file(GLOB TEST_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/tests/*"
# "${CMAKE_CURRENT_SOURCE_DIR}/tests/*"
"${CMAKE_CURRENT_SOURCE_DIR}/src/*"
)
list(REMOVE_ITEM TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/main.c")
Expand Down
13 changes: 7 additions & 6 deletions include/osc.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#pragma once
#include "vec.h"
#include "wavetable.h"
#include "config.h"

typedef struct {
double phase; // current phase (in table index units)
double phase_inc; // phase increment per sample
Wavetable *wt; // pointer to this oscillator's wavetable
int wt_index; // index into the shared wavetable array
} Osc;

Osc *Osc_create(Waveform type, size_t length, double freq);
// 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);
void Osc_destroy(Osc *osc);

DECLARE_VEC_TYPE(Osc *, OscPtr, (ElemDestroyFunc)Osc_destroy)
// (No Osc_destroy is needed since the array is owned by State.)
26 changes: 26 additions & 0 deletions include/state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include "osc.h"
#include "wavetable.h"

// Fixed constants.
extern const int NUM_OSCS; // oscillators per voice (e.g., 4)
extern const int NUM_WAVETABLES; // number of shared wavetables (e.g., 4)
extern const int NUM_VOICES; // maximum polyphony (e.g., 8)

typedef struct {
Osc *oscs; // array of oscillators; size = NUM_VOICES * NUM_OSCS
Wavetable *wts; // shared array of NUM_WAVETABLES wavetables
float *wt_levels; // per-wavetable level multipliers; array of NUM_WAVETABLES floats
int *active; // for each voice (size NUM_VOICES), 1 if active, 0 if not
} State;

State *State_create(void);
void State_destroy(State *state);

// For a given voice (0-indexed), set the note (all oscillators in that voice).
void State_set_note(State *state, int voice, double freq);
// Clear (turn off) a given voice.
void State_clear_voice(State *state, int voice);

// Mix and return one audio sample.
float State_mix_sample(State *state);
24 changes: 3 additions & 21 deletions include/wavetable.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
#pragma once
#include "stddef.h"
#include "vec.h"
#include <stdlib.h>

typedef enum {
WAVEFORM_SINE,
WAVEFORM_SQUARE,
WAVEFORM_SAW,
WAVEFORM_TRIANGLE,
WAVEFORM_CUSTOM // Option to use custom waveform data
} Waveform;
typedef enum { WAVEFORM_SINE, WAVEFORM_SAW, WAVEFORM_SQUARE, WAVEFORM_TRIANGLE } Waveform;

typedef struct {
float *data;
size_t length; // Number of samples in the wavetable.
size_t length;
Waveform type;
} Wavetable;

Wavetable *Wavetable_create(Waveform type, size_t length);
void Wavetable_set_custom(Wavetable *wt, float *data, size_t length);
void Wavetable_destroy(Wavetable *wt);

typedef struct {
Vec *vec;
} WtVec;

WtVec *WtVec_create(void);
void WtVec_push(WtVec *wv, Wavetable *wt);
Wavetable *WtVec_get(const WtVec *wv, size_t index);
size_t WtVec_size(const WtVec *wv);
void WtVec_destroy(WtVec *wv);
Loading