Kenion
Kenion

Reputation: 27

Some noise when attempting to produce sound wia external DAC for esp323

I am trying to make a voice changing device with esp32 board (PlatformIO + ArduinoIDE).

I have:

  1. external microphone on input
  2. esp32, to process voice (internal ADC is used for incoming signal - I2S protocol)
  3. external DAC (PCM5102) to output changed voice (I2S protocol)

My scheme is working. The sound goes to micro -> adc (I2S) -> process on esp32 -> dac (I2S). But after some time of working (~1 min) I begin to hear a terrible loid fading noise (decay period ~3 sec). Then noise appears once every 5 seconds constantly.

What could be the problem? Maybe I'm setting up adc and dac incorrectly?

Connection scheme (connection is made on a breadboard): enter image description here

Code:

#include <driver/i2s.h>

#define I2S_SAMPLE_RATE     100000
#define I2S_DMA_BUF_LEN     1024

#define ADC_INPUT           ADC1_CHANNEL_4 //pin 32

#define I2S_DATA_PIN        GPIO_NUM_22 // DIN
#define I2S_LRCK_PIN        GPIO_NUM_25 // LCK
#define I2S_BCLK_PIN        GPIO_NUM_26 // BCK


/*
 * Configure ADC.
 */
void i2sInputInit() {
    i2s_config_t i2s_config = {
            .mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN),
            .sample_rate =  I2S_SAMPLE_RATE,              // The format of the signal using ADC_BUILT_IN
            .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // is fixed at 12bit, stereo, MSB
            .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
            .communication_format = I2S_COMM_FORMAT_STAND_I2S,
            .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
            .dma_buf_count = 2048,
            .dma_buf_len = I2S_DMA_BUF_LEN,
            .use_apll = true,
            .tx_desc_auto_clear = true,
            .fixed_mclk = 0
    };

    i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
    i2s_set_adc_mode(ADC_UNIT_1, ADC_INPUT);
//    i2s_adc_enable(I2S_NUM_0);
//    adc1_config_channel_atten(ADC_INPUT, ADC_ATTEN_DB_11);
}


/*
 * Configure DAC.
 */
void i2sOutputInit() {

    // Initialize I2S
    i2s_config_t i2s_config = {
            .mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_TX),
            .sample_rate = I2S_SAMPLE_RATE,
            .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
            .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
            .communication_format = I2S_COMM_FORMAT_STAND_I2S,
            .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
            .dma_buf_count = 2048,
            .dma_buf_len = I2S_DMA_BUF_LEN,
            .use_apll = false,
            .tx_desc_auto_clear = true,
            .fixed_mclk = 0
    };
    i2s_driver_install(I2S_NUM_1, &i2s_config, 0, NULL);

    // Configure I2S pins
    i2s_pin_config_t pin_config = {
            .bck_io_num = I2S_BCLK_PIN,
            .ws_io_num = I2S_LRCK_PIN,
            .data_out_num = I2S_DATA_PIN,
            .data_in_num = I2S_PIN_NO_CHANGE,
    };
    i2s_set_pin(I2S_NUM_1, &pin_config);
}


void setup() {
    Serial.begin(115200);
    i2sInputInit();
    i2sOutputInit();
}


void loop() {
    size_t bytes_read;
    size_t bytes_written;

    int *int_buffer = (int *) malloc(I2S_DMA_BUF_LEN * sizeof(int));
    i2s_read(I2S_NUM_0, int_buffer, I2S_DMA_BUF_LEN * sizeof(int), &bytes_read, portMAX_DELAY);
    applyEffect(int_buffer);
    i2s_write(I2S_NUM_1, int_buffer, I2S_DMA_BUF_LEN * sizeof(int), &bytes_written, portMAX_DELAY);
    free(int_buffer);
}

Upvotes: 0

Views: 190

Answers (0)

Related Questions