Custom audio element Samples
Posted: 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:
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:
- ESP_LOGI(TAG, "[ 2 ] Start codec chip");
- audio_board_handle_t board_handle = audio_board_init();
- audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START);
- audio_hal_set_volume(board_handle->audio_hal, 60); //60
- ESP_LOGI(TAG, "[ 2 ] Create audio pipeline for playback");
- audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
- pipeline = audio_pipeline_init(&pipeline_cfg);
- ESP_LOGI(TAG, "[ 3 ] Create i2s stream to write data to codec chip");
- i2s_stream_cfg_t i2s_cfg_w = I2S_STREAM_CFG_DEFAULT();
- i2s_cfg_w.type = AUDIO_STREAM_WRITER;
- i2s_stream_set_channel_type(&i2s_cfg_w,I2S_CHANNEL_TYPE_ALL_LEFT);
- i2s_stream_writer = i2s_stream_init(&i2s_cfg_w);
- ESP_LOGI(TAG, "[ 3.1 ] Create i2s stream to read data from codec chip");
- i2s_stream_cfg_t i2s_cfg_r = I2S_STREAM_CFG_DEFAULT();
- i2s_cfg_r.type = AUDIO_STREAM_READER;
- i2s_stream_set_channel_type(&i2s_cfg_r,I2S_CHANNEL_TYPE_ALL_LEFT);
- i2s_stream_reader = i2s_stream_init(&i2s_cfg_r);
- static int audio_inverter_process(audio_element_handle_t self, char* buf, int len)
- {
- int rsize = audio_element_input(self, buf, len);
- if (len != rsize || (rsize % 4) != 0) {
- ESP_LOGW(TAG, "unexpected rsize: %d, len: %d", rsize, len);
- }
- uint16_t LSample;
- char * lsamplep = (char *) &LSample;
- uint16_t RSample;
- char * rsamplep = (char *) &RSample;
- float sample = 0;
- for (int i =0; i<rsize; i+=4) {
- rsamplep[0] = buf[i];
- rsamplep[1] = buf[i+1];
- lsamplep[0] = buf[i+2];
- lsamplep[1] = buf[i+3];
- buf[i] = rsamplep[0];
- buf[i+1] = rsamplep[1];
- buf[i+2] = lsamplep[0];
- buf[i+3] = lsamplep[1];
- }
- ESP_LOGI(TAG, "RSample: %hu",RSample);
- rsize = audio_element_output(self, buf, rsize);
- return rsize;
- }