Browse Source

audio engine..

tmp1
heck 2 years ago
parent
commit
e53203e432
  1. 132
      src/cppmain.cc

132
src/cppmain.cc

@ -10,14 +10,20 @@
#define SAMPLE_FREQ 44200
#define SAMPLE_MAX 4096
#define DOUBLE_BUFFER_SIZE 16
#define BUFFER_SIZE 8
#define BUFFER_SIZE 16
#define BLOCK_SIZE 8
const double pi{ std::acos(-1) };
static u32 double_buffer[DOUBLE_BUFFER_SIZE];
// Buffer and block
static u32 audio_buffer[BUFFER_SIZE];
static u32* block = &audio_buffer[0];
static volatile bool process_block{ false };
// time
static u64 time{ 0 };
static volatile bool dma_to_dac_half_complete{ false };
static volatile bool dma_to_dac_complete{ false };
// misc
static volatile int freq = 100;
namespace Heck {
@ -37,11 +43,6 @@ namespace Heck {
CDC_Transmit_FS((uint8_t *)out.data(), out.size());
}
// DAC
// ---
// LED
// ---
void led_green_toggle()
@ -59,7 +60,6 @@ namespace Heck {
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
}
// CALLBACKS
void irq1_cb()
{
@ -93,46 +93,46 @@ namespace Heck {
ret %= max;
return ret;
}
//
// void buffer_init_noise()
// {
// log("buffer_init_noise()");
// for (int i = 0; i < DOUBLE_BUFFER_SIZE; i++) {
// u32 val = random(SAMPLE_MAX);
// double_buffer[i] = val;
// log(std::to_string(val));
// }
// }
//
// void buffer_init_sin()
// {
// log("buffer_init_sin()");
// for (int i = 0; i < DOUBLE_BUFFER_SIZE; i++) {
// float p = float(i) / (float)DOUBLE_BUFFER_SIZE - 0.5;
// u32 val = sin(p) * SAMPLE_MAX;
// double_buffer[i] = val;
// log(std::to_string(val));
// }
// }
//
// void buffer_div(int div)
// {
// log("init_scale()");
// for (int i = 0; i < DOUBLE_BUFFER_SIZE; i++) {
// u32 val = double_buffer[i] / div;
// double_buffer[i] = val;
// log(std::to_string(val));
// }
// }
//
// void buffer_randomize(int max)
// {
// log("buffer_randomize()");
// u32 buf_index = random(DOUBLE_BUFFER_SIZE);
// u32 buf_val = random(max);
// double_buffer[buf_index] = buf_val;
// log("i:" + std::to_string(buf_index) + " v:" + std::to_string(buf_val));
// }
void buffer_init_noise()
{
log("buffer_init_noise()");
for (int i = 0; i < BUFFER_SIZE; i++) {
u32 val = random(SAMPLE_MAX);
audio_buffer[i] = val;
log(std::to_string(val));
}
}
void buffer_init_sin()
{
log("buffer_init_sin()");
for (int i = 0; i < BUFFER_SIZE; i++) {
float p = float(i) / (float)BUFFER_SIZE - 0.5;
u32 val = sin(p) * SAMPLE_MAX;
audio_buffer[i] = val;
log(std::to_string(val));
}
}
void buffer_div(int div)
{
log("init_scale()");
for (int i = 0; i < BUFFER_SIZE; i++) {
u32 val = audio_buffer[i] / div;
audio_buffer[i] = val;
log(std::to_string(val));
}
}
void buffer_randomize(int max)
{
log("buffer_randomize()");
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)
{
@ -150,14 +150,14 @@ namespace Heck {
}
void calculate_audio(u32 *buffer)
void calculate_audio()
{
log("calculate_audio time: ");
// log("calculate_audio time: ");
u64 t = time;
u32 samps = hz_to_samps(freq);
for (int i = 0; i < BUFFER_SIZE; i++) {
buffer[i] = t % samps;
for (int i = 0; i < BLOCK_SIZE; i++) {
block[i] = t % samps;
t++;
}
}
@ -168,21 +168,15 @@ namespace Heck {
log("Starting...");
HAL_TIM_Base_Start_IT(&htim_blinky_led);
HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, (u32 *)double_buffer, DOUBLE_BUFFER_SIZE, DAC_ALIGN_12B_R);
HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, (u32 *)audio_buffer, BUFFER_SIZE, DAC_ALIGN_12B_R);
HAL_TIM_Base_Start_IT(&htim_dac1);
log("Entering MainLoop...");
while (true) {
if (dma_to_dac_half_complete) {
calculate_audio(&double_buffer[0]);
time += BUFFER_SIZE;
dma_to_dac_half_complete = false;
}
if (dma_to_dac_complete) {
calculate_audio(&double_buffer[BUFFER_SIZE]);
time += BUFFER_SIZE;
dma_to_dac_complete = false;
if (process_block) {
calculate_audio();
time += BLOCK_SIZE;
process_block = false;
}
}
}
@ -232,13 +226,15 @@ extern "C" void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
extern "C" void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
{
Heck::log("HAL_DAC_ConvHalfCpltCallbackCh1");
dma_to_dac_half_complete = true;
block = &audio_buffer[0];
process_block = true;
}
extern "C" void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
{
Heck::log("HAL_DAC_ConvCpltCallbackCh1");
dma_to_dac_complete = true;
block = &audio_buffer[BUFFER_SIZE];
process_block = true;
}

Loading…
Cancel
Save