Skip to content

Commit

Permalink
fix noise
Browse files Browse the repository at this point in the history
  • Loading branch information
Comma Device committed Nov 18, 2024
1 parent acc15af commit c134c59
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions board/stm32h7/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,41 @@ __attribute__((section(".sram4"))) static uint16_t sound_rx_buf[2][SOUND_RX_BUF_

static uint8_t sound_idle_count;

void sound_tick(void) {
if (sound_idle_count > 0U) {
sound_idle_count--;
if (sound_idle_count == 0U) {
current_board->set_amp_enabled(false);
}
}
}

// Playback processing
static void BDMA_Channel0_IRQ_Handler(void) {
__attribute__((section(".sram4"))) static uint16_t tx_buf[SOUND_TX_BUF_SIZE];

BDMA->IFCR |= BDMA_IFCR_CGIF0; // clear flag

// process samples (shift to 12b and bias to be unsigned)
// since we are playing mono and receiving stereo, we take every other sample
uint8_t buf_idx = (((BDMA_Channel0->CCR & BDMA_CCR_CT) >> BDMA_CCR_CT_Pos) == 1U) ? 0U : 1U;

// process samples (shift to 12b and bias to be unsigned)
bool sound_playing = false;
for (uint16_t i=0U; i < SOUND_RX_BUF_SIZE; i += 2U) {
// since we are playing mono and receiving stereo, we take every other sample
tx_buf[i/2U] = ((sound_rx_buf[buf_idx][i] + (1UL << 14)) >> 3);
if (sound_rx_buf[buf_idx][i] > 0U) {
sound_playing = true;
}
}

if (sound_idle_count == 0U) {
current_board->set_amp_enabled(true);
// manage amp state
if (sound_playing) {
if (sound_idle_count == 0U) {
current_board->set_amp_enabled(true);
}
sound_idle_count = 4U;
}
sound_idle_count = 2U;
sound_tick();

DMA1->LIFCR |= 0xF40;
DMA1_Stream1->CR &= ~DMA_SxCR_EN;
Expand All @@ -30,15 +48,6 @@ static void BDMA_Channel0_IRQ_Handler(void) {
DMA1_Stream1->CR |= DMA_SxCR_EN;
}

void sound_tick(void) {
if (sound_idle_count > 0U) {
sound_idle_count--;
if (sound_idle_count == 0U) {
current_board->set_amp_enabled(false);
}
}
}

void sound_init(void) {
REGISTER_INTERRUPT(BDMA_Channel0_IRQn, BDMA_Channel0_IRQ_Handler, 64U, FAULT_INTERRUPT_RATE_SOUND_DMA)

Expand Down

0 comments on commit c134c59

Please sign in to comment.