Skip to content

Commit 364f11d

Browse files
committed
better impl. for syncr. lyrics for ID3 and FLAC
1 parent be02e69 commit 364f11d

File tree

4 files changed

+318
-146
lines changed

4 files changed

+318
-146
lines changed

examples/Synchronised lyrics/synchronised_lyrics.ino

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44
#include "Ticker.h"
55

66
Audio audio;
7-
Ticker ticker;
8-
char *lyricsText;
9-
size_t lyricsTextSize = 0;
10-
uint16_t lyricsPtr = 0;
11-
uint32_t timeStamp = 0;
12-
uint32_t ms = 0;
13-
char chbuf[512];
147

158
#define I2S_LRC 26
169
#define I2S_DOUT 25
@@ -22,32 +15,6 @@ char chbuf[512];
2215
#define SD_MMC_CMD 15
2316

2417

25-
size_t bigEndian(char* base, uint8_t numBytes, uint8_t shiftLeft = 8){
26-
uint64_t result = 0;
27-
if(numBytes < 1 || numBytes > 8) return 0;
28-
for (int i = 0; i < numBytes; i++) {
29-
result += *(base + i) << (numBytes -i - 1) * shiftLeft;
30-
}
31-
if(result > SIZE_MAX) {log_e("range overflow"); result = 0;} // overflow
32-
return (size_t)result;
33-
}
34-
35-
void tckr(){ // caller every 100ms
36-
if(audio.isRunning()){
37-
ms += 100;
38-
if(ms >= timeStamp){
39-
Serial.print(chbuf);
40-
strcpy(chbuf, lyricsText + lyricsPtr); lyricsPtr += strlen(chbuf) + 1; // strlen + '\0'
41-
timeStamp = bigEndian(lyricsText + lyricsPtr, 4); lyricsPtr += 4;
42-
}
43-
}
44-
else{
45-
if(lyricsText) {free(lyricsText); lyricsText = NULL; lyricsTextSize = 0;}
46-
47-
ticker.detach();
48-
}
49-
}
50-
5118
void setup() {
5219
pinMode(SD_MMC_D0, INPUT_PULLUP);
5320
Serial.begin(115200);
@@ -77,20 +44,8 @@ void audio_eof_mp3(const char *info){ //end of file
7744
Serial.print("eof_mp3 ");Serial.println(info);
7845
}
7946

80-
void audio_id3lyrics(File &file, const size_t pos, const size_t size) {
81-
Serial.printf("\naudio_id3lyrics, pos: %d, size: %d\n", pos, size);
82-
lyricsText = (char *)malloc(size);
83-
lyricsTextSize = size;
84-
file.seek(pos);
85-
file.read((uint8_t *)lyricsText, size);
86-
Serial.printf("text encoding: %i\n", lyricsText[0]); // 0: ASCII, 3: UTF-8
87-
char lang[14]; memcpy(lang, (const char*)lyricsText + 1, 3); lang[3] = '\0'; Serial.printf("language: %s\n", lang);
88-
Serial.printf("time stamp format: %i\n", lyricsText[4]);
89-
Serial.printf("content type: %i\n", lyricsText[5]);
90-
Serial.printf("content descriptor: %i\n\n", lyricsText[6]);
91-
lyricsPtr = 7;
92-
strcpy(chbuf, lyricsText + lyricsPtr); lyricsPtr += strlen(chbuf) + 1; // strlen + '\0'
93-
timeStamp = bigEndian(lyricsText + lyricsPtr, 4);
94-
ticker.attach(0.1, tckr); lyricsPtr += 4;
47+
void audio_id3lyrics(const char* text) {
48+
Serial.printf("%s\n", text);
49+
9550
}
9651

0 commit comments

Comments
 (0)