Update to 2.0.1

This commit is contained in:
2025-09-15 22:04:01 +08:00
parent 5c43129024
commit 7d7f5eae3d
74 changed files with 5253 additions and 439 deletions

View File

@@ -166,11 +166,12 @@ Esp32Music::Esp32Music() : last_downloaded_data_(), current_music_url_(), curren
song_name_displayed_(false), current_lyric_url_(), lyrics_(),
current_lyric_index_(-1), lyric_thread_(), is_lyric_running_(false),
display_mode_(DISPLAY_MODE_LYRICS), is_playing_(false), is_downloading_(false),
play_thread_(), download_thread_(), audio_buffer_(), buffer_mutex_(),
is_paused_(false), play_thread_(), download_thread_(), audio_buffer_(), buffer_mutex_(),
buffer_cv_(), buffer_size_(0), mp3_decoder_(nullptr), mp3_frame_info_(),
mp3_decoder_initialized_(false) {
ESP_LOGI(TAG, "Music player initialized with default spectrum display mode");
InitializeMp3Decoder();
// 延迟MP3解码器初始化避免在构造函数中初始化导致的问题
// InitializeMp3Decoder();
}
Esp32Music::~Esp32Music() {
@@ -282,9 +283,9 @@ Esp32Music::~Esp32Music() {
}
bool Esp32Music::Download(const std::string& song_name, const std::string& artist_name) {
ESP_LOGI(TAG, "Starting to get music details for: %s", song_name.c_str());
ESP_LOGI(TAG, "云端由MeowEmbeddedMusicServer喵波音律嵌入式提供");
ESP_LOGI(TAG, "喵波音律QQ交流群:865754861");
ESP_LOGI(TAG, "Starting to get music details for: %s", song_name.c_str());
// 清空之前的下载数据
last_downloaded_data_.clear();
@@ -357,21 +358,17 @@ bool Esp32Music::Download(const std::string& song_name, const std::string& artis
if (cJSON_IsString(audio_url) && audio_url->valuestring && strlen(audio_url->valuestring) > 0) {
ESP_LOGI(TAG, "Audio URL path: %s", audio_url->valuestring);
// 第二步:直接使用audio_url播放音乐
std::string audio_path = audio_url->valuestring;
current_music_url_ = audio_path;
// 第二步:直接使用音频URL开始流式播放
std::string current_music_url_ = audio_url->valuestring;
ESP_LOGI(TAG, "云端由MeowEmbeddedMusicServer喵波音律嵌入式提供");
ESP_LOGI(TAG, "喵波音律QQ交流群:865754861");
ESP_LOGI(TAG, "Starting streaming playback for: %s", song_name.c_str());
song_name_displayed_ = false; // 重置歌名显示标志
StartStreaming(current_music_url_);
// 处理歌词URL - 只有在歌词显示模式下才启动歌词
if (cJSON_IsString(lyric_url) && lyric_url->valuestring && strlen(lyric_url->valuestring) > 0) {
// 直接使用歌词URL
std::string lyric_path = lyric_url->valuestring;
current_lyric_url_ = lyric_path;
// 使用歌词URL获取歌词
std::string current_lyric_url_ = lyric_url->valuestring;
// 根据显示模式决定是否启动歌词
if (display_mode_ == DISPLAY_MODE_LYRICS) {
@@ -431,6 +428,14 @@ bool Esp32Music::StartStreaming(const std::string& music_url) {
ESP_LOGD(TAG, "Starting streaming for URL: %s", music_url.c_str());
// 确保MP3解码器已初始化
if (!mp3_decoder_initialized_) {
if (!InitializeMp3Decoder()) {
ESP_LOGE(TAG, "Failed to initialize MP3 decoder");
return false;
}
}
// 停止之前的播放和下载
is_downloading_ = false;
is_playing_ = false;
@@ -491,6 +496,7 @@ bool Esp32Music::StopStreaming() {
// 停止下载和播放标志
is_downloading_ = false;
is_playing_ = false;
is_paused_ = false; // 重置暂停状态
// 清空歌名显示
auto& board = Board::GetInstance();
@@ -723,8 +729,6 @@ void Esp32Music::PlayAudioStream() {
});
}
ESP_LOGI(TAG, "云端由MeowEmbeddedMusicServer喵波音律嵌入式提供");
ESP_LOGI(TAG, "喵波音律QQ交流群:865754861");
ESP_LOGI(TAG, "Starting playback with buffer size: %d", buffer_size_);
size_t total_played = 0;
@@ -744,18 +748,20 @@ void Esp32Music::PlayAudioStream() {
bool id3_processed = false;
while (is_playing_) {
// 检查是否被暂停
if (is_paused_) {
ESP_LOGD(TAG, "Music playback paused, waiting...");
vTaskDelay(pdMS_TO_TICKS(100));
continue;
}
// 检查设备状态,只有在空闲状态才播放音乐
auto& app = Application::GetInstance();
DeviceState current_state = app.GetDeviceState();
// 状态转换:说话中-》聆听中-》待机状态-》播放音乐
if (current_state == kDeviceStateListening || current_state == kDeviceStateSpeaking) {
if (current_state == kDeviceStateSpeaking) {
ESP_LOGI(TAG, "Device is in speaking state, switching to listening state for music playback");
}
if (current_state == kDeviceStateListening) {
ESP_LOGI(TAG, "Device is in listening state, switching to idle state for music playback");
}
// 等小智把话说完了,变成聆听状态之后,马上转成待机状态,进入音乐播放
if (current_state == kDeviceStateListening) {
ESP_LOGI(TAG, "Device is in listening state, switching to idle state for music playback");
// 切换状态
app.ToggleChatState(); // 变成待机状态
vTaskDelay(pdMS_TO_TICKS(300));
@@ -1133,8 +1139,6 @@ bool Esp32Music::DownloadLyrics(const std::string& lyric_url) {
add_auth_headers(http.get());
// 打开GET连接
ESP_LOGI(TAG, "云端由MeowEmbeddedMusicServer喵波音律嵌入式提供");
ESP_LOGI(TAG, "喵波音律QQ交流群:865754861");
if (!http->Open("GET", current_url)) {
ESP_LOGE(TAG, "Failed to open HTTP connection for lyrics");
// 移除delete http; 因为unique_ptr会自动管理内存
@@ -1426,4 +1430,99 @@ void Esp32Music::SetDisplayMode(DisplayMode mode) {
ESP_LOGI(TAG, "Display mode changed from %s to %s",
(old_mode == DISPLAY_MODE_SPECTRUM) ? "SPECTRUM" : "LYRICS",
(mode == DISPLAY_MODE_SPECTRUM) ? "SPECTRUM" : "LYRICS");
}
// MCP工具需要的方法实现
bool Esp32Music::SetVolume(int volume) {
ESP_LOGI(TAG, "SetVolume called with volume: %d", volume);
// 验证音量范围
if (volume < 0 || volume > 100) {
ESP_LOGW(TAG, "Invalid volume level: %d, must be between 0-100", volume);
return false;
}
// 通过Board获取AudioCodec并设置音量
auto& board = Board::GetInstance();
auto codec = board.GetAudioCodec();
if (codec) {
codec->SetOutputVolume(volume);
ESP_LOGI(TAG, "Volume set to %d%%", volume);
return true;
} else {
ESP_LOGE(TAG, "No audio codec available");
return false;
}
}
bool Esp32Music::PlaySong() {
ESP_LOGI(TAG, "PlaySong called");
return false;
}
bool Esp32Music::StopSong() {
ESP_LOGI(TAG, "StopSong called");
return StopStreaming();
}
bool Esp32Music::PauseSong() {
ESP_LOGI(TAG, "PauseSong called");
// 检查是否正在播放
if (!is_playing_) {
ESP_LOGW(TAG, "No music is currently playing");
return false;
}
// 检查是否已经暂停
if (is_paused_) {
ESP_LOGW(TAG, "Music is already paused");
return true;
}
// 设置暂停标志
is_paused_ = true;
ESP_LOGI(TAG, "Music playback paused");
// 更新显示状态
auto& board = Board::GetInstance();
auto display = board.GetDisplay();
if (display && !current_song_name_.empty()) {
std::string formatted_song_name = "" + current_song_name_ + "》已暂停";
display->SetMusicInfo(formatted_song_name.c_str());
ESP_LOGI(TAG, "Updated display: %s", formatted_song_name.c_str());
}
return true;
}
bool Esp32Music::ResumeSong() {
ESP_LOGI(TAG, "ResumeSong called");
// 检查是否正在播放
if (!is_playing_) {
ESP_LOGW(TAG, "No music is currently playing");
return false;
}
// 检查是否已经恢复
if (!is_paused_) {
ESP_LOGW(TAG, "Music is not paused");
return true;
}
// 清除暂停标志
is_paused_ = false;
ESP_LOGI(TAG, "Music playback resumed");
// 更新显示状态
auto& board = Board::GetInstance();
auto display = board.GetDisplay();
if (display && !current_song_name_.empty()) {
std::string formatted_song_name = "" + current_song_name_ + "》播放中...";
display->SetMusicInfo(formatted_song_name.c_str());
ESP_LOGI(TAG, "Updated display: %s", formatted_song_name.c_str());
}
return true;
}