harbaum / galagino

A Galaga, Pac-Man and Donkey Kong arcade emulator for the ESP32
315 stars 21 forks source link

Crackling sound with ESP32 board support >= 2.0.10 #27

Closed harbaum closed 11 months ago

harbaum commented 11 months ago

While sound was working fine with ESP board support 2.0.6 it only gives a crackling noise in the 2.0.10 and the latest 2.0.11

harbaum commented 11 months ago

Works up to 2.0.9 and is broken since 2.0.10

The following sketch gives a clean rectangular signal up to 2.0.9 and the crackling noise on later versions.

#include "driver/i2s.h"

unsigned short snd_buffer[64];

void setup() {
    static const i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
    .sample_rate = 24000,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,

    .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
    .intr_alloc_flags = 0,
    .dma_buf_count = 4,
    .dma_buf_len = 64,   // 64 samples
    .use_apll = true
  };
  i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  i2s_set_dac_mode(I2S_DAC_CHANNEL_RIGHT_EN);
}

void loop() {
  // generate square wave
  for(int i=0;i<32;i++)  snd_buffer[i] = 0x0000;
  for(int i=32;i<64;i++) snd_buffer[i] = 0x1000;

  size_t bytesOut = 0;
  i2s_write(I2S_NUM_0, snd_buffer, sizeof(snd_buffer), &bytesOut, 0);
  printf("BytesOut: %d\n", bytesOut);
}
harbaum commented 11 months ago

See https://github.com/espressif/arduino-esp32/issues/8467

harbaum commented 11 months ago

Please try the latest commit. The donkey kong sounds are the most critical ones and may be affected most by the implemented work around.

Still unclear what's causing the apll to generate the crackling noise.

KlausMu commented 11 months ago

I tried the latest commit with ESP32 board version 2.0.11. The workaround worked in my tests. Thanks a lot.

bjotos commented 11 months ago

I started with this project after reading about it in the latest Make and ran in exact the issue with crackling noise from the speakers w/o this fix (cloned repo last week). And this commit solved it.

But I was getting this noise also in game select screen where it should be silent, which makes me wonder if that's because I'm using single-wire SC8002B, while PAM8302A uses differential audio signal, or some other difference with these solutions (different amps, different filters, ...)

P.S.: Thanks for the great work, love it ;)

harbaum commented 11 months ago

The crackling sound even happens when there's silence at a signal level != zero. So yes, it was expected to happen in the menu as well.