1
0

id3.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. #include "../id3v2/id3_tag.h"
  2. #include "id3.h"
  3. #include "config.h"
  4. #include "../nu/ns_wc.h"
  5. #include <strsafe.h>
  6. #define _isdigit(x) (( x ) >= '0' && ( x ) <= '9')
  7. /* id3 helper functions */
  8. void SetFrameEncoding(ID3_Frame *frame, int encoding)
  9. {
  10. switch (encoding)
  11. {
  12. case ENCODING_AUTO:
  13. if (config_write_mode == WRITE_UTF16)
  14. frame->Field(ID3FN_TEXTENC).Set(ID3TE_UNICODE);
  15. else
  16. frame->Field(ID3FN_TEXTENC).Set(ID3TE_ASCII);
  17. break;
  18. case ENCODING_FORCE_ASCII:
  19. frame->Field(ID3FN_TEXTENC).Set(ID3TE_ASCII);
  20. break;
  21. case ENCODING_FORCE_UNICODE:
  22. frame->Field(ID3FN_TEXTENC).Set(ID3TE_UNICODE);
  23. break;
  24. }
  25. }
  26. char *ID3_GetString(ID3_Frame *frame, ID3_FieldID fldName, size_t nIndex)
  27. {
  28. char *text = NULL;
  29. if (NULL != frame)
  30. {
  31. size_t nText = frame->Field(fldName).Size();
  32. text = (char *)calloc(nText + 1, sizeof(char));
  33. frame->Field(fldName).GetLocal(text, nText + 1, nIndex);
  34. }
  35. return text;
  36. }
  37. wchar_t *ID3_GetUnicodeString(ID3_Frame *frame, ID3_FieldID fldName, size_t nIndex)
  38. {
  39. wchar_t *text = NULL;
  40. if (NULL != frame)
  41. {
  42. size_t nText = frame->Field(fldName).Size();
  43. text = (wchar_t *)calloc(sizeof(wchar_t) * (nText + 1), sizeof(wchar_t));
  44. frame->Field(fldName).GetUnicode(text, nText + 1, nIndex);
  45. }
  46. return text;
  47. }
  48. wchar_t *ID3_FillUnicodeString(ID3_Frame *frame, ID3_FieldID fldName, wchar_t *dest, size_t destlen, size_t nIndex)
  49. {
  50. memset(dest, 0, destlen * sizeof(wchar_t));
  51. if (NULL != frame)
  52. {
  53. frame->Field(fldName).GetUnicode(dest, destlen, nIndex);
  54. return dest;
  55. }
  56. else
  57. return NULL;
  58. }
  59. wchar_t *ID3_GetTitle(ID3_Tag *tag)
  60. {
  61. wchar_t*sTitle = NULL;
  62. if (NULL == tag)
  63. {
  64. return sTitle;
  65. }
  66. ID3_Frame *frame = tag->Find(ID3FID_TITLE);
  67. if (frame != NULL)
  68. {
  69. sTitle = ID3_GetUnicodeString(frame, ID3FN_TEXT);
  70. }
  71. return sTitle;
  72. }
  73. wchar_t *ID3_GetArtist(ID3_Tag *tag)
  74. {
  75. if (!tag) return 0;
  76. wchar_t *sArtist = NULL;
  77. ID3_Frame *frame = NULL;
  78. if ((frame = tag->Find(ID3FID_LEADARTIST)) || (frame = tag->Find(ID3FID_BAND)))
  79. {
  80. sArtist = ID3_GetUnicodeString(frame, ID3FN_TEXT);
  81. }
  82. return sArtist;
  83. }
  84. wchar_t *ID3_GetAlbum(ID3_Tag *tag)
  85. {
  86. wchar_t *sAlbum = NULL;
  87. if (NULL == tag)
  88. {
  89. return sAlbum;
  90. }
  91. ID3_Frame *frame = tag->Find(ID3FID_ALBUM);
  92. if (frame != NULL)
  93. {
  94. sAlbum = ID3_GetUnicodeString(frame, ID3FN_TEXT);
  95. }
  96. return sAlbum;
  97. }
  98. wchar_t *ID3_GetYear(ID3_Tag *tag)
  99. {
  100. wchar_t *sYear = NULL;
  101. if (NULL == tag)
  102. {
  103. return sYear;
  104. }
  105. ID3_Frame *frame = tag->Find(ID3FID_RECORDINGTIME);
  106. if (frame != NULL)
  107. sYear = ID3_GetUnicodeString(frame, ID3FN_TEXT);
  108. if (!sYear || !*sYear)
  109. {
  110. frame = tag->Find(ID3FID_YEAR);
  111. if (frame != NULL)
  112. sYear = ID3_GetUnicodeString(frame, ID3FN_TEXT);
  113. }
  114. return sYear;
  115. }
  116. void ID3_AddSetComment(ID3_Tag *tag, const wchar_t *comment)
  117. {
  118. ID3_Frame *frame = tag->Find(ID3FID_COMMENT, ID3FN_DESCRIPTION, L"");
  119. if (frame)
  120. {
  121. if (!comment || !comment[0])
  122. tag->RemoveFrame(frame);
  123. else
  124. {
  125. SetFrameEncoding(frame);
  126. frame->Field(ID3FN_TEXT).SetUnicode(comment);
  127. unsigned char null3[3] = {0, 0, 0};
  128. frame->Field(ID3FN_LANGUAGE).Get(null3, 3);
  129. if (!null3[0]) frame->Field(ID3FN_LANGUAGE).SetLatin("eng");
  130. }
  131. }
  132. else if (comment && comment[0])
  133. {
  134. frame = new ID3_Frame(ID3FID_COMMENT);
  135. SetFrameEncoding(frame);
  136. frame->Field(ID3FN_LANGUAGE).SetLatin("eng");
  137. //frame->Field(ID3FN_LANGUAGE).Set(null3, 3);
  138. frame->Field(ID3FN_DESCRIPTION).SetUnicode(L"");
  139. frame->Field(ID3FN_TEXT).SetUnicode(comment);
  140. tag->AddFrame(frame, TRUE);
  141. }
  142. }
  143. void ID3_AddSetRating(ID3_Tag *tag, const wchar_t *rating)
  144. {
  145. luint rating_integer = 0;
  146. if (rating)
  147. rating_integer = _wtoi(rating);
  148. bool custom_frame = false, own_frame = false;
  149. ID3_Frame* frame = NULL;
  150. if (config_rating_email[0])
  151. {
  152. frame = tag->Find(ID3FID_POPULARIMETER, ID3FN_EMAIL, config_rating_email);
  153. if (!frame) custom_frame = true;
  154. }
  155. if (!frame)
  156. {
  157. frame = tag->Find(ID3FID_POPULARIMETER, ID3FN_EMAIL, "[email protected]\0");
  158. if (frame) own_frame = true;
  159. }
  160. if (!frame)
  161. {
  162. frame = tag->Find(ID3FID_POPULARIMETER);
  163. if (frame) own_frame = true;
  164. }
  165. // try to use a custom field if our own was present and the custom wasn't
  166. if (custom_frame && own_frame)
  167. {
  168. frame->Clear();
  169. frame = NULL;
  170. }
  171. if (!frame)
  172. {
  173. frame = new ID3_Frame(ID3FID_POPULARIMETER);
  174. if (!config_rating_email[0])
  175. frame->Field(ID3FN_EMAIL).Set((uchar *)"[email protected]\0", 18);
  176. else
  177. {
  178. frame->Field(ID3FN_EMAIL).Set((uchar *)config_rating_email, strlen(config_rating_email)+1);
  179. }
  180. tag->AddFrame(frame, TRUE);
  181. }
  182. if (frame)
  183. {
  184. switch(rating_integer)
  185. {
  186. case 2:
  187. rating_integer=64;
  188. break;
  189. case 3:
  190. rating_integer=128;
  191. break;
  192. case 4:
  193. rating_integer=196;
  194. break;
  195. case 5:
  196. rating_integer = 255;
  197. break;
  198. }
  199. if (!rating_integer)
  200. tag->RemoveFrame(frame);
  201. else
  202. frame->Field(ID3FN_RATING).Set(rating_integer);
  203. }
  204. }
  205. wchar_t *ID3_GetComment(ID3_Tag *tag, wchar_t *dest, size_t destlen)
  206. {
  207. wchar_t *comment = NULL;
  208. if (NULL == tag)
  209. {
  210. return comment;
  211. }
  212. ID3_Frame* frame = tag->Find(ID3FID_COMMENT, ID3FN_DESCRIPTION, L"");
  213. if (frame)
  214. {
  215. comment = ID3_FillUnicodeString(frame, ID3FN_TEXT, dest, destlen);
  216. }
  217. return comment;
  218. }
  219. wchar_t *ID3_GetRating(ID3_Tag *tag, wchar_t *dest, size_t destlen)
  220. {
  221. if (NULL == tag)
  222. {
  223. return NULL;
  224. }
  225. ID3_Frame* frame = NULL;
  226. if (config_rating_email[0])
  227. frame = tag->Find(ID3FID_POPULARIMETER, ID3FN_EMAIL, config_rating_email);
  228. if (!frame)
  229. frame = tag->Find(ID3FID_POPULARIMETER, ID3FN_EMAIL, "[email protected]\0");
  230. if (!frame)
  231. frame = tag->Find(ID3FID_POPULARIMETER);
  232. if (frame)
  233. {
  234. int rating = (int)frame->Field(ID3FN_RATING).Get();
  235. if (rating >= 224 && rating <= 255)
  236. rating = 5;
  237. else if (rating >= 160 && rating <= 223)
  238. rating = 4;
  239. else if (rating >= 96 && rating <= 159)
  240. rating = 3;
  241. else if (rating >= 32 && rating <= 95)
  242. rating = 2;
  243. else if (rating >= 1 && rating <= 31)
  244. rating = 1;
  245. else
  246. rating = 0;
  247. StringCchPrintfW(dest, destlen, L"%u", rating);
  248. return dest;
  249. }
  250. return 0;
  251. }
  252. wchar_t *ID3_GetComment(ID3_Tag *tag, const wchar_t *desc, wchar_t *dest, size_t destlen)
  253. {
  254. wchar_t *comment = NULL;
  255. if (NULL == tag)
  256. {
  257. return comment;
  258. }
  259. ID3_Frame* frame = tag->Find(ID3FID_COMMENT, ID3FN_DESCRIPTION, desc);
  260. if (frame)
  261. {
  262. comment = ID3_FillUnicodeString(frame, ID3FN_TEXT, dest, destlen);
  263. }
  264. return comment;
  265. }
  266. wchar_t *ID3_GetMusicbrainzRecordingID(ID3_Tag *tag, wchar_t *dest, size_t destlen)
  267. {
  268. if (NULL == tag)
  269. {
  270. return 0;
  271. }
  272. ID3_Frame* frame = tag->Find(ID3FID_UNIQUEFILEID, ID3FN_OWNER, L"http://musicbrainz.org");
  273. if (frame)
  274. {
  275. uchar data[64] = {0};
  276. luint dataSize = frame->Field(ID3FN_DATA).Size();
  277. frame->Field(ID3FN_DATA).Get(data, 64);
  278. int converted = MultiByteToWideCharSZ(CP_ACP, 0, (const char *)data, (int)dataSize, dest, (int)destlen);
  279. dest[converted]=0;
  280. return dest;
  281. }
  282. return 0;
  283. }
  284. wchar_t *ID3_GetGracenoteTagID(ID3_Tag *tag)
  285. {
  286. if (NULL == tag)
  287. {
  288. return 0;
  289. }
  290. ID3_Frame* frame = tag->Find(ID3FID_UNIQUEFILEID, ID3FN_OWNER, L"http://www.cddb.com/id3/taginfo1.html");
  291. if (frame)
  292. {
  293. uchar data[64] = {0};
  294. luint dataSize = frame->Field(ID3FN_DATA).Size();
  295. frame->Field(ID3FN_DATA).Get(data, 64);
  296. int converted = MultiByteToWideChar(CP_ACP, 0, (const char *)data, (int)dataSize, 0, 0);
  297. wchar_t *dest = (wchar_t *)calloc((converted+1), sizeof(wchar_t));
  298. converted = MultiByteToWideChar(CP_ACP, 0, (const char *)data, (int)dataSize, dest, converted);
  299. dest[converted]=0;
  300. return dest;
  301. }
  302. return 0;
  303. }
  304. wchar_t *ID3_GetGracenoteTagID(ID3_Tag *tag, wchar_t *dest, size_t destlen)
  305. {
  306. if (NULL == tag)
  307. {
  308. return 0;
  309. }
  310. ID3_Frame* frame = tag->Find(ID3FID_UNIQUEFILEID, ID3FN_OWNER, L"http://www.cddb.com/id3/taginfo1.html");
  311. if (frame)
  312. {
  313. uchar data[64] = {0};
  314. luint dataSize = frame->Field(ID3FN_DATA).Size();
  315. frame->Field(ID3FN_DATA).Get(data, 64);
  316. int converted = MultiByteToWideCharSZ(CP_ACP, 0, (const char *)data, (int)dataSize, dest, (int)destlen);
  317. dest[converted]=0;
  318. return dest;
  319. }
  320. return 0;
  321. }
  322. void ID3_AddSetGracenoteTagID(ID3_Tag *tag, const wchar_t *tagID)
  323. {
  324. ID3_Frame *frame = tag->Find(ID3FID_UNIQUEFILEID, ID3FN_OWNER, L"http://www.cddb.com/id3/taginfo1.html");
  325. if (frame)
  326. {
  327. if (!tagID || !tagID[0])
  328. tag->RemoveFrame(frame);
  329. else
  330. {
  331. size_t origLen = wcslen(tagID); // so we can not write the null terminator
  332. uchar data[64] = {0};
  333. luint dataSize = WideCharToMultiByte(CP_ACP, 0, tagID, (int)origLen, (char *)data, 64, 0, 0);
  334. frame->Field(ID3FN_DATA).Set(data, dataSize);
  335. }
  336. }
  337. else if (tagID && tagID[0])
  338. {
  339. frame = new ID3_Frame(ID3FID_UNIQUEFILEID);
  340. SetFrameEncoding(frame, ENCODING_FORCE_ASCII);
  341. frame->Field(ID3FN_OWNER).SetLatin("http://www.cddb.com/id3/taginfo1.html");
  342. size_t origLen = wcslen(tagID); // so we can not write the null terminator
  343. uchar data[64] = {0};
  344. luint dataSize = WideCharToMultiByte(CP_ACP, 0, tagID, (int)origLen, (char *)data, 64, 0, 0);
  345. frame->Field(ID3FN_DATA).Set(data, dataSize);
  346. tag->AddFrame(frame, TRUE);
  347. }
  348. }
  349. #if 0 // benski> CUT
  350. char *ID3_GetTUID(ID3_Tag *tag)
  351. {
  352. char *tuid = NULL;
  353. if (NULL == tag)
  354. {
  355. return tuid;
  356. }
  357. ID3_Frame* frame = NULL;
  358. frame = tag->Find(ID3FID_UNIQUEFILEID);
  359. if (frame)
  360. {
  361. char *tmp = ID3_GetString(frame, ID3FN_DATA);
  362. if (tmp)
  363. {
  364. // verify first four characters are '3CD3'
  365. if (!strncmp(tmp, "3CD3", 4))
  366. {
  367. char m, n;
  368. char *p = tmp + 4;
  369. n = *p++;
  370. m = 'P' - n;
  371. p += m;
  372. n = *p++;
  373. m = 'Z' - n; // length of TUID;
  374. tuid = _strdup(p);
  375. tuid[m] = 0; // null terminate
  376. }
  377. free(tmp);
  378. }
  379. }
  380. return tuid;
  381. }
  382. #endif
  383. char *ID3_GetGenre(ID3_Tag *tag)
  384. {
  385. char *sGenre = NULL;
  386. if (NULL == tag)
  387. {
  388. return sGenre;
  389. }
  390. ID3_Frame *frame = tag->Find(ID3FID_CONTENTTYPE);
  391. if (frame != NULL)
  392. {
  393. sGenre = ID3_GetString(frame, ID3FN_TEXT);
  394. }
  395. return sGenre;
  396. }
  397. void ID3_AddUserText(ID3_Tag *tag, wchar_t *desc, const wchar_t *value, int encoding)
  398. {
  399. ID3_Frame *frame = tag->Find(ID3FID_USERTEXT, ID3FN_DESCRIPTION, desc);
  400. if (frame)
  401. {
  402. if (!value || !value[0])
  403. tag->RemoveFrame(frame);
  404. else
  405. {
  406. SetFrameEncoding(frame, encoding);
  407. frame->Field(ID3FN_TEXT).SetUnicode(value);
  408. }
  409. }
  410. else if (value && value[0])
  411. {
  412. frame = new ID3_Frame(ID3FID_USERTEXT);
  413. SetFrameEncoding(frame, encoding);
  414. frame->Field(ID3FN_DESCRIPTION).SetUnicode(desc);
  415. frame->Field(ID3FN_TEXT).SetUnicode(value);
  416. tag->AddFrame(frame, TRUE);
  417. }
  418. }
  419. wchar_t *ID3_GetUserText(ID3_Tag *tag, wchar_t *desc)
  420. {
  421. if (tag == NULL)
  422. return NULL;
  423. ID3_Frame *frame = tag->Find(ID3FID_USERTEXT, ID3FN_DESCRIPTION, desc);
  424. if (frame)
  425. return ID3_GetUnicodeString(frame, ID3FN_TEXT);
  426. else
  427. return 0;
  428. }
  429. wchar_t *ID3_GetUserText(ID3_Tag *tag, wchar_t *desc, wchar_t *dest, size_t destlen)
  430. {
  431. if (tag == NULL)
  432. return NULL;
  433. ID3_Frame *frame = tag->Find(ID3FID_USERTEXT, ID3FN_DESCRIPTION, desc);
  434. if (frame)
  435. return ID3_FillUnicodeString(frame, ID3FN_TEXT, dest, destlen);
  436. else
  437. return 0;
  438. }
  439. wchar_t *ID3_GetTagText(ID3_Tag *tag, ID3_FrameID f)
  440. {
  441. wchar_t *sComposer = NULL;
  442. if (NULL == tag)
  443. {
  444. return sComposer;
  445. }
  446. ID3_Frame *frame = tag->Find(f);
  447. if (frame != NULL)
  448. {
  449. sComposer = ID3_GetUnicodeString(frame, ID3FN_TEXT);
  450. }
  451. return sComposer;
  452. }
  453. wchar_t *ID3_GetTagText(ID3_Tag *tag, ID3_FrameID f, wchar_t *dest, size_t destlen)
  454. {
  455. wchar_t *sComposer = NULL;
  456. if (NULL == tag)
  457. {
  458. return sComposer;
  459. }
  460. ID3_Frame *frame = tag->Find(f);
  461. if (frame != NULL)
  462. {
  463. sComposer = ID3_FillUnicodeString(frame, ID3FN_TEXT, dest, destlen);
  464. }
  465. return sComposer;
  466. }
  467. wchar_t *ID3_GetTagUrl(ID3_Tag *tag, ID3_FrameID f, wchar_t *dest, size_t destlen)
  468. {
  469. wchar_t *sComposer = NULL;
  470. if (NULL == tag)
  471. {
  472. return sComposer;
  473. }
  474. ID3_Frame *frame = tag->Find(f);
  475. if (frame != NULL)
  476. {
  477. sComposer = ID3_FillUnicodeString(frame, ID3FN_URL, dest, destlen);
  478. }
  479. return sComposer;
  480. }
  481. #if 0
  482. char *ID3_GetGenreDisplayable(ID3_Tag *tag)
  483. {
  484. char *sGenre = ID3_GetGenre(tag);
  485. if (!sGenre) return NULL;
  486. while (sGenre && *sGenre == ' ') sGenre++;
  487. if (sGenre[0] == '(' || _isdigit(sGenre[0]))
  488. {
  489. int isparam = !_isdigit(sGenre[0]);
  490. char *pCur = &sGenre[isparam];
  491. int cnt = 0;
  492. while (_isdigit(*pCur))
  493. {
  494. cnt++;
  495. pCur++;
  496. }
  497. while (pCur && *pCur == ' ') pCur++;
  498. if (cnt > 0 && (isparam && *pCur == ')') || (!isparam && !*pCur))
  499. {
  500. // if the genre number is greater than 255, its invalid.
  501. size_t ulGenre = atoi(&sGenre[isparam]);
  502. if (ulGenre >= 0 && ulGenre < numberOfGenres)
  503. {
  504. char *tmp = (char*)malloc(strlen(genres[ulGenre]) + 1);
  505. if (tmp)
  506. {
  507. memcpy(tmp, genres[ulGenre], strlen(genres[ulGenre]) + 1);
  508. free(sGenre);
  509. sGenre = tmp;
  510. }
  511. }
  512. }
  513. }
  514. return sGenre;
  515. }
  516. #endif