diff --git a/components/bsp/include/bsp_codec.h b/components/bsp/include/bsp_codec.h index b88f63e..706b0af 100644 --- a/components/bsp/include/bsp_codec.h +++ b/components/bsp/include/bsp_codec.h @@ -75,6 +75,17 @@ esp_err_t bsp_codec_set_voice_gain(uint8_t channel_mask, uint8_t volume); */ esp_err_t bsp_codec_set_fmt(audio_hal_iface_format_t fmt); +/** + * @brief Configure DAC mute or not. Basically you can use this function to mute the output or unmute + * + * @param enable enable(1) or disable(0) + * @return + * - ESP_OK: Success + * - ESP_FAIL: Fail + * - ESP_ERR_NOT_FOUND: Codec not detected on I2C bus + */ +esp_err_t bsp_codec_set_mute(bool enable); + #ifdef __cplusplus } #endif diff --git a/components/bsp/src/peripherals/bsp_codec.c b/components/bsp/src/peripherals/bsp_codec.c index aa6ebbf..671ef8f 100644 --- a/components/bsp/src/peripherals/bsp_codec.c +++ b/components/bsp/src/peripherals/bsp_codec.c @@ -193,3 +193,13 @@ esp_err_t bsp_codec_set_voice_gain(uint8_t channel_mask, uint8_t volume) } return ESP_ERR_NOT_FOUND; } + +esp_err_t bsp_codec_set_mute(bool enable) +{ + if (bsp_codec_has_dev(CODEC_DEV_ES8311)) { + return es8311_set_voice_mute(enable); + } else if (bsp_codec_has_dev(CODEC_DEV_ES8156)) { + return es8156_codec_set_voice_mute(enable); + } + return ESP_ERR_NOT_FOUND; +} diff --git a/examples/factory_demo/main/app/app_sr_handler.c b/examples/factory_demo/main/app/app_sr_handler.c index 9ecb05d..f1c24b6 100644 --- a/examples/factory_demo/main/app/app_sr_handler.c +++ b/examples/factory_demo/main/app/app_sr_handler.c @@ -20,7 +20,6 @@ #include "ui_sr.h" #include "app_sr_handler.h" #include "settings.h" -#include "es8311.h" static const char *TAG = "sr_handler"; @@ -80,13 +79,13 @@ static esp_err_t sr_echo_play(audio_segment_t audio) size_t len = g_audio_data[audio].len - sizeof(wav_header_t); len = len & 0xfffffffc; ESP_LOGD(TAG, "frame_rate=%d, ch=%d, width=%d", wav_head->SampleRate, wav_head->NumChannels, wav_head->BitsPerSample); - i2s_set_clk(I2S_NUM_0,wav_head->SampleRate,wav_head->BitsPerSample,I2S_CHANNEL_STEREO); + i2s_set_clk(I2S_NUM_0, wav_head->SampleRate, wav_head->BitsPerSample, I2S_CHANNEL_STEREO); i2s_zero_dma_buffer(I2S_NUM_0); sys_param_t *param = settings_get_parameter(); bsp_codec_set_voice_volume(param->volume); - es8311_set_voice_mute(false); - ESP_LOGI(TAG, "bsp_codec_set_voice_volume=%d", param->volume); + bsp_codec_set_mute(false); + ESP_LOGD(TAG, "bsp_codec_set_voice_volume=%d", param->volume); vTaskDelay(pdMS_TO_TICKS(50)); bsp_board_power_ctrl(POWER_MODULE_AUDIO, true);// turn on the speaker diff --git a/examples/factory_demo/main/main.c b/examples/factory_demo/main/main.c index 07add7f..974dd3d 100644 --- a/examples/factory_demo/main/main.c +++ b/examples/factory_demo/main/main.c @@ -13,6 +13,7 @@ #include "esp_check.h" #include "nvs_flash.h" #include "nvs.h" +#include "bsp_codec.h" #include "bsp_board.h" #include "bsp_lcd.h" #include "bsp_btn.h" @@ -25,7 +26,6 @@ #include "audio_player.h" #include "file_iterator.h" #include "gui/ui_main.h" -#include "es8311.h" static const char *TAG = "main"; @@ -75,11 +75,11 @@ static esp_err_t audio_mute_function(AUDIO_PLAYER_MUTE_SETTING setting) { last_volume = param->volume; } - ESP_RETURN_ON_ERROR(es8311_set_voice_mute(setting == AUDIO_PLAYER_MUTE ? true : false), TAG, "set voice mute"); + ESP_RETURN_ON_ERROR(bsp_codec_set_mute(setting == AUDIO_PLAYER_MUTE ? true : false), TAG, "set voice mute"); // restore the voice volume upon unmuting if(setting == AUDIO_PLAYER_UNMUTE) { - es8311_codec_set_voice_volume(last_volume); + bsp_codec_set_voice_volume(last_volume); } ESP_LOGI(TAG, "mute setting %d, volume:%d", setting, last_volume); diff --git a/examples/mp3_demo/main/include/audio.h b/examples/mp3_demo/main/include/audio.h deleted file mode 100644 index 0aff154..0000000 --- a/examples/mp3_demo/main/include/audio.h +++ /dev/null @@ -1,132 +0,0 @@ -/** - * @file - * @version 0.1 - * @date 2021-11-11 - * - * @copyright Copyright 2021 Espressif Systems (Shanghai) Co. Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include "esp_err.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum { - AUDIO_EVENT_NONE = 0, - AUDIO_EVENT_PAUSE, - AUDIO_EVENT_PLAY, - AUDIO_EVENT_CHANGE, - AUDIO_EVENT_FILE_SCAN_DONE, - AUDIO_EVENT_MAX, -} audio_event_t; - -typedef struct { - audio_event_t audio_event; - void *user_ctx; -} audio_cb_ctx_t; - -/* Audio callback function type */ -typedef void (*audio_cb_t)(audio_cb_ctx_t *); - -/** - * @brief Start MP3 player - * - * @param file_path Folder containing MP3 file(s) - * @return - * - ESP_OK: Success - * - Others: Fail - */ -esp_err_t mp3_player_start(char *file_path); - -/** - * @brief Play song - * - * @return - * - ESP_OK: Success - * - Others: Fail - */ -esp_err_t audio_play(void); - -/** - * @brief Pause the song - * - * @return - * - ESP_OK: Success - * - Others: Fail - */ -esp_err_t audio_pause(void); - -/** - * @brief Play next song - * - * @return - * - ESP_OK: Success - * - Others: Fail - */ -esp_err_t audio_play_next(void); - -/** - * @brief Play previous song - * - * @return - * - ESP_OK: Success - * - Others: Fail - */ -esp_err_t audio_play_prev(void); - -/** - * @brief Play the specified song - * - * @param index Index of audio file - * @return - * - ESP_OK: Success - * - Others: Fail - */ -esp_err_t audio_play_index(size_t index); - -/** - * @brief Get the index of the audio being played - * - * @return Index of audio being played - */ -size_t audio_get_index(void); - -/** - * @brief Get file name of given index - * - * @param index Index of audio file - * @param base_path Base path add before file name. No base path added if NULL. - * @return Name of audio file with given index. NULL if not exist. - */ -char *audio_get_name_from_index(size_t index, char *base_path); - -/** - * @brief Register callback for audio event - * - * @param call_back Call back function - * @param user_ctx User context - * @return - * - ESP_OK: Success - * - Others: Fail - */ -esp_err_t audio_callback_register(audio_cb_t call_back, void *user_ctx); - -#ifdef __cplusplus -} -#endif diff --git a/examples/mp3_demo/main/include/ui_audio.h b/examples/mp3_demo/main/include/ui_audio.h index ad4c6f8..6d24d50 100644 --- a/examples/mp3_demo/main/include/ui_audio.h +++ b/examples/mp3_demo/main/include/ui_audio.h @@ -32,6 +32,12 @@ extern "C" { */ void ui_audio_start(file_iterator_instance_t *i); +/** + * @brief get system volume + * + */ +uint8_t get_sys_volume(); + #ifdef __cplusplus } #endif diff --git a/examples/mp3_demo/main/mp3_demo.c b/examples/mp3_demo/main/mp3_demo.c index d5801f2..afd6589 100644 --- a/examples/mp3_demo/main/mp3_demo.c +++ b/examples/mp3_demo/main/mp3_demo.c @@ -33,29 +33,26 @@ #include "lvgl.h" #include "ui_audio.h" #include "file_iterator.h" -#include "es8311.h" static const char *TAG = "mp3_demo"; static esp_err_t audio_mute_function(AUDIO_PLAYER_MUTE_SETTING setting) { // Volume saved when muting and restored when unmuting. Restoring volume is necessary // as es8311_set_voice_mute(true) results in voice volume (REG32) being set to zero. - static int last_volume; + static uint8_t last_volume; - ESP_LOGI(TAG, "mute setting %d", setting); - int volume; - - ESP_RETURN_ON_ERROR(es8311_codec_get_voice_volume(&volume), TAG, "get voice volume"); + uint8_t volume = get_sys_volume(); if(volume != 0) { last_volume = volume; } - - ESP_RETURN_ON_ERROR(es8311_set_voice_mute(setting == AUDIO_PLAYER_MUTE ? true : false), TAG, "set voice mute"); + + ESP_RETURN_ON_ERROR(bsp_codec_set_mute(setting == AUDIO_PLAYER_MUTE ? true : false), TAG, "set voice mute"); // restore the voice volume upon unmuting if(setting == AUDIO_PLAYER_UNMUTE) { - es8311_codec_set_voice_volume(last_volume); + bsp_codec_set_voice_volume(last_volume); } + ESP_LOGI(TAG, "mute setting %d, volume:%d", setting, last_volume); return ESP_OK; } diff --git a/examples/mp3_demo/main/ui_audio.c b/examples/mp3_demo/main/ui_audio.c index 48e1bd5..5a6ccf9 100644 --- a/examples/mp3_demo/main/ui_audio.c +++ b/examples/mp3_demo/main/ui_audio.c @@ -23,11 +23,12 @@ #include "file_iterator.h" #include "esp_err.h" #include "esp_log.h" -#include "es8311.h" +#include "bsp_codec.h" static const char *TAG = "ui_audio"; static file_iterator_instance_t* file_iterator; +static uint8_t g_sys_volume; #if CONFIG_ESP32_S3_BOX_LITE_BOARD static void register_button_callback(lv_obj_t *btn_list[]); @@ -97,7 +98,9 @@ static void volume_slider_cb(lv_event_t *event) { lv_obj_t *slider = (lv_obj_t *) event->target; int volume = lv_slider_get_value(slider); - es8311_codec_set_voice_volume(volume); + bsp_codec_set_voice_volume(volume); + g_sys_volume = volume; + ESP_LOGI(TAG, "volume '%d'", volume); } static void build_file_list(lv_obj_t *music_list) @@ -165,9 +168,15 @@ static void music_list_cb(lv_event_t *event) } } +uint8_t get_sys_volume() +{ + return g_sys_volume; +} + void ui_audio_start(file_iterator_instance_t *i) { file_iterator = i; + g_sys_volume = 60; /* Create audio control button */ lv_obj_t *btn_play_pause = lv_btn_create(lv_scr_act()); @@ -206,7 +215,7 @@ void ui_audio_start(file_iterator_instance_t *i) lv_obj_set_ext_click_area(volume_slider, 15); lv_obj_align(volume_slider, LV_ALIGN_BOTTOM_MID, 0, -20); lv_slider_set_range(volume_slider, 0, 75); - lv_slider_set_value(volume_slider, 50, LV_ANIM_ON); + lv_slider_set_value(volume_slider, g_sys_volume, LV_ANIM_ON); lv_obj_add_event_cb(volume_slider, volume_slider_cb, LV_EVENT_VALUE_CHANGED, NULL); lv_obj_t *lab_vol_min = lv_label_create(lv_scr_act());