1
0

ID3v1.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "ID3v1.h"
  2. #include "../nu/ns_wc.h"
  3. #include <windows.h>
  4. #include "config.h"
  5. #include <strsafe.h>
  6. const wchar_t *id3v1_genres[] =
  7. {
  8. L"Blues", L"Classic Rock", L"Country", L"Dance", L"Disco", L"Funk", L"Grunge",
  9. L"Hip-Hop", L"Jazz", L"Metal", L"New Age", L"Oldies", L"Other", L"Pop", L"R&B",
  10. L"Rap", L"Reggae", L"Rock", L"Techno", L"Industrial", L"Alternative", L"Ska",
  11. L"Death Metal", L"Pranks", L"Soundtrack", L"Euro-Techno", L"Ambient", L"Trip-Hop",
  12. L"Vocal", L"Jazz+Funk", L"Fusion", L"Trance", L"Classical", L"Instrumental",
  13. L"Acid", L"House", L"Game", L"Sound Clip", L"Gospel", L"Noise", L"Alt Rock",
  14. L"Bass", L"Soul", L"Punk", L"Space", L"Meditative", L"Instrumental Pop",
  15. L"Instrumental Rock", L"Ethnic", L"Gothic", L"Darkwave", L"Techno-Industrial",
  16. L"Electronic", L"Pop-Folk", L"Eurodance", L"Dream", L"Southern Rock", L"Comedy",
  17. L"Cult", L"Gangsta Rap", L"Top 40", L"Christian Rap", L"Pop/Funk", L"Jungle",
  18. L"Native American", L"Cabaret", L"New Wave", L"Psychedelic", L"Rave", L"Showtunes",
  19. L"Trailer", L"Lo-Fi", L"Tribal", L"Acid Punk", L"Acid Jazz", L"Polka", L"Retro",
  20. L"Musical", L"Rock & Roll", L"Hard Rock", L"Folk", L"Folk-Rock", L"National Folk",
  21. L"Swing", L"Fast-Fusion", L"Bebop", L"Latin", L"Revival", L"Celtic", L"Bluegrass",
  22. L"Avantgarde", L"Gothic Rock", L"Progressive Rock", L"Psychedelic Rock",
  23. L"Symphonic Rock", L"Slow Rock", L"Big Band", L"Chorus", L"Easy Listening",
  24. L"Acoustic", L"Humour", L"Speech", L"Chanson", L"Opera", L"Chamber Music", L"Sonata",
  25. L"Symphony", L"Booty Bass", L"Primus", L"Porn Groove", L"Satire", L"Slow Jam",
  26. L"Club", L"Tango", L"Samba", L"Folklore", L"Ballad", L"Power Ballad", L"Rhythmic Soul",
  27. L"Freestyle", L"Duet", L"Punk Rock", L"Drum Solo", L"A Cappella", L"Euro-House",
  28. L"Dance Hall", L"Goa", L"Drum & Bass", L"Club-House", L"Hardcore", L"Terror", L"Indie",
  29. L"BritPop", L"Afro-Punk", L"Polsk Punk", L"Beat", L"Christian Gangsta Rap",
  30. L"Heavy Metal", L"Black Metal", L"Crossover", L"Contemporary Christian",
  31. L"Christian Rock", L"Merengue", L"Salsa", L"Thrash Metal", L"Anime", L"JPop",
  32. L"Synthpop", L"Abstract", L"Art Rock", L"Baroque", L"Bhangra", L"Big Beat",
  33. L"Breakbeat", L"Chillout", L"Downtempo", L"Dub", L"EBM", L"Eclectic", L"Electro",
  34. L"Electroclash", L"Emo", L"Experimental", L"Garage", L"Global", L"IDM", L"Illbient",
  35. L"Industro-Goth", L"Jam Band", L"Krautrock", L"Leftfield", L"Lounge", L"Math Rock",
  36. L"New Romantic", L"Nu-Breakz", L"Post-Punk", L"Post-Rock", L"Psytrance", L"Shoegaze",
  37. L"Space Rock", L"Trop Rock", L"World Music", L"Neoclassical", L"Audiobook",
  38. L"Audio Theatre", L"Neue Deutsche Welle", L"Podcast", L"Indie Rock", L"G-Funk",
  39. L"Dubstep", L"Garage Rock", L"Psybient", L"Glam Rock", L"Dream Pop", L"Merseybeat",
  40. L"K-Pop", L"Chiptune", L"Grime", L"Grindcore", L"Indietronic", L"Indietronica",
  41. L"Jazz Rock", L"Jazz Fusion", L"Post-Punk Revival", L"Electronica", L"Psychill",
  42. L"Ethnotronic", L"Americana", L"Ambient Dub", L"Digital Dub", L"Chillwave", L"Stoner Rock",
  43. L"Slowcore", L"Softcore", L"Flamenco", L"Hi-NRG", L"Ethereal", L"Drone", L"Doom Metal",
  44. L"Doom Jazz", L"Mainstream", L"Glitch", L"Balearic", L"Modern Classical", L"Mod",
  45. L"Contemporary Classical", L"Psybreaks", L"Psystep", L"Psydub", L"Chillstep", L"Berlin School",
  46. L"Future Jazz", L"Djent", L"Musique Concrète", L"Electroacoustic", L"Folktronica", L"Texas Country", L"Red Dirt",
  47. L"Arabic", L"Asian", L"Bachata", L"Bollywood", L"Cajun", L"Calypso", L"Creole", L"Darkstep", L"Jewish", L"Reggaeton", L"Smooth Jazz",
  48. L"Soca", L"Spiritual", L"Turntablism", L"Zouk", L"Neofolk", L"Nu Jazz",
  49. };
  50. size_t numGenres = sizeof(id3v1_genres)/sizeof(*id3v1_genres);
  51. ID3v1::ID3v1()
  52. {
  53. title[0]=0;
  54. artist[0]=0;
  55. album[0]=0;
  56. comment[0]=0;
  57. year[0]=0;
  58. genre=255;
  59. track=0;
  60. hasData=false;
  61. dirty=false;
  62. title[30]=0;
  63. artist[30]=0;
  64. album[30]=0;
  65. comment[30]=0;
  66. year[4]=0;
  67. }
  68. int ID3v1::Decode(const void *data)
  69. {
  70. const char *fbuf = (const char *)data;
  71. ptrdiff_t x;
  72. hasData = false;
  73. title[0] = artist[0] = album[0] = year[0] = comment[0] = 0;
  74. genre = 255; track = 0;
  75. if (memcmp(fbuf, "TAG", 3))
  76. {
  77. return 1;
  78. }
  79. memcpy(title, fbuf + 3, 30); x = 29; while (x >= 0 && title[x] == ' ') x--; title[x + 1] = 0;
  80. memcpy(artist, fbuf + 33, 30); x = 29; while (x >= 0 && artist[x] == ' ') x--; artist[x + 1] = 0;
  81. memcpy(album, fbuf + 63, 30); x = 29; while (x >= 0 && album[x] == ' ') x--; album[x + 1] = 0;
  82. memcpy(year, fbuf + 93, 4); x = 3; while (x >= 0 && year[x] == ' ') x--; year[x + 1] = 0;
  83. memcpy(comment, fbuf + 97, 30); x = 29; while (x >= 0 && comment[x] == ' ') x--; comment[x + 1] = 0;
  84. if (fbuf[97 + 28] == 0 && fbuf[97 + 28 + 1] != 0) track = fbuf[97 + 28 + 1];
  85. genre = ((unsigned char *)fbuf)[127];
  86. hasData = 1;
  87. return 0;
  88. }
  89. int ID3v1::Encode(void *data)
  90. {
  91. if (!hasData)
  92. return 1;
  93. char *fbuf = (char *)data;
  94. size_t x;
  95. fbuf[0] = 'T';fbuf[1] = 'A';fbuf[2] = 'G';
  96. if (title) strncpy(fbuf + 3, title, 30); for (x = 3 + strlen(title); x < 33; x ++) fbuf[x] = 0;
  97. if (artist) strncpy(fbuf + 33, artist, 30); for (x = 33 + strlen(artist); x < 63; x ++) fbuf[x] = 0;
  98. if (album) strncpy(fbuf + 63, album, 30); for (x = 63 + strlen(album); x < 93; x ++) fbuf[x] = 0;
  99. if (year) strncpy(fbuf + 93, year, 4); for (x = 93 + strlen(year); x < 97; x ++) fbuf[x] = 0;
  100. if (comment) strncpy(fbuf + 97, comment, 30); for (x = 97 + strlen(comment); x < 127; x ++) fbuf[x] = 0;
  101. if (track)
  102. {
  103. fbuf[97 + 28] = 0;
  104. fbuf[97 + 28 + 1] = track;
  105. }
  106. ((unsigned char *)fbuf)[127] = genre;
  107. return 0;
  108. }
  109. #define ID3V1_CODEPAGE ((config_read_mode==READ_LOCAL)?CP_ACP:28591)
  110. int ID3v1::GetString(const char *tag, wchar_t *data, int dataLen)
  111. {
  112. if (!hasData)
  113. return 0;
  114. if (!_stricmp(tag, "title"))
  115. {
  116. MultiByteToWideCharSZ(ID3V1_CODEPAGE, 0, title, -1, data, dataLen);
  117. return 1;
  118. }
  119. else if (!_stricmp(tag, "artist"))
  120. {
  121. MultiByteToWideCharSZ(ID3V1_CODEPAGE, 0, artist, -1, data, dataLen);
  122. return 1;
  123. }
  124. else if (!_stricmp(tag, "album"))
  125. {
  126. MultiByteToWideCharSZ(ID3V1_CODEPAGE, 0, album, -1, data, dataLen);
  127. return 1;
  128. }
  129. else if (!_stricmp(tag, "comment"))
  130. {
  131. MultiByteToWideCharSZ(ID3V1_CODEPAGE, 0, comment, -1, data, dataLen);
  132. return 1;
  133. }
  134. else if (!_stricmp(tag, "year"))
  135. {
  136. MultiByteToWideCharSZ(ID3V1_CODEPAGE, 0, year, -1, data, dataLen);
  137. return 1;
  138. }
  139. else if (!_stricmp(tag, "genre"))
  140. {
  141. if (genre >= numGenres) return -1;
  142. StringCchCopyW(data, dataLen, id3v1_genres[genre]);
  143. return 1;
  144. }
  145. else if (!_stricmp(tag, "track"))
  146. {
  147. if (track == 0) return -1;
  148. StringCchPrintfW(data, dataLen, L"%u", track);
  149. return 1;
  150. }
  151. else
  152. return 0;
  153. }
  154. int ID3v1::SetString(const char *tag, const wchar_t *data)
  155. {
  156. if (!_stricmp(tag, "title"))
  157. WideCharToMultiByteSZ(ID3V1_CODEPAGE, 0, data, -1, title, 31, 0 ,0);
  158. else if (!_stricmp(tag, "artist"))
  159. WideCharToMultiByteSZ(ID3V1_CODEPAGE, 0, data, -1, artist, 31, 0 ,0);
  160. else if (!_stricmp(tag, "album"))
  161. WideCharToMultiByteSZ(ID3V1_CODEPAGE, 0, data, -1, album, 31, 0 ,0);
  162. else if (!_stricmp(tag, "comment"))
  163. WideCharToMultiByteSZ(ID3V1_CODEPAGE, 0, data, -1, comment, 31, 0 ,0);
  164. else if (!_stricmp(tag, "year"))
  165. WideCharToMultiByteSZ(ID3V1_CODEPAGE, 0, data, -1, year, 5, 0 ,0);
  166. else if (!_stricmp(tag, "genre"))
  167. {
  168. genre=255;
  169. if (data)
  170. {
  171. for (size_t i=0;i<numGenres;i++)
  172. {
  173. if (!_wcsicmp(id3v1_genres[i], data))
  174. {
  175. genre= (unsigned char)i;
  176. }
  177. }
  178. }
  179. }
  180. else if (!_stricmp(tag, "track"))
  181. {
  182. int t = _wtoi(data);
  183. if(t > 255) track = 0;
  184. else track = t;
  185. }
  186. else
  187. return 0;
  188. dirty=true;
  189. hasData = 1;
  190. return 1;
  191. }
  192. void ID3v1::Clear()
  193. {
  194. hasData=false;
  195. dirty=true;
  196. //clear data
  197. title[0]=artist[0]=album[0]=comment[0]=year[0]=0;
  198. genre = track = 0;
  199. }