1
0

r_multidelay.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. // video delay
  25. // copyright tom holden, 2002
  26. // mail: [email protected]
  27. #include <windows.h>
  28. #include "resource.h"
  29. #include "r_defs.h"
  30. #include "../Agave/Language/api_language.h"
  31. #define MOD_NAME "Trans / Multi Delay"
  32. #define C_DELAY C_MultiDelayClass
  33. // R_MultiDelay / #define UNIQUEIDSTRING "Holden05: Multi Delay"
  34. // saved
  35. bool usebeats[6];
  36. int delay[6];
  37. // unsaved
  38. LPVOID buffer[6];
  39. LPVOID inpos[6];
  40. LPVOID outpos[6];
  41. unsigned long buffersize[6];
  42. unsigned long virtualbuffersize[6];
  43. unsigned long oldvirtualbuffersize[6];
  44. unsigned long framedelay[6];
  45. unsigned int numinstances = 0;
  46. unsigned long framessincebeat;
  47. unsigned long framesperbeat;
  48. unsigned long framemem;
  49. unsigned long oldframemem;
  50. unsigned int renderid;
  51. class C_DELAY : public C_RBASE
  52. {
  53. protected:
  54. public:
  55. // standard ape members
  56. C_DELAY();
  57. virtual ~C_DELAY();
  58. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  59. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  60. virtual char *get_desc();
  61. virtual void load_config(unsigned char *data, int len);
  62. virtual int save_config(unsigned char *data);
  63. // saved members
  64. int mode;
  65. int activebuffer;
  66. // unsaved members
  67. unsigned int creationid;
  68. };
  69. // global configuration dialog pointer
  70. static C_DELAY *g_Delay;
  71. // global DLL instance pointer
  72. static HINSTANCE g_hDllInstance;
  73. // configuration screen
  74. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  75. {
  76. char value[16];
  77. unsigned int objectcode, objectmessage;
  78. HWND hwndEdit;
  79. switch (uMsg)
  80. {
  81. case WM_INITDIALOG: //init
  82. {
  83. int i;
  84. for (i=0;i<3;i++) CheckDlgButton(hwndDlg,1100+i,g_Delay->mode==i);
  85. for (i=0;i<6;i++)
  86. {
  87. CheckDlgButton(hwndDlg,1000+i,g_Delay->activebuffer==i);
  88. CheckDlgButton(hwndDlg,1020+i,usebeats[i]);
  89. CheckDlgButton(hwndDlg,1030+i,!usebeats[i]);
  90. hwndEdit = GetDlgItem(hwndDlg,1010+i);
  91. _itoa(delay[i],value,10);
  92. SetWindowText(hwndEdit,value);
  93. }
  94. }
  95. return 1;
  96. case WM_COMMAND:
  97. objectcode = LOWORD(wParam);
  98. objectmessage = HIWORD(wParam);
  99. // mode stuff
  100. if (objectcode >= 1100)
  101. {
  102. if(IsDlgButtonChecked(hwndDlg,objectcode)==1)
  103. {
  104. g_Delay->mode = objectcode - 1100;
  105. for (int i=1100;i<1103;i++) if (objectcode != i) CheckDlgButton(hwndDlg,i,BST_UNCHECKED);
  106. }
  107. return 0;
  108. }
  109. // frames stuff
  110. if (objectcode >= 1030)
  111. {
  112. if(IsDlgButtonChecked(hwndDlg,objectcode)==1)
  113. {
  114. usebeats[objectcode-1030] = false;
  115. CheckDlgButton(hwndDlg,objectcode-10,BST_UNCHECKED);
  116. framedelay[objectcode-1030] = delay[objectcode-1030]+1;
  117. }
  118. else usebeats[objectcode-1030] = true;
  119. return 0;
  120. }
  121. // beats stuff
  122. if (objectcode >= 1020)
  123. {
  124. if(IsDlgButtonChecked(hwndDlg,objectcode)==1)
  125. {
  126. usebeats[objectcode-1020] = true;
  127. CheckDlgButton(hwndDlg,objectcode+10,BST_UNCHECKED);
  128. framedelay[objectcode-1020] = framesperbeat+1;
  129. }
  130. else usebeats[objectcode-1020] = false;
  131. return 0;
  132. }
  133. // edit box stuff
  134. if (objectcode >= 1010)
  135. {
  136. hwndEdit = GetDlgItem(hwndDlg,objectcode);
  137. if (objectmessage == EN_CHANGE)
  138. {
  139. GetWindowText(hwndEdit,value,16);
  140. delay[objectcode-1010] = max(atoi(value),0);
  141. framedelay[objectcode-1010] = (usebeats[objectcode-1010]?framesperbeat:delay[objectcode-1010])+1;
  142. }
  143. else if (objectmessage == EN_KILLFOCUS)
  144. {
  145. _itoa(delay[objectcode-1010],value,10);
  146. SetWindowText(hwndEdit,value);
  147. }
  148. return 0;
  149. }
  150. // active buffer stuff
  151. if (objectcode >= 1000)
  152. {
  153. if(IsDlgButtonChecked(hwndDlg,objectcode)==1)
  154. {
  155. g_Delay->activebuffer = objectcode - 1000;
  156. for (int i=1000;i<1006;i++) if (objectcode != i) CheckDlgButton(hwndDlg,i,BST_UNCHECKED);
  157. }
  158. return 0;
  159. }
  160. }
  161. return 0;
  162. }
  163. // set up default configuration
  164. C_DELAY::C_DELAY()
  165. {
  166. // enable
  167. mode = 0;
  168. activebuffer = 0;
  169. numinstances++;
  170. creationid = numinstances;
  171. if (creationid == 1)
  172. {
  173. for (int i=0; i<6; i++)
  174. {
  175. renderid = 0;
  176. framessincebeat = 0;
  177. framesperbeat = 0;
  178. framemem = 1;
  179. oldframemem = 1;
  180. usebeats[i] = false;
  181. delay[i] = 0;
  182. framedelay[i] = 0;
  183. buffersize[i] = 1;
  184. virtualbuffersize[i] = 1;
  185. oldvirtualbuffersize[i] = 1;
  186. buffer[i] = VirtualAlloc(NULL,buffersize[i],MEM_COMMIT,PAGE_READWRITE);
  187. inpos[i] = buffer[i];
  188. outpos[i] = buffer[i];
  189. }
  190. }
  191. }
  192. // virtual destructor
  193. C_DELAY::~C_DELAY()
  194. {
  195. numinstances--;
  196. if (numinstances == 0) for (int i=0; i<6; i++) VirtualFree(buffer[i],buffersize[i],MEM_DECOMMIT);
  197. }
  198. // RENDER FUNCTION:
  199. // render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout
  200. // w and h are the-width and height of the screen, in pixels.
  201. // isBeat is 1 if a beat has been detected.
  202. // visdata is in the format of [spectrum:0,wave:1][channel][band].
  203. int C_DELAY::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  204. {
  205. if (isBeat&0x80000000) return 0;
  206. if (renderid == numinstances) renderid = 0;
  207. renderid++;
  208. if (renderid == 1)
  209. {
  210. framemem = w*h*4;
  211. if (isBeat)
  212. {
  213. framesperbeat = framessincebeat;
  214. for (int i=0;i<6;i++) if (usebeats[i]) framedelay[i] = framesperbeat+1;
  215. framessincebeat = 0;
  216. }
  217. framessincebeat++;
  218. for (int i=0;i<6;i++)
  219. {
  220. if (framedelay[i]>1)
  221. {
  222. virtualbuffersize[i] = framedelay[i]*framemem;
  223. if (framemem == oldframemem)
  224. {
  225. if (virtualbuffersize[i] != oldvirtualbuffersize[i])
  226. {
  227. if (virtualbuffersize[i] > oldvirtualbuffersize[i])
  228. {
  229. if (virtualbuffersize[i] > buffersize[i])
  230. {
  231. // allocate new memory
  232. if (!VirtualFree(buffer[i],buffersize[i],MEM_DECOMMIT)) return 0;
  233. if (usebeats[i])
  234. {
  235. buffersize[i] = 2*virtualbuffersize[i];
  236. buffer[i] = VirtualAlloc(NULL,buffersize[i],MEM_COMMIT,PAGE_READWRITE);
  237. if (buffer[i] == NULL)
  238. {
  239. buffersize[i] = virtualbuffersize[i];
  240. buffer[i] = VirtualAlloc(NULL,buffersize[i],MEM_COMMIT,PAGE_READWRITE);
  241. }
  242. }
  243. else
  244. {
  245. buffersize[i] = virtualbuffersize[i];
  246. buffer[i] = VirtualAlloc(NULL,buffersize[i],MEM_COMMIT,PAGE_READWRITE);
  247. }
  248. outpos[i] = buffer[i];
  249. inpos[i] = (LPVOID)(((unsigned long)buffer[i])+virtualbuffersize[i]-framemem);
  250. if (buffer[i] == NULL)
  251. {
  252. framedelay[i] = 0;
  253. if (usebeats[i])
  254. {
  255. framesperbeat = 0;
  256. framessincebeat = 0;
  257. framedelay[i] = 0;
  258. delay[i] = 0;
  259. }
  260. }
  261. }
  262. else
  263. {
  264. unsigned long size = (((unsigned long)buffer[i])+oldvirtualbuffersize[i]) - ((unsigned long)outpos[i]);
  265. unsigned long l = ((unsigned long)buffer[i])+virtualbuffersize[i];
  266. unsigned long d = l - size;
  267. MoveMemory((LPVOID)d, outpos[i], size);
  268. for (l = (unsigned long)outpos[i]; l < d; l += framemem) CopyMemory((LPVOID)l,(LPVOID)d,framemem);
  269. }
  270. }
  271. else
  272. { // virtualbuffersize < oldvirtualbuffersize
  273. unsigned long presegsize = ((unsigned long)outpos[i])-((unsigned long)buffer[i]);
  274. if (presegsize > virtualbuffersize[i])
  275. {
  276. MoveMemory(buffer[i],(LPVOID)(((unsigned long)buffer[i])+presegsize-virtualbuffersize[i]),virtualbuffersize[i]);
  277. inpos[i] = (LPVOID)(((unsigned long)buffer[i])+virtualbuffersize[i]-framemem);
  278. outpos[i] = buffer[i];
  279. }
  280. else if (presegsize < virtualbuffersize[i]) MoveMemory(outpos[i],(LPVOID)(((unsigned long)buffer[i])+oldvirtualbuffersize[i]+presegsize-virtualbuffersize[i]),virtualbuffersize[i]-presegsize);
  281. }
  282. oldvirtualbuffersize[i] = virtualbuffersize[i];
  283. }
  284. }
  285. else
  286. {
  287. // allocate new memory
  288. if (!VirtualFree(buffer[i],buffersize[i],MEM_DECOMMIT)) return 0;
  289. if (usebeats[i])
  290. {
  291. buffersize[i] = 2*virtualbuffersize[i];
  292. buffer[i] = VirtualAlloc(NULL,buffersize[i],MEM_COMMIT,PAGE_READWRITE);
  293. if (buffer[i] == NULL)
  294. {
  295. buffersize[i] = virtualbuffersize[i];
  296. buffer[i] = VirtualAlloc(NULL,buffersize[i],MEM_COMMIT,PAGE_READWRITE);
  297. }
  298. }
  299. else
  300. {
  301. buffersize[i] = virtualbuffersize[i];
  302. buffer[i] = VirtualAlloc(NULL,buffersize[i],MEM_COMMIT,PAGE_READWRITE);
  303. }
  304. outpos[i] = buffer[i];
  305. inpos[i] = (LPVOID)(((unsigned long)buffer[i])+virtualbuffersize[i]-framemem);
  306. if (buffer[i] == NULL)
  307. {
  308. framedelay[i] = 0;
  309. if (usebeats[i])
  310. {
  311. framesperbeat = 0;
  312. framessincebeat = 0;
  313. framedelay[i] = 0;
  314. delay[i] = 0;
  315. }
  316. }
  317. oldvirtualbuffersize[i] = virtualbuffersize[i];
  318. }
  319. oldframemem = framemem;
  320. }
  321. }
  322. }
  323. if (mode != 0 && framedelay[activebuffer]>1)
  324. {
  325. if (mode == 2) CopyMemory(framebuffer,outpos[activebuffer],framemem);
  326. else CopyMemory(inpos[activebuffer],framebuffer,framemem);
  327. }
  328. if (renderid == numinstances) for (int i=0;i<6;i++)
  329. {
  330. inpos[i] = (LPVOID)(((unsigned long)inpos[i])+framemem);
  331. outpos[i] = (LPVOID)(((unsigned long)outpos[i])+framemem);
  332. if ((unsigned long)inpos[i]>=((unsigned long)buffer[i])+virtualbuffersize[i]) inpos[i] = buffer[i];
  333. if ((unsigned long)outpos[i]>=((unsigned long)buffer[i])+virtualbuffersize[i]) outpos[i] = buffer[i];
  334. }
  335. return 0;
  336. }
  337. HWND C_DELAY::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
  338. {
  339. g_Delay = this;
  340. return WASABI_API_CREATEDIALOG(IDD_CFG_MULTIDELAY,hwndParent,g_DlgProc);
  341. }
  342. char *C_DELAY::get_desc(void)
  343. {
  344. static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_TRANS_MULTI_DELAY,desc,128):desc);
  345. }
  346. // load_/save_config are called when saving and loading presets (.avs files)
  347. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  348. void C_DELAY::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
  349. {
  350. int pos=0;
  351. // always ensure there is data to be loaded
  352. if (len-pos >= 4)
  353. {
  354. // load mode
  355. mode=GET_INT();
  356. pos+=4;
  357. }
  358. if (len-pos >= 4)
  359. {
  360. // load active buffer
  361. activebuffer=GET_INT();
  362. pos+=4;
  363. }
  364. if (len-pos >= 4)
  365. {
  366. for (int i=0;i<6;i++)
  367. {
  368. if (len-pos >= 4)
  369. {
  370. // load usebeats
  371. usebeats[i]=(GET_INT()==1);
  372. pos+=4;
  373. }
  374. if (len-pos >= 4)
  375. {
  376. // load delay
  377. delay[i]=GET_INT();
  378. framedelay[i] = (usebeats[i]?framesperbeat:delay[i])+1;
  379. pos+=4;
  380. }
  381. }
  382. }
  383. }
  384. // write configuration to data, return length. config data should not exceed 64k.
  385. #define PUT_INT(y) data[pos]=(y)&255; data[pos+1]=(y>>8)&255; data[pos+2]=(y>>16)&255; data[pos+3]=(y>>24)&255
  386. int C_DELAY::save_config(unsigned char *data)
  387. {
  388. int pos=0;
  389. PUT_INT(mode);
  390. pos+=4;
  391. PUT_INT(activebuffer);
  392. pos+=4;
  393. if (creationid == 1)
  394. {
  395. for (int i=0;i<6;i++)
  396. {
  397. PUT_INT((int)usebeats[i]);
  398. pos+=4;
  399. PUT_INT(delay[i]);
  400. pos+=4;
  401. }
  402. }
  403. return pos;
  404. }
  405. // export stuff
  406. C_RBASE *R_MultiDelay(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  407. {
  408. if (desc)
  409. {
  410. strcpy(desc,MOD_NAME);
  411. return NULL;
  412. }
  413. return (C_RBASE *) new C_DELAY();
  414. }