Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

H7 sound: fix noise with empty stream #2083

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading