|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * @file PlayMP3FromSD.ino |
| 9 | + * @brief M5AtomS3 Atomic SPK SDCard MP3 Player |
| 10 | + * @version 0.1 |
| 11 | + * @date 2024-11-22 |
| 12 | + * |
| 13 | + * |
| 14 | + * @Hardwares: M5AtomS3 + Atomic SPK Base + MicroSD Card |
| 15 | + * @Platform Version: Arduino M5Stack Board Manager v2.1.2 |
| 16 | + * @Dependent Library: |
| 17 | + * M5GFX: https://github.com/m5stack/M5GFX |
| 18 | + * M5Unified: https://github.com/m5stack/M5Unified |
| 19 | + * M5AtomS3: https://github.com/m5stack/M5AtomS3 |
| 20 | + * ESP8266Audio: https://github.com/earlephilhower/ESP8266Audio |
| 21 | + * MP3 file link: http://gitfile.oss-cn-beijing.aliyuncs.com/11-fanfare.mp3 |
| 22 | + */ |
| 23 | + |
| 24 | +#include "M5AtomS3.h" |
| 25 | +#include <WiFi.h> |
| 26 | + |
| 27 | +#include "SPIFFS.h" |
| 28 | +#include "AudioFileSourceSD.h" |
| 29 | +#include "AudioGeneratorMP3.h" |
| 30 | +#include "AudioOutputI2S.h" |
| 31 | +#include "AudioFileSourceID3.h" |
| 32 | + |
| 33 | +AudioGeneratorMP3 *mp3; |
| 34 | +AudioFileSourceSD *file; |
| 35 | +AudioOutputI2S *out; |
| 36 | +AudioFileSourceID3 *id3; |
| 37 | + |
| 38 | +#define SCK 7 |
| 39 | +#define MISO 8 |
| 40 | +#define MOSI 6 |
| 41 | + |
| 42 | +void StatusCallback(void *cbData, int code, const char *string) |
| 43 | +{ |
| 44 | + const char *ptr = reinterpret_cast<const char *>(cbData); |
| 45 | + char s1[64]; |
| 46 | + strncpy_P(s1, string, sizeof(s1)); |
| 47 | + s1[sizeof(s1) - 1] = 0; |
| 48 | + Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1); |
| 49 | + Serial.flush(); |
| 50 | +} |
| 51 | + |
| 52 | +void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string) |
| 53 | +{ |
| 54 | + (void)cbData; |
| 55 | + Serial.printf("ID3 callback for: %s = '", type); |
| 56 | + |
| 57 | + if (isUnicode) { |
| 58 | + string += 2; |
| 59 | + } |
| 60 | + |
| 61 | + while (*string) { |
| 62 | + char a = *(string++); |
| 63 | + if (isUnicode) { |
| 64 | + string++; |
| 65 | + } |
| 66 | + Serial.printf("%c", a); |
| 67 | + } |
| 68 | + Serial.printf("'\n"); |
| 69 | + Serial.flush(); |
| 70 | +} |
| 71 | + |
| 72 | +void setup() |
| 73 | +{ |
| 74 | + AtomS3.begin(); |
| 75 | + Serial.begin(115200); |
| 76 | + SPI.begin(SCK, MISO, MOSI, -1); |
| 77 | + if (!SD.begin()) { |
| 78 | + Serial.println("Card Mount Failed"); |
| 79 | + return; |
| 80 | + } |
| 81 | + audioLogger = &Serial; |
| 82 | + file = new AudioFileSourceSD("/11-fanfare.mp3"); |
| 83 | + id3 = new AudioFileSourceID3(file); |
| 84 | + id3->RegisterMetadataCB(MDCallback, (void *)"ID3TAG"); |
| 85 | + out = new AudioOutputI2S(); |
| 86 | + |
| 87 | + out->SetPinout(5, 39, 38); |
| 88 | + |
| 89 | + mp3 = new AudioGeneratorMP3(); |
| 90 | + mp3->RegisterStatusCB(StatusCallback, (void *)"mp3"); |
| 91 | + mp3->begin(id3, out); |
| 92 | +} |
| 93 | + |
| 94 | +void loop() |
| 95 | +{ |
| 96 | + if (mp3->isRunning()) { |
| 97 | + if (!mp3->loop()) mp3->stop(); |
| 98 | + } else { |
| 99 | + Serial.printf("MP3 done\n"); |
| 100 | + delay(1000); |
| 101 | + } |
| 102 | +} |
0 commit comments