-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathAudio.cpp
6211 lines (5697 loc) · 270 KB
/
Audio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Audio.cpp
*
* Created on: Oct 28.2018
*
* Version 3.1.0d
* Updated on: Feb 01.2025
* Author: Wolle (schreibfaul1)
*
*/
#include "Audio.h"
#include "aac_decoder/aac_decoder.h"
#include "flac_decoder/flac_decoder.h"
#include "mp3_decoder/mp3_decoder.h"
#include "opus_decoder/opus_decoder.h"
#include "vorbis_decoder/vorbis_decoder.h"
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
AudioBuffer::AudioBuffer(size_t maxBlockSize) {
if(maxBlockSize) m_resBuffSizeRAM = maxBlockSize;
if(maxBlockSize) m_maxBlockSize = maxBlockSize;
}
AudioBuffer::~AudioBuffer() {
if(m_buffer) free(m_buffer);
m_buffer = NULL;
}
int32_t AudioBuffer::getBufsize() { return m_buffSize; }
size_t AudioBuffer::init() {
if(m_buffer) free(m_buffer);
m_buffer = NULL;
if(psramInit() && m_buffSizePSRAM > 0) { // PSRAM found, AudioBuffer will be allocated in PSRAM
m_f_psram = true;
m_buffSize = m_buffSizePSRAM;
m_buffer = (uint8_t*)ps_calloc(m_buffSize, sizeof(uint8_t));
m_buffSize = m_buffSizePSRAM - m_resBuffSizePSRAM;
}
if(m_buffer == NULL) { // PSRAM not found, not configured or not enough available
m_f_psram = false;
m_buffer = (uint8_t*)heap_caps_calloc(m_buffSizeRAM, sizeof(uint8_t), MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL);
m_buffSize = m_buffSizeRAM - m_resBuffSizeRAM;
}
if(!m_buffer) return 0;
m_f_init = true;
resetBuffer();
return m_buffSize;
}
void AudioBuffer::changeMaxBlockSize(uint16_t mbs) {
m_maxBlockSize = mbs;
return;
}
uint16_t AudioBuffer::getMaxBlockSize() { return m_maxBlockSize; }
size_t AudioBuffer::freeSpace() {
if(m_readPtr == m_writePtr) {
if(m_f_isEmpty == true) m_freeSpace = m_buffSize;
else m_freeSpace = 0;
}
if(m_readPtr < m_writePtr) {
m_freeSpace = (m_endPtr - m_writePtr) + (m_readPtr - m_buffer);
}
if(m_readPtr > m_writePtr) {
m_freeSpace = m_readPtr - m_writePtr;
}
return m_freeSpace;
}
size_t AudioBuffer::writeSpace() {
if(m_readPtr == m_writePtr) {
if(m_f_isEmpty == true) m_writeSpace = m_endPtr - m_writePtr;
else m_writeSpace = 0;
}
if(m_readPtr < m_writePtr) {
m_writeSpace = m_endPtr - m_writePtr;
}
if(m_readPtr > m_writePtr) {
m_writeSpace = m_readPtr - m_writePtr;
}
return m_writeSpace;
}
size_t AudioBuffer::bufferFilled() {
if(m_readPtr == m_writePtr) {
if(m_f_isEmpty == true) m_dataLength = 0;
else m_dataLength = m_buffSize;
}
if(m_readPtr < m_writePtr) {
m_dataLength = m_writePtr - m_readPtr;
}
if(m_readPtr > m_writePtr) {
m_dataLength = (m_endPtr - m_readPtr) + (m_writePtr - m_buffer);
}
return m_dataLength;
}
size_t AudioBuffer::getMaxAvailableBytes() {
if(m_readPtr == m_writePtr) {
// if(m_f_start)m_dataLength = 0;
if(m_f_isEmpty == true) m_dataLength = 0;
else m_dataLength = (m_endPtr - m_readPtr);
}
if(m_readPtr < m_writePtr) {
m_dataLength = m_writePtr - m_readPtr;
}
if(m_readPtr > m_writePtr) {
m_dataLength = (m_endPtr - m_readPtr);
}
return m_dataLength;
}
void AudioBuffer::bytesWritten(size_t bw) {
if(!bw) return;
m_writePtr += bw;
if(m_writePtr == m_endPtr) { m_writePtr = m_buffer; }
if(m_writePtr > m_endPtr) log_e("m_writePtr %i, m_endPtr %i", m_writePtr, m_endPtr);
m_f_isEmpty = false;
}
void AudioBuffer::bytesWasRead(size_t br) {
if(!br) return;
m_readPtr += br;
if(m_readPtr >= m_endPtr) {
size_t tmp = m_readPtr - m_endPtr;
m_readPtr = m_buffer + tmp;
}
if(m_readPtr == m_writePtr) m_f_isEmpty = true;
}
uint8_t* AudioBuffer::getWritePtr() { return m_writePtr; }
uint8_t* AudioBuffer::getReadPtr() {
int32_t len = m_endPtr - m_readPtr;
if(len < m_maxBlockSize) { // be sure the last frame is completed
memcpy(m_endPtr, m_buffer, m_maxBlockSize - (len)); // cpy from m_buffer to m_endPtr with len
}
return m_readPtr;
}
void AudioBuffer::resetBuffer() {
m_writePtr = m_buffer;
m_readPtr = m_buffer;
m_endPtr = m_buffer + m_buffSize;
m_f_isEmpty = true;
}
uint32_t AudioBuffer::getWritePos() { return m_writePtr - m_buffer; }
uint32_t AudioBuffer::getReadPos() { return m_readPtr - m_buffer; }
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// clang-format off
Audio::Audio(uint8_t i2sPort) {
mutex_playAudioData = xSemaphoreCreateMutex();
mutex_audioTask = xSemaphoreCreateMutex();
m_chbufSize = 512 + 64;
m_ibuffSize = 512 + 64;
m_chbuf = (char*) malloc(m_chbufSize);
m_ibuff = (char*) malloc(m_ibuffSize);
m_outBuff = (int16_t*)malloc(m_outbuffSize * sizeof(int16_t));
if(!m_chbuf || !m_outBuff || !m_ibuff) log_e("oom");
#ifdef AUDIO_LOG
m_f_Log = true;
#endif
#define AUDIO_INFO(...) { snprintf(m_ibuff, m_ibuffSize, __VA_ARGS__); if(audio_info) audio_info(m_ibuff); }
clientsecure.setInsecure();
m_i2s_num = i2sPort; // i2s port number
// -------- I2S configuration -------------------------------------------------------------------------------------------
m_i2s_chan_cfg.id = (i2s_port_t)m_i2s_num; // I2S_NUM_AUTO, I2S_NUM_0, I2S_NUM_1
m_i2s_chan_cfg.role = I2S_ROLE_MASTER; // I2S controller master role, bclk and lrc signal will be set to output
m_i2s_chan_cfg.dma_desc_num = 32; // number of DMA buffer
m_i2s_chan_cfg.dma_frame_num = 256; // I2S frame number in one DMA buffer.
m_i2s_chan_cfg.auto_clear = true; // i2s will always send zero automatically if no data to send
i2s_new_channel(&m_i2s_chan_cfg, &m_i2s_tx_handle, NULL);
m_i2s_std_cfg.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO); // Set to enable bit shift in Philips mode
m_i2s_std_cfg.gpio_cfg.bclk = I2S_GPIO_UNUSED; // BCLK, Assignment in setPinout()
m_i2s_std_cfg.gpio_cfg.din = I2S_GPIO_UNUSED; // not used
m_i2s_std_cfg.gpio_cfg.dout = I2S_GPIO_UNUSED; // DOUT, Assignment in setPinout()
m_i2s_std_cfg.gpio_cfg.mclk = I2S_GPIO_UNUSED; // MCLK, Assignment in setPinout()
m_i2s_std_cfg.gpio_cfg.ws = I2S_GPIO_UNUSED; // LRC, Assignment in setPinout()
m_i2s_std_cfg.gpio_cfg.invert_flags.mclk_inv = false;
m_i2s_std_cfg.gpio_cfg.invert_flags.bclk_inv = false;
m_i2s_std_cfg.gpio_cfg.invert_flags.ws_inv = false;
m_i2s_std_cfg.clk_cfg.sample_rate_hz = 44100;
m_i2s_std_cfg.clk_cfg.clk_src = I2S_CLK_SRC_DEFAULT; // Select PLL_F160M as the default source clock
m_i2s_std_cfg.clk_cfg.mclk_multiple = I2S_MCLK_MULTIPLE_128; // mclk = sample_rate * 256
i2s_channel_init_std_mode(m_i2s_tx_handle, &m_i2s_std_cfg);
I2Sstart(m_i2s_num);
m_sampleRate = 44100;
for(int i = 0; i < 3; i++) {
m_filter[i].a0 = 1;
m_filter[i].a1 = 0;
m_filter[i].a2 = 0;
m_filter[i].b1 = 0;
m_filter[i].b2 = 0;
}
computeLimit(); // first init, vol = 21, vol_steps = 21
startAudioTask();
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Audio::~Audio() {
// I2Sstop(m_i2s_num);
// InBuff.~AudioBuffer(); #215 the AudioBuffer is automatically destroyed by the destructor
setDefaults();
i2s_channel_disable(m_i2s_tx_handle);
i2s_del_channel(m_i2s_tx_handle);
x_ps_free(&m_playlistBuff);
x_ps_free(&m_chbuf);
x_ps_free(&m_lastHost);
x_ps_free(&m_outBuff);
x_ps_free(&m_ibuff);
x_ps_free(&m_lastM3U8host);
x_ps_free(&m_speechtxt);
stopAudioTask();
vSemaphoreDelete(mutex_playAudioData);
vSemaphoreDelete(mutex_audioTask);
}
// clang-format on
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Audio::initInBuff() {
if(!InBuff.isInitialized()) {
size_t size = InBuff.init();
if(size > 0) { AUDIO_INFO("PSRAM %sfound, inputBufferSize: %u bytes", InBuff.havePSRAM() ? "" : "not ", size - 1); }
}
changeMaxBlockSize(1600); // default size mp3 or aac
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
esp_err_t Audio::I2Sstart(uint8_t i2s_num) {
return i2s_channel_enable(m_i2s_tx_handle);
}
esp_err_t Audio::I2Sstop(uint8_t i2s_num) {
return i2s_channel_disable(m_i2s_tx_handle);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Audio::setDefaults() {
stopSong();
initInBuff(); // initialize InputBuffer if not already done
InBuff.resetBuffer();
MP3Decoder_FreeBuffers();
FLACDecoder_FreeBuffers();
AACDecoder_FreeBuffers();
OPUSDecoder_FreeBuffers();
VORBISDecoder_FreeBuffers();
memset(m_outBuff, 0, m_outbuffSize * sizeof(int16_t)); // Clear OutputBuffer
x_ps_free(&m_playlistBuff);
vector_clear_and_shrink(m_playlistURL);
vector_clear_and_shrink(m_playlistContent);
m_hashQueue.clear();
m_hashQueue.shrink_to_fit(); // uint32_t vector
client.stop();
// client.clear(); // delete all leftovers in the receive buffer
clientsecure.stop();
// clientsecure.clear(); // delete all leftovers in the receive buffer
_client = static_cast<WiFiClient*>(&client); /* default to *something* so that no NULL deref can happen */
ts_parsePacket(0, 0, 0); // reset ts routine
x_ps_free(&m_lastM3U8host);
x_ps_free(&m_speechtxt);
AUDIO_INFO("buffers freed, free Heap: %lu bytes", (long unsigned int)ESP.getFreeHeap());
m_f_timeout = false;
m_f_chunked = false; // Assume not chunked
m_f_firstmetabyte = false;
m_f_playing = false;
// m_f_ssl = false;
m_f_metadata = false;
m_f_tts = false;
m_f_firstCall = true; // InitSequence for processWebstream and processLocalFile
m_f_firstCurTimeCall = true; // InitSequence for computeAudioTime
m_f_firstM3U8call = true; // InitSequence for parsePlaylist_M3U8
m_f_firstPlayCall = true; // InitSequence for playAudioData
// m_f_running = false; // already done in stopSong
m_f_loop = false; // Set if audio file should loop
m_f_unsync = false; // set within ID3 tag but not used
m_f_exthdr = false; // ID3 extended header
m_f_rtsp = false; // RTSP (m3u8)stream
m_f_m3u8data = false; // set again in processM3U8entries() if necessary
m_f_continue = false;
m_f_ts = false;
m_f_m4aID3dataAreRead = false;
m_f_stream = false;
m_f_decode_ready = false;
m_f_eof = false;
m_f_ID3v1TagFound = false;
m_f_lockInBuffer = false;
m_f_acceptRanges = false;
m_streamType = ST_NONE;
m_codec = CODEC_NONE;
m_playlistFormat = FORMAT_NONE;
m_dataMode = AUDIO_NONE;
m_audioCurrentTime = 0; // Reset playtimer
m_audioFileDuration = 0;
m_audioDataStart = 0;
m_audioDataSize = 0;
m_avr_bitrate = 0; // the same as m_bitrate if CBR, median if VBR
m_bitRate = 0; // Bitrate still unknown
m_bytesNotDecoded = 0; // counts all not decodable bytes
m_chunkcount = 0; // for chunked streams
m_contentlength = 0; // If Content-Length is known, count it
m_curSample = 0;
m_metaint = 0; // No metaint yet
m_LFcount = 0; // For end of header detection
m_controlCounter = 0; // Status within readID3data() and readWaveHeader()
m_channels = 2; // assume stereo #209
m_streamTitleHash = 0;
m_fileSize = 0;
m_ID3Size = 0;
m_haveNewFilePos = 0;
m_validSamples = 0;
m_M4A_chConfig = 0;
m_M4A_objectType = 0;
m_M4A_sampleRate = 0;
m_sumBytesDecoded = 0;
m_vuLeft = m_vuRight = 0; // #835
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Audio::setConnectionTimeout(uint16_t timeout_ms, uint16_t timeout_ms_ssl) {
if(timeout_ms) m_timeout_ms = timeout_ms;
if(timeout_ms_ssl) m_timeout_ms_ssl = timeout_ms_ssl;
}
/*
Text to speech API provides a speech endpoint based on our TTS (text-to-speech) model.
More info: https://platform.openai.com/docs/guides/text-to-speech/text-to-speech
Request body:
model (string) [Required] - One of the available TTS models: tts-1 or tts-1-hd
input (string) [Required] - The text to generate audio for. The maximum length is 4096 characters.
voice (string) [Required] - The voice to use when generating the audio. Supported voices are alloy, echo, fable, onyx, nova, and shimmer.
response_format (string) [Optional] - Defaults to mp3. The format to audio in. Supported formats are mp3, opus, aac, and flac.
speed (number) [Optional] - Defaults to 1. The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.
Usage: audio.openai_speech(OPENAI_API_KEY, "tts-1", input, "shimmer", "mp3", "1");
*/
bool Audio::openai_speech(const String& api_key, const String& model, const String& input, const String& voice, const String& response_format, const String& speed) {
char host[] = "api.openai.com";
char path[] = "/v1/audio/speech";
if (input == "") {
AUDIO_INFO("input text is empty");
stopSong();
return false;
}
xSemaphoreTakeRecursive(mutex_playAudioData, 0.3 * configTICK_RATE_HZ);
setDefaults();
m_f_ssl = true;
String input_clean = "";
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == '\"') {
input_clean += "\\\"";
} else if (c == '\n') {
input_clean += "\\n";
} else {
input_clean += c;
}
}
String post_body = "{"
"\"model\": \"" + model + "\"," +
"\"input\": \"" + input_clean + "\"," +
"\"voice\": \"" + voice + "\"," +
"\"response_format\": \"" + response_format + "\"," +
"\"speed\": \"" + speed + "\"" +
"}";
String http_request =
"POST " + String(path) + " HTTP/1.0\r\n" // UNKNOWN ERROR CODE (0050) - crashing on HTTP/1.1 need to use HTTP/1.0
+ "Host: " + String(host) + "\r\n"
+ "Authorization: Bearer " + api_key + "\r\n"
+ "Accept-Encoding: identity;q=1,*;q=0\r\n"
+ "User-Agent: nArija/1.0\r\n"
+ "Content-Type: application/json; charset=utf-8\r\n"
+ "Content-Length: " + post_body.length() + "\r\n"
+ "Connection: keep-alive\r\n" + "\r\n"
+ post_body + "\r\n"
;
bool res = true;
int port = 443;
_client = static_cast<WiFiClient*>(&clientsecure);
uint32_t t = millis();
AUDIO_INFO("Connect to: \"%s\"", host);
res = _client->connect(host, port, m_timeout_ms_ssl);
if (res) {
uint32_t dt = millis() - t;
x_ps_free(&m_lastHost);
m_lastHost = x_ps_strdup(host);
AUDIO_INFO("%s has been established in %lu ms, free Heap: %lu bytes", "SSL", (long unsigned int) dt, (long unsigned int) ESP.getFreeHeap());
m_f_running = true;
}
m_expectedCodec = CODEC_NONE;
m_expectedPlsFmt = FORMAT_NONE;
if (res) {
_client->print(http_request);
if (response_format == "mp3") m_expectedCodec = CODEC_MP3;
if (response_format == "opus") m_expectedCodec = CODEC_OPUS;
if (response_format == "aac") m_expectedCodec = CODEC_AAC;
if (response_format == "flac") m_expectedCodec = CODEC_FLAC;
m_dataMode = HTTP_RESPONSE_HEADER;
m_streamType = ST_WEBSTREAM;
} else {
AUDIO_INFO("Request %s failed!", host);
// x_ps_free(&m_lastHost);
}
xSemaphoreGiveRecursive(mutex_playAudioData);
return res;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bool Audio::connecttohost(const char* host, const char* user, const char* pwd) { // user and pwd for authentification only, can be empty
bool res = false; // return value
char* c_host = NULL; // copy of host
uint16_t lenHost = 0; // length of hostname
uint16_t port = 0; // port number
uint16_t authLen = 0; // length of authorization
int16_t pos_slash = 0; // position of "/" in hostname
int16_t pos_colon = 0; // position of ":" in hostname
int16_t pos_ampersand = 0; // position of "&" in hostname
uint32_t timestamp = 0; // timeout surveillance
uint16_t hostwoext_begin = 0;
// char* authorization = NULL; // authorization
char* rqh = NULL; // request header
char* toEncode = NULL; // temporary memory for base64 encoding
char* h_host = NULL;
// https://edge.live.mp3.mdn.newmedia.nacamar.net:8000/ps-charivariwb/livestream.mp3;&user=ps-charivariwb;&pwd=ps-charivariwb-------
// | | | | |
// | | | | | (query string)
// ssl?| |<-----host without extension-------->|port|<----- --extension----------->|<-first parameter->|<-second parameter->.......
xSemaphoreTakeRecursive(mutex_playAudioData, 0.3 * configTICK_RATE_HZ);
// optional basic authorization
if(user && pwd) authLen = strlen(user) + strlen(pwd);
char authorization[base64_encode_expected_len(authLen + 1) + 1];
authorization[0] = '\0';
if(authLen > 0) {
char toEncode[authLen + 4];
strcpy(toEncode, user);
strcat(toEncode, ":");
strcat(toEncode, pwd);
b64encode((const char*)toEncode, strlen(toEncode), authorization);
}
if (host == NULL) { AUDIO_INFO("Hostaddress is empty"); stopSong(); goto exit;}
if (strlen(host) > 2048) { AUDIO_INFO("Hostaddress is too long"); stopSong(); goto exit;} // max length in Chrome DevTools
c_host = x_ps_strdup(host); // make a copy
h_host = urlencode(c_host, true);
trim(h_host); // remove leading and trailing spaces
lenHost = strlen(h_host);
if(!startsWith(h_host, "http")) { AUDIO_INFO("Hostaddress is not valid"); stopSong(); goto exit;}
if(startsWith(h_host, "https")) {m_f_ssl = true; hostwoext_begin = 8; port = 443;}
else {m_f_ssl = false; hostwoext_begin = 7; port = 80;}
// In the URL there may be an extension, like noisefm.ru:8000/play.m3u&t=.m3u
pos_slash = indexOf(h_host, "/", 10); // position of "/" in hostname
pos_colon = indexOf(h_host, ":", 10); if(isalpha(c_host[pos_colon + 1])) pos_colon = -1; // no portnumber follows
pos_ampersand = indexOf(h_host, "&", 10); // position of "&" in hostname
if(pos_slash > 0) h_host[pos_slash] = '\0';
if((pos_colon > 0) && ((pos_ampersand == -1) || (pos_ampersand > pos_colon))) {
port = atoi(c_host + pos_colon + 1); // Get portnumber as integer
h_host[pos_colon] = '\0';
}
setDefaults();
rqh = x_ps_calloc(lenHost + strlen(authorization) + 300, 1); // http request header
if(!rqh) {AUDIO_INFO("out of memory"); stopSong(); goto exit;}
strcat(rqh, "GET /");
if(pos_slash > 0){ strcat(rqh, h_host + pos_slash + 1);}
strcat(rqh, " HTTP/1.1\r\n");
strcat(rqh, "Host: ");
strcat(rqh, h_host + hostwoext_begin);
strcat(rqh, "\r\n");
strcat(rqh, "Icy-MetaData:1\r\n");
strcat(rqh, "Icy-MetaData:2\r\n");
strcat(rqh, "Accept:*/*\r\n");
strcat(rqh, "User-Agent: VLC/3.0.21 LibVLC/3.0.21\r\n");
if(authLen > 0) { strcat(rqh, "Authorization: Basic ");
strcat(rqh, authorization);
strcat(rqh, "\r\n"); }
strcat(rqh, "Accept-Encoding: identity;q=1,*;q=0\r\n");
strcat(rqh, "Connection: keep-alive\r\n\r\n");
if(m_f_ssl) { _client = static_cast<WiFiClient*>(&clientsecure);}
else { _client = static_cast<WiFiClient*>(&client); }
timestamp = millis();
_client->setTimeout(m_f_ssl ? m_timeout_ms_ssl : m_timeout_ms);
AUDIO_INFO("connect to: \"%s\" on port %d path \"/%s\"", h_host + hostwoext_begin, port, h_host + pos_slash + 1);
res = _client->connect(h_host + hostwoext_begin, port);
if(pos_slash > 0) h_host[pos_slash] = '/';
if(pos_colon > 0) h_host[pos_colon] = ':';
m_expectedCodec = CODEC_NONE;
m_expectedPlsFmt = FORMAT_NONE;
if(res) {
uint32_t dt = millis() - timestamp;
x_ps_free(&m_lastHost);
m_lastHost = x_ps_strdup(c_host);
AUDIO_INFO("%s has been established in %lu ms, free Heap: %lu bytes", m_f_ssl ? "SSL" : "Connection", (long unsigned int)dt, (long unsigned int)ESP.getFreeHeap());
m_f_running = true;
_client->print(rqh);
if(endsWith(h_host, ".mp3" )) m_expectedCodec = CODEC_MP3;
if(endsWith(h_host, ".aac" )) m_expectedCodec = CODEC_AAC;
if(endsWith(h_host, ".wav" )) m_expectedCodec = CODEC_WAV;
if(endsWith(h_host, ".m4a" )) m_expectedCodec = CODEC_M4A;
if(endsWith(h_host, ".ogg" )) m_expectedCodec = CODEC_OGG;
if(endsWith(h_host, ".flac")) m_expectedCodec = CODEC_FLAC;
if(endsWith(h_host, "-flac")) m_expectedCodec = CODEC_FLAC;
if(endsWith(h_host, ".opus")) m_expectedCodec = CODEC_OPUS;
if(endsWith(h_host, "/opus")) m_expectedCodec = CODEC_OPUS;
if(endsWith(h_host, ".asx" )) m_expectedPlsFmt = FORMAT_ASX;
if(endsWith(h_host, ".m3u" )) m_expectedPlsFmt = FORMAT_M3U;
if(endsWith(h_host, ".pls" )) m_expectedPlsFmt = FORMAT_PLS;
if(endsWith(h_host, ".m3u8")) {
m_expectedPlsFmt = FORMAT_M3U8;
if(audio_lasthost) audio_lasthost(m_lastHost);
}
m_dataMode = HTTP_RESPONSE_HEADER; // Handle header
m_streamType = ST_WEBSTREAM;
}
else {
AUDIO_INFO("Request %s failed!", c_host);
m_f_running = false;
if(audio_showstation) audio_showstation("");
if(audio_showstreamtitle) audio_showstreamtitle("");
if(audio_icydescription) audio_icydescription("");
if(audio_icyurl) audio_icyurl("");
}
exit:
xSemaphoreGiveRecursive(mutex_playAudioData);
x_ps_free(&c_host);
x_ps_free(&h_host);
x_ps_free(&rqh);
x_ps_free(&toEncode);
return res;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bool Audio::httpPrint(const char* host) {
// user and pwd for authentification only, can be empty
if(!m_f_running) return false;
if(host == NULL) {
AUDIO_INFO("Hostaddress is empty");
stopSong();
return false;
}
char* h_host = NULL; // pointer of l_host without http:// or https://
if(startsWith(host, "https")) m_f_ssl = true;
else m_f_ssl = false;
if(m_f_ssl) h_host = strdup(host + 8);
else h_host = strdup(host + 7);
int16_t pos_slash; // position of "/" in hostname
int16_t pos_colon; // position of ":" in hostname
int16_t pos_ampersand; // position of "&" in hostname
uint16_t port = 80; // port number
// In the URL there may be an extension, like noisefm.ru:8000/play.m3u&t=.m3u
pos_slash = indexOf(h_host, "/", 0);
pos_colon = indexOf(h_host, ":", 0);
if(isalpha(h_host[pos_colon + 1])) pos_colon = -1; // no portnumber follows
pos_ampersand = indexOf(h_host, "&", 0);
char* hostwoext = NULL; // "skonto.ls.lv:8002" in "skonto.ls.lv:8002/mp3"
char* extension = NULL; // "/mp3" in "skonto.ls.lv:8002/mp3"
if(pos_slash > 1) {
hostwoext = (char*)malloc(pos_slash + 1);
memcpy(hostwoext, h_host, pos_slash);
hostwoext[pos_slash] = '\0';
extension = urlencode(h_host + pos_slash, true);
}
else { // url has no extension
hostwoext = strdup(h_host);
extension = strdup("/");
}
if((pos_colon >= 0) && ((pos_ampersand == -1) || (pos_ampersand > pos_colon))) {
port = atoi(h_host + pos_colon + 1); // Get portnumber as integer
hostwoext[pos_colon] = '\0'; // Host without portnumber
}
char rqh[strlen(h_host) + 300]; // http request header
rqh[0] = '\0';
strcat(rqh, "GET ");
strcat(rqh, extension);
strcat(rqh, " HTTP/1.1\r\n");
strcat(rqh, "Host: ");
strcat(rqh, hostwoext);
strcat(rqh, "\r\n");
strcat(rqh, "Accept: */*\r\n");
strcat(rqh, "User-Agent: VLC/3.0.21 LibVLC/3.0.21\r\n");
strcat(rqh, "Accept-Encoding: identity;q=1,*;q=0\r\n");
strcat(rqh, "Connection: keep-alive\r\n\r\n");
AUDIO_INFO("connect to: \"%s\"", host);
if(!_client->connected()) {
if(m_f_ssl) { _client = static_cast<WiFiClient*>(&clientsecure); if(m_f_ssl && port == 80) port = 443;}
else { _client = static_cast<WiFiClient*>(&client); }
AUDIO_INFO("The host has disconnected, reconnecting");
if(!_client->connect(hostwoext, port)) {
log_e("connection lost");
stopSong();
return false;
}
}
_client->print(rqh);
if( endsWith(extension, ".mp3")) m_expectedCodec = CODEC_MP3;
else if(endsWith(extension, ".aac")) m_expectedCodec = CODEC_AAC;
else if(endsWith(extension, ".wav")) m_expectedCodec = CODEC_WAV;
else if(endsWith(extension, ".m4a")) m_expectedCodec = CODEC_M4A;
else if(endsWith(extension, ".flac")) m_expectedCodec = CODEC_FLAC;
else m_expectedCodec = CODEC_NONE;
if( endsWith(extension, ".asx")) m_expectedPlsFmt = FORMAT_ASX;
else if(endsWith(extension, ".m3u")) m_expectedPlsFmt = FORMAT_M3U;
else if(indexOf( extension, ".m3u8") >= 0) m_expectedPlsFmt = FORMAT_M3U8;
else if(endsWith(extension, ".pls")) m_expectedPlsFmt = FORMAT_PLS;
else m_expectedPlsFmt = FORMAT_NONE;
m_dataMode = HTTP_RESPONSE_HEADER; // Handle header
m_streamType = ST_WEBSTREAM;
m_contentlength = 0;
m_f_chunked = false;
x_ps_free(&hostwoext);
x_ps_free(&extension);
x_ps_free(&h_host);
return true;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bool Audio::httpRange(const char* host, uint32_t range){
// user and pwd for authentification only, can be empty
if(!m_f_running) return false;
if(host == NULL) {
AUDIO_INFO("Hostaddress is empty");
stopSong();
return false;
}
char* h_host = NULL; // pointer of host without http:// or https://
if(startsWith(host, "https")) m_f_ssl = true;
else m_f_ssl = false;
if(m_f_ssl) h_host = strdup(host + 8);
else h_host = strdup(host + 7);
int16_t pos_slash; // position of "/" in hostname
int16_t pos_colon; // position of ":" in hostname
int16_t pos_ampersand; // position of "&" in hostname
uint16_t port = 80; // port number
// In the URL there may be an extension, like noisefm.ru:8000/play.m3u&t=.m3u
pos_slash = indexOf(h_host, "/", 0);
pos_colon = indexOf(h_host, ":", 0);
if(isalpha(h_host[pos_colon + 1])) pos_colon = -1; // no portnumber follows
pos_ampersand = indexOf(h_host, "&", 0);
char* hostwoext = NULL; // "skonto.ls.lv:8002" in "skonto.ls.lv:8002/mp3"
char* extension = NULL; // "/mp3" in "skonto.ls.lv:8002/mp3"
if(pos_slash > 1) {
hostwoext = (char*)malloc(pos_slash + 1);
memcpy(hostwoext, h_host, pos_slash);
hostwoext[pos_slash] = '\0';
extension = urlencode(h_host + pos_slash, true);
}
else { // url has no extension
hostwoext = strdup(h_host);
extension = strdup("/");
}
if((pos_colon >= 0) && ((pos_ampersand == -1) || (pos_ampersand > pos_colon))) {
port = atoi(h_host + pos_colon + 1); // Get portnumber as integer
hostwoext[pos_colon] = '\0'; // Host without portnumber
}
char rqh[strlen(h_host) + strlen(host) + 300]; // http request header
rqh[0] = '\0';
char ch_range[12];
ltoa(range, ch_range, 10);
AUDIO_INFO("skip to position: %li", (long int)range);
strcat(rqh, "GET ");
strcat(rqh, extension);
strcat(rqh, " HTTP/1.1\r\n");
strcat(rqh, "Host: ");
strcat(rqh, hostwoext);
strcat(rqh, "\r\n");
strcat(rqh, "Range: bytes=");
strcat(rqh, (const char*)ch_range);
strcat(rqh, "-\r\n");
strcat(rqh, "Referer: ");
strcat(rqh, host);
strcat(rqh, "\r\n");
strcat(rqh, "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36\r\n");
strcat(rqh, "Connection: keep-alive\r\n\r\n");
log_e("%s", rqh);
_client->stop();
if(m_f_ssl) { _client = static_cast<WiFiClient*>(&clientsecure); if(m_f_ssl && port == 80) port = 443;}
else { _client = static_cast<WiFiClient*>(&client); }
AUDIO_INFO("The host has disconnected, reconnecting");
if(!_client->connect(hostwoext, port)) {
log_e("connection lost");
stopSong();
return false;
}
_client->print(rqh);
if(endsWith(extension, ".mp3")) m_expectedCodec = CODEC_MP3;
if(endsWith(extension, ".aac")) m_expectedCodec = CODEC_AAC;
if(endsWith(extension, ".wav")) m_expectedCodec = CODEC_WAV;
if(endsWith(extension, ".m4a")) m_expectedCodec = CODEC_M4A;
if(endsWith(extension, ".flac")) m_expectedCodec = CODEC_FLAC;
if(endsWith(extension, ".asx")) m_expectedPlsFmt = FORMAT_ASX;
if(endsWith(extension, ".m3u")) m_expectedPlsFmt = FORMAT_M3U;
if(indexOf( extension, ".m3u8") >= 0) m_expectedPlsFmt = FORMAT_M3U8;
if(endsWith(extension, ".pls")) m_expectedPlsFmt = FORMAT_PLS;
m_dataMode = HTTP_RESPONSE_HEADER; // Handle header
m_streamType = ST_WEBFILE;
m_contentlength = 0;
m_f_chunked = false;
x_ps_free(&hostwoext);
x_ps_free(&extension);
x_ps_free(&h_host);
return true;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bool Audio::setFileLoop(bool input) {
if(m_codec == CODEC_M4A) return 0;
m_f_loop = input;
return input;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// clang-format off
void Audio::UTF8toASCII(char* str) {
const uint8_t ascii[60] = {
//129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148 // UTF8(C3)
// Ä Å Æ Ç É Ñ // CHAR
000, 000, 000, 142, 143, 146, 128, 000, 144, 000, 000, 000, 000, 000, 000, 000, 165, 000, 000, 000, // ASCII
//149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168
// Ö Ü ß à ä å æ è
000, 153, 000, 000, 000, 000, 000, 154, 000, 000, 225, 133, 000, 000, 000, 132, 134, 145, 000, 138,
//169, 170, 171, 172. 173. 174. 175, 176, 177, 179, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188
// ê ë ì î ï ñ ò ô ö ù û ü
000, 136, 137, 141, 000, 140, 139, 000, 164, 149, 000, 147, 000, 148, 000, 000, 151, 000, 150, 129};
uint16_t i = 0, j = 0, s = 0;
bool f_C3_seen = false;
while(str[i] != 0) { // convert UTF8 to ASCII
if(str[i] == 195) { // C3
i++;
f_C3_seen = true;
continue;
}
str[j] = str[i];
if(str[j] > 128 && str[j] < 189 && f_C3_seen == true) {
s = ascii[str[j] - 129];
if(s != 0) str[j] = s; // found a related ASCII sign
f_C3_seen = false;
}
i++;
j++;
}
str[j] = 0;
}
// clang-format on
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bool Audio::connecttoFS(fs::FS& fs, const char* path, int32_t fileStartPos) {
xSemaphoreTakeRecursive(mutex_playAudioData, 0.3 * configTICK_RATE_HZ);
bool res = false;
int16_t dotPos;
char* audioPath = NULL;
m_fileStartPos = fileStartPos;
uint8_t codec = CODEC_NONE;
if(!path) {printProcessLog(AUDIOLOG_PATH_IS_NULL); goto exit;} // guard
dotPos = lastIndexOf(path, ".");
if(dotPos == -1) {AUDIO_INFO("No file extension found"); goto exit;} // guard
setDefaults(); // free buffers an set defaults
if(endsWith(path, ".mp3")) codec = CODEC_MP3;
if(endsWith(path, ".m4a")) codec = CODEC_M4A;
if(endsWith(path, ".aac")) codec = CODEC_AAC;
if(endsWith(path, ".wav")) codec = CODEC_WAV;
if(endsWith(path, ".flac")) codec = CODEC_FLAC;
if(endsWith(path, ".opus")) codec = CODEC_OPUS;
if(endsWith(path, ".ogg")) codec = CODEC_OGG;
if(endsWith(path, ".oga")) codec = CODEC_OGG;
if(codec == CODEC_NONE) {AUDIO_INFO("The %s format is not supported", path + dotPos); goto exit;} // guard
audioPath = (char *)x_ps_calloc(strlen(path) + 2, sizeof(char));
if(!audioPath){printProcessLog(AUDIOLOG_OUT_OF_MEMORY); goto exit;};
if(path[0] != '/')audioPath[0] = '/';
strcat(audioPath, path);
if(!fs.exists(audioPath)) {printProcessLog(AUDIOLOG_FILE_NOT_FOUND, audioPath); goto exit;}
AUDIO_INFO("Reading file: \"%s\"", audioPath);
audiofile = fs.open(audioPath);
m_dataMode = AUDIO_LOCALFILE;
m_fileSize = audiofile.size();
res = initializeDecoder(codec);
m_codec = codec;
if(res) m_f_running = true;
else audiofile.close();
exit:
x_ps_free(&audioPath);
xSemaphoreGiveRecursive(mutex_playAudioData);
return res;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bool Audio::connecttospeech(const char* speech, const char* lang) {
xSemaphoreTakeRecursive(mutex_playAudioData, 0.3 * configTICK_RATE_HZ);
setDefaults();
char host[] = "translate.google.com.vn";
char path[] = "/translate_tts";
x_ps_free(&m_speechtxt);
m_speechtxt = x_ps_strdup(speech);
char* urlStr = urlencode(speech, false); // percent encoding
if(!urlStr) {
log_e("out of memory");
xSemaphoreGiveRecursive(mutex_playAudioData);
return false;
}
char resp[strlen(urlStr) + 200] = "";
strcat(resp, "GET ");
strcat(resp, path);
strcat(resp, "?ie=UTF-8&tl=");
strcat(resp, lang);
strcat(resp, "&client=tw-ob&q=");
strcat(resp, urlStr);
strcat(resp, " HTTP/1.1\r\n");
strcat(resp, "Host: ");
strcat(resp, host);
strcat(resp, "\r\n");
strcat(resp, "User-Agent: Mozilla/5.0 \r\n");
strcat(resp, "Accept-Encoding: identity\r\n");
strcat(resp, "Accept: text/html\r\n");
strcat(resp, "Connection: close\r\n\r\n");
x_ps_free(&urlStr);
_client = static_cast<WiFiClient*>(&client);
AUDIO_INFO("connect to \"%s\"", host);
if(!_client->connect(host, 80)) {
log_e("Connection failed");
xSemaphoreGiveRecursive(mutex_playAudioData);
return false;
}
_client->print(resp);
m_streamType = ST_WEBFILE;
m_f_running = true;
m_f_ssl = false;
m_f_tts = true;
m_dataMode = HTTP_RESPONSE_HEADER;
x_ps_free(&m_lastHost); m_lastHost = x_ps_strdup(host);
xSemaphoreGiveRecursive(mutex_playAudioData);
return true;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Audio::showID3Tag(const char* tag, const char* value) {
m_chbuf[0] = 0;
// V2.2
if(!strcmp(tag, "CNT")) sprintf(m_chbuf, "Play counter: %s", value);
// if(!strcmp(tag, "COM")) sprintf(m_chbuf, "Comments: %s", value);
if(!strcmp(tag, "CRA")) sprintf(m_chbuf, "Audio encryption: %s", value);
if(!strcmp(tag, "CRM")) sprintf(m_chbuf, "Encrypted meta frame: %s", value);
if(!strcmp(tag, "ETC")) sprintf(m_chbuf, "Event timing codes: %s", value);
if(!strcmp(tag, "EQU")) sprintf(m_chbuf, "Equalization: %s", value);
if(!strcmp(tag, "IPL")) sprintf(m_chbuf, "Involved people list: %s", value);
if(!strcmp(tag, "PIC")) sprintf(m_chbuf, "Attached picture: %s", value);
if(!strcmp(tag, "SLT")) sprintf(m_chbuf, "Synchronized lyric/text: %s", value);
if(!strcmp(tag, "TAL")) sprintf(m_chbuf, "Album/Movie/Show title: %s", value);
if(!strcmp(tag, "TBP")) sprintf(m_chbuf, "BPM (Beats Per Minute): %s", value);
if(!strcmp(tag, "TCM")) sprintf(m_chbuf, "Composer: %s", value);
if(!strcmp(tag, "TCO")) sprintf(m_chbuf, "Content type: %s", value);
if(!strcmp(tag, "TCR")) sprintf(m_chbuf, "Copyright message: %s", value);
if(!strcmp(tag, "TDA")) sprintf(m_chbuf, "Date: %s", value);
if(!strcmp(tag, "TDY")) sprintf(m_chbuf, "Playlist delay: %s", value);
if(!strcmp(tag, "TEN")) sprintf(m_chbuf, "Encoded by: %s", value);
if(!strcmp(tag, "TFT")) sprintf(m_chbuf, "File type: %s", value);
if(!strcmp(tag, "TIM")) sprintf(m_chbuf, "Time: %s", value);
if(!strcmp(tag, "TKE")) sprintf(m_chbuf, "Initial key: %s", value);
if(!strcmp(tag, "TLA")) sprintf(m_chbuf, "Language(s): %s", value);
if(!strcmp(tag, "TLE")) sprintf(m_chbuf, "Length: %s", value);
if(!strcmp(tag, "TMT")) sprintf(m_chbuf, "Media type: %s", value);
if(!strcmp(tag, "TOA")) sprintf(m_chbuf, "Original artist(s)/performer(s): %s", value);
if(!strcmp(tag, "TOF")) sprintf(m_chbuf, "Original filename: %s", value);
if(!strcmp(tag, "TOL")) sprintf(m_chbuf, "Original Lyricist(s)/text writer(s): %s", value);
if(!strcmp(tag, "TOR")) sprintf(m_chbuf, "Original release year: %s", value);
if(!strcmp(tag, "TOT")) sprintf(m_chbuf, "Original album/Movie/Show title: %s", value);
if(!strcmp(tag, "TP1")) sprintf(m_chbuf, "Lead artist(s)/Lead performer(s)/Soloist(s)/Performing group: %s", value);
if(!strcmp(tag, "TP2")) sprintf(m_chbuf, "Band/Orchestra/Accompaniment: %s", value);
if(!strcmp(tag, "TP3")) sprintf(m_chbuf, "Conductor/Performer refinement: %s", value);
if(!strcmp(tag, "TP4")) sprintf(m_chbuf, "Interpreted, remixed, or otherwise modified by: %s", value);
if(!strcmp(tag, "TPA")) sprintf(m_chbuf, "Part of a set: %s", value);
if(!strcmp(tag, "TPB")) sprintf(m_chbuf, "Publisher: %s", value);
if(!strcmp(tag, "TRC")) sprintf(m_chbuf, "ISRC (International Standard Recording Code): %s", value);
if(!strcmp(tag, "TRD")) sprintf(m_chbuf, "Recording dates: %s", value);
if(!strcmp(tag, "TRK")) sprintf(m_chbuf, "Track number/Position in set: %s", value);
if(!strcmp(tag, "TSI")) sprintf(m_chbuf, "Size: %s", value);
if(!strcmp(tag, "TSS")) sprintf(m_chbuf, "Software/hardware and settings used for encoding: %s", value);
if(!strcmp(tag, "TT1")) sprintf(m_chbuf, "Content group description: %s", value);
if(!strcmp(tag, "TT2")) sprintf(m_chbuf, "Title/Songname/Content description: %s", value);
if(!strcmp(tag, "TT3")) sprintf(m_chbuf, "Subtitle/Description refinement: %s", value);
if(!strcmp(tag, "TXT")) sprintf(m_chbuf, "Lyricist/text writer: %s", value);
if(!strcmp(tag, "TXX")) sprintf(m_chbuf, "User defined text information frame: %s", value);
if(!strcmp(tag, "TYE")) sprintf(m_chbuf, "Year: %s", value);
if(!strcmp(tag, "UFI")) sprintf(m_chbuf, "Unique file identifier: %s", value);
if(!strcmp(tag, "ULT")) sprintf(m_chbuf, "Unsychronized lyric/text transcription: %s", value);
if(!strcmp(tag, "WAF")) sprintf(m_chbuf, "Official audio file webpage: %s", value);
if(!strcmp(tag, "WAR")) sprintf(m_chbuf, "Official artist/performer webpage: %s", value);
if(!strcmp(tag, "WAS")) sprintf(m_chbuf, "Official audio source webpage: %s", value);
if(!strcmp(tag, "WCM")) sprintf(m_chbuf, "Commercial information: %s", value);
if(!strcmp(tag, "WCP")) sprintf(m_chbuf, "Copyright/Legal information: %s", value);
if(!strcmp(tag, "WPB")) sprintf(m_chbuf, "Publishers official webpage: %s", value);
if(!strcmp(tag, "WXX")) sprintf(m_chbuf, "User defined URL link frame: %s", value);
// V2.3 V2.4 tags
// if(!strcmp(tag, "COMM")) sprintf(m_chbuf, "Comment: %s", value);
if(!strcmp(tag, "OWNE")) sprintf(m_chbuf, "Ownership: %s", value);
// if(!strcmp(tag, "PRIV")) sprintf(m_chbuf, "Private: %s", value);
if(!strcmp(tag, "SYLT")) sprintf(m_chbuf, "SynLyrics: %s", value);
if(!strcmp(tag, "TALB")) sprintf(m_chbuf, "Album: %s", value);
if(!strcmp(tag, "TBPM")) sprintf(m_chbuf, "BeatsPerMinute: %s", value);
if(!strcmp(tag, "TCMP")) sprintf(m_chbuf, "Compilation: %s", value);
if(!strcmp(tag, "TCOM")) sprintf(m_chbuf, "Composer: %s", value);
if(!strcmp(tag, "TCON")) sprintf(m_chbuf, "ContentType: %s", value);
if(!strcmp(tag, "TCOP")) sprintf(m_chbuf, "Copyright: %s", value);
if(!strcmp(tag, "TDAT")) sprintf(m_chbuf, "Date: %s", value);
if(!strcmp(tag, "TEXT")) sprintf(m_chbuf, "Lyricist: %s", value);
if(!strcmp(tag, "TIME")) sprintf(m_chbuf, "Time: %s", value);
if(!strcmp(tag, "TIT1")) sprintf(m_chbuf, "Grouping: %s", value);
if(!strcmp(tag, "TIT2")) sprintf(m_chbuf, "Title: %s", value);
if(!strcmp(tag, "TIT3")) sprintf(m_chbuf, "Subtitle: %s", value);
if(!strcmp(tag, "TLAN")) sprintf(m_chbuf, "Language: %s", value);
if(!strcmp(tag, "TLEN")) sprintf(m_chbuf, "Length (ms): %s", value);
if(!strcmp(tag, "TMED")) sprintf(m_chbuf, "Media: %s", value);
if(!strcmp(tag, "TOAL")) sprintf(m_chbuf, "OriginalAlbum: %s", value);
if(!strcmp(tag, "TOPE")) sprintf(m_chbuf, "OriginalArtist: %s", value);
if(!strcmp(tag, "TORY")) sprintf(m_chbuf, "OriginalReleaseYear: %s", value);
if(!strcmp(tag, "TPE1")) sprintf(m_chbuf, "Artist: %s", value);
if(!strcmp(tag, "TPE2")) sprintf(m_chbuf, "Band: %s", value);
if(!strcmp(tag, "TPE3")) sprintf(m_chbuf, "Conductor: %s", value);
if(!strcmp(tag, "TPE4")) sprintf(m_chbuf, "InterpretedBy: %s", value);
if(!strcmp(tag, "TPOS")) sprintf(m_chbuf, "PartOfSet: %s", value);
if(!strcmp(tag, "TPUB")) sprintf(m_chbuf, "Publisher: %s", value);