Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply audio filter using esp_audio.h #36

Open
teddykishi opened this issue Aug 13, 2024 · 0 comments
Open

Apply audio filter using esp_audio.h #36

teddykishi opened this issue Aug 13, 2024 · 0 comments

Comments

@teddykishi
Copy link

Hi

Here my setup:
esp-idf: V4.4.4
esp-adf: V2.4
Hardware: ESP32-Lyrat Mini

i'm using the library esp_audio.h (esp_audio/include/esp_audio.h) and it work fine.

But I would like to apply audio filters from esp_sonic.h (esp_codec/include/processing/esp_sonic.h) to my audio such as the pitch or write my own audio processing code to change compression or echo.

How to get the audio data from esp_audio.h in order to alter them then to play it live ?

here is my code ```// initializing esp_audio as an audio player -- START
static void setup_player(void) {
if (player ) {
return ;
}
esp_audio_cfg_t cfg = DEFAULT_ESP_AUDIO_CONFIG();
//audio_board_handle_t board_handle = audio_board_init();
board_handle = audio_board_init();
cfg.vol_handle = board_handle->audio_hal;
cfg.vol_set = (audio_volume_set)audio_hal_set_volume;
cfg.vol_get = (audio_volume_get)audio_hal_get_volume;
cfg.prefer_type = ESP_AUDIO_PREFER_MEM; // ESP_AUDIO_PREFER_SPEED
cfg.resample_rate = 48000;
cfg.evt_que = xQueueCreate(3, sizeof(esp_audio_state_t));
player = esp_audio_create(&cfg);
audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START);
xTaskCreate(esp_audio_state_task, "player_task", 4096, cfg.evt_que, 1, NULL);

// Create readers and add to esp_audio
fatfs_stream_cfg_t fs_reader = FATFS_STREAM_CFG_DEFAULT();
fs_reader.type = AUDIO_STREAM_READER;
i2s_stream_cfg_t i2s_reader = I2S_STREAM_CFG_DEFAULT();
i2s_reader.type = AUDIO_STREAM_READER;
raw_stream_cfg_t raw_reader = RAW_STREAM_CFG_DEFAULT();
raw_reader.type = AUDIO_STREAM_READER;


audio_element_handle_t i2s_read_h = i2s_stream_init(&i2s_reader);
i2s_stream_set_clk(i2s_read_h, 48000, 16, 2);
esp_audio_input_stream_add(player, raw_stream_init(&raw_reader));
esp_audio_input_stream_add(player, fatfs_stream_init(&fs_reader));
esp_audio_input_stream_add(player, i2s_read_h);
http_stream_cfg_t http_cfg = HTTP_STREAM_CFG_DEFAULT();
http_cfg.event_handle = _http_stream_event_handle;
http_cfg.type = AUDIO_STREAM_READER;
http_cfg.enable_playlist_parser = true;
audio_element_handle_t http_stream_reader = http_stream_init(&http_cfg);
esp_audio_input_stream_add(player, http_stream_reader);

amr_decoder_cfg_t  amr_dec_cfg  = DEFAULT_AMR_DECODER_CONFIG();
flac_decoder_cfg_t flac_dec_cfg = DEFAULT_FLAC_DECODER_CONFIG();
ogg_decoder_cfg_t  ogg_dec_cfg  = DEFAULT_OGG_DECODER_CONFIG();
opus_decoder_cfg_t opus_dec_cfg = DEFAULT_OPUS_DECODER_CONFIG();
mp3_decoder_cfg_t  mp3_dec_cfg  = DEFAULT_MP3_DECODER_CONFIG();
wav_decoder_cfg_t  wav_dec_cfg  = DEFAULT_WAV_DECODER_CONFIG();
aac_decoder_cfg_t  aac_dec_cfg  = DEFAULT_AAC_DECODER_CONFIG();

esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_DECODER, amr_decoder_init(&amr_dec_cfg));
esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_DECODER, flac_decoder_init(&flac_dec_cfg));
esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_DECODER, ogg_decoder_init(&ogg_dec_cfg));
esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_DECODER, decoder_opus_init(&opus_dec_cfg));
esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_DECODER, wav_decoder_init(&wav_dec_cfg));
esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_DECODER, aac_decoder_init(&aac_dec_cfg));

mp3_el = mp3_decoder_init(&mp3_dec_cfg);
esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_DECODER, mp3_el);
audio_element_handle_t m4a_dec_cfg = aac_decoder_init(&aac_dec_cfg);
audio_element_set_tag(m4a_dec_cfg, "m4a");
esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_DECODER, m4a_dec_cfg);

audio_element_handle_t ts_dec_cfg = aac_decoder_init(&aac_dec_cfg);
audio_element_set_tag(ts_dec_cfg, "ts");
esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_DECODER, ts_dec_cfg);

wav_encoder_cfg_t wav_enc_cfg = DEFAULT_WAV_ENCODER_CONFIG();
esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_ENCODER, wav_encoder_init(&wav_enc_cfg));
// END WAIT TO PLAY

// Create writers and add to esp_audio
fatfs_stream_cfg_t fs_writer = FATFS_STREAM_CFG_DEFAULT();
fs_writer.type = AUDIO_STREAM_WRITER;

i2s_stream_cfg_t i2s_writer = I2S_STREAM_CFG_DEFAULT();
i2s_writer.type = AUDIO_STREAM_WRITER;

raw_stream_cfg_t raw_writer = RAW_STREAM_CFG_DEFAULT();
raw_writer.type = AUDIO_STREAM_WRITER;
audio_element_handle_t i2s_h = i2s_stream_init(&i2s_writer);
int channels = 2; // mono 2 for stereo
i2s_stream_set_clk(i2s_h, 48000, 16, channels);

equalizer_cfg_t equalizer_cfg = DEFAULT_EQUALIZER_CONFIG();
int set_value_gain[10] = {-12, -6, 0, 6, 12, 12, 6, 0, -6, -12};
equalizer_cfg.samplerate = 48000;
equalizer_cfg.channel = 2;
equalizer_cfg.set_gain = set_value_gain; 
audio_element_handle_t equalizer = equalizer_init(&equalizer_cfg);

esp_audio_output_stream_add(player, i2s_h);
esp_audio_output_stream_add(player, equalizer);
esp_audio_output_stream_add(player, fatfs_stream_init(&fs_writer));
esp_audio_output_stream_add(player, raw_stream_init(&raw_writer));


// Set default volume
esp_audio_vol_set(player, 50);

AUDIO_MEM_SHOW(TAG);
ESP_LOGI(TAG, "esp_audio instance is:%p\r\n", player);

}

void on_button_press(uint8_t num, char* song_url) {

esp_audio_media_type_set(player, MEDIA_SRC_TYPE_MUSIC_SD);

esp_err_t ret = esp_audio_play(player, AUDIO_CODEC_TYPE_DECODER, song_url, 0);

esp_audio_music_info_t music_info = {0};
esp_audio_music_info_get(player, &music_info);

ESP_LOGI(TAG, "[ * ] Receive music info from wav decoder, sample_rates=%d, bits=%d, ch=%d, bps=%d",
            music_info.sample_rates, music_info.bits, music_info.channels, music_info.bps);

if (ret == ESP_OK) {
    // [TO DO] add pitch filter 
    // [TO DO] add echo filter 
} else {
    ESP_LOGE(TAG, "Failed to play URL: %s", song_url);
}

}


If I'm not clear enough please ask me questions.
Thank you very much


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant