1
0

In.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. #include "Main.h"
  2. #include "resource.h"
  3. #include <math.h>
  4. #include "api.h"
  5. //#define PROFILE_PLUGINS_LOAD_TIME
  6. #include "timing.h"
  7. #include "../nu/AutoWide.h"
  8. #include "../nu/AutoChar.h"
  9. #include "../nu/ns_wc.h"
  10. #include "WinampAttributes.h"
  11. #include "eq10dsp.h"
  12. #include "../nsutil/pcm.h"
  13. int filter_srate = 0, filter_enabled = 1, filter_top = 0, filter_top2 = 10;
  14. static In_Module* dsp_init_mod = 0;
  15. static int dsp_in_init = 0;
  16. std::vector<In_Module*> in_modules;
  17. In_Module* in_mod = 0;
  18. float preamp_val = 1.0f;
  19. eq10_t* eq = 0;
  20. extern "C" volatile int sa_override;
  21. static void setinfo(int bitrate, int srate, int stereo, int synched);
  22. static void vissa_init(int maxlatency_in_ms, int srate);
  23. static void vissa_deinit();
  24. static int sa_getmode();
  25. static int eq_dosamples_4front(short* samples, int numsamples, int bps, int nch, int srate);
  26. static int eq_dosamples(short* samples, int numsamples, int bps, int nch, int srate);
  27. int benskiQ_eq_dosamples(short* samples, int numsamples, int bps, int nch, int srate);
  28. static int eq_isactive();
  29. static int myisourfile(const char* filename) // mjf bullshit
  30. {
  31. return 0;
  32. }
  33. typedef struct _PLUGINORDER
  34. {
  35. LPCWSTR name;
  36. bool found;
  37. } PLUGINORDER;
  38. static PLUGINORDER preload[] =
  39. {
  40. // the one plug-in to rule all
  41. { L"in_mp3.dll", false },
  42. // no extension configuration
  43. { L"in_avi.dll", false },
  44. { L"in_cdda.dll", false },
  45. { L"in_linein.dll", false },
  46. { L"in_mkv.dll", false },
  47. { L"in_nsv.dll", false },
  48. { L"in_swf.dll", false },
  49. { L"in_vorbis.dll", false },
  50. // extension configuration
  51. { L"in_mp4.dll", false },
  52. { L"in_dshow.dll", false }, // load after in_avi and in_mkv as often those extensions are associated with this one as well
  53. { L"in_flac.dll", false },
  54. { L"in_flv.dll", false },
  55. { L"in_wm.dll", false },
  56. // tend to have a lot of extensions so do later
  57. { L"in_wave.dll", false },
  58. { L"in_midi.dll", false },
  59. { L"in_mod.dll", false }
  60. };
  61. static void LoadInputPlugin(const wchar_t* filename)
  62. {
  63. wchar_t file[MAX_PATH] = { 0 };
  64. PathCombineW(file, PLUGINDIR, filename);
  65. HINSTANCE hLib = LoadLibraryW(file);
  66. if ( hLib == NULL )
  67. return;
  68. In_Module* (*pr)();
  69. pr = (In_Module * (__cdecl*)(void)) GetProcAddress(hLib, "winampGetInModule2");
  70. if ( pr == NULL )
  71. return;
  72. In_Module* mod = pr();
  73. if ( mod == NULL )
  74. {
  75. FreeModule(hLib);
  76. return;
  77. }
  78. int ver = ((mod->version & ~IN_UNICODE) & ~IN_INIT_RET);
  79. if ( !(ver == IN_VER || ver == IN_VER_OLD) )
  80. {
  81. FreeModule(hLib);
  82. return;
  83. }
  84. if ( g_safeMode )
  85. {
  86. char desc[128] = { 0 };
  87. lstrcpynA(desc, mod->description, sizeof(desc));
  88. if ( desc[0] && !memcmp(desc, "nullsoft(", 9) )
  89. {
  90. char* p = strrchr(desc, ')');
  91. if ( p )
  92. {
  93. *p = 0;
  94. if ( _wcsicmp(filename, AutoWide(desc + 9)) )
  95. {
  96. FreeModule(hLib);
  97. return;
  98. }
  99. }
  100. }
  101. else
  102. {
  103. FreeModule(hLib);
  104. return;
  105. }
  106. }
  107. mod->hDllInstance = hLib;
  108. mod->dsp_dosamples = eq_dosamples;
  109. mod->dsp_isactive = eq_isactive;
  110. mod->SAGetMode = sa_getmode;
  111. mod->SAAdd = (int(__cdecl*)(void*, int, int))sa_add;
  112. mod->SAVSAInit = vissa_init;
  113. mod->SAVSADeInit = vissa_deinit;
  114. mod->VSASetInfo = vis_setinfo;
  115. mod->VSAAdd = vsa_add;
  116. mod->VSAGetMode = vsa_getmode;
  117. mod->SAAddPCMData = sa_addpcmdata;
  118. mod->VSAAddPCMData = vsa_addpcmdata;
  119. mod->hMainWindow = hMainWindow;
  120. mod->SetInfo = NULL;
  121. mod->UsesOutputPlug &= ~(2 | 4 | 8 | 16);
  122. if ( !_wcsicmp(filename, L"in_mjf.dll") )
  123. {
  124. mod->IsOurFile = myisourfile;
  125. }
  126. // add 5.66+ to do better error handling on issues, etc
  127. if ( mod->version & IN_INIT_RET )
  128. {
  129. mod->service = WASABI_API_SVC;
  130. }
  131. int ret = mod->Init();
  132. if ( (mod->version & IN_INIT_RET) && (ret == IN_INIT_FAILURE) )
  133. {
  134. FreeModule(hLib);
  135. return;
  136. }
  137. in_modules.push_back(mod);
  138. if ( mod->SetInfo )
  139. {
  140. char* p = (char*)mod->SetInfo;
  141. if ( p && *p )
  142. {
  143. StringCchCatA(metric_plugin_list, 512, ":");
  144. StringCchCatA(metric_plugin_list, 512, p);
  145. }
  146. }
  147. mod->SetInfo = setinfo;
  148. if ( !g_has_video_plugin )
  149. {
  150. int (*gefiW)(const wchar_t* fn, const char* data, wchar_t* dest, int destlen);
  151. gefiW = (int(__cdecl*)(const wchar_t*, const char*, wchar_t*, int))GetProcAddress(hLib, "winampGetExtendedFileInfoW");
  152. if ( gefiW )
  153. {
  154. wchar_t dest[16] = { 0 };
  155. gefiW(L"", "type", dest, 16);
  156. if ( _wtoi(dest) == 1 ) g_has_video_plugin = 1;
  157. }
  158. int (*gefi)(const char* fn, const char* data, char* dest, int destlen);
  159. gefi = (int(__cdecl*)(const char*, const char*, char*, int))GetProcAddress(hLib, "winampGetExtendedFileInfo");
  160. if ( gefi )
  161. {
  162. char dest[16] = { 0 };
  163. gefi("", "type", dest, 16);
  164. if ( atoi(dest) == 1 ) g_has_video_plugin = 1;
  165. }
  166. }
  167. }
  168. int in_init()
  169. {
  170. // input plugins require the main window, at least for IPC calls
  171. if ( !CreateMainWindow() )
  172. return FALSE;
  173. int i = 0, count = sizeof(preload) / sizeof(PLUGINORDER);
  174. for ( ; i < count; i++ )
  175. LoadInputPlugin(preload[i].name);
  176. WIN32_FIND_DATAW d = { 0 };
  177. wchar_t dirstr[MAX_PATH] = { 0 };
  178. PathCombineW(dirstr, PLUGINDIR, L"IN_*.DLL");
  179. HANDLE h = FindFirstFileW(dirstr, &d);
  180. if ( h != INVALID_HANDLE_VALUE )
  181. {
  182. do
  183. {
  184. for ( i = 0; i < count && (preload[i].found || lstrcmpiW(preload[i].name, d.cFileName)); i++ );
  185. if ( i == count ) LoadInputPlugin(d.cFileName);
  186. else preload[i].found = true;
  187. } while ( FindNextFileW(h, &d) );
  188. FindClose(h);
  189. }
  190. if ( (g_no_video_loaded = _r_i("no_video", 0)) ) g_has_video_plugin = 0;
  191. return TRUE;
  192. }
  193. In_Module* g_in_infobox = 0;
  194. void in_deinit()
  195. {
  196. size_t x = in_modules.size();
  197. while ( x-- )
  198. {
  199. In_Module*& mod = in_modules[x];
  200. // make sure there's something valid due to the dynamic unload changes in 5.5+
  201. if ( mod != g_in_infobox && mod )
  202. {
  203. //HINSTANCE hLib = mod->hDllInstance;
  204. mod->Quit();
  205. //FreeLibrary(hLib); // benski> we're just going to let it leak because it might be subclassing
  206. mod = 0;
  207. }
  208. }
  209. in_modules.clear();
  210. }
  211. In_Module* in_setmod_noplay(const wchar_t* filename, int* start_offs)
  212. {
  213. size_t x;
  214. char ext[128] = { 0 };
  215. extension_ex(AutoChar(filename), ext, sizeof(ext));
  216. for ( x = start_offs ? *start_offs : 0; x < in_modules.size(); x++ )
  217. {
  218. if ( InW_IsOurFile(in_modules[x], filename) )
  219. {
  220. if ( start_offs )
  221. *start_offs = (int)x;
  222. if ( !in_modules[x]->hMainWindow )
  223. in_modules[x]->hMainWindow = hMainWindow;
  224. return in_modules[x];
  225. }
  226. }
  227. for ( x = start_offs ? *start_offs : 0; x < in_modules.size(); x++ )
  228. {
  229. if ( in_modules[x] )
  230. {
  231. char* allExtension = in_modules[x]->FileExtensions;
  232. while ( allExtension && *allExtension )
  233. {
  234. char* splitterBuffer = allExtension;
  235. char* extensionBuffer;
  236. do
  237. {
  238. // string split on ';'
  239. char currentExtension[20] = { 0 };
  240. lstrcpynA(currentExtension, splitterBuffer, 20);
  241. if ( (extensionBuffer = strstr(splitterBuffer, ";")) )
  242. {
  243. if ( (extensionBuffer - splitterBuffer) < 15 )
  244. currentExtension[extensionBuffer - splitterBuffer] = 0;
  245. }
  246. //else
  247. //d[lstrlen(splitterBuffer)] = 0;
  248. if ( !lstrcmpiA(ext, currentExtension) )
  249. {
  250. if ( start_offs )
  251. *start_offs = (int)x;
  252. if ( !in_modules[x]->hMainWindow )
  253. in_modules[x]->hMainWindow = hMainWindow;
  254. return in_modules[x];
  255. }
  256. splitterBuffer = extensionBuffer + 1;
  257. } while ( extensionBuffer );
  258. allExtension += lstrlenA(allExtension) + 1;
  259. if ( !*allExtension )
  260. break;
  261. allExtension += lstrlenA(allExtension) + 1;
  262. }
  263. }
  264. }
  265. if ( start_offs )
  266. {
  267. *start_offs = -1;
  268. return 0;
  269. }
  270. {
  271. static int r;
  272. const wchar_t* p;
  273. if ( PathFindExtensionW(filename)[0] && (p = wcsstr(filename, L"://")) && (p = wcsstr(p, L"?")) )
  274. {
  275. wchar_t* d = _wcsdup(filename);
  276. In_Module* v;
  277. d[p - filename] = 0;
  278. v = in_setmod_noplay(d, 0);
  279. free(d);
  280. return v;
  281. }
  282. if ( !config_defext[0] || config_defext[0] == ' ' )
  283. StringCchCopyA(config_defext, 32, "mp3");
  284. if ( !r )
  285. {
  286. wchar_t a[128] = L"hi.";
  287. In_Module* v;
  288. MultiByteToWideCharSZ(CP_ACP, 0, config_defext, -1, a + 3, 120);
  289. r = 1;
  290. v = in_setmod_noplay(a, 0);
  291. r = 0;
  292. return v;
  293. }
  294. else
  295. return 0;
  296. }
  297. }
  298. In_Module* in_setmod(const wchar_t* filename)
  299. {
  300. In_Module* i = in_setmod_noplay(filename, 0);
  301. if ( !i ) return 0;
  302. if ( i->UsesOutputPlug & IN_MODULE_FLAG_USES_OUTPUT_PLUGIN )
  303. {
  304. int t;
  305. for ( t = 0; out_modules[t] && _stricmp(config_outname, (char*)out_modules[t]->id); t++ );
  306. if ( !out_modules[t] )
  307. {
  308. for ( t = 0; out_modules[t]; t++ )
  309. {
  310. if ( !_stricmp("out_ds.dll", (char*)out_modules[t]->id) )
  311. {
  312. break;
  313. }
  314. else if ( !_stricmp("out_wave.dll", (char*)out_modules[t]->id) )
  315. {
  316. break;
  317. }
  318. }
  319. if ( !out_modules[t] )
  320. {
  321. LPMessageBox(DIALOG_PARENT(hMainWindow), IDS_NOOUTPUT, IDS_ERROR, MB_OK | MB_ICONERROR);
  322. return (In_Module*)-31337;
  323. }
  324. }
  325. // TODO only call out_changed(..) if we are different from before
  326. // though currently changing the output prefs and playing a
  327. // new track will generate a change (which is expected but
  328. // it might be assumed to be wrong so may need to document)
  329. int changed = 0;
  330. if ( !i->outMod || i->outMod && i->outMod->hDllInstance != out_modules[t]->hDllInstance )
  331. {
  332. changed = 1;
  333. if ( i->outMod ) out_changed(i->outMod->hDllInstance, OUT_UNSET | OUT_PLAYBACK);
  334. }
  335. i->outMod = out_modules[t];
  336. i->outMod->hMainWindow = hMainWindow;
  337. if ( changed ) out_changed(i->outMod->hDllInstance, OUT_SET | OUT_PLAYBACK);
  338. }
  339. else
  340. {
  341. if ( i->outMod ) out_changed(i->outMod->hDllInstance, OUT_UNSET | OUT_PLAYBACK);
  342. i->outMod = NULL;
  343. }
  344. return i;
  345. }
  346. void in_flush(int ms)
  347. {
  348. if ( in_mod && in_mod->outMod )
  349. {
  350. in_mod->outMod->Flush(ms);
  351. }
  352. }
  353. int in_getouttime(void)
  354. {
  355. if ( in_mod ) return in_mod->GetOutputTime();
  356. return 0;
  357. }
  358. int in_getlength(void)
  359. {
  360. if ( in_mod )
  361. {
  362. int t = in_mod->GetLength() / 1000;
  363. if ( t < 1 && t != -1 )
  364. t = 1;
  365. return t;
  366. }
  367. return -1;
  368. }
  369. void in_pause(int p)
  370. {
  371. if ( in_mod )
  372. {
  373. if ( p )
  374. in_mod->Pause();
  375. else
  376. in_mod->UnPause();
  377. }
  378. }
  379. void in_setvol(int v)
  380. {
  381. if ( in_mod )
  382. in_mod->SetVolume(v);
  383. if ( config_eq_ws && config_eq_open )
  384. draw_eq_tbar(GetForegroundWindow() == hEQWindow ? 1 : (config_hilite ? 0 : 1));
  385. PostMessageW(hMainWindow, WM_WA_IPC, IPC_CB_MISC_VOLUME, IPC_CB_MISC);
  386. //////CefRefPtr<wa_Cef_App> l_CefApp = wa_Cef_App::GetInstance();
  387. //////if ( l_CefApp.get() != nullptr )
  388. //////{
  389. ////// l_CefApp.get()->setVolume( v );
  390. //////}
  391. }
  392. void in_setpan(int p)
  393. {
  394. if ( in_mod )
  395. in_mod->SetPan(p);
  396. if ( config_eq_ws && config_eq_open )
  397. draw_eq_tbar(GetForegroundWindow() == hEQWindow ? 1 : (config_hilite ? 0 : 1));
  398. PostMessageW(hMainWindow, WM_WA_IPC, IPC_CB_MISC_VOLUME, IPC_CB_MISC);
  399. }
  400. int in_seek(int time_in_ms)
  401. {
  402. if ( in_mod )
  403. in_mod->SetOutputTime(time_in_ms);
  404. return 0;
  405. }
  406. extern "C" int g_need_trusted_dsp = 0;
  407. int in_open(const wchar_t* fn)
  408. {
  409. in_mod = in_setmod(fn);
  410. if ( (int)in_mod <= 0 ) return (!in_mod ? 1 : *(int*)in_mod);
  411. sa_setthread(config_sa);
  412. in_setvol(config_volume);
  413. in_setpan(config_pan);
  414. dsp_in_init = 0;
  415. dsp_init_mod = in_mod;
  416. filter_srate = 0;
  417. int r = InW_Play(in_mod, fn);
  418. return r;
  419. }
  420. void in_close(void)
  421. {
  422. if ( in_mod )
  423. in_mod->Stop();
  424. if ( NULL != eq )
  425. {
  426. free(eq);
  427. eq = NULL;
  428. }
  429. timingPrint();
  430. }
  431. enum FileInfoMode
  432. {
  433. FILEINFO_MODE_0,
  434. FILEINFO_MODE_1,
  435. };
  436. int ModernInfoBox(In_Module* in, FileInfoMode mode, const wchar_t* filename, HWND parent);
  437. typedef int(__cdecl* UseUnifiedFileInfoDlg)(const wchar_t* fn);
  438. int in_infobox(HWND hwnd, const wchar_t* fn)
  439. {
  440. const wchar_t* p = wcsstr(fn, L"aol.com/uvox");
  441. if ( p ) return 0;
  442. if ( g_in_infobox ) return 0;
  443. In_Module* mod = in_setmod_noplay(fn, 0);
  444. if ( !mod ) return 0;
  445. g_in_infobox = mod;
  446. UseUnifiedFileInfoDlg uufid = (UseUnifiedFileInfoDlg)GetProcAddress(mod->hDllInstance, "winampUseUnifiedFileInfoDlg");
  447. // focus often gets reverted back to the main window after this dialog
  448. // so we will remember what window used to have focus
  449. HWND oldFocus = GetFocus();
  450. int a = 0;
  451. int ret = 0;
  452. if ( uufid ) {
  453. ret = uufid(fn);
  454. if ( ret == 1 ) {
  455. a = ModernInfoBox(mod, FILEINFO_MODE_0, fn, hwnd);
  456. }
  457. else if ( ret == 2 )
  458. a = ModernInfoBox(mod, FILEINFO_MODE_1, fn, hwnd);
  459. }
  460. if ( ret == 0 ) {
  461. // added 5.66 so plug-ins can get a hint that something may change...
  462. SendMessageW(hMainWindow, WM_WA_IPC, (WPARAM)fn, IPC_FILE_TAG_MAY_UPDATEW);
  463. a = InW_InfoBox(mod, fn, hwnd);
  464. }
  465. SetFocus(oldFocus);
  466. if ( !a )
  467. {
  468. SendMessageW(hMainWindow, WM_WA_IPC, (WPARAM)fn, IPC_FILE_TAG_MAY_HAVE_UPDATEDW);
  469. }
  470. g_in_infobox = 0;
  471. return a;
  472. }
  473. static void AddFilterString(char* p, size_t size, const char* a)
  474. {
  475. while ( a && *a )
  476. {
  477. char* t = 0;
  478. StringCchCatExA(p, size, ";*.", &t, &size, 0);
  479. t = p + lstrlenA(p);
  480. while ( a && *a && *a != ';' ) *t++ = *a++;
  481. *t = 0;
  482. if ( a ) a++;
  483. if ( a && a[-1] ) continue;
  484. if ( !*a ) break;
  485. if ( a ) a += lstrlenA(a) + 1;
  486. }
  487. }
  488. char* in_getfltstr()
  489. {
  490. int in_mod = -1;
  491. int in_wave = -1;
  492. size_t size = 256 * 1024;
  493. char* buf = (char*)GlobalAlloc(GPTR, size); // this is gay, should have a growing buffer or somethin.
  494. char* p = buf;
  495. size_t x;
  496. getString(IDS_ALLTYPES, p, size);
  497. p = p + lstrlenA(p) + 1;
  498. wchar_t playlistString[1024] = { 0 };
  499. playlistManager->GetFilterList(playlistString, 1024);
  500. WideCharToMultiByteSZ(CP_ACP, 0, playlistString, -1, p, (int)size, 0, 0);
  501. for ( x = 0; x < in_modules.size(); x++ )
  502. {
  503. char* a = in_modules[x]->FileExtensions;
  504. if ( a && *a )
  505. {
  506. /* we want to skip in_mod and in_wave because they have TOO MANY extensions and we are limited to MAX_PATH (260)
  507. we'll tack them at the end just in case we have enough room for 'em */
  508. if ( in_mod < 0 && !strncmp(a, "mod;", 4) )
  509. in_mod = (int)x;
  510. else if ( in_wave < 0 && strstr(a, "aiff") ) // detection for in_wave. not the best but should work
  511. in_wave = (int)x;
  512. else AddFilterString(p, size, a);
  513. }
  514. }
  515. /* add in_wave and in_mod last */
  516. if ( in_wave >= 0 )
  517. AddFilterString(p, size, in_modules[in_wave]->FileExtensions);
  518. if ( in_mod >= 0 ) // fuck you in_mod :)
  519. AddFilterString(p, size, in_modules[in_mod]->FileExtensions);
  520. p = p + lstrlenA(p) + 1;
  521. getString(IDS_PLAYLISTSTRING, p, size - (p - buf));
  522. p += lstrlenA(p) + 1;
  523. playlistManager->GetFilterList(playlistString, 1024);
  524. WideCharToMultiByteSZ(CP_ACP, 0, playlistString, -1, p, (int)size, 0, 0);
  525. p += lstrlenA(p) + 1;
  526. for ( x = 0; x < in_modules.size(); x++ )
  527. {
  528. char* a = in_modules[x]->FileExtensions;
  529. while ( a && *a )
  530. {
  531. char* b = a;
  532. a += lstrlenA(a) + 1;
  533. if ( !*a ) break;
  534. StringCchCopyExA(p, size, a, &p, &size, 0);
  535. p++;
  536. {
  537. do
  538. {
  539. char* c;
  540. StringCchCopyA(p, size, "*.");
  541. StringCchCatA(p, size, b);
  542. if ( (b = strstr(b, ";")) ) b++;
  543. if ( (c = strstr(p, ";")) ) c[1] = 0;
  544. p += lstrlenA(p);
  545. } while ( b );
  546. p++;
  547. a += lstrlenA(a) + 1;
  548. }
  549. }
  550. }
  551. StringCchCopyExA(p, size, getString(IDS_OFD_ALL_FILES, NULL, 0), &p, &size, 0);
  552. p++; size--;
  553. lstrcpynA(p, "*.*", (int)size);
  554. p += 3;
  555. *p = 0;
  556. {
  557. char* newbuf;
  558. size = p + 5 - buf;
  559. newbuf = (char*)GlobalAlloc(GPTR, size);
  560. memcpy(newbuf, buf, size);
  561. GlobalFree(buf);
  562. return newbuf;
  563. }
  564. }
  565. static void AddFilterStringW(wchar_t* p, size_t& size, const char* a, BOOL skip)
  566. {
  567. while ( a && *a )
  568. {
  569. wchar_t* t = 0;
  570. StringCchCatExW(p, size, ((*p) ? L";*." : L"*."), &t, &size, 0);
  571. size_t extsize = 0;
  572. while ( a[extsize] && a[extsize] != ';' )
  573. extsize++;
  574. *(t += MultiByteToWideCharSZ(CP_ACP, 0, a, (int)extsize, t, (int)size) - 1);
  575. a += extsize + 1;
  576. if ( a[-1] ) continue;
  577. if ( !*a ) break;
  578. a += lstrlenA(a) + 1;
  579. // limit the length of the filter to fit into MAX_PATH otherwise
  580. // if things go past this then mainly for the all supported type
  581. // it can sometimes act like *.* due to where the filter is cut
  582. if ( !skip && lstrlenW(p) >= MAX_PATH )
  583. {
  584. // if we end with a . then need to fake things to act like a new
  585. // filter since windows will interpret the . as a *.* which is bad
  586. if ( *(p + MAX_PATH) == L'.' )
  587. {
  588. *(p + MAX_PATH - 1) = 0;
  589. *(p + MAX_PATH) = 0;
  590. }
  591. }
  592. }
  593. }
  594. static wchar_t* inc(wchar_t* p, size_t& size, int extra = 0)
  595. {
  596. int len = lstrlenW(p) + extra;
  597. size -= len;
  598. p += len;
  599. return p;
  600. }
  601. wchar_t* in_getfltstrW(BOOL skip)
  602. {
  603. int in_mod = -1;
  604. int in_wave = -1;
  605. size_t size = 256 * 1024;
  606. wchar_t* buf = (wchar_t*)GlobalAlloc(GPTR, size * sizeof(wchar_t)); // this is gay, should have a growing buffer or somethin.
  607. wchar_t* p = buf, * ps;
  608. *p = 0;
  609. size_t x = 0;
  610. {
  611. int cnt = lstrlenW(getStringW(IDS_ALLTYPES, buf, size)) + 1;
  612. p += cnt; size -= cnt;
  613. if ( playlistManager )
  614. {
  615. playlistManager->GetExtensionList(p, size);
  616. size -= lstrlenW(p);
  617. }
  618. }
  619. ps = p;
  620. for ( x = 0; x < in_modules.size(); x++ )
  621. {
  622. char* a = in_modules[x]->FileExtensions;
  623. if ( a && *a )
  624. {
  625. /* we want to skip in_mod and in_wave because they have TOO MANY extensions and we are limited to MAX_PATH (260)
  626. we'll tack them at the end just in case we have enough room for 'em */
  627. if ( in_mod < 0 && !strncmp(a, "mod;", 4) )
  628. in_mod = (int)x;
  629. else if ( in_wave < 0 && strstr(a, "aiff") ) // detection for in_wave. not the best but should work
  630. in_wave = (int)x;
  631. else AddFilterStringW(p, size, a, skip);
  632. }
  633. }
  634. /* add in_wave and in_mod last */
  635. if ( in_wave >= 0 )
  636. AddFilterStringW(p, size, in_modules[in_wave]->FileExtensions, skip);
  637. if ( in_mod >= 0 ) // fuck you in_mod :)
  638. AddFilterStringW(p, size, in_modules[in_mod]->FileExtensions, skip);
  639. if ( *p )
  640. p += lstrlenW(p) + 1; // don't decrement size here cause it was done already
  641. // uppercase the extensions so is consistent as can be
  642. CharUpperBuffW(ps, 256 * 1024 - (p - ps));
  643. if ( playlistManager )
  644. {
  645. wchar_t ext[512] = { 0 };
  646. playlistManager->GetExtensionList(ext, 512);
  647. StringCchPrintfW(p, size, getStringW(IDS_PLAYLISTSTRING_NEW, NULL, 0), ext);
  648. int cnt = lstrlenW(p) + 1;
  649. p += cnt; size -= cnt;
  650. StringCchCatW(p, size, ext);
  651. p = inc(p, size, 1);
  652. }
  653. for ( x = 0; x < in_modules.size(); x++ )
  654. {
  655. char* a = in_modules[x]->FileExtensions;
  656. while ( a && *a )
  657. {
  658. char* b = a;
  659. a += lstrlenA(a) + 1;
  660. if ( !*a ) break;
  661. // adjust down by 1 so that we have the actual string length exluding null termination
  662. int cnt = MultiByteToWideCharSZ(CP_ACP, 0, a, -1, p, (int)size) - 1;
  663. p += cnt; size -= cnt;
  664. {
  665. do
  666. {
  667. wchar_t* c = 0;
  668. StringCchCopyW(p, size, L"*.");
  669. StringCchCatW(p, size, AutoWide(b));
  670. if ( (b = strstr(b, ";")) ) b++;
  671. if ( (c = wcsstr(p, L";")) ) c[1] = 0;
  672. p = inc(p, size);
  673. } while ( b );
  674. p++;
  675. size--;
  676. a += lstrlenA(a) + 1;
  677. }
  678. }
  679. }
  680. StringCchCopyExW(p, size, getStringW(IDS_OFD_ALL_FILES, NULL, 0), &p, &size, 0);
  681. p++; size--;
  682. lstrcpynW(p, L"*.*", (int)size);
  683. p += 3;
  684. *p = 0;
  685. {
  686. wchar_t* newbuf = 0;
  687. size = p + 5 - buf;
  688. newbuf = (wchar_t*)GlobalAlloc(GPTR, size * sizeof(wchar_t));
  689. memcpy(newbuf, buf, size * sizeof(wchar_t));
  690. GlobalFree(buf);
  691. return newbuf;
  692. }
  693. }
  694. char* in_getextlist()
  695. {
  696. size_t x;
  697. char* mem = NULL, * p;
  698. int size = 1024;
  699. std::vector<LPSTR> exts;
  700. for ( x = 0; x != in_modules.size(); x++ )
  701. {
  702. char* a = in_modules[x]->FileExtensions;
  703. while ( a && *a )
  704. {
  705. char* b = a, * c = 0;
  706. do
  707. {
  708. c = strstr(b, ";");
  709. if ( c )
  710. {
  711. char temp[32] = { 0 };
  712. lstrcpynA(temp, b, c - b + 1);
  713. int count = CharUpperBuffA(temp, ARRAYSIZE(temp));
  714. bool skip = false;
  715. for ( size_t i = 0; i < exts.size(); i++ )
  716. {
  717. if ( !stricmp(exts[i], temp) )
  718. {
  719. skip = true;
  720. break;
  721. }
  722. }
  723. if ( !skip )
  724. {
  725. exts.push_back(_strdup(temp));
  726. size += count + 2;
  727. }
  728. }
  729. else
  730. {
  731. char temp[32] = { 0 };
  732. lstrcpynA(temp, b, c - b + 1);
  733. int count = CharUpperBuffA(temp, ARRAYSIZE(temp));
  734. bool skip = false;
  735. for ( size_t i = 0; i < exts.size(); i++ )
  736. {
  737. if ( !stricmp(exts[i], temp) )
  738. {
  739. skip = true;
  740. break;
  741. }
  742. }
  743. if ( !skip )
  744. {
  745. exts.push_back(_strdup(temp));
  746. size += count + 2;
  747. }
  748. }
  749. b = c + 1;
  750. } while ( c && *c );
  751. a += lstrlenA(a) + 1;
  752. if ( !*a ) break;
  753. a += lstrlenA(a) + 1;
  754. }
  755. }
  756. if ( size > 0 )
  757. {
  758. p = mem = (char*)GlobalAlloc(GPTR, size);
  759. for ( x = 0; x != exts.size(); x++ )
  760. {
  761. char* e = exts[x];
  762. if ( e && *e )
  763. {
  764. lstrcpynA(p, e, 32);
  765. p += lstrlenA(p) + 1;
  766. free(e);
  767. }
  768. }
  769. *p = 0;
  770. }
  771. return mem;
  772. }
  773. wchar_t* in_getextlistW()
  774. {
  775. size_t x = 0;
  776. wchar_t* mem = NULL, * p = 0;
  777. int size = 0;
  778. std::vector<LPWSTR> exts;
  779. for ( x = 0; x != in_modules.size(); x++ )
  780. {
  781. char* a = in_modules[x]->FileExtensions;
  782. while ( a && *a )
  783. {
  784. char* b = a, * c;
  785. do
  786. {
  787. wchar_t temp[32] = { 0 };
  788. c = strstr(b, ";");
  789. if ( c )
  790. {
  791. int count = MultiByteToWideChar(CP_ACP, 0, b, c - b, 0, 0);
  792. MultiByteToWideChar(CP_ACP, 0, b, c - b, temp, count);
  793. CharUpperBuffW(temp, ARRAYSIZE(temp));
  794. bool skip = false;
  795. for ( size_t i = 0; i < exts.size(); i++ )
  796. {
  797. if ( !wcsicmp(exts[i], temp) )
  798. {
  799. skip = true;
  800. break;
  801. }
  802. }
  803. if ( !skip )
  804. {
  805. exts.push_back(_wcsdup(temp));
  806. size += count + 2;
  807. }
  808. }
  809. else
  810. {
  811. int count = MultiByteToWideChar(CP_ACP, 0, b, -1, 0, 0);
  812. MultiByteToWideChar(CP_ACP, 0, b, -1, temp, count);
  813. CharUpperBuffW(temp, ARRAYSIZE(temp));
  814. bool skip = false;
  815. for ( size_t i = 0; i < exts.size(); i++ )
  816. {
  817. if ( !wcsicmp(exts[i], temp) )
  818. {
  819. skip = true;
  820. break;
  821. }
  822. }
  823. if ( !skip )
  824. {
  825. exts.push_back(_wcsdup(temp));
  826. size += count + 2;
  827. }
  828. }
  829. b = c + 1;
  830. } while ( c && *c );
  831. a += lstrlenA(a) + 1;
  832. if ( !*a ) break;
  833. a += lstrlenA(a) + 1;
  834. }
  835. }
  836. if ( size > 0 )
  837. {
  838. p = mem = (wchar_t*)GlobalAlloc(GPTR, size * sizeof(wchar_t));
  839. for ( x = 0; x != exts.size(); x++ )
  840. {
  841. wchar_t* e = exts[x];
  842. if ( e && *e )
  843. {
  844. lstrcpynW(p, e, 32);
  845. p += lstrlenW(p) + 1;
  846. free(e);
  847. }
  848. }
  849. *p = 0;
  850. }
  851. return mem;
  852. }
  853. static void vissa_init(int maxlatency_in_ms, int srate)
  854. {
  855. g_srate_exact = srate;
  856. int nf = MulDiv(maxlatency_in_ms * 4, srate, 450000);
  857. //int nf = maxlatency_in_ms/5;
  858. sa_init(nf);
  859. vsa_init(nf);
  860. vu_init(nf, srate);
  861. }
  862. static void vissa_deinit()
  863. {
  864. sa_deinit();
  865. vsa_deinit();
  866. vu_deinit();
  867. }
  868. static void setinfo(int bitrate, int srate, int stereo, int synched)
  869. {
  870. int last_brate = g_brate, last_srate = g_srate, last_nch = g_nch;
  871. if ( stereo != -1 )
  872. {
  873. g_nch = stereo;
  874. // dynamic channels
  875. if ( g_nch > 0 )
  876. {
  877. if ( NULL != eq ) { free(eq); eq = NULL; }
  878. eq = (eq10_t*)calloc(g_nch, sizeof(eq10_t));
  879. if ( NULL == eq )
  880. {
  881. // bad. Need to handle this.
  882. g_nch = 0;
  883. }
  884. eq_set(config_use_eq, (char*)eq_tab, config_preamp);
  885. }
  886. }
  887. if ( bitrate != -1 && srate != -1 )
  888. {
  889. g_need_titleupd = 1;
  890. }
  891. if ( bitrate != -1 )
  892. g_brate = bitrate;
  893. if ( srate != -1 )
  894. {
  895. g_srate = srate;
  896. switch ( srate )
  897. {
  898. case 11:
  899. g_srate_exact = 11025; break;
  900. case 22:
  901. g_srate_exact = 22050; break;
  902. case 44:
  903. g_srate_exact = 44100; break;
  904. case 88:
  905. g_srate_exact = 88200; break;
  906. default:
  907. g_srate_exact = srate * 1000; break;
  908. }
  909. }
  910. if ( bitrate != -1 || srate != -1 || stereo != -1 )
  911. {
  912. static unsigned int last_t;
  913. unsigned int now = GetTickCount();
  914. //detect wrap with the first one
  915. if ( now < last_t || now > last_t + 500 || last_brate != g_brate || last_srate != g_srate || last_nch != g_nch )
  916. {
  917. last_t = now;
  918. PostMessageW(hMainWindow, WM_WA_IPC, IPC_CB_MISC_INFO, IPC_CB_MISC);
  919. }
  920. }
  921. if ( synched != -1 || stereo != -1 )
  922. {
  923. g_need_infoupd = synched | (stereo != -1 ? 8 : 4) | (g_need_infoupd & 8);
  924. }
  925. }
  926. static int sa_getmode()
  927. {
  928. if ( sa_override ) return 3;
  929. if ( sa_curmode == 4 && !config_mw_open && config_pe_open )
  930. return 1;
  931. return sa_curmode;
  932. }
  933. static int eq_isactive()
  934. {
  935. int r = dsp_isactive();
  936. if ( r ) return 1;
  937. if ( in_mod && !(in_mod->UsesOutputPlug & IN_MODULE_FLAG_REPLAYGAIN) && config_replaygain.GetBool() && (config_replaygain_non_rg_gain.GetFloat() != 0) ) return 1;
  938. if ( in_mod && !(in_mod->UsesOutputPlug & IN_MODULE_FLAG_REPLAYGAIN_PREAMP) && config_replaygain.GetBool() && (config_replaygain_preamp.GetFloat() != 0) ) return 1;
  939. if ( filter_enabled ) return 2;
  940. return 0;
  941. }
  942. static float eq_lookup1[64] = {
  943. 4.000000f, 3.610166f, 3.320019f, 3.088821f, 2.896617f,
  944. 2.732131f, 2.588368f, 2.460685f, 2.345845f, 2.241498f,
  945. 2.145887f, 2.057660f, 1.975760f, 1.899338f, 1.827707f,
  946. 1.760303f, 1.696653f, 1.636363f, 1.579094f, 1.524558f,
  947. 1.472507f, 1.422724f, 1.375019f, 1.329225f, 1.285197f,
  948. 1.242801f, 1.201923f, 1.162456f, 1.124306f, 1.087389f,
  949. 1.051628f, 1.000000f, 0.983296f, 0.950604f, 0.918821f,
  950. 0.887898f, 0.857789f, 0.828454f, 0.799853f, 0.771950f,
  951. 0.744712f, 0.718108f, 0.692110f, 0.666689f, 0.641822f,
  952. 0.617485f, 0.593655f, 0.570311f, 0.547435f, 0.525008f,
  953. 0.503013f, 0.481433f, 0.460253f, 0.439458f, 0.419035f,
  954. 0.398970f, 0.379252f, 0.359868f, 0.340807f, 0.322060f,
  955. 0.303614f, 0.285462f, 0.267593f, 0.250000f
  956. };
  957. ///////////////// EQ CODE /////////////////////////
  958. static int __inline float_to_int(double a)
  959. {
  960. a += ((((65536.0 * 65536.0 * 16) + (65536.0 * 0.5)) * 65536.0));
  961. return ((*(int*)&a) - 0x80000000);
  962. }
  963. float* splbuf = 0;
  964. int splbuf_alloc = 0;
  965. float eqt[10] = { 0 };
  966. static void FillFloat(float* floatBuf, void* samples, size_t bps, size_t numSamples, size_t numChannels, float preamp)
  967. {
  968. nsutil_pcm_IntToFloat_Interleaved_Gain(floatBuf, samples, (int)bps, numSamples * numChannels, preamp);
  969. }
  970. static void FillSamples(void* samples, float* floatBuf, size_t bps, size_t numSamples, size_t numChannels)
  971. {
  972. nsutil_pcm_FloatToInt_Interleaved(samples, floatBuf, (int)bps, numSamples * numChannels);
  973. }
  974. static float NonReplayGainAdjust()
  975. {
  976. if ( !(in_mod->UsesOutputPlug & IN_MODULE_FLAG_REPLAYGAIN) && config_replaygain.GetBool() )
  977. return pow(10.0f, (float)config_replaygain_non_rg_gain / 20.0f);
  978. else
  979. return 1.0f;
  980. }
  981. static float ReplayGainPreamp()
  982. {
  983. if ( !(in_mod->UsesOutputPlug & IN_MODULE_FLAG_REPLAYGAIN_PREAMP) && config_replaygain.GetBool() )
  984. return pow(10.0f, (float)config_replaygain_preamp / 20.0f);
  985. else
  986. return 1.0f;
  987. }
  988. static int eq_dosamples_4front(short* samples, int numsamples, int bps, int nch, int srate)
  989. {
  990. g_srate_exact = srate;
  991. //char *csamples = (char *)samples;
  992. short* oldsamples = samples;
  993. if ( filter_enabled && in_mod && !(in_mod->UsesOutputPlug & IN_MODULE_FLAG_EQ) )
  994. {
  995. if ( !eq || filter_srate != srate )
  996. {
  997. if ( !eq ) eq = (eq10_t*)calloc(nch, sizeof(eq10_t));
  998. eq10_setup(eq, nch, (float)srate); // initialize
  999. for ( int x = 0; x < 10; x++ )
  1000. eq10_setgain(eq, nch, x, eqt[x]);
  1001. filter_srate = srate;
  1002. }
  1003. if ( splbuf_alloc < numsamples * nch )
  1004. {
  1005. int new_splbuf_alloc = numsamples * nch;
  1006. float* new_splbuf = (float*)realloc(splbuf, 2 * sizeof(float) * new_splbuf_alloc);
  1007. if ( new_splbuf )
  1008. {
  1009. splbuf_alloc = new_splbuf_alloc;
  1010. splbuf = new_splbuf;
  1011. }
  1012. }
  1013. if ( splbuf && eq )
  1014. {
  1015. int y = nch * numsamples;
  1016. FillFloat(splbuf, samples, bps, numsamples, nch, preamp_val * NonReplayGainAdjust() * ReplayGainPreamp());
  1017. for ( int x = 0; x < nch; x++ )
  1018. {
  1019. eq10_processf(eq + x, splbuf, splbuf + y, numsamples, x, nch);
  1020. }
  1021. FillSamples(samples, splbuf + y, bps, numsamples, nch);
  1022. }
  1023. }
  1024. else if ( !(in_mod->UsesOutputPlug & IN_MODULE_FLAG_REPLAYGAIN) && config_replaygain.GetBool() && (config_replaygain_non_rg_gain.GetFloat() != 0) )
  1025. {
  1026. if ( splbuf_alloc < numsamples * nch )
  1027. {
  1028. int new_splbuf_alloc = numsamples * nch;
  1029. float* new_splbuf = (float*)realloc(splbuf, 2 * sizeof(float) * new_splbuf_alloc);
  1030. if ( new_splbuf )
  1031. {
  1032. splbuf_alloc = new_splbuf_alloc;
  1033. splbuf = new_splbuf;
  1034. }
  1035. }
  1036. if ( splbuf )
  1037. {
  1038. FillFloat(splbuf, samples, bps, numsamples, nch, NonReplayGainAdjust() * ReplayGainPreamp());
  1039. FillSamples(samples, splbuf, bps, numsamples, nch);
  1040. }
  1041. }
  1042. else if ( !(in_mod->UsesOutputPlug & IN_MODULE_FLAG_REPLAYGAIN_PREAMP) && config_replaygain.GetBool() && (config_replaygain_preamp.GetFloat() != 0) )
  1043. {
  1044. if ( splbuf_alloc < numsamples * nch )
  1045. {
  1046. int new_splbuf_alloc = numsamples * nch;
  1047. float* new_splbuf = (float*)realloc(splbuf, 2 * sizeof(float) * new_splbuf_alloc);
  1048. if ( new_splbuf )
  1049. {
  1050. splbuf_alloc = new_splbuf_alloc;
  1051. splbuf = new_splbuf;
  1052. }
  1053. }
  1054. if ( splbuf )
  1055. {
  1056. FillFloat(splbuf, samples, bps, numsamples, nch, ReplayGainPreamp());
  1057. FillSamples(samples, splbuf, bps, numsamples, nch);
  1058. }
  1059. }
  1060. else
  1061. filter_srate = 0;
  1062. return dsp_dosamples(oldsamples, numsamples, bps, nch, srate);
  1063. }
  1064. static __inline double VALTODB(int v)
  1065. {
  1066. v -= 31;
  1067. if ( v < -31 ) v = -31;
  1068. if ( v > 32 ) v = 32;
  1069. if ( v > 0 ) return -12.0 * (v / 32.0);
  1070. else if ( v < 0 )
  1071. {
  1072. return -12.0 * (v / 31.0);
  1073. }
  1074. return 0.0f;
  1075. }
  1076. static void eq_set_4front(char data[10])
  1077. {
  1078. if ( !eq )
  1079. return;
  1080. for ( int x = 0; x < 10; x++ )
  1081. {
  1082. eqt[x] = (float)VALTODB(data[x]);
  1083. if ( filter_srate ) eq10_setgain(eq, g_nch, x, eqt[x]);
  1084. }
  1085. }
  1086. static bool eq_do_first = true;
  1087. static int startup_config_eq_type = EQ_TYPE_4FRONT;
  1088. void benskiQ_eq_set(char data[10]);
  1089. void eq_set(int on, char data[10], int preamp)
  1090. {
  1091. if ( eq_do_first )
  1092. {
  1093. startup_config_eq_type = config_eq_type;
  1094. eq_do_first = false;
  1095. }
  1096. int x;
  1097. if ( in_mod && in_mod->EQSet ) in_mod->EQSet(on, data, preamp);
  1098. for ( x = 9; x >= 0 && data[x] == 31; x++ );
  1099. if ( x >= 0 )
  1100. filter_top2 = x;
  1101. for ( x = 0; x < 10 && data[x] == 31; x++ );
  1102. if ( !on || (preamp == 31 && x == 10) )
  1103. {
  1104. filter_enabled = 0;
  1105. }
  1106. else filter_enabled = 1;
  1107. preamp_val = (float)eq_lookup1[preamp];
  1108. if ( startup_config_eq_type == EQ_TYPE_4FRONT )
  1109. eq_set_4front(data);
  1110. if ( startup_config_eq_type == EQ_TYPE_CONSTANT_Q )
  1111. benskiQ_eq_set(data);
  1112. }
  1113. static int eq_dosamples(short* samples, int numsamples, int bps, int nch, int srate)
  1114. {
  1115. if ( eq_do_first )
  1116. {
  1117. startup_config_eq_type = config_eq_type;
  1118. eq_do_first = false;
  1119. }
  1120. if ( startup_config_eq_type == EQ_TYPE_4FRONT )
  1121. return eq_dosamples_4front(samples, numsamples, bps, nch, srate);
  1122. else
  1123. return benskiQ_eq_dosamples(samples, numsamples, bps, nch, srate);
  1124. }