diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2023-07-07 01:09:12 +0300 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2023-07-07 01:09:12 +0300 |
commit | c8cd7f9298de876f2046fddd2e322a63c421a505 (patch) | |
tree | 1b764fed928592f8f3465e9ef28552b451efad8a /src/osc.c | |
parent | f170fb058a07175e6e753f2a6d20283dc7200a88 (diff) | |
download | synth-project-c8cd7f9298de876f2046fddd2e322a63c421a505.tar.gz synth-project-c8cd7f9298de876f2046fddd2e322a63c421a505.tar.bz2 synth-project-c8cd7f9298de876f2046fddd2e322a63c421a505.zip |
suppa
Diffstat (limited to 'src/osc.c')
-rw-r--r-- | src/osc.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/osc.c b/src/osc.c new file mode 100644 index 0000000..295c83d --- /dev/null +++ b/src/osc.c @@ -0,0 +1,97 @@ +#include "synth_common.h" +#include "osc.h" + +#include <sndfile.h> +#include <stdlib.h> +#include <stddef.h> + + +float +osc_interpolate(float offset, float start, float end) { + // linear interpolation between start and end + float m = (end - start) / (1 - 0); + float b = start /* + m * 0 */; + + // the value to pick + float x = offset - (int)offset; + + return m * x + b; +} + +int +osc_next_index(osc_t * osc, float offset) +{ + if (offset >= osc->len) offset = 0; + + int current_index = (int)offset; + return current_index + 1 >= osc->len ? 0 : current_index + 1; +} + + +float +osc_next_offset(osc_t * osc, float f, float offset) +{ + if (offset >= osc->len) offset = 0; + + if (osc->type == SAMPLE) { + // this makes A2 be the base ??? + // A2 = 220.0 Hz is base ??? + // get this from osc* + f = f / (16.35160 * pow(2, (2 + 5 / 12.0))); + } + + float step = f * ((float)osc->len / SAMPLE_RATE); + + offset += step; + if (offset >= osc->len) { + offset -= osc->len; + } + + return offset; +} + +int +osc_load_wav(osc_t * osc, const char * path) +{ + SNDFILE* file; + SF_INFO fileInfo; + int numSamplesRead; + + // Open the WAV file + file = sf_open(path, SFM_READ, &fileInfo); + if (!file) { + printf("Failed to open the WAV file: %s\n", sf_strerror(NULL)); + return -1; + } + + printf("Opened %s: \n", path); + printf("frames: %ld sr: %d chan: %d format: %d sections: %d seekable: %d\n", fileInfo.frames, fileInfo.samplerate, fileInfo.channels, fileInfo.format, fileInfo.sections, fileInfo.seekable); + + // Ensure the WAV file has only one channel + if (fileInfo.channels != 1) { + printf("Only mono WAV files are supported.\n"); + sf_close(file); + return -1; + } + + osc->len = fileInfo.frames; + osc->end = fileInfo.frames; + osc->data = (float *) malloc(sizeof(float) * fileInfo.frames); + + // Read the WAV file into the buffer + numSamplesRead = sf_readf_float(file, osc->data, fileInfo.frames); + + /* float max = -1000; */ + /* float min = 1000; */ + /* for (int i = 0; i < numSamplesRead; i++) { */ + /* float s = osc_sound_data[i]; */ + /* // printf("Sample %d: %f\n", i, s); */ + /* if (s < min) min = s; */ + /* if (s > max) max = s; */ + /* } */ + /* printf("Min: %f Max: %f\n", min, max); */ + + // Close the WAV file + sf_close(file); + return 0; +} |