11#pragma once
22
3- #include " AudioTools/AudioCodecs/HeaderParserMP3.h"
43#include " AudioTools/AudioCodecs/HeaderParserAAC.h"
4+ #include " AudioTools/AudioCodecs/HeaderParserMP3.h"
55#include " AudioTools/CoreAudio/AudioBasic/StrView.h"
66
77namespace audio_tools {
@@ -23,11 +23,12 @@ namespace audio_tools {
2323class MimeDetector {
2424 public:
2525 MimeDetector () {
26- setCheck (" audio/mpeg" , checkMP3Ext);
27- setCheck (" audio/aac" , checkAACExt);
2826 setCheck (" audio/vnd.wave" , checkWAV);
2927 setCheck (" audio/ogg" , checkOGG);
3028 setCheck (" video/MP2T" , checkMP2T);
29+ setCheck (" audio/prs.sid" , checkSID);
30+ setCheck (" audio/mpeg" , checkMP3Ext);
31+ setCheck (" audio/aac" , checkAACExt);
3132 }
3233
3334 bool begin () {
@@ -37,6 +38,7 @@ class MimeDetector {
3738
3839 // / write the header to determine the mime
3940 size_t write (uint8_t * data, size_t len) {
41+ actual_mime = default_mime;
4042 determineMime (data, len);
4143 return len;
4244 }
@@ -63,25 +65,29 @@ class MimeDetector {
6365
6466 // / Provides the actual mime type, that was determined from the first
6567 // / available data
66- const char * mime () { return actual_mime; }
68+ const char * mime () {
69+ return actual_mime;
70+ }
6771
6872 static bool checkAAC (uint8_t * start, size_t len) {
6973 return start[0 ] == 0xFF &&
7074 (start[1 ] == 0xF0 || start[1 ] == 0xF1 || start[1 ] == 0xF9 );
7175 }
7276
7377 static bool checkAACExt (uint8_t * start, size_t len) {
74- // quick check
75- if (!(start[0 ] == 0xFF &&
76- (start[1 ] == 0xF0 || start[1 ] == 0xF1 || start[1 ] == 0xF9 )))
77- return false ;
78- HeaderParserMP3 mp3;
78+ // checking logic for files
79+ if (memcmp (start+4 , " ftypM4A" , 7 ) == 0 ) {
80+ return true ;
81+ }
82+ // check for streaming
83+ HeaderParserAAC aac;
7984 // it should start with a synch word
80- if (mp3.findSyncWord ((const uint8_t *)start, len) != 0 ) {
85+ int pos = aac.findSyncWord ((const uint8_t *)start, len);
86+ if (pos == -1 ) {
8187 return false ;
8288 }
8389 // make sure that it is not an mp3
84- if (mp3 .isValid (start, len)) {
90+ if (aac .isValid (start+pos , len-pos )) {
8591 return false ;
8692 }
8793 return true ;
@@ -98,7 +104,7 @@ class MimeDetector {
98104 }
99105
100106 static bool checkWAV (uint8_t * start, size_t len) {
101- return memcmp (start, " OggS " , 4 ) == 0 ;
107+ return memcmp (start, " RIFF " , 4 ) == 0 ;
102108 }
103109
104110 static bool checkOGG (uint8_t * start, size_t len) {
@@ -107,12 +113,19 @@ class MimeDetector {
107113
108114 // / MPEG-2 TS Byte Stream Format
109115 static bool checkMP2T (uint8_t * start, size_t len) {
110- if (len < 189 )
111- return start[0 ] == 0x47 ;
116+ if (len < 189 ) return start[0 ] == 0x47 ;
112117
113118 return start[0 ] == 0x47 && start[188 ] == 0x47 ;
114119 }
115120
121+ // / Commodore 64 SID File
122+ static bool checkSID (uint8_t * start, size_t len) {
123+ return memcmp (start, " PSID" , 4 ) == 0 || memcmp (start, " RSID" , 4 ) == 0 ;
124+ }
125+
126+ // / Provides the default mime type if no mime could be determined
127+ void setDefaultMime (const char * mime) { default_mime = mime; }
128+
116129 protected:
117130 struct Check {
118131 const char * mime = nullptr ;
@@ -126,6 +139,7 @@ class MimeDetector {
126139 Vector<Check> checks{0 };
127140 bool is_first = false ;
128141 const char * actual_mime = nullptr ;
142+ const char * default_mime = nullptr ;
129143 void (*notifyMimeCallback)(const char * mime) = nullptr ;
130144
131145 // / Update the mime type
@@ -147,7 +161,7 @@ class MimeDetector {
147161 return l_check.mime ;
148162 }
149163 }
150- return nullptr ;
164+ return default_mime ;
151165 }
152166};
153167
0 commit comments