1
0

ExtendedFileInfo.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. ** Copyright (C) 2007-2011 Nullsoft, Inc.
  3. **
  4. ** This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held
  5. ** liable for any damages arising from the use of this software.
  6. **
  7. ** Permission is granted to anyone to use this software for any purpose, including commercial applications, and to
  8. ** alter it and redistribute it freely, subject to the following restrictions:
  9. **
  10. ** 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  11. ** If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  12. **
  13. ** 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  14. **
  15. ** 3. This notice may not be removed or altered from any source distribution.
  16. **
  17. ** Author: Ben Allison [email protected]
  18. ** Created: March 1, 2007
  19. **
  20. */
  21. #include "main.h"
  22. #include "Metadata.h"
  23. #include "../nu/ns_wc.h"
  24. #include "../nu/AutoChar.h"
  25. #include "../Winamp/wa_ipc.h"
  26. #include <shlwapi.h>
  27. #include "resource.h"
  28. #include "../Agave/Language/api_language.h"
  29. #include "Stopper.h"
  30. #include <strsafe.h>
  31. static int FillFileInfo(wchar_t *infoStr, size_t len, FLACMetadata &metadata)
  32. {
  33. const FLAC__StreamMetadata_StreamInfo *info = metadata.GetStreamInfo();
  34. if (info)
  35. {
  36. unsigned __int64 length = info->total_samples / info->sample_rate;
  37. StringCchPrintfExW(infoStr, len, &infoStr, &len, 0, WASABI_API_LNGSTRINGW(IDS_LENGTH_IN_SECONDS), length);
  38. StringCchPrintfExW(infoStr, len, &infoStr, &len, 0, WASABI_API_LNGSTRINGW(IDS_CHANNELS), info->channels);
  39. StringCchPrintfExW(infoStr, len, &infoStr, &len, 0, WASABI_API_LNGSTRINGW(IDS_BITS_PER_SAMPLE), info->bits_per_sample);
  40. StringCchPrintfExW(infoStr, len, &infoStr, &len, 0, WASABI_API_LNGSTRINGW(IDS_SAMPLE_RATE), info->sample_rate);
  41. __int64 filesize = metadata.GetFileSize();
  42. StringCchPrintfExW(infoStr, len, &infoStr, &len, 0, WASABI_API_LNGSTRINGW(IDS_FILE_SIZE_IN_BYTES), filesize);
  43. if (info->total_samples)
  44. {
  45. StringCchPrintfExW(infoStr, len, &infoStr, &len, 0, WASABI_API_LNGSTRINGW(IDS_AVERAGE_BITRATE), filesize / (125*info->total_samples / (__int64)info->sample_rate)); // (125 is 1000/8)
  46. int percent = (int)((100*filesize) / (info->total_samples * (info->bits_per_sample/8) * info->channels));
  47. StringCchPrintfExW(infoStr, len, &infoStr, &len, 0, WASABI_API_LNGSTRINGW(IDS_COMPRESSION_RATIO), percent);
  48. }
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. bool KeywordMatch(const char *mainString, const char *keyword)
  54. {
  55. return !_stricmp(mainString, keyword);
  56. }
  57. Info *info = 0;
  58. FLACMetadata *getMetadata = 0;
  59. wchar_t *getFileInfoFn = 0;
  60. static FILETIME ftLastWriteTime;
  61. // is used to determine if the last write time of the file has changed when
  62. // asked to get the metadata for the same cached file so we can update things
  63. BOOL HasFileTimeChanged(const wchar_t *fn)
  64. {
  65. WIN32_FILE_ATTRIBUTE_DATA fileData = {0};
  66. if (GetFileAttributesExW(fn, GetFileExInfoStandard, &fileData) == TRUE)
  67. {
  68. if(CompareFileTime(&ftLastWriteTime, &fileData.ftLastWriteTime))
  69. {
  70. ftLastWriteTime = fileData.ftLastWriteTime;
  71. return TRUE;
  72. }
  73. }
  74. return FALSE;
  75. }
  76. void UpdateFileTimeChanged(const wchar_t *fn)
  77. {
  78. WIN32_FILE_ATTRIBUTE_DATA fileData;
  79. if (GetFileAttributesExW(fn, GetFileExInfoStandard, &fileData) == TRUE)
  80. {
  81. ftLastWriteTime = fileData.ftLastWriteTime;
  82. }
  83. }
  84. void ResetMetadataCache()
  85. {
  86. // cheap way to trigger a metadata reset in a thread-safe manner
  87. wchar_t d[10] = {0};
  88. extendedFileInfoStructW reset_info = {0};
  89. reset_info.filename=L".flac";
  90. reset_info.metadata=L"artist";
  91. reset_info.ret = d;
  92. reset_info.retlen=10;
  93. SendMessage(plugin.hMainWindow, WM_WA_IPC, (WPARAM)&reset_info, IPC_GET_EXTENDED_FILE_INFOW);
  94. }
  95. #define START_TAG_ALIAS(name, alias) if (KeywordMatch(data, name)) lookup=alias
  96. #define TAG_ALIAS(name, alias) else if (KeywordMatch(data, name)) lookup=alias
  97. extern "C" __declspec( dllexport ) int winampGetExtendedFileInfoW(const wchar_t *fn, const char *data, wchar_t *dest, int destlen)
  98. {
  99. if (KeywordMatch(data, "type"))
  100. {
  101. dest[0] = '0';
  102. dest[1] = 0;
  103. return 1;
  104. }
  105. if (KeywordMatch(data, "rateable"))
  106. {
  107. dest[0] = '1';
  108. dest[1] = 0;
  109. return 1;
  110. }
  111. else if (KeywordMatch(data, "lossless"))
  112. {
  113. dest[0] = '1';
  114. dest[1] = 0;
  115. return 1;
  116. }
  117. if (!fn || (fn && !fn[0]))
  118. return 0;
  119. if (KeywordMatch(data, "family"))
  120. {
  121. LPCWSTR e;
  122. int pID = -1;
  123. DWORD lcid;
  124. e = PathFindExtensionW(fn);
  125. if (L'.' != *e) return 0;
  126. e++;
  127. lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  128. if (CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, e, -1, L"FLAC", -1) ||
  129. CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, e, -1, L"FLA", -1)) pID = IDS_FAMILY_STRING;
  130. if (pID != -1 && S_OK == StringCchCopyW(dest, destlen, WASABI_API_LNGSTRINGW(pID))) return 1;
  131. return 0;
  132. }
  133. if (KeywordMatch(data, "mime"))
  134. {
  135. StringCchCopyW(dest, destlen, L"audio/flac");
  136. return 1;
  137. }
  138. if (!getMetadata || !getFileInfoFn || _wcsicmp(fn, getFileInfoFn) || HasFileTimeChanged(fn))
  139. {
  140. if (getMetadata)
  141. getMetadata->Reset();
  142. else
  143. getMetadata = new FLACMetadata;
  144. if (!getMetadata->Open(fn))
  145. {
  146. delete getMetadata;
  147. getMetadata = 0;
  148. dest[0]=0;
  149. return 0;
  150. }
  151. free(getFileInfoFn);
  152. getFileInfoFn = _wcsdup(fn);
  153. }
  154. FLACMetadata &metadata = *getMetadata;
  155. if(KeywordMatch(data, "formatinformation"))
  156. return FillFileInfo(dest,destlen,metadata);
  157. const char *lookup=0;
  158. if (KeywordMatch(data, "length"))
  159. {
  160. unsigned __int64 length_in_msec;
  161. if (metadata.GetLengthMilliseconds(&length_in_msec))
  162. StringCchPrintfW(dest, destlen, L"%d", length_in_msec);
  163. else
  164. dest[0]=0;
  165. return 1;
  166. }
  167. else if (KeywordMatch(data, "bitrate"))
  168. {
  169. // TODO: move this into FLACMetadata
  170. const FLAC__StreamMetadata_StreamInfo *streaminfo = metadata.GetStreamInfo();
  171. if (streaminfo)
  172. {
  173. if (streaminfo->total_samples == 0 || streaminfo->sample_rate == 0) // prevent divide-by-zero
  174. dest[0]=0;
  175. else
  176. StringCchPrintfW(dest, destlen, L"%I64d", metadata.GetFileSize() / (125*streaminfo->total_samples / (__int64)streaminfo->sample_rate)); // (125 is 1000/8)
  177. }
  178. else
  179. dest[0]=0;
  180. return 1;
  181. }
  182. TAG_ALIAS("title", "TITLE");
  183. TAG_ALIAS("artist", "ARTIST");
  184. TAG_ALIAS("album", "ALBUM");
  185. TAG_ALIAS("genre", "GENRE");
  186. TAG_ALIAS("comment", "COMMENT");
  187. TAG_ALIAS("year", "DATE");
  188. TAG_ALIAS("track", "TRACKNUMBER");
  189. TAG_ALIAS("albumartist", "ALBUM ARTIST");
  190. TAG_ALIAS("composer", "COMPOSER");
  191. TAG_ALIAS("disc", "DISCNUMBER");
  192. TAG_ALIAS("publisher", "PUBLISHER");
  193. TAG_ALIAS("conductor", "CONDUCTOR");
  194. TAG_ALIAS("tool", "ENCODED-BY");
  195. TAG_ALIAS("replaygain_track_gain", "REPLAYGAIN_TRACK_GAIN");
  196. TAG_ALIAS("replaygain_track_peak", "REPLAYGAIN_TRACK_PEAK");
  197. TAG_ALIAS("replaygain_album_gain", "REPLAYGAIN_ALBUM_GAIN");
  198. TAG_ALIAS("replaygain_album_peak", "REPLAYGAIN_ALBUM_PEAK");
  199. TAG_ALIAS("GracenoteFileID", "GRACENOTEFILEID");
  200. TAG_ALIAS("GracenoteExtData", "GRACENOTEEXTDATA");
  201. TAG_ALIAS("bpm", "BPM");
  202. TAG_ALIAS("remixing", "REMIXING");
  203. TAG_ALIAS("subtitle", "VERSION");
  204. TAG_ALIAS("isrc", "ISRC");
  205. TAG_ALIAS("category", "CATEGORY");
  206. TAG_ALIAS("rating", "RATING");
  207. TAG_ALIAS("producer", "PRODUCER");
  208. if (!lookup)
  209. return 0;
  210. const char *value = metadata.GetMetadata(lookup);
  211. if(KeywordMatch("comment",data)) {
  212. if(!value || !*value) value = metadata.GetMetadata("DESCRIPTION");
  213. }
  214. if(KeywordMatch("year",data)) {
  215. if(!value || !*value) value = metadata.GetMetadata("YEAR");
  216. }
  217. if(KeywordMatch("track",data)) {
  218. if(!value || !*value) value = metadata.GetMetadata("TRACK");
  219. }
  220. if(KeywordMatch("albumartist",data)) {
  221. if(!value || !*value) value = metadata.GetMetadata("ALBUMARTIST");
  222. if(!value || !*value) value = metadata.GetMetadata("ENSEMBLE");
  223. }
  224. if(KeywordMatch("publisher",data)) {
  225. if(!value || !*value) value = metadata.GetMetadata("ORGANIZATION");
  226. }
  227. if(KeywordMatch("category",data)) {
  228. if(!value || !*value) value = metadata.GetMetadata("CONTENTGROUP");
  229. if(!value || !*value) value = metadata.GetMetadata("GROUPING");
  230. }
  231. if(KeywordMatch(data, "rating")) {
  232. if(!value || !*value) value = metadata.GetMetadata("RATING");
  233. if(value && *value) {
  234. int rating = atoi(value);
  235. // keeps things limited to our range of 0-100
  236. if (rating >= 100) {
  237. rating = 5;
  238. }
  239. // 1-100 case
  240. else if (rating > 5 && rating < 100) {
  241. rating = (rating /= 20);
  242. // shift up by one rating when in next band
  243. // 1-20 = 1, 21-40 = 2, 41-60 = 3, 61-80 = 4, 81-100 = 5
  244. rating += ((atoi(value) - (rating * 20)) > 0);
  245. }
  246. else if (rating > 0 && rating <= 5) {
  247. }
  248. // otherwise just make sure and set zero
  249. else {
  250. rating = 0;
  251. }
  252. StringCchPrintfW(dest, destlen, L"%u", rating);
  253. return 1;
  254. }
  255. }
  256. MultiByteToWideCharSZ(CP_UTF8, 0, value, -1, dest, destlen);
  257. return 1;
  258. }
  259. FLACMetadata *setMetadata=0;
  260. wchar_t *setFn=0;
  261. extern "C" __declspec( dllexport ) int winampSetExtendedFileInfoW(const wchar_t *fn, const char *data, wchar_t *val)
  262. {
  263. if (!setMetadata || !setFn || lstrcmpiW(fn, setFn))
  264. {
  265. free(setFn);
  266. setFn=_wcsdup(fn);
  267. if (!setMetadata)
  268. setMetadata = new FLACMetadata;
  269. if (setMetadata->Open(setFn, true) == false)
  270. {
  271. delete setMetadata;
  272. setMetadata=0;
  273. return 0;
  274. }
  275. }
  276. const char *lookup=0;
  277. START_TAG_ALIAS("artist", "ARTIST");
  278. TAG_ALIAS("title", "TITLE");
  279. TAG_ALIAS("album", "ALBUM");
  280. TAG_ALIAS("genre", "GENRE");
  281. TAG_ALIAS("comment", "COMMENT");
  282. TAG_ALIAS("year", "DATE");
  283. TAG_ALIAS("track", "TRACKNUMBER");
  284. TAG_ALIAS("albumartist", "ALBUM ARTIST");
  285. TAG_ALIAS("composer", "COMPOSER");
  286. TAG_ALIAS("disc", "DISCNUMBER");
  287. TAG_ALIAS("publisher", "PUBLISHER");
  288. TAG_ALIAS("conductor", "CONDUCTOR");
  289. TAG_ALIAS("tool", "ENCODED-BY");
  290. TAG_ALIAS("replaygain_track_gain", "REPLAYGAIN_TRACK_GAIN");
  291. TAG_ALIAS("replaygain_track_peak", "REPLAYGAIN_TRACK_PEAK");
  292. TAG_ALIAS("replaygain_album_gain", "REPLAYGAIN_ALBUM_GAIN");
  293. TAG_ALIAS("replaygain_album_peak", "REPLAYGAIN_ALBUM_PEAK");
  294. TAG_ALIAS("GracenoteFileID", "GRACENOTEFILEID");
  295. TAG_ALIAS("GracenoteExtData", "GRACENOTEEXTDATA");
  296. TAG_ALIAS("bpm", "BPM");
  297. TAG_ALIAS("remixing", "REMIXING");
  298. TAG_ALIAS("subtitle", "VERSION");
  299. TAG_ALIAS("isrc", "ISRC");
  300. TAG_ALIAS("category", "CATEGORY");
  301. TAG_ALIAS("rating", "RATING");
  302. TAG_ALIAS("producer", "PRODUCER");
  303. if (!lookup)
  304. return 0;
  305. if (val && *val)
  306. {
  307. if(KeywordMatch("rating",data))
  308. {
  309. char temp[128] = {0};
  310. StringCchPrintfA(temp, 128, "%u", _wtoi(val)*20);
  311. setMetadata->SetMetadata(lookup, temp);
  312. }
  313. else
  314. {
  315. setMetadata->SetMetadata(lookup, AutoChar(val, CP_UTF8));
  316. }
  317. }
  318. else
  319. {
  320. setMetadata->RemoveMetadata(lookup);
  321. if(KeywordMatch("comment",data))
  322. {
  323. // need to remove this one also, or else it's gonna look like delete doesn't work
  324. // if the file was tagged using this alternate field
  325. setMetadata->RemoveMetadata("DESCRIPTION");
  326. }
  327. else if(KeywordMatch("year",data))
  328. {
  329. // need to remove this one also, or else it's gonna look like delete doesn't work
  330. // if the file was tagged using this alternate field
  331. setMetadata->RemoveMetadata("YEAR");
  332. }
  333. else if(KeywordMatch("track",data))
  334. {
  335. // need to remove this one also, or else it's gonna look like delete doesn't work
  336. // if the file was tagged using this alternate field
  337. setMetadata->RemoveMetadata("TRACK");
  338. }
  339. else if(KeywordMatch("albumartist",data))
  340. {
  341. // need to remove these two, also, or else it's gonna look like delete doesn't work
  342. // if the file was tagged using these alternate fields
  343. setMetadata->RemoveMetadata("ALBUMARTIST");
  344. setMetadata->RemoveMetadata("ENSEMBLE");
  345. }
  346. else if(KeywordMatch("publisher",data))
  347. {
  348. // need to remove this one also, or else it's gonna look like delete doesn't work
  349. // if the file was tagged using this alternate field
  350. setMetadata->RemoveMetadata("ORGANIZATION");
  351. }
  352. else if(KeywordMatch("category",data))
  353. {
  354. // need to remove these two also, or else it's gonna look like delete doesn't work
  355. // if the file was tagged using these alternate fields
  356. setMetadata->RemoveMetadata("CONTENTGROUP");
  357. setMetadata->RemoveMetadata("GROUPING");
  358. }
  359. }
  360. return 1;
  361. }
  362. extern "C" __declspec(dllexport) int winampWriteExtendedFileInfo()
  363. {
  364. if (setFn && setMetadata)
  365. {
  366. Stopper stopper;
  367. if (lastfn && !_wcsicmp(lastfn, setFn))
  368. stopper.Stop();
  369. bool success = setMetadata->Save(setFn);
  370. stopper.Play();
  371. setMetadata->Reset();
  372. free(setFn);
  373. setFn=0;
  374. delete getMetadata;
  375. getMetadata=0;
  376. // update last modified so we're not re-queried on our own updates
  377. UpdateFileTimeChanged(setFn);
  378. return !!success;
  379. }
  380. return 1;
  381. }
  382. extern "C" __declspec(dllexport) const wchar_t *winampWriteExtendedGetLastError()
  383. {
  384. return 0;
  385. }