pschatzmann / arduino-audiokit

Arduino ADF/Audiokit HAL (support for ESP32-A1S, AI-Thinker, LyraT for ES7148, ES7210, ES7243, ES8311, ES8347, ES8388, TAS5805M, AC101 audio chips)
GNU General Public License v3.0
157 stars 42 forks source link

Bad quality when forwarding audio in to output #71

Closed apiel closed 1 year ago

apiel commented 1 year ago

I am planning to implement an audio filter using my esp32 audio kit (v2.2 ESP32-A1S 2974)

When I am simply trying to forward audio input directly to output, I am getting very bad quality sound. Is there anyway to improve this?

#include <Arduino.h>
#include <Wire.h>

// #define AUDIOKIT_BOARD 1

#include "AudioKitHAL.h"
#include "SineWaveGenerator.h"

AudioKit kit;
SineWaveGenerator wave;
const int BUFFER_SIZE = 1024;
uint8_t buffer[BUFFER_SIZE];

void setup()
{
  LOGLEVEL_AUDIOKIT = AudioKitInfo;
  Serial.begin(115200);

  auto cfg = kit.defaultConfig();
  cfg.adc_input = AUDIO_HAL_ADC_INPUT_LINE2;
  cfg.sample_rate = AUDIO_HAL_16K_SAMPLES;

  kit.begin(cfg);
}

void loop()
{
  size_t len = kit.read(buffer, BUFFER_SIZE);

  // HERE will be some code to apply filters

  kit.write(buffer, len);
}

Full repo example https://github.com/apiel/esp32-filter-effect

pschatzmann commented 1 year ago

I am not really sure what you mean with bad quality, but in any case I recommend to leave the log level at warning and change it to info if you need to check the processing.

If you want to use filters I suggest to use the functionality of the AudioTools: https://github.com/pschatzmann/arduino-audio-tools/wiki/Filters together with the AudioKitStream class.

Here are some more Filter examples: https://nbviewer.org/urls/pschatzmann.github.io/Resources/jupyter/JupyterNoise.ipynb

apiel commented 1 year ago

Hard to describe bad sound quality but If I plug my synth directly to the speaker the sound is much better. If I pass through the ESP32 it feel like there is some sound lost and a bit of noise. I tried also to power to the board with powerbank but it make no difference. I changed the log output to warning and tried different sample rate, but no difference :-/

pschatzmann commented 1 year ago

This is just a guess, but maybe this comes from the microphones which can not be switched off. I am just using some cheap pc speakers, so I would not hear the difference...

Anyhow I do not think that this is related to my software, so I am closing this issue.

Did you try to adjust the input gain ?

apiel commented 1 year ago

I tried the example from AudioTools, it's getting even worse :p Maybe I should solder away the microphone ^^

apiel commented 1 year ago

But hang on, I am actually surprise that reading and writing the audio signal happen in the main loop. It's not common way of doing audio processing (or at least from what I have seen so far). Wouldn't it be better to use timer interrupt for this and to run it in IRAM_ATTR?

I was implementing something like that for PWM audio https://github.com/apiel/kick23/blob/main/src/boards/esp32c3_main.h#L68

pschatzmann commented 1 year ago

Absolutly not: The ESP32 I2S API is doing this in the background using DMA. You just need to make sure to keep the buffers filled...