example: Update mp3_demo to use package manager

This commit is contained in:
Wu Jian Gang
2023-01-05 19:48:49 +08:00
parent 3fae2f08fe
commit 71f96b3e2b
6 changed files with 110 additions and 147 deletions
+3 -3
View File
@@ -2,10 +2,10 @@ cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(EXTRA_COMPONENT_DIRS
../../components)
add_compile_options(-fdiagnostics-color=always)
# The following line can be removed after LVGL update to v8.3.5.
add_compile_options(-Wno-format)
project(mp3_demo)
+2 -15
View File
@@ -2,18 +2,5 @@
dependencies:
chmorgan/esp-libhelix-mp3: "1.0.3"
chmorgan/esp-file-iterator: "1.0.0"
chmorgan/esp-audio-player: "1.0.3"
## Required IDF version
idf:
version: ">=4.1.0"
# # Put list of dependencies here
# # For components maintained by Espressif:
# component: "~1.0.0"
# # For 3rd party components:
# username/component: ">=1.0.0,<2.0.0"
# username2/component2:
# version: "~1.0.0"
# # For transient dependencies `public` flag can be set.
# # `public` flag doesn't have an effect dependencies of the `main` component.
# # All dependencies of `main` are public by default.
# public: true
chmorgan/esp-audio-player: "1.0.4"
espressif/esp-box: "2.*"
+3 -17
View File
@@ -1,21 +1,7 @@
/**
* @file
* @version 0.1
* @date 2021-11-11
*
* @copyright Copyright 2021 Espressif Systems (Shanghai) Co. Ltd.
/*
* SPDX-FileCopyrightText: 2022-2023 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.
* SPDX-License-Identifier: CC0-1.0
*/
#pragma once
+75 -61
View File
@@ -1,41 +1,39 @@
/**
* @file
* @version 0.1
* @date 2021-11-11
*
* @copyright Copyright 2021 Espressif Systems (Shanghai) Co. Ltd.
/*
* SPDX-FileCopyrightText: 2022-2023 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.
* SPDX-License-Identifier: CC0-1.0
*/
#include "audio_player.h"
#include "bsp_board.h"
#include "bsp_lcd.h"
#include "bsp_storage.h"
#include "bsp_i2s.h"
#include "bsp_codec.h"
#include "esp_err.h"
#include "esp_log.h"
#include "bsp/esp-bsp.h"
#include "esp_check.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "lv_port.h"
#include "lvgl.h"
#include "ui_audio.h"
#include "esp_log.h"
#include "file_iterator.h"
#include "es8311.h"
#include "ui_audio.h"
static const char *TAG = "mp3_demo";
static i2s_chan_handle_t i2s_tx_chan;
static es8311_handle_t es8311_dev = NULL;
/* Buffer for reading/writing to I2S driver. Same length as SPIFFS buffer and I2S buffer, for optimal read/write performance.
Recording audio data path:
I2S peripheral -> I2S buffer (DMA) -> App buffer (RAM) -> SPIFFS buffer -> External SPI Flash.
Vice versa for playback. */
#define BUFFER_SIZE (1024)
#define SAMPLE_RATE (44100)
#define DEFAULT_VOLUME (60)
esp_err_t audio_codec_set_voice_volume(uint8_t volume)
{
float v = volume;
v *= 0.6f;
v = -0.01f * (v * v) + 2.0f * v;
return es8311_voice_volume_set(es8311_dev, volume, NULL);
}
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.
@@ -46,55 +44,71 @@ static esp_err_t audio_mute_function(AUDIO_PLAYER_MUTE_SETTING setting) {
last_volume = volume;
}
ESP_RETURN_ON_ERROR(bsp_codec_set_mute(setting == AUDIO_PLAYER_MUTE ? true : false), TAG, "set voice mute");
ESP_RETURN_ON_ERROR(es8311_voice_mute(es8311_dev, setting == AUDIO_PLAYER_MUTE ? true : false), TAG, "set voice mute");
// restore the voice volume upon unmuting
if(setting == AUDIO_PLAYER_UNMUTE) {
bsp_codec_set_voice_volume(last_volume);
audio_codec_set_voice_volume(last_volume);
}
ESP_LOGI(TAG, "mute setting %d, volume:%d", setting, last_volume);
return ESP_OK;
}
static esp_err_t audio_i2s_write(void * audio_buffer, size_t len, size_t *bytes_written, uint32_t timeout_ms)
{
esp_err_t ret = ESP_OK;
ret = i2s_channel_write(i2s_tx_chan, (char *)audio_buffer, len, bytes_written, timeout_ms);
return ret;
}
static esp_err_t audio_i2s_reconfig_clk(uint32_t rate, uint32_t bits_cfg, i2s_slot_mode_t ch)
{
esp_err_t ret = ESP_OK;
i2s_std_config_t std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(rate),
.slot_cfg = I2S_STD_PHILIP_SLOT_DEFAULT_CONFIG((i2s_data_bit_width_t)bits_cfg, (i2s_slot_mode_t)ch),
.gpio_cfg = BSP_I2S_GPIO_CFG,
};
ret |= i2s_channel_disable(i2s_tx_chan);
ret |= i2s_channel_reconfig_std_clock(i2s_tx_chan, &std_cfg.clk_cfg);
ret |= i2s_channel_reconfig_std_slot(i2s_tx_chan, &std_cfg.slot_cfg);
ret |= i2s_channel_enable(i2s_tx_chan);
return ret;
}
void app_main(void)
{
ESP_ERROR_CHECK(bsp_board_init());
ESP_ERROR_CHECK(bsp_board_power_ctrl(POWER_MODULE_AUDIO, true));
/* Initialize I2C (for touch and audio) */
bsp_i2c_init();
ESP_ERROR_CHECK(bsp_spiffs_init("storage", "/spiffs", 2));
ESP_ERROR_CHECK(lv_port_init());
bsp_lcd_set_backlight(true);
/* Initialize display and LVGL */
bsp_display_start();
file_iterator_instance_t *file_iterator = file_iterator_new("/spiffs");
/* Set display brightness to 100% */
bsp_display_backlight_on();
bsp_spiffs_mount();
file_iterator_instance_t *file_iterator = file_iterator_new(BSP_MOUNT_POINT);
assert(file_iterator != NULL);
/**
* @brief Initialize I2S and audio codec
*
* @note `MP3GetLastFrameInfo` is used to fill the `MP3FrameInfo`, which includes `samprate`,
* and the sampling rate is updated during playback using this value.
*/
esp_err_t ret = bsp_codec_init(AUDIO_HAL_44K_SAMPLES);
if(ret != ESP_OK) {
ESP_LOGE(TAG, "bsp_codec_init failed with %d", ret);
return;
}
/* Create and configure ES8311 I2C driver */
es8311_dev = es8311_create(BSP_I2C_NUM, ES8311_ADDRRES_0);
const es8311_clock_config_t clk_cfg = BSP_ES8311_SCLK_CONFIG(SAMPLE_RATE);
es8311_init(es8311_dev, &clk_cfg, ES8311_RESOLUTION_16, ES8311_RESOLUTION_16);
es8311_voice_volume_set(es8311_dev, DEFAULT_VOLUME, NULL);
bsp_i2s_init(I2S_NUM_0, 44100);
// TODO: original code isn't checking return value here, this is failing with a value of 0x103 which is
// who the heck knows what error
// ESP_RETURN_ON_ERROR(ret, TAG, "bsp_i2s_init");
/* Configure I2S peripheral and Power Amplifier */
bsp_audio_init(NULL, &i2s_tx_chan, NULL);
bsp_audio_poweramp_enable(true);
audio_player_config_t config = { .port = I2S_NUM_0,
.mute_fn = audio_mute_function,
audio_player_config_t config = { .mute_fn = audio_mute_function,
.write_fn = audio_i2s_write,
.clk_set_fn = audio_i2s_reconfig_clk,
.priority = 1 };
ESP_ERROR_CHECK(audio_player_new(config));
esp_err_t ret = audio_player_new(config);
ui_audio_start(file_iterator);
do {
lv_task_handler();
} while (vTaskDelay(1), true);
}
}
+6 -19
View File
@@ -1,21 +1,7 @@
/**
* @file
* @version 0.1
* @date 2021-11-10
*
* @copyright Copyright 2021 Espressif Systems (Shanghai) Co. Ltd.
/*
* SPDX-FileCopyrightText: 2022-2023 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.
* SPDX-License-Identifier: CC0-1.0
*/
#include "lvgl.h"
@@ -23,7 +9,6 @@
#include "file_iterator.h"
#include "esp_err.h"
#include "esp_log.h"
#include "bsp_codec.h"
static const char *TAG = "ui_audio";
@@ -94,11 +79,13 @@ static void btn_prev_next_cb(lv_event_t *event)
}
}
extern esp_err_t audio_codec_set_voice_volume(uint8_t volume);
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);
bsp_codec_set_voice_volume(volume);
audio_codec_set_voice_volume(volume);
g_sys_volume = volume;
ESP_LOGI(TAG, "volume '%d'", volume);
}
+21 -32
View File
@@ -1,16 +1,24 @@
CONFIG_IDF_TARGET="esp32s3"
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF=y
CONFIG_BOOTLOADER_LOG_LEVEL_NONE=y
CONFIG_BOOTLOADER_SKIP_VALIDATE_ALWAYS=y
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096
CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y
CONFIG_ESP_TIMER_TASK_STACK_SIZE=4096
CONFIG_ESP32S3_DATA_CACHE_64KB=y
CONFIG_ESP32S3_DATA_CACHE_LINE_64B=y
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_240=y
CONFIG_ESP32S3_INSTRUCTION_CACHE_32KB=y
CONFIG_ESP32S3_SPIRAM_SUPPORT=y
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
# CONFIG_USE_WAKENET is not set
# CONFIG_USE_MULTINET is not set
CONFIG_LV_COLOR_16_SWAP=y
# CONFIG_LV_USE_PERF_MONITOR is not set
# CONFIG_LV_USE_API_EXTENSION_V6 is not set
# CONFIG_LV_USE_API_EXTENSION_V7 is not set
CONFIG_LV_FONT_MONTSERRAT_18=y
CONFIG_LV_FONT_MONTSERRAT_20=y
CONFIG_LV_FONT_MONTSERRAT_22=y
@@ -18,31 +26,12 @@ CONFIG_LV_FONT_MONTSERRAT_24=y
CONFIG_LV_FONT_MONTSERRAT_26=y
CONFIG_LV_FONT_MONTSERRAT_28=y
CONFIG_LV_FONT_MONTSERRAT_32=y
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_240=y
CONFIG_ESP32S3_INSTRUCTION_CACHE_32KB=y
CONFIG_ESP32S3_DATA_CACHE_64KB=y
CONFIG_ESP32S3_DATA_CACHE_LINE_64B=y
CONFIG_ESP32S3_SPIRAM_SUPPORT=y
CONFIG_SPIRAM_MODE_OCT=y
CONFIG_SPIRAM_SPEED_80M=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=4096
CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=8192
CONFIG_HTTPD_MAX_REQ_HDR_LEN=2048
CONFIG_HTTPD_MAX_URI_LEN=2048
CONFIG_HTTPD_WS_SUPPORT=y
CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y
CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
CONFIG_ESP_TIMER_TASK_STACK_SIZE=4096
CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=4
CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=4
CONFIG_ESP32_WIFI_STATIC_TX_BUFFER_NUM=4
CONFIG_ESP32_WIFI_CACHE_TX_BUFFER_NUM=16
CONFIG_ESP32_WIFI_TX_BA_WIN=2
CONFIG_ESP32_WIFI_RX_BA_WIN=2
# CONFIG_ESP32_WIFI_IRAM_OPT is not set
# CONFIG_ESP32_WIFI_RX_IRAM_OPT is not set
# CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE is not set
CONFIG_FATFS_LFN_STACK=y
# CONFIG_LWIP_IPV6 is not set
CONFIG_SPIFFS_OBJ_NAME_LEN=128
CONFIG_SPIRAM_MODE_OCT=y
CONFIG_SPIRAM_SPEED_80M=y
CONFIG_SPIFFS_OBJ_NAME_LEN=128