autofill.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. ** This is adapted from the autofill code in ml_ipod. It was originally written by and is
  3. ** Copyright (C) Will Fisher and Justin Frankel. It remains under the following licence:
  4. **
  5. ** This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held
  6. ** liable for any damages arising from the use of this software.
  7. **
  8. ** Permission is granted to anyone to use this software for any purpose, including commercial applications, and to
  9. ** alter it and redistribute it freely, subject to the following restrictions:
  10. **
  11. ** 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  12. ** If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  13. **
  14. ** 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  15. **
  16. ** 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <windows.h>
  19. #include <time.h>
  20. #include "../../General/gen_ml/ml.h"
  21. #include "pmp.h"
  22. #include "../../General/gen_ml/itemlist.h"
  23. #include "DeviceView.h"
  24. #include "mt19937ar.h"
  25. #include "config.h"
  26. #include "../nu/AutoChar.h"
  27. #include "metadata_utils.h"
  28. #include "api__ml_pmp.h"
  29. extern winampMediaLibraryPlugin plugin;
  30. extern C_ItemList * getSelectedItems(bool all=false);
  31. typedef struct {
  32. wchar_t * albumName;
  33. int rating;
  34. float ratingf;
  35. int lastplayed;
  36. int size;
  37. bool album;
  38. itemRecordW * ice;
  39. } AutoFillItem;
  40. static void randomizeList(void *list, int elems, int elemsize) {
  41. if (elems < 2) return;
  42. if (elemsize < 1) return;
  43. char *plist=(char*)list;
  44. int p;
  45. char *tmp=(char*)calloc(elemsize, sizeof(char));
  46. if (!tmp) return;
  47. for (p = 0; p < elems; p ++) {
  48. int np=genrand_int31()%elems;
  49. if (p != np) {
  50. np*=elemsize;
  51. int pp=p*elemsize;
  52. memcpy(tmp,plist+pp,elemsize);
  53. memcpy(plist+pp,plist+np,elemsize);
  54. memcpy(plist+np,tmp,elemsize);
  55. }
  56. }
  57. free(tmp);
  58. }
  59. #define atoi_NULLOK(s) ((s)?atoi(s):0)
  60. #define STRCMP_NULLOK(a,b) _wcsicmp(a?a:L"",b?b:L"")
  61. static int sortFunc_icealbums(const void *elem1, const void *elem2) {
  62. itemRecordW * a = (itemRecordW *)elem1;
  63. itemRecordW * b = (itemRecordW *)elem2;
  64. return STRCMP_NULLOK(a->album,b->album);
  65. }
  66. static void autoFill_songfilter(Device * dev, C_ItemList * list, itemRecordListW * results) {
  67. if (results) {
  68. for(int i=0; i < results->Size; i++) {
  69. AutoFillItem * a = (AutoFillItem *)calloc(sizeof(AutoFillItem),1);
  70. a->rating = results->Items[i].rating;
  71. a->lastplayed = (int)results->Items[i].lastplay;
  72. a->size = (int)dev->getTrackSizeOnDevice(&results->Items[i]);
  73. a->album = false;
  74. a->ice = &results->Items[i];
  75. a->albumName=L"arse";
  76. list->Add(a);
  77. }
  78. }
  79. }
  80. static void autoFill_songfilter(Device * dev, C_ItemList * list, C_ItemList * results) {
  81. for(int i=0; i < results->GetSize(); i++) {
  82. itemRecordW *r = (itemRecordW*)results->Get(i);
  83. AutoFillItem * a = (AutoFillItem *)calloc(sizeof(AutoFillItem),1);
  84. a->rating = r->rating;
  85. a->lastplayed = (int)r->lastplay;
  86. a->size = (int)dev->getTrackSizeOnDevice(r);
  87. a->album = false;
  88. a->ice = r;
  89. a->albumName=L"arse";
  90. list->Add(a);
  91. }
  92. }
  93. static void autoFill_albumfilter(Device * dev, C_ItemList * list, itemRecordListW * results, C_ItemList * res = NULL) {
  94. if (results)
  95. qsort(results->Items,results->Size,sizeof(itemRecordW),sortFunc_icealbums);
  96. AutoFillItem * curalbum = NULL;
  97. wchar_t * albumName=NULL;
  98. int albumSize=0;
  99. __int64 lastplayacc=0;
  100. int size = res?res->GetSize():(results?results->Size:0);
  101. int i;
  102. for(i=0; i < size; i++) {
  103. itemRecordW * item = res?(itemRecordW*)res->Get(i):&results->Items[i];
  104. if(!curalbum || STRCMP_NULLOK(item->album,albumName) != 0) {
  105. if(curalbum && albumSize) {
  106. curalbum->lastplayed = (int)(lastplayacc / (__int64)albumSize);
  107. curalbum->ratingf /= (float)albumSize;
  108. list->Add(curalbum);
  109. }
  110. AutoFillItem * a = (AutoFillItem *)calloc(sizeof(AutoFillItem),1);
  111. a->ratingf = (float)item->rating;
  112. lastplayacc = item->lastplay;
  113. a->size = (int)dev->getTrackSizeOnDevice(item);
  114. a->album = true;
  115. a->albumName = item->album;
  116. curalbum = a;
  117. albumName = a->albumName;
  118. albumSize++;
  119. } else {
  120. albumSize++;
  121. curalbum->size += (int)dev->getTrackSizeOnDevice(item);
  122. curalbum->ratingf += (float)item->rating;
  123. lastplayacc += item->lastplay;
  124. }
  125. }
  126. if(curalbum && albumSize) {
  127. curalbum->lastplayed = (int)(lastplayacc / (__int64)albumSize);
  128. curalbum->ratingf /= (float)albumSize;
  129. list->Add(curalbum);
  130. }
  131. //FUCKO: think of something clever to make the ratings relevant
  132. for(i=0; i < list->GetSize(); i++) {
  133. AutoFillItem * a = (AutoFillItem *)list->Get(i);
  134. a->rating = (int)(a->ratingf + 0.5);
  135. if(a->rating > 5) a->rating = 5;
  136. }
  137. }
  138. // FUCKO lame O(n^2) gayness. Make this better.
  139. static void autoFill_addAlbums(C_ItemList * albums, itemRecordListW * songs, C_ItemList * dest) {
  140. if (songs) {
  141. for(int i=0; i < songs->Size; i++)
  142. for(int j=0; j < albums->GetSize(); j++)
  143. if(STRCMP_NULLOK(songs->Items[i].album,(wchar_t *)albums->Get(j))==0)
  144. dest->Add(&songs->Items[i]);
  145. }
  146. }
  147. static int sortFunc_autofill(const void *elem1, const void *elem2)
  148. {
  149. AutoFillItem *a=(AutoFillItem *)*(void **)elem1;
  150. AutoFillItem *b=(AutoFillItem *)*(void **)elem2;
  151. return a->lastplayed - b->lastplayed;
  152. }
  153. itemRecordListW * generateAutoFillList(DeviceView * dev, C_Config * config)
  154. {
  155. // Settings for an autofill
  156. int lastAutofill=config->ReadInt(L"LastAutoFill",(int)time(NULL));
  157. int fillpc = config->ReadInt(L"FillPercent",90);; // fill device 90% full.
  158. bool byAlbum = dev->dev->getDeviceCapacityTotal() > (__int64)1500000000;
  159. byAlbum = config->ReadInt(L"AlbumAutoFill",byAlbum?1:0)==1;
  160. wchar_t * afquery = _wcsdup(config->ReadString(L"AutoFillQuery",L"length > 30"));
  161. wchar_t * squery = _wcsdup(config->ReadString(L"SyncQuery",L"type = 0"));
  162. char * tmp2 = AutoCharDup(config->ReadString(L"AutoFillRatings",L"")); // ratings ratio string (eg "3:1:1:1:0:0")
  163. int len = 2*strlen(tmp2)+2;
  164. char * tmp = (char*)calloc(len,sizeof(char));
  165. strncpy(tmp,tmp2,len); free(tmp2);
  166. int ratingsRatios[6]={0,0,0,0,0,0,};
  167. bool useratings=true;
  168. if(tmp[0]==0) useratings=false;
  169. int i=0;
  170. int ratingsRatiosTotal=0;
  171. if(useratings) {
  172. int len = strlen(tmp);
  173. while(tmp[++i]!=0) if(tmp[i]==':') tmp[i]=0;
  174. tmp[i+1]=0;
  175. char * p = &tmp[0];
  176. for(i=0; i<6; i++) {
  177. if(p > len + tmp) break;
  178. if(*p == 0) break;
  179. ratingsRatios[i] = atoi_NULLOK(p);
  180. ratingsRatiosTotal+=ratingsRatios[i];
  181. p+=strlen(p)+1;
  182. }
  183. }
  184. if(ratingsRatiosTotal==0) {
  185. ratingsRatiosTotal=6;
  186. for(i=0; i<6; i++) ratingsRatios[i]=1;
  187. }
  188. //construct query
  189. wchar_t query[2048]=L"";
  190. if(afquery[0]==0) wsprintf(query,L"%s",squery);
  191. else wsprintf(query,L"(%s) AND (%s)",squery,afquery);
  192. //run query
  193. itemRecordListW *items = 0;
  194. itemRecordListW *results = AGAVE_API_MLDB?AGAVE_API_MLDB->Query(query):0;
  195. if (results)
  196. {
  197. C_ItemList * songList = new C_ItemList;
  198. if(!byAlbum) autoFill_songfilter(dev->dev,songList, results);
  199. else autoFill_albumfilter(dev->dev,songList, results);
  200. //dump into ratings "bins"
  201. C_ItemList * songs[7];
  202. for(i=0; i<7; i++) songs[i] = new C_ItemList;
  203. for(i=0; i<songList->GetSize(); i++) {
  204. if(useratings) {
  205. int rating = ((AutoFillItem*)songList->Get(i))->rating;
  206. switch (rating) {
  207. case 1: songs[1]->Add(songList->Get(i)); break;
  208. case 2: songs[2]->Add(songList->Get(i)); break;
  209. case 3: songs[3]->Add(songList->Get(i)); break;
  210. case 4: songs[4]->Add(songList->Get(i)); break;
  211. case 5: songs[5]->Add(songList->Get(i)); break;
  212. default: songs[0]->Add(songList->Get(i)); break;
  213. }
  214. } else songs[0]->Add(songList->Get(i));
  215. }
  216. for(i=0; i<6; i++)
  217. {
  218. randomizeList(songs[i]->GetAll(),songs[i]->GetSize(),sizeof(void*)); // randomize
  219. qsort(songs[i]->GetAll(),songs[i]->GetSize(),sizeof(void*),sortFunc_autofill); // sort by date
  220. }
  221. __int64 sizeToFill, totalSize=0;
  222. sizeToFill = (((__int64)fillpc) * dev->dev->getDeviceCapacityTotal()) / ((__int64)100);
  223. //sizeToFill = dev->dev->getDeviceCapacityTotal();
  224. C_ItemList * alreadyIn = new C_ItemList;
  225. C_ItemList * notGoingIn = new C_ItemList;
  226. C_ItemList * songsToSend = new C_ItemList;
  227. if(!byAlbum) {
  228. int numTracks = dev->dev->getPlaylistLength(0);
  229. /* Hmm. This should make autofill just replace selected songs
  230. ** gonna leave it out for now, until i have good ipod shuffle (et al) support.
  231. C_ItemList * selected = getSelectedItems(false);
  232. int j=0;
  233. if(selected) {
  234. if(selected->GetSize() > 0 && selected->GetSize() < numTracks) {
  235. for(i=0; i<numTracks; i++) {
  236. if(j < selected->GetSize()) if((songid_t)selected->Get(j) == dev->dev->getPlaylistTrack(0,i)) {
  237. j++;
  238. notGoingIn->Add(selected->Get(j));
  239. } else alreadyIn->Add(dev->dev->getPlaylistTrack(0,i));
  240. }
  241. }
  242. delete selected;
  243. }
  244. */
  245. // otherwise replace ones played since last autofill, unless none played, in which case replace all
  246. if(notGoingIn->GetSize() == 0) {
  247. delete alreadyIn;
  248. alreadyIn = new C_ItemList;
  249. if(lastAutofill > 0)
  250. {
  251. for(i=0; i<numTracks; ++i)
  252. if(dev->dev->getTrackLastPlayed(dev->dev->getPlaylistTrack(0,i)) >= lastAutofill) break;
  253. if (i >= numTracks) // this means nothing has been played, so in this case we set everything to be replaced
  254. {
  255. for(i=0; i<numTracks; ++i) notGoingIn->Add((void*)dev->dev->getPlaylistTrack(0,i));
  256. }
  257. else
  258. {
  259. for(i=0; i < numTracks; ++i)
  260. {
  261. songid_t s = dev->dev->getPlaylistTrack(0,i);
  262. if(dev->dev->getTrackLastPlayed(s) <= lastAutofill) alreadyIn->Add((void*)s);
  263. }
  264. }
  265. }
  266. }
  267. //remove our already selected songs from the bins
  268. for(i=0; i<notGoingIn->GetSize(); ++i) {
  269. songid_t s = (songid_t)notGoingIn->Get(i);
  270. for (int b=0; b < 6; b ++)
  271. {
  272. for(int j=0; j<songs[b]->GetSize(); ++j)
  273. {
  274. if(!compareItemRecordAndSongId(((AutoFillItem *)songs[b]->Get(j))->ice,s, dev->dev))
  275. { // FUCKO
  276. songs[6]->Add(songs[b]->Get(j));
  277. songs[b]->Del(j);
  278. b=6;
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. for(i=0; i<alreadyIn->GetSize(); ++i) {
  285. songid_t s = (songid_t)alreadyIn->Get(i);
  286. for (int b= 0; b < 6; b ++)
  287. {
  288. for(int j=0; j<songs[b]->GetSize(); ++j)
  289. {
  290. if(!compareItemRecordAndSongId(((AutoFillItem *)songs[b]->Get(j))->ice,s, dev->dev))
  291. {
  292. songsToSend->Add(((AutoFillItem *)songs[b]->Get(j))->ice);
  293. songs[b]->Del(j);
  294. b=6;
  295. break;
  296. }
  297. }
  298. }
  299. __int64 size = (__int64)dev->dev->getTrackSize(s);
  300. if(totalSize + size >= sizeToFill) break;
  301. totalSize += size;
  302. }
  303. } // end if(!byAlbum)
  304. //select the rest!
  305. C_ItemList * albumsToSend = new C_ItemList;
  306. while(true) {
  307. int bin=0;
  308. if (useratings)
  309. {
  310. int val = genrand_int31() % ratingsRatiosTotal;
  311. bin=-1;
  312. while(val>=0 && bin < 5) val -= ratingsRatios[++bin];
  313. if (bin > 5) bin=5;
  314. else if(bin<0) bin=0;
  315. bin = 5-bin; // work in reverse mapped bin now (since ratingsRatio is 5-0)
  316. int sbin=bin;
  317. while(bin<6&&songs[bin]->GetSize()<=0) { bin++; } // if our bin is empty, go to a higher bin
  318. if (bin > 5) // out of higher rated bins, go lower
  319. {
  320. bin=sbin-1; // start at one lower than where we started
  321. while (bin >= 0 && songs[bin]->GetSize()<=0) bin--; // if our bin is still empty, go to a lower bin
  322. }
  323. if (bin < 0) bin = 6;
  324. } else bin = songs[0]->GetSize()?0:6;
  325. if(!songs[bin])
  326. break;
  327. int bsize=songs[bin]->GetSize();
  328. if (bsize<1)
  329. break;
  330. // JF> will had a %(bsize/2) here effectively, which I think is too major.
  331. // I propose a nice simple weighted thing, where stuff near the beginning is more likely
  332. // to be picked.
  333. int snum = genrand_int31() % bsize;
  334. if (genrand_int31()&1) snum/=2; // half the time, just use the first half (less recently played items)
  335. int size = ((AutoFillItem *)songs[bin]->Get(snum))->size;
  336. if(size <= 0)
  337. {
  338. songs[bin]->Del(snum);
  339. continue;
  340. }
  341. totalSize += (__int64)size;
  342. /*{
  343. wchar_t buf[100] = {0};
  344. wsprintf(buf,L"not full. must fill: %d, so far: %d, this track: %d",(int)sizeToFill,(int)totalSize,(int)size);
  345. OutputDebugString(buf);
  346. }*/
  347. if(totalSize >= sizeToFill)
  348. {
  349. /*OutputDebugString(L"full");*/
  350. break;
  351. }
  352. if(byAlbum) {
  353. albumsToSend->Add(((AutoFillItem *)songs[bin]->Get(snum))->albumName);
  354. } else {
  355. songsToSend->Add(((AutoFillItem *)songs[bin]->Get(snum))->ice);
  356. }
  357. songs[bin]->Del(snum);
  358. }
  359. // dump our albums into songsToSend
  360. if(albumsToSend->GetSize() > 0)
  361. autoFill_addAlbums(albumsToSend, results, songsToSend);
  362. items = new itemRecordListW;
  363. items->Alloc = songsToSend->GetSize();
  364. items->Size = songsToSend->GetSize();
  365. items->Items = (itemRecordW*)calloc(items->Alloc, sizeof(itemRecordW));
  366. for(i=0; i<songsToSend->GetSize(); ++i) copyRecord(&items->Items[i],(itemRecordW*)songsToSend->Get(i));
  367. // clear stuff up
  368. for(i=0; i < songList->GetSize(); i++) free(songList->Get(i));
  369. for(i=0; i<7; ++i) delete songs[i];
  370. delete songsToSend;
  371. delete albumsToSend;
  372. delete songList;
  373. delete notGoingIn;
  374. delete alreadyIn;
  375. AGAVE_API_MLDB->FreeRecordList(results); //free memory
  376. }
  377. free(afquery);
  378. free(squery);
  379. free(tmp);
  380. return items;
  381. }