64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
#include "Audio_Player.h"
|
|
#include "Audio.h"
|
|
|
|
static Audio audio;
|
|
static TaskHandle_t audioTaskHandle = NULL;
|
|
static volatile bool audioTaskRunning = false;
|
|
|
|
// ── Background decode task (Core 0) ──────────────────────────────────────────
|
|
static void audioLoopTask(void* param) {
|
|
audioTaskRunning = true;
|
|
while (audioTaskRunning) {
|
|
audio.loop();
|
|
vTaskDelay(pdMS_TO_TICKS(1));
|
|
}
|
|
audioTaskHandle = NULL;
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
void initAudioPlayer() {
|
|
audio.setPinout(I2S_BCLK, I2S_LRCK, I2S_DIN);
|
|
audio.setVolume(g_volume);
|
|
if (!audioTaskHandle) {
|
|
xTaskCreatePinnedToCore(audioLoopTask, "audioLoop", 16384, NULL, 5, &audioTaskHandle, 0);
|
|
}
|
|
}
|
|
|
|
// ── Public controls ───────────────────────────────────────────────────────────
|
|
void playAudio(const char* fullPath) {
|
|
printf("Audio: playing %s\r\n", fullPath);
|
|
audio.connecttoFS(SD_MMC, fullPath);
|
|
}
|
|
|
|
void playMemoryAudio(int memoryIndex) {
|
|
if (memoryIndex < 0 || memoryIndex >= g_memoryCount) return;
|
|
const char* audioFile = g_memories[memoryIndex].audio;
|
|
if (audioFile[0] == '\0') return; // no audio for this memory
|
|
char path[120];
|
|
snprintf(path, sizeof(path), "/audio/%s", audioFile);
|
|
playAudio(path);
|
|
}
|
|
|
|
void stopAudio() {
|
|
audio.stopSong();
|
|
}
|
|
|
|
void pauseResumeAudio() {
|
|
audio.pauseResume();
|
|
}
|
|
|
|
bool isAudioPlaying() {
|
|
return audio.isRunning();
|
|
}
|
|
|
|
void setPlayerVolume(uint8_t volume) {
|
|
if (volume > 21) volume = 21;
|
|
g_volume = volume;
|
|
audio.setVolume(g_volume);
|
|
printf("Audio: volume -> %d/21\r\n", g_volume);
|
|
}
|
|
|
|
uint8_t getPlayerVolume() {
|
|
return g_volume;
|
|
}
|