From 02c0552df3bbdfc9b2f63c86729c2ca71c8ccf21 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 6 Sep 2023 19:27:51 +0200 Subject: [PATCH] audio engine:double buffering working nicely now --- src/cppmain.cc | 66 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/src/cppmain.cc b/src/cppmain.cc index 173e37f..90862b9 100644 --- a/src/cppmain.cc +++ b/src/cppmain.cc @@ -7,21 +7,23 @@ #include #include "limits" #include +#include #define SAMPLE_FREQ 44200 #define SAMPLE_MAX 4096 -#define BUFFER_SIZE 16 -#define BLOCK_SIZE 8 +#define BUFFER_SIZE 256 +#define BLOCK_SIZE 128 const double pi{ std::acos(-1) }; // Buffer and block static u32 audio_buffer[BUFFER_SIZE]; -static u32* block = &audio_buffer[0]; -static volatile bool process_block{ false }; +static u32 *block = &audio_buffer[0]; +static volatile bool process_block{ true }; +static volatile bool process_block2{ true }; // time -static u64 time{ 0 }; +static u64 inf_phasor{ 0 }; // misc static volatile int freq = 100; @@ -40,7 +42,10 @@ namespace Heck { { std::string out{ msg }; out.append("\r\n"); - CDC_Transmit_FS((uint8_t *)out.data(), out.size()); + u8 status = CDC_Transmit_FS((uint8_t *)out.data(), out.size()); + if (status == USBD_BUSY) { + // usb buffer overflow + } } // LED @@ -72,8 +77,6 @@ namespace Heck { led_green_toggle(); } - // dont forget to start the DAC - // HAL_DAC_Start(&hdac, DAC_CHANNEL_1); void bytebeat() { long t{ 0 }; @@ -100,7 +103,16 @@ namespace Heck { for (int i = 0; i < BUFFER_SIZE; i++) { u32 val = random(SAMPLE_MAX); audio_buffer[i] = val; - log(std::to_string(val)); + } + } + + void block_fill_saw() + { + u32 *b = block; + u32 step = (SAMPLE_MAX - (SAMPLE_MAX / 4)) / BLOCK_SIZE; + for (int i = 0; i < BLOCK_SIZE; i++) { + u32 val = i * step; + b[i] = val; } } @@ -111,7 +123,6 @@ namespace Heck { float p = float(i) / (float)BUFFER_SIZE - 0.5; u32 val = sin(p) * SAMPLE_MAX; audio_buffer[i] = val; - log(std::to_string(val)); } } @@ -121,7 +132,6 @@ namespace Heck { for (int i = 0; i < BUFFER_SIZE; i++) { u32 val = audio_buffer[i] / div; audio_buffer[i] = val; - log(std::to_string(val)); } } @@ -131,7 +141,6 @@ namespace Heck { u32 buf_index = random(BUFFER_SIZE); u32 buf_val = random(max); audio_buffer[buf_index] = buf_val; - log("i:" + std::to_string(buf_index) + " v:" + std::to_string(buf_val)); } std::string reg_to_string(uint32_t val) @@ -152,31 +161,43 @@ namespace Heck { void calculate_audio() { -// log("calculate_audio time: "); - u64 t = time; + // log("calculate_audio time: "); + u32 *b = block; + u64 t = inf_phasor; u32 samps = hz_to_samps(freq); - + u32 val{ 0 }; for (int i = 0; i < BLOCK_SIZE; i++) { - block[i] = t % samps; + val = t % samps; + b[i] = val * 8; t++; } } + std::string block_to_string() + { + u32 *b = block; + std::stringstream ss{}; + for (int i = 0; i < BLOCK_SIZE; i++) { + ss << std::setw(4) << b[i] << " "; + } + return ss.str(); + } + void main() { heck_debug_suspend(); log("Starting..."); HAL_TIM_Base_Start_IT(&htim_blinky_led); - HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, (u32 *)audio_buffer, BUFFER_SIZE, DAC_ALIGN_12B_R); + HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, audio_buffer, BUFFER_SIZE, DAC_ALIGN_12B_R); HAL_TIM_Base_Start_IT(&htim_dac1); log("Entering MainLoop..."); while (true) { if (process_block) { - calculate_audio(); - time += BLOCK_SIZE; process_block = false; + calculate_audio(); + inf_phasor += BLOCK_SIZE; } } } @@ -209,7 +230,6 @@ extern "C" void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) // Heck::log( // "TIM2 timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + // " channel: " + std::to_string(htim->Channel)); - // Heck::log(std::to_string(time)); } else if (htim->Instance == TIM3) { Heck::log( "TIM3 timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + @@ -225,15 +245,15 @@ extern "C" void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) extern "C" void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac) { - Heck::log("HAL_DAC_ConvHalfCpltCallbackCh1"); + // Heck::log("HAL_DAC_ConvHalfCpltCallbackCh1"); block = &audio_buffer[0]; process_block = true; } extern "C" void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac) { - Heck::log("HAL_DAC_ConvCpltCallbackCh1"); - block = &audio_buffer[BUFFER_SIZE]; + // Heck::log("HAL_DAC_ConvCpltCallbackCh1"); + block = &audio_buffer[BLOCK_SIZE]; process_block = true; }