Reading and playing back audio samples realtime

tacriela
Posts: 1
Joined: Sat Jan 21, 2023 8:21 pm

Reading and playing back audio samples realtime

Postby tacriela » Sat Jan 21, 2023 8:39 pm

Dear fellow forummembers,

For a school project, I would like to get a bass-amplifier up-and-running with an ESP32. My idea for doing this is as follows:
I want to sample audio from AUX via I2S reading. Then I want to filter out the bass frequencies via a low-pass-filter. Next I will add a multiple of these frequency values to the original sample, and I will output them once again via I2S. I think I have the reading part down, however, I'm kind of stuck on writing with I2S. My code is as follows:
  1. // Global defines
  2. #define SERIAL_BAUDRATE 9600
  3.  
  4. // I2S variables
  5. #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))       // Set the array size for the samples
  6. #define ADC_INPUT_PIN ADC1_CHANNEL_4                  // Pin we want to input from, this is pin 32
  7. #define ADC_OUTPUT_PIN ADC1_CHANNEL_5                 // Pin we want to output to, this is pin 33
  8. const i2s_port_t I2S_PORT = I2S_NUM_0;                // I2S port used (we use I2S_NUM_0 because this supports ADC and DAC)
  9. const double SAMPLING_FREQUENCY = 41000;              // Sampling frequency
  10. const int SAMPLE_BLOCK = 512;                         // Amount of samples we want to store in one block
  11. const uint16_t OFFSET = (int)ADC_INPUT_PIN * 0x1000;  // Offset for the input pin
  12.  
  13. // Setup part
  14. I2SManager::I2SManager() {
  15.   Serial.println("Configuring I2S...");
  16.   esp_err_t err;
  17.  
  18.   // The I2S configuration
  19.   const i2s_config_t i2s_config = {
  20.     .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_TX | I2S_MODE_ADC_BUILT_IN),
  21.     .sample_rate = SAMPLING_FREQUENCY,
  22.     .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
  23.     .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  24.     .communication_format = I2S_COMM_FORMAT_I2S_MSB,
  25.     .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  26.     .dma_buf_count = 4,
  27.     .dma_buf_len = SAMPLE_BLOCK
  28.   };
  29.  
  30.   // Set the driver for the I2S port
  31.   err = i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  32.   if (err != ESP_OK) {
  33.     Serial.printf("Failed installing driver: %d\n", err);
  34.     while (true)
  35.       ;
  36.   }
  37.  
  38.   static const i2s_pin_config_t pin_config = {
  39.     .bck_io_num = -1,
  40.     .ws_io_num = -1,
  41.     .data_out_num = ADC_OUTPUT_PIN,
  42.     .data_in_num = ADC_INPUT_PIN
  43.   };
  44.  
  45.   // Set pin config
  46.   i2s_set_pin(I2S_PORT, &pin_config);
  47.  
  48.   // Enable DAC mode
  49.   i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  50.  
  51.   // Set input pin to ADC mode
  52.   err = i2s_set_adc_mode(ADC_UNIT_1, ADC_INPUT_PIN);
  53.   if (err != ESP_OK) {
  54.     Serial.printf("Failed setting up adc mode: %d\n", err);
  55.     while (true)
  56.       ;
  57.   }
  58.  
  59.   Serial.println("I2S driver installed.");
  60. }
  61.  
  62. // Loop part
  63. void I2SManager::getSamples() {
  64.   // Read samples from I2S buffer
  65.   size_t bytesRead = 0;
  66.   i2s_read(I2S_PORT, (void*)samples, sizeof(samples), &bytesRead, portMAX_DELAY);
  67.  
  68.   // Check if all samples are filled
  69.   if (bytesRead != sizeof(samples)) {
  70.     Serial.printf("Could only read %u bytes of %u in FillBufferI2S()\n", bytesRead, sizeof(samples));
  71.     return;
  72.   }
  73.  
  74.   // This prints some data, I don't know if its correct because I can't check it yet
  75.   for (uint16_t i = 0; i < ARRAYSIZE(samples); i++) {
  76.     Serial.printf("%7d\n", samples[i]);
  77.   }
  78.  
  79.   int bytesWritten;
  80.   i2s_write(I2S_PORT, (void*)samples, sizeof(samples), (size_t*)&bytesWritten, portMAX_DELAY);
  81. }
If anyone is able to help me that would be very much appreciated. If you have any questions for me, do not hesitate to ask, I'll be monitoring this post as much as I can :).

Thanks in advance and kind regards,

Thom

Who is online

Users browsing this forum: No registered users and 34 guests