From 210dc81b251b491144b401f699f8221ba783f9e0 Mon Sep 17 00:00:00 2001 From: rkcf Date: Fri, 24 Feb 2023 17:56:27 -0600 Subject: [PATCH] Feat: add sine wave lookup table generation Refs: #12 --- src/main/CMakeLists.txt | 2 +- src/main/esp32_eurorack_generic.c | 5 +++++ src/main/wavetable.c | 35 +++++++++++++++++++++++++++++++ src/main/wavetable.h | 7 +++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/main/wavetable.c create mode 100644 src/main/wavetable.h diff --git a/src/main/CMakeLists.txt b/src/main/CMakeLists.txt index b48d63e..cf31283 100644 --- a/src/main/CMakeLists.txt +++ b/src/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "esp32_eurorack_generic.c" +idf_component_register(SRCS "esp32_eurorack_generic.c" "wavetable.c" INCLUDE_DIRS ".") diff --git a/src/main/esp32_eurorack_generic.c b/src/main/esp32_eurorack_generic.c index e36297c..f0110d0 100644 --- a/src/main/esp32_eurorack_generic.c +++ b/src/main/esp32_eurorack_generic.c @@ -8,6 +8,8 @@ #include "freertos/portmacro.h" #include "hal/i2c_types.h" +#include "wavetable.h" + /* DAC Peripheral Config */ #define DAC_SCL_PIN 12 #define DAC_SDA_PIN 13 @@ -117,6 +119,9 @@ void app_main(void) ESP_LOGI(TAG, "Sucessfully initialized DAC"); } + /* Generate wave tables */ + gen_wavetables(); + /* Loop to test DAC output */ while (1) { dac_out_test(); diff --git a/src/main/wavetable.c b/src/main/wavetable.c new file mode 100644 index 0000000..20340fd --- /dev/null +++ b/src/main/wavetable.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +#include "esp_log.h" + +#include "wavetable.h" + +#define SAMPLE_RATE 22000 /* 22kHz */ +#define FREQ 440 +#define TIME 1 + +float_t *sin_tbl; + +static const char *TAG = "wavetable"; + +static void gen_sin_table(void) +{ + /* Allocate memory */ + uint16_t table_length = 1024; /* TODO make this accurate to sample rate and frequency */ + sin_tbl = (float *)malloc(sizeof(float) * table_length); + + /* Loop through values and store in table */ + for (uint16_t i = 0; i < table_length; i++) { + sin_tbl[i] = sin((2 * M_PI * i) / table_length); + } +} + +void gen_wavetables(void) +{ + ESP_LOGI(TAG, "Generating sine wavetable"); + gen_sin_table(); +} diff --git a/src/main/wavetable.h b/src/main/wavetable.h new file mode 100644 index 0000000..b173928 --- /dev/null +++ b/src/main/wavetable.h @@ -0,0 +1,7 @@ +#ifndef WAVETABLE_H_ /* Guard */ +#define WAVETABLE_H_ + +/* Generates all wavetables */ +void gen_wavetables(void); + +#endif