r_bpm.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #include <windows.h>
  25. #include <stdlib.h>
  26. #include <vfw.h>
  27. #include <commctrl.h>
  28. #include <stdio.h>
  29. #include <math.h>
  30. #include "resource.h"
  31. #include "r_defs.h"
  32. #include "../Agave/Language/api_language.h"
  33. #define MOD_NAME "Misc / Custom BPM"
  34. #define C_THISCLASS C_BpmClass
  35. #define SET_BEAT 0x10000000
  36. #define CLR_BEAT 0x20000000
  37. class C_THISCLASS : public C_RBASE {
  38. protected:
  39. public:
  40. C_THISCLASS();
  41. virtual ~C_THISCLASS();
  42. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  43. virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_MISC_CUSTOM_BPM,desc,128):desc); }
  44. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  45. virtual void load_config(unsigned char *data, int len);
  46. virtual int save_config(unsigned char *data);
  47. void SliderStep(int Ctl, int *slide);
  48. int enabled;
  49. int arbitrary, skip, invert; // Type of action, adapt = detect beat
  50. int arbVal, skipVal; // Values of arbitrary beat and beat skip
  51. DWORD arbLastTC; // Last tick count used for arbitrary beat
  52. int skipCount; // Beat counter used by beat skipper
  53. int inInc, outInc; // +1/-1, Used by the nifty beatsynced sliders
  54. int inSlide, outSlide; // Positions of sliders
  55. int oldInSlide, oldOutSlide; // Used by timer to detect changes in sliders
  56. int skipfirst;
  57. int count;
  58. };
  59. static C_THISCLASS *g_ConfigThis; // global configuration dialog pointer
  60. static HINSTANCE g_hDllInstance; // global DLL instance pointer (not needed in this example, but could be useful)
  61. C_THISCLASS::~C_THISCLASS()
  62. {
  63. }
  64. // configuration read/write
  65. C_THISCLASS::C_THISCLASS() // set up default configuration
  66. {
  67. skipfirst=0;
  68. inSlide=0;
  69. outSlide=0;
  70. oldInSlide=0;
  71. oldOutSlide=0;
  72. enabled = 1;
  73. arbLastTC = GetTickCount();
  74. arbitrary=1;
  75. arbVal = 500;
  76. skipVal = 1;
  77. skipCount = 0;
  78. skip=0;
  79. invert=0;
  80. }
  81. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  82. void C_THISCLASS::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
  83. {
  84. int pos=0;
  85. if (len-pos >= 4) { enabled=GET_INT(); pos+=4; }
  86. if (len-pos >= 4) { arbitrary=GET_INT(); pos+=4; }
  87. if (len-pos >= 4) { skip=GET_INT(); pos+=4; }
  88. if (len-pos >= 4) { invert=GET_INT(); pos+=4; }
  89. if (len-pos >= 4) { arbVal=GET_INT(); pos+=4; }
  90. if (len-pos >= 4) { skipVal=GET_INT(); pos+=4; }
  91. if (len-pos >= 4) { skipfirst=GET_INT(); pos+=4; }
  92. }
  93. #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
  94. int C_THISCLASS::save_config(unsigned char *data) // write configuration to data, return length. config data should not exceed 64k.
  95. {
  96. int pos=0;
  97. PUT_INT(enabled); pos+=4;
  98. PUT_INT(arbitrary); pos+=4;
  99. PUT_INT(skip); pos+=4;
  100. PUT_INT(invert); pos+=4;
  101. PUT_INT(arbVal); pos+=4;
  102. PUT_INT(skipVal); pos+=4;
  103. PUT_INT(skipfirst); pos+=4;
  104. return pos;
  105. }
  106. void C_THISCLASS::SliderStep(int Ctl, int *slide)
  107. {
  108. *slide += Ctl == IDC_IN ? inInc : outInc;
  109. if (!*slide || *slide == 8) (Ctl == IDC_IN ? inInc : outInc) *= -1;
  110. }
  111. // render function
  112. // render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout. this is
  113. // used when you want to do something that you'd otherwise need to make a copy of the framebuffer.
  114. // w and h are the width and height of the screen, in pixels.
  115. // isBeat is 1 if a beat has been detected.
  116. // visdata is in the format of [spectrum:0,wave:1][channel][band].
  117. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  118. {
  119. if (!enabled) return 0;
  120. if (isBeat&0x80000000) return 0;
  121. if (isBeat) // Show the beat received from AVS
  122. {
  123. SliderStep(IDC_IN, &inSlide);
  124. count++;
  125. }
  126. if (skipfirst != 0 && count <= skipfirst)
  127. return isBeat ? CLR_BEAT : 0;
  128. if (arbitrary)
  129. {
  130. DWORD TCNow = GetTickCount();
  131. if (TCNow > arbLastTC + arbVal)
  132. {
  133. arbLastTC = TCNow;
  134. SliderStep(IDC_OUT, &outSlide);
  135. return SET_BEAT;
  136. }
  137. return CLR_BEAT;
  138. }
  139. if (skip)
  140. {
  141. if (isBeat && ++skipCount >= skipVal+1)
  142. {
  143. skipCount = 0;
  144. SliderStep(IDC_OUT, &outSlide);
  145. return SET_BEAT;
  146. }
  147. return CLR_BEAT;
  148. }
  149. if (invert)
  150. {
  151. if (isBeat)
  152. return CLR_BEAT;
  153. else
  154. {
  155. SliderStep(IDC_OUT, &outSlide);
  156. return SET_BEAT;
  157. }
  158. }
  159. return 0;
  160. }
  161. // configuration dialog stuff
  162. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  163. {
  164. switch (uMsg)
  165. {
  166. case WM_INITDIALOG:
  167. {
  168. char txt[40], beat[16], beats[16];
  169. g_ConfigThis->inInc = 1;
  170. g_ConfigThis->outInc = 1;
  171. g_ConfigThis->inSlide = 0;
  172. g_ConfigThis->outSlide = 0;
  173. if (g_ConfigThis->enabled) CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  174. if (g_ConfigThis->arbitrary) CheckDlgButton(hwndDlg,IDC_ARBITRARY,BST_CHECKED);
  175. if (g_ConfigThis->skip) CheckDlgButton(hwndDlg,IDC_SKIP,BST_CHECKED);
  176. if (g_ConfigThis->invert) CheckDlgButton(hwndDlg,IDC_INVERT,BST_CHECKED);
  177. SendDlgItemMessage(hwndDlg, IDC_ARBVAL, TBM_SETTICFREQ, 100, 0);
  178. SendDlgItemMessage(hwndDlg, IDC_SKIPVAL, TBM_SETTICFREQ, 1, 0);
  179. SendDlgItemMessage(hwndDlg, IDC_ARBVAL, TBM_SETRANGE, TRUE, MAKELONG(200, 10000));
  180. SendDlgItemMessage(hwndDlg, IDC_SKIPVAL, TBM_SETRANGE, TRUE, MAKELONG(1, 16));
  181. SendDlgItemMessage(hwndDlg, IDC_ARBVAL, TBM_SETPOS, TRUE, g_ConfigThis->arbVal);
  182. SendDlgItemMessage(hwndDlg, IDC_SKIPVAL, TBM_SETPOS, TRUE, g_ConfigThis->skipVal);
  183. SendDlgItemMessage(hwndDlg, IDC_IN, TBM_SETTICFREQ, 1, 0);
  184. SendDlgItemMessage(hwndDlg, IDC_IN, TBM_SETRANGE, TRUE, MAKELONG(0, 8));
  185. SendDlgItemMessage(hwndDlg, IDC_OUT, TBM_SETTICFREQ, 1, 0);
  186. SendDlgItemMessage(hwndDlg, IDC_OUT, TBM_SETRANGE, TRUE, MAKELONG(0, 8));
  187. SendDlgItemMessage(hwndDlg, IDC_SKIPFIRST, TBM_SETTICFREQ, 1, 0);
  188. SendDlgItemMessage(hwndDlg, IDC_SKIPFIRST, TBM_SETRANGE, TRUE, MAKELONG(0, 64));
  189. SendDlgItemMessage(hwndDlg, IDC_SKIPFIRST, TBM_SETPOS, TRUE, g_ConfigThis->skipfirst);
  190. wsprintf(txt, "%d bpm", 60000 / g_ConfigThis->arbVal);
  191. SetDlgItemText(hwndDlg, IDC_ARBTXT, txt);
  192. WASABI_API_LNGSTRING_BUF(IDS_BEAT,beat,16);
  193. WASABI_API_LNGSTRING_BUF(IDS_BEATS,beats,16);
  194. wsprintf(txt, "%d %s", g_ConfigThis->skipVal, g_ConfigThis->skipVal > 1 ? beats : beat);
  195. SetDlgItemText(hwndDlg, IDC_SKIPTXT, txt);
  196. wsprintf(txt, "%d %s", g_ConfigThis->skipfirst, g_ConfigThis->skipfirst > 1 ? beats : beat);
  197. SetDlgItemText(hwndDlg, IDC_SKIPFIRSTTXT, txt);
  198. SetTimer(hwndDlg, 0, 50, NULL);
  199. }
  200. return 1;
  201. case WM_TIMER:
  202. {
  203. if (g_ConfigThis->oldInSlide != g_ConfigThis->inSlide) {
  204. SendDlgItemMessage(hwndDlg, IDC_IN, TBM_SETPOS, TRUE, g_ConfigThis->inSlide); g_ConfigThis->oldInSlide=g_ConfigThis->inSlide; }
  205. if (g_ConfigThis->oldOutSlide != g_ConfigThis->outSlide) {
  206. SendDlgItemMessage(hwndDlg, IDC_OUT, TBM_SETPOS, TRUE, g_ConfigThis->outSlide); g_ConfigThis->oldOutSlide=g_ConfigThis->outSlide; }
  207. }
  208. return 0;
  209. case WM_NOTIFY:
  210. {
  211. char txt[40], beat[16], beats[16];
  212. if (LOWORD(wParam) == IDC_ARBVAL)
  213. g_ConfigThis->arbVal = SendDlgItemMessage(hwndDlg, IDC_ARBVAL, TBM_GETPOS, 0, 0);
  214. if (LOWORD(wParam) == IDC_SKIPVAL)
  215. g_ConfigThis->skipVal = SendDlgItemMessage(hwndDlg, IDC_SKIPVAL, TBM_GETPOS, 0, 0);
  216. if (LOWORD(wParam) == IDC_SKIPFIRST)
  217. g_ConfigThis->skipfirst = SendDlgItemMessage(hwndDlg, IDC_SKIPFIRST, TBM_GETPOS, 0, 0);
  218. wsprintf(txt, "%d bpm", 60000 / g_ConfigThis->arbVal);
  219. SetDlgItemText(hwndDlg, IDC_ARBTXT, txt);
  220. WASABI_API_LNGSTRING_BUF(IDS_BEAT,beat,16);
  221. WASABI_API_LNGSTRING_BUF(IDS_BEATS,beats,16);
  222. wsprintf(txt, "%d %s", g_ConfigThis->skipVal, g_ConfigThis->skipVal > 1 ? beats : beat);
  223. SetDlgItemText(hwndDlg, IDC_SKIPTXT, txt);
  224. wsprintf(txt, "%d %s", g_ConfigThis->skipfirst, g_ConfigThis->skipfirst > 1 ? beats : beat);
  225. SetDlgItemText(hwndDlg, IDC_SKIPFIRSTTXT, txt);
  226. return 0;
  227. }
  228. case WM_COMMAND:
  229. if ((LOWORD(wParam) == IDC_CHECK1) ||
  230. (LOWORD(wParam) == IDC_ARBITRARY) ||
  231. (LOWORD(wParam) == IDC_SKIP) ||
  232. (LOWORD(wParam) == IDC_INVERT))
  233. {
  234. g_ConfigThis->enabled=IsDlgButtonChecked(hwndDlg,IDC_CHECK1)?1:0;
  235. g_ConfigThis->arbitrary=IsDlgButtonChecked(hwndDlg,IDC_ARBITRARY)?1:0;
  236. g_ConfigThis->skip=IsDlgButtonChecked(hwndDlg,IDC_SKIP)?1:0;
  237. g_ConfigThis->invert=IsDlgButtonChecked(hwndDlg,IDC_INVERT)?1:0;
  238. }
  239. return 0;
  240. case WM_DESTROY:
  241. KillTimer(hwndDlg, 0);
  242. return 0;
  243. }
  244. return 0;
  245. }
  246. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
  247. {
  248. g_ConfigThis = this;
  249. return WASABI_API_CREATEDIALOG(IDD_CFG_BPM,hwndParent,g_DlgProc);
  250. }
  251. C_RBASE *R_Bpm(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  252. {
  253. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  254. return (C_RBASE *) new C_THISCLASS();
  255. }