Custom audio element Samples

CoolingCoder
Posts: 1
Joined: Mon Oct 21, 2024 5:33 pm

Custom audio element Samples

Postby CoolingCoder » Mon Oct 21, 2024 5:45 pm

Hi. I'm currently developing code using ESP-ADF, where I created a custom audio element to process the samples of the right channel in order to implement a feedback process in some kind of passthru program. However, I don't know how to properly understand the samples since I expected that whenever I had silence detected from the microphone, the samples should be around the value of 32768 (which is half the maximum value). Instead, I am getting samples around the maximum and minimum values:

I (58355) PASSTHRU: RSample: 65146
I (58365) PASSTHRU: RSample: 64844
I (58385) PASSTHRU: RSample: 64878
I (58415) PASSTHRU: RSample: 85
I (58445) PASSTHRU: RSample: 765
I (58475) PASSTHRU: RSample: 867
I (58485) PASSTHRU: RSample: 204
I (58505) PASSTHRU: RSample: 64932
I (58535) PASSTHRU: RSample: 64773
I (58565) PASSTHRU: RSample: 286
I (58595) PASSTHRU: RSample: 131
I (58605) PASSTHRU: RSample: 651
I (58625) PASSTHRU: RSample: 184
I (58655) PASSTHRU: RSample: 398
I (58685) PASSTHRU: RSample: 64980

Additionally, it all sounds good when I plug the output into a speaker, so I know it's purely an interpretation issue.
I hope some of you can help me understand how these samples truly work so I can continue developing my program.


This is the code I'm currently using:
  1. ESP_LOGI(TAG, "[ 2 ] Start codec chip");
  2.     audio_board_handle_t board_handle = audio_board_init();
  3.     audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START);
  4.     audio_hal_set_volume(board_handle->audio_hal, 60); //60
  5.  
  6.     ESP_LOGI(TAG, "[ 2 ] Create audio pipeline for playback");
  7.     audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  8.     pipeline = audio_pipeline_init(&pipeline_cfg);
  9.  
  10.     ESP_LOGI(TAG, "[ 3 ] Create i2s stream to write data to codec chip");
  11.     i2s_stream_cfg_t i2s_cfg_w = I2S_STREAM_CFG_DEFAULT();
  12.     i2s_cfg_w.type = AUDIO_STREAM_WRITER;
  13.     i2s_stream_set_channel_type(&i2s_cfg_w,I2S_CHANNEL_TYPE_ALL_LEFT);
  14.     i2s_stream_writer = i2s_stream_init(&i2s_cfg_w);
  15.  
  16.     ESP_LOGI(TAG, "[ 3.1 ] Create i2s stream to read data from codec chip");
  17.     i2s_stream_cfg_t i2s_cfg_r = I2S_STREAM_CFG_DEFAULT();
  18.     i2s_cfg_r.type = AUDIO_STREAM_READER;
  19.     i2s_stream_set_channel_type(&i2s_cfg_r,I2S_CHANNEL_TYPE_ALL_LEFT);
  20.     i2s_stream_reader = i2s_stream_init(&i2s_cfg_r);
  1. static int audio_inverter_process(audio_element_handle_t self, char* buf, int len)
  2. {
  3.     int rsize = audio_element_input(self, buf, len);
  4.  
  5.     if (len != rsize || (rsize % 4) != 0) {
  6.         ESP_LOGW(TAG, "unexpected rsize: %d, len: %d", rsize, len);
  7.     }
  8.  
  9.     uint16_t LSample;
  10.     char * lsamplep = (char *) &LSample;
  11.     uint16_t RSample;
  12.     char * rsamplep = (char *) &RSample;
  13.     float sample = 0;
  14.  
  15.     for (int i =0; i<rsize; i+=4) {
  16.         rsamplep[0] = buf[i];
  17.         rsamplep[1] = buf[i+1];
  18.         lsamplep[0] = buf[i+2];
  19.         lsamplep[1] = buf[i+3];
  20.        
  21.         buf[i] = rsamplep[0];
  22.         buf[i+1] = rsamplep[1];
  23.         buf[i+2] = lsamplep[0];
  24.         buf[i+3] = lsamplep[1];
  25.     }
  26.  
  27.     ESP_LOGI(TAG, "RSample: %hu",RSample);
  28.    
  29.     rsize = audio_element_output(self, buf, rsize);
  30.  
  31.     return rsize;
  32. }

MicroController
Posts: 1724
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Custom audio element Samples

Postby MicroController » Tue Oct 22, 2024 8:20 am

CoolingCoder wrote:
Mon Oct 21, 2024 5:45 pm
so I know it's purely an interpretation issue.
I hope some of you can help me understand how these samples truly work
Interpret the samples as signed values, i.e. int16_t.

Who is online

Users browsing this forum: pepgra and 150 guests