1
0

VIS.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. #include <windowsx.h>
  2. #include "Main.h"
  3. #include "vis.h"
  4. #include "fft.h"
  5. #include <bfc/platform/types.h>
  6. #include <math.h>
  7. #include <assert.h>
  8. #include "./api.h"
  9. #include "../nsutil/window.h"
  10. #include "../nu/threadname.h"
  11. static winampVisModule *vis_mod;
  12. static DWORD WINAPI vis_thread(void *tmp);
  13. static HANDLE hThread;
  14. static DWORD visThreadId=0;
  15. static volatile int killThread;
  16. static int _nch = 2, _numframes=1;
  17. int _srate = 44100;
  18. static wchar_t _visplugin_name[512];
  19. static int _visplugin_num;
  20. static char *vsa_data;
  21. static int vsa_position, vsa_entrysize=577*4;
  22. static int vsa_length,size=576*4;
  23. static int vis_stopping;
  24. static CRITICAL_SECTION cs;
  25. static HWND external_window = NULL;
  26. static HWND external_window_host = NULL;
  27. #ifdef _M_IX86
  28. __inline static int lrint(float flt)
  29. {
  30. int intgr;
  31. _asm
  32. {
  33. fld flt
  34. fistp intgr
  35. }
  36. return intgr;
  37. }
  38. #else
  39. __inline static int lrint(float flt)
  40. {
  41. return (int)flt;
  42. }
  43. #endif
  44. // quantizes to 23 bits - use appropriately
  45. #define FASTMIN(x,b) { x = b - x; x += (float)fabs(x); x *= 0.5f; x = b - x; }
  46. static size_t vis_refCount=0;
  47. static winampVisModule *GetVis()
  48. {
  49. winampVisModule *ret = 0;
  50. EnterCriticalSection(&cs);
  51. if (vis_stopping)
  52. {
  53. LeaveCriticalSection(&cs);
  54. return 0;
  55. }
  56. if (vis_mod)
  57. {
  58. ret = vis_mod;
  59. vis_refCount++;
  60. }
  61. LeaveCriticalSection(&cs);
  62. return ret;
  63. }
  64. static void DestroyVis()
  65. {
  66. killThread=1;
  67. vis_stopping=1;
  68. if (GetCurrentThreadId() != visThreadId)
  69. {
  70. HANDLE thisThread = hThread;
  71. LeaveCriticalSection(&cs);
  72. // run message pump. this shouldn't last long.
  73. int x=200;
  74. while (WaitForSingleObject(thisThread,10) == WAIT_TIMEOUT && x-- > 0)
  75. {
  76. WASABI_API_APP->app_messageLoopStep();
  77. }
  78. WaitForSingleObject(thisThread,INFINITE);
  79. EnterCriticalSection(&cs);
  80. }
  81. CloseHandle(hThread);
  82. if (vis_mod)
  83. FreeLibrary(vis_mod->hDllInstance);
  84. vis_stopping=0;
  85. vis_mod=0;
  86. hThread=0;
  87. killThread=0;
  88. }
  89. static void ReleaseVis(winampVisModule *vis)
  90. {
  91. if (vis)
  92. {
  93. EnterCriticalSection(&cs);
  94. vis_refCount--;
  95. if (vis_refCount == 0)
  96. {
  97. DestroyVis();
  98. }
  99. LeaveCriticalSection(&cs);
  100. }
  101. }
  102. void vis_init(void)
  103. {
  104. InitializeCriticalSectionAndSpinCount(&cs, 4000);
  105. }
  106. static char *vsa_get(int timestamp);
  107. static void vsa_setdatasize();
  108. int vis_running()
  109. {
  110. int running=0;
  111. winampVisModule *vis = GetVis();
  112. if (vis)
  113. {
  114. running = !vis_stopping;
  115. ReleaseVis(vis);
  116. }
  117. return running;
  118. }
  119. static int priorities[5] =
  120. {
  121. THREAD_PRIORITY_LOWEST,
  122. THREAD_PRIORITY_BELOW_NORMAL,
  123. THREAD_PRIORITY_NORMAL,
  124. THREAD_PRIORITY_ABOVE_NORMAL,
  125. THREAD_PRIORITY_HIGHEST
  126. };
  127. void vis_start(HWND hwnd, wchar_t *fn)
  128. {
  129. if (vis_stopping || g_safeMode) return;
  130. vis_stop();
  131. vsa_deinit();
  132. if (!config_visplugin_name[0]) return;
  133. killThread=0;
  134. if (!fn || !*fn)
  135. {
  136. PathCombineW(_visplugin_name, VISDIR, config_visplugin_name);
  137. _visplugin_num=config_visplugin_num;
  138. }
  139. else
  140. {
  141. wchar_t buf[MAX_PATH] = {0};
  142. wchar_t *p;
  143. StringCchCopyW(buf,MAX_PATH,fn);
  144. p=wcsstr(buf,L",");
  145. if (p)
  146. {
  147. *p++=0;
  148. _visplugin_num=_wtoi(p);
  149. }
  150. else _visplugin_num=0;
  151. if (PathIsFileSpecW(buf) || PathIsRelativeW(buf))
  152. PathCombineW(_visplugin_name, VISDIR, buf);
  153. else
  154. StringCchCopyW(_visplugin_name,512,buf);
  155. }
  156. hThread = (HANDLE) CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) vis_thread,NULL,0,&visThreadId);
  157. SetThreadPriority(hThread,priorities[config_visplugin_priority]);
  158. }
  159. void vis_setprio()
  160. {
  161. if (vis_stopping) return;
  162. if (hThread) SetThreadPriority(hThread,priorities[config_visplugin_priority]);
  163. }
  164. void vis_stop()
  165. {
  166. if (vis_stopping||!hThread) return;
  167. EnterCriticalSection(&cs); // go into critical section so vis_mod doesn't suddenly appear out of nowhere
  168. winampVisModule *thisVis = vis_mod;
  169. LeaveCriticalSection(&cs);
  170. ReleaseVis(thisVis);
  171. }
  172. void vis_setinfo(int srate, int nch)
  173. {
  174. if (srate > 0) _srate = srate;
  175. if (nch > 0) _nch = nch;
  176. if (!vis_running()) return;
  177. EnterCriticalSection(&cs);
  178. winampVisModule *vis = GetVis();
  179. if (vis)
  180. {
  181. vis->sRate = _srate;
  182. vis->nCh = _nch;
  183. ReleaseVis(vis);
  184. }
  185. LeaveCriticalSection(&cs);
  186. }
  187. void vis_setextwindow(HWND hwnd)
  188. {
  189. HWND test_window;
  190. unsigned int test_window_style_ex;
  191. unsigned long window_thread_id;
  192. EnterCriticalSection(&cs);
  193. external_window = hwnd;
  194. external_window_host = external_window;
  195. window_thread_id = GetWindowThreadProcessId(external_window, NULL);
  196. while(NULL != external_window_host)
  197. {
  198. test_window = GetAncestor(external_window_host, GA_PARENT);
  199. if (NULL != test_window &&
  200. window_thread_id == GetWindowThreadProcessId(test_window, NULL))
  201. {
  202. test_window_style_ex = (unsigned int)GetWindowLongPtrW(test_window, GWL_STYLE);
  203. if (0 != (WS_EX_CONTROLPARENT & test_window_style_ex))
  204. {
  205. external_window_host = test_window;
  206. continue;
  207. }
  208. }
  209. break;
  210. }
  211. LeaveCriticalSection(&cs);
  212. }
  213. static winampVisModule *CreateVis(HINSTANCE visLib)
  214. {
  215. EnterCriticalSection(&cs);
  216. winampVisModule *ret = 0;
  217. ret = GetVis();
  218. if (ret)
  219. {
  220. LeaveCriticalSection(&cs);
  221. return ret;
  222. }
  223. winampVisGetHeaderType pr;
  224. pr = (winampVisGetHeaderType) GetProcAddress(visLib,"winampVisGetHeader");
  225. if (!pr)
  226. {
  227. LeaveCriticalSection(&cs);
  228. return 0;
  229. }
  230. winampVisHeader* pv = pr(hMainWindow);
  231. if (!pv)
  232. {
  233. LeaveCriticalSection(&cs);
  234. return 0;
  235. }
  236. vis_mod = pv->getModule(_visplugin_num);
  237. if (!vis_mod)
  238. {
  239. LeaveCriticalSection(&cs);
  240. return 0;
  241. }
  242. vis_mod->sRate = _srate;
  243. vis_mod->nCh = _nch;
  244. vis_mod->hwndParent = hMainWindow;
  245. vis_mod->hDllInstance = visLib;
  246. vis_refCount++;
  247. LeaveCriticalSection(&cs);
  248. return vis_mod;
  249. }
  250. static BOOL vis_process_message(MSG *msg)
  251. {
  252. if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST &&
  253. msg->hwnd == external_window &&
  254. NULL != external_window)
  255. {
  256. return FALSE;
  257. }
  258. if (WM_MOUSEWHEEL == msg->message &&
  259. NULL != external_window_host)
  260. {
  261. POINT cursor;
  262. HWND target_window;
  263. POINTSTOPOINT(cursor, msg->lParam);
  264. target_window = WindowFromPoint(cursor);
  265. if (NULL != target_window &&
  266. FALSE == IsChild(external_window_host, target_window ) &&
  267. GetWindowThreadProcessId(target_window, NULL) != GetWindowThreadProcessId(external_window_host, NULL))
  268. {
  269. PostMessageW(hMainWindow, msg->message, msg->wParam, msg->lParam);
  270. return TRUE;
  271. }
  272. }
  273. if (NULL != external_window_host)
  274. return IsDialogMessageW(external_window_host, msg);
  275. return FALSE;
  276. }
  277. static DWORD WINAPI vis_thread(void *tmp)
  278. {
  279. winampVisModule *vis = 0;
  280. MSG Msg;
  281. HINSTANCE hLib=0;
  282. int t=0;
  283. SetThreadName((DWORD)-1, "Vis (plugin) thread");
  284. hLib = LoadLibrary(_visplugin_name);
  285. if (!hLib)
  286. {
  287. t=1;
  288. }
  289. else
  290. {
  291. vis = CreateVis(hLib);
  292. if (!vis)
  293. {
  294. FreeLibrary(hLib);
  295. hLib = 0;
  296. t=1;
  297. }
  298. }
  299. if (!t)
  300. {
  301. if (!(config_no_visseh&1))
  302. {
  303. __try
  304. {
  305. t = (vis ? vis->Init(vis) : 1);
  306. }
  307. __except(EXCEPTION_EXECUTE_HANDLER)
  308. {
  309. t=1;
  310. char errstr[512] = {0};
  311. char caption[512] = {0};
  312. getString(IDS_PLUGINERROR,errstr,512);
  313. StringCchCatA(errstr, 512, " (1)");
  314. MessageBoxA(NULL,errstr,getString(IDS_ERROR,caption,512),MB_OK|MB_ICONEXCLAMATION);
  315. }
  316. }
  317. else
  318. {
  319. t = vis->Init(vis);
  320. }
  321. }
  322. if (!t)
  323. {
  324. if (config_disvis) sa_setthread(0);
  325. vsa_setdatasize();
  326. while (!killThread)
  327. {
  328. if (PeekMessage(&Msg,NULL,0,0,PM_REMOVE))
  329. {
  330. if (Msg.message == WM_QUIT)
  331. break;
  332. if (FALSE == vis_process_message(&Msg))
  333. {
  334. TranslateMessage(&Msg);
  335. DispatchMessage(&Msg);
  336. }
  337. }
  338. else if (!paused)
  339. {
  340. static int upd;
  341. int p=playing;
  342. char *data=0;
  343. if (in_mod && p) data = vsa_get(in_mod->GetOutputTime()+vis->latencyMs);
  344. if (data)
  345. {
  346. int l=vis->spectrumNch;
  347. for (int n = 0; n < l; n ++)
  348. {
  349. memcpy(vis->spectrumData[n],data,576);
  350. data += 576;
  351. }
  352. l=vis->waveformNch;
  353. for (int n = 0; n < l; n ++)
  354. {
  355. memcpy(vis->waveformData[n],data,576);
  356. data += 576;
  357. }
  358. }
  359. if (!data)
  360. {
  361. memset(vis->spectrumData,0,576*2);
  362. memset(vis->waveformData,0,576*2);
  363. }
  364. if (p) upd=1;
  365. if (upd)
  366. if (!(config_no_visseh&1))
  367. {
  368. __try
  369. {
  370. if (vis->Render(vis))
  371. {
  372. break;
  373. }
  374. }
  375. __except(EXCEPTION_EXECUTE_HANDLER)
  376. {
  377. char errstr[512] = {0};
  378. char caption[512] = {0};
  379. getString(IDS_PLUGINERROR,errstr,512);
  380. StringCchCatA(errstr, 512, " (2)");
  381. MessageBoxA(NULL,errstr,getString(IDS_ERROR,caption,512),MB_OK|MB_ICONEXCLAMATION);
  382. break;
  383. }
  384. }
  385. else
  386. {
  387. if (vis->Render(vis))
  388. {
  389. break;
  390. }
  391. }
  392. if (!p) upd=0;
  393. Sleep(vis->delayMs);
  394. }
  395. else Sleep(min(1,vis->delayMs));
  396. }
  397. vsa_deinit();
  398. sa_setthread(config_sa);
  399. if (!(config_no_visseh&1))
  400. {
  401. __try
  402. {
  403. vis->Quit(vis);
  404. }
  405. __except(EXCEPTION_EXECUTE_HANDLER)
  406. {
  407. wchar_t errstr[512] = {0};
  408. wchar_t caption[512] = {0};
  409. getStringW(IDS_PLUGINERROR,errstr,512);
  410. StringCchCatW(errstr, 512, L" (3)");
  411. MessageBoxW(NULL,errstr,getStringW(IDS_ERROR,caption,512),MB_OK|MB_ICONEXCLAMATION);
  412. }
  413. }
  414. else
  415. {
  416. vis->Quit(vis);
  417. }
  418. EnterCriticalSection(&cs);
  419. if (killThread)
  420. {
  421. LeaveCriticalSection(&cs);
  422. return 0;
  423. }
  424. else
  425. {
  426. ReleaseVis(vis);
  427. LeaveCriticalSection(&cs);
  428. return 0;
  429. }
  430. }
  431. ReleaseVis(vis);
  432. if (hLib)
  433. FreeLibrary(hLib);
  434. hLib = 0;
  435. return 0;
  436. }
  437. static int last_pos;
  438. static void _vsa_init()
  439. {
  440. vsa_deinit();
  441. if (_numframes < 1) _numframes=1;
  442. vsa_entrysize = 4+size;
  443. vsa_data = (char *) GlobalAlloc(GPTR,vsa_entrysize * _numframes);
  444. vsa_position=0;
  445. vsa_length = _numframes;
  446. }
  447. void vsa_init(int numframes)
  448. {
  449. EnterCriticalSection(&cs);
  450. if (vis_running())
  451. {
  452. last_pos=0;
  453. _numframes=numframes;
  454. _vsa_init();
  455. }
  456. else
  457. {
  458. last_pos=0;
  459. _numframes=numframes;
  460. }
  461. LeaveCriticalSection(&cs);
  462. }
  463. static void vsa_setdatasize()
  464. {
  465. EnterCriticalSection(&cs);
  466. winampVisModule *vis = GetVis();
  467. if (vis)
  468. {
  469. size=576*(vis->waveformNch+vis->spectrumNch);
  470. _vsa_init();
  471. ReleaseVis(vis);
  472. }
  473. LeaveCriticalSection(&cs);
  474. }
  475. int vsa_add(void *data, int timestamp)
  476. {
  477. if (!vsa_data) return 1;
  478. EnterCriticalSection(&cs);
  479. if (vsa_data) // check again, it might have gone away while we were waiting on the CS
  480. {
  481. if (vsa_length < 2)
  482. {
  483. vsa_position = 0;
  484. }
  485. char *c = vsa_data + vsa_position*vsa_entrysize;
  486. *(int32_t *)c=timestamp;
  487. memcpy(c+4,data,vsa_entrysize-4);
  488. if (++vsa_position >= vsa_length) vsa_position -= vsa_length;
  489. LeaveCriticalSection(&cs);
  490. return 0;
  491. }
  492. else
  493. {
  494. LeaveCriticalSection(&cs);
  495. return 1;
  496. }
  497. }
  498. void vsa_deinit(void)
  499. {
  500. EnterCriticalSection(&cs);
  501. if (vsa_data)
  502. {
  503. GlobalFree(vsa_data);
  504. vsa_data=0;
  505. vsa_length=0;
  506. }
  507. LeaveCriticalSection(&cs);
  508. }
  509. static char *vsa_get(int timestamp)
  510. {
  511. int i,x, closest=1000000, closest_v = -1;
  512. if (!vsa_data) return NULL;
  513. if (vsa_length<2)
  514. {
  515. return vsa_data+4;
  516. }
  517. EnterCriticalSection(&cs);
  518. x=last_pos;
  519. if (x >= vsa_length) x=0;
  520. for (i = 0; i < vsa_length; i ++)
  521. {
  522. int *q = (int *)(vsa_data+x*vsa_entrysize);
  523. int d = timestamp-*q;
  524. if (++x == vsa_length) x=0;
  525. if (d < 0) d = -d;
  526. if (d < closest)
  527. {
  528. closest = d;
  529. closest_v = x;
  530. }
  531. else if (closest<200) break;
  532. }
  533. if (closest_v >= 0)
  534. {
  535. static char data[576*4];
  536. last_pos=closest_v;
  537. memcpy(data,vsa_data+vsa_entrysize*closest_v+4,vsa_entrysize-4);
  538. LeaveCriticalSection(&cs);
  539. return data;
  540. }
  541. else
  542. {
  543. LeaveCriticalSection(&cs);
  544. return 0;
  545. }
  546. }
  547. int vsa_getmode(int *sp, int *wa)
  548. {
  549. int rv=0;
  550. winampVisModule *vis = GetVis();
  551. if (vis)
  552. {
  553. EnterCriticalSection(&cs);
  554. *sp=vis->spectrumNch;
  555. *wa=vis->waveformNch;
  556. rv=1;
  557. LeaveCriticalSection(&cs);
  558. ReleaseVis(vis);
  559. }
  560. else *sp=*wa=0;
  561. return rv;
  562. }
  563. void FillRealSamples_8Bit(unsigned char *data, const int stride, const int channels, float *samples, const float divider)
  564. {
  565. int frame,c;
  566. const float p = (float)channels*divider;
  567. for (frame = 0; frame <512; frame ++)
  568. {
  569. //done by memset - samples[x*2]=0;
  570. for (c=0;c<channels;c++)
  571. {
  572. samples[frame] += (float)(*data - 128);
  573. data+=stride; // jump to the next sample (channels are interleaved)
  574. }
  575. samples[frame] /= p;
  576. //done by memset - wavetrum[x*2+1] = 0.0f;
  577. }
  578. nsutil_window_Hann_F32_IP(samples, 512);
  579. }
  580. #define SA_DC_FILTER
  581. void FillRealSamples(char *ptr, const int stride, const int channels, float *samples, const float divider)
  582. {
  583. #ifdef SA_DC_FILTER
  584. float x1=0, y1=0;
  585. #endif
  586. int frame, c;
  587. const float p=(float)channels * divider;
  588. // we're calculating using only the most significant byte,
  589. // because we only end up with 6 bit data anyway
  590. // if you want full resolution, check out CVS tag BETA_2005_1122_182830, file: vis.c
  591. for (frame = 0;frame <512;frame++)
  592. {
  593. //done by memset - wavetrum[x*2]=0;
  594. float x=0;
  595. for (c=0;c<channels;c++)
  596. {
  597. x += (float)(*ptr);
  598. ptr+=stride; // jump to the next sample (channels are interleaved)
  599. }
  600. #ifdef SA_DC_FILTER
  601. float y = x - x1 + 0.99f * y1;
  602. y1=y;
  603. x1=x;
  604. #else
  605. float y=x;
  606. #endif
  607. y/=p;
  608. samples[frame]=y;
  609. //done by memset - wavetrum[x*2+1] = 0.0f;
  610. }
  611. nsutil_window_Hann_F32_IP(samples, 512);
  612. }
  613. void vsa_addpcmdata(void *_data_buf, int numChannels, int numBits, int ts)
  614. {
  615. char *data_buf = reinterpret_cast<char *>(_data_buf);
  616. // begin vis plugin stuff
  617. winampVisModule *vis = GetVis();
  618. if (vis)
  619. {
  620. __declspec(align(32)) float wavetrum[512];
  621. extern int vsa_add(void *data, int timestamp);
  622. char data[576*4*2] = {0};
  623. int data_offs=0;
  624. int y,x,spectrumChannels, waveformChannels, stride;
  625. spectrumChannels=min(numChannels,vis->spectrumNch);
  626. stride=numBits/8;
  627. for (y = 0; y < spectrumChannels; y ++)
  628. {
  629. if (spectrumChannels == 1) // downmix to mono, if necessary
  630. {
  631. if (numBits == 8)
  632. FillRealSamples_8Bit((unsigned char*)data_buf, 1, numChannels, wavetrum, 1.f);
  633. else
  634. {
  635. const int stride=numBits/8; // number of bytes between samples
  636. char *ptr = data_buf+y*stride+stride-1; // offset for little endian
  637. FillRealSamples(ptr, stride, numChannels, wavetrum, 1.f);
  638. }
  639. }
  640. else // TODO: deal with 'downmixing' to stereo if channels>2
  641. {
  642. if (numBits == 8)
  643. FillRealSamples_8Bit((unsigned char*)data_buf, numChannels, 1, wavetrum, 1.f);
  644. else
  645. {
  646. const int stride=numBits/8; // number of bytes between samples
  647. char *ptr = data_buf+y*stride+stride-1; // offset for little endian
  648. FillRealSamples(ptr, stride*numChannels, 1, wavetrum, 1.f);
  649. }
  650. }
  651. fft_9(wavetrum);
  652. {
  653. float la=0;
  654. int thisBand=0;
  655. for (x = 0; x < 256; x ++)
  656. {
  657. float sinT = wavetrum[x*2];
  658. float cosT = wavetrum[x*2+1];
  659. float thisValue=(float)sqrt(sinT*sinT+cosT*cosT)/16.0f;
  660. thisBand++;
  661. FASTMIN(thisValue, 255.f);
  662. data[data_offs++] = lrint((thisValue + la)/2.f);
  663. //data[data_offs++] = lrint((thisValue + thisValue + la)/3.f);
  664. data[data_offs++] = lrint(thisValue);
  665. la=thisValue;
  666. }
  667. while ((data_offs % 576)!=0)
  668. {
  669. la/=2;
  670. data[data_offs++]=lrint(la);
  671. }
  672. assert((data_offs % 576)==0);
  673. }
  674. }
  675. if (numChannels == 1 && vis->spectrumNch == 2) // upmix, if necessary
  676. {
  677. memcpy(data+data_offs,data+data_offs-576,576);
  678. data_offs+=576;
  679. }
  680. waveformChannels=min(numChannels,vis->waveformNch);
  681. if (waveformChannels == 1) // downmix to mono, if necessary
  682. {
  683. char *ptr = data_buf+stride-1; // offset for little endian
  684. for (x=0;x<576;x++)
  685. {
  686. __int32 mix=0;
  687. for (int channel=0;channel<numChannels;channel++)
  688. {
  689. mix += (*ptr);
  690. ptr+=stride; // jump to the next sample (channels are interleaved)
  691. }
  692. data[data_offs++] = (char)(mix / numChannels);
  693. }
  694. }
  695. else // TODO: deal with 'downmixing' to stereo if numChannels>2
  696. {
  697. for (y = 0; y < waveformChannels; y++)
  698. {
  699. char *ptr = data_buf+y*stride+stride-1; // offset for little endian
  700. for (x=0;x<576;x++)
  701. {
  702. data[data_offs++] = *ptr;
  703. ptr+=stride*numChannels;
  704. }
  705. }
  706. }
  707. if (numChannels == 1 && vis->waveformNch == 2)
  708. {
  709. memcpy(data+data_offs,data+data_offs-576,576);
  710. //data_offs+=576;
  711. }
  712. vsa_add(data,ts);
  713. ReleaseVis(vis);
  714. }
  715. }
  716. HWND hVisWindow, hPLVisWindow;
  717. LRESULT CALLBACK VIS_WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  718. {
  719. switch (message)
  720. {
  721. case WM_LBUTTONDOWN:
  722. case WM_LBUTTONUP:
  723. case WM_RBUTTONDOWN:
  724. case WM_RBUTTONUP:
  725. case WM_LBUTTONDBLCLK:
  726. {
  727. RECT r1, r2;
  728. int xPos = GET_X_LPARAM(lParam); // horizontal position of cursor
  729. int yPos = GET_Y_LPARAM(lParam);
  730. if (hwnd == hVisWindow)
  731. {
  732. GetWindowRect(hMainWindow,&r1);
  733. }
  734. else GetWindowRect(hPLWindow,&r1);
  735. GetWindowRect(hwnd,&r2);
  736. xPos += r2.left-r1.left;
  737. yPos += r2.top-r1.top;
  738. lParam = MAKELPARAM(xPos,yPos);
  739. SendMessageW(hwnd == hVisWindow?hMainWindow:hPLWindow,message,wParam,lParam);
  740. return 0;
  741. }
  742. case WM_USER+0xebe:
  743. case WM_DROPFILES:
  744. return SendMessageW(GetParent(hwnd),message,wParam,lParam);
  745. case WM_CREATE:
  746. if (NULL != WASABI_API_APP)
  747. WASABI_API_APP->app_registerGlobalWindow(hwnd);
  748. break;
  749. case WM_DESTROY:
  750. if (NULL != WASABI_API_APP)
  751. WASABI_API_APP->app_unregisterGlobalWindow(hwnd);
  752. break;
  753. }
  754. if (FALSE != IsDirectMouseWheelMessage(message))
  755. {
  756. SendMessageW(hwnd, WM_MOUSEWHEEL, wParam, lParam);
  757. return TRUE;
  758. }
  759. return DefWindowProcW(hwnd,message,wParam,lParam);
  760. }