1
0

seqvis.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include <precomp.h>
  2. #include <api/wnd/popup.h>
  3. #include <math.h>
  4. #include <api/skin/skinparse.h>
  5. #include <api/service/svc_enum.h>
  6. #include <api/service/svcs/svc_skinfilter.h>
  7. #include <api/skin/widgets/seqvis.h>
  8. #include <api/core/api_core.h>
  9. #include <tataki/canvas/bltcanvas.h>
  10. const wchar_t eqVisXuiStr[] = L"EQVis"; // This is the xml tag
  11. char eqVisXuiSvcName[] = "EQVis xui object"; // this is the name of the xuiservice
  12. XMLParamPair SEQVis::params[] =
  13. {
  14. {
  15. SEQVIS_SETCOLORBOTTOM, L"COLORBOTTOM"
  16. },
  17. {SEQVIS_SETCOLORMIDDLE, L"COLORMIDDLE"},
  18. {SEQVIS_SETCOLORPREAMP, L"COLORPREAMP"},
  19. {SEQVIS_SETCOLORTOP, L"COLORTOP"},
  20. {SEQVIS_SETALPHA, L"GAMMA"}, // BACKWARD COMPAT
  21. };
  22. SEQVis::SEQVis()
  23. {
  24. getScriptObject()->vcpu_setInterface(eqvisGuid, (void *)static_cast<SEQVis *>(this));
  25. getScriptObject()->vcpu_setClassName(L"EqVis");
  26. getScriptObject()->vcpu_setController(eqvisController);
  27. colortop = colormid = colorbottom = 0xffffff;
  28. colorpreamp = 0x888888;
  29. shadedColors = NULL;
  30. bc = NULL;
  31. sfe = new SkinFilterEnum();
  32. while (1)
  33. {
  34. svc_skinFilter *obj = sfe->getNext();
  35. if (!obj) break;
  36. filters.addItem(obj);
  37. }
  38. xuihandle = newXuiHandle();
  39. CreateXMLParameters(xuihandle);
  40. }
  41. void SEQVis::CreateXMLParameters(int master_handle)
  42. {
  43. //SEQVIS_PARENT::CreateXMLParameters(master_handle);
  44. int numParams = sizeof(params) / sizeof(params[0]);
  45. hintNumberOfParams(xuihandle, numParams);
  46. for (int i = 0;i < numParams;i++)
  47. addParam(xuihandle, params[i], XUI_ATTRIBUTE_IMPLIED);
  48. }
  49. int SEQVis::onInit()
  50. {
  51. SEQVIS_PARENT::onInit();
  52. WASABI_API_MEDIACORE->core_addCallback(0, this);
  53. return 1;
  54. }
  55. SEQVis::~SEQVis()
  56. {
  57. foreach(filters)
  58. sfe->release(filters.getfor());
  59. endfor;
  60. delete sfe;
  61. if (shadedColors) FREE(shadedColors);
  62. delete(bc);
  63. WASABI_API_MEDIACORE->core_delCallback(0, this);
  64. }
  65. int SEQVis::setXuiParam(int _xuihandle, int attrid, const wchar_t *paramname, const wchar_t *strvalue)
  66. {
  67. if (_xuihandle != xuihandle) return SEQVIS_PARENT::setXuiParam(_xuihandle, attrid, paramname, strvalue);
  68. switch (attrid)
  69. {
  70. case SEQVIS_SETALPHA:
  71. getGuiObject()->guiobject_setAlpha(WTOI(strvalue));
  72. break;
  73. case SEQVIS_SETCOLORTOP:
  74. colortop = RGBTOBGR(SkinParser::parseColor(strvalue));
  75. break;
  76. case SEQVIS_SETCOLORMIDDLE:
  77. colormid = RGBTOBGR(SkinParser::parseColor(strvalue));
  78. break;
  79. case SEQVIS_SETCOLORBOTTOM:
  80. colorbottom = RGBTOBGR(SkinParser::parseColor(strvalue));
  81. break;
  82. case SEQVIS_SETCOLORPREAMP:
  83. colorpreamp = RGBTOBGR(SkinParser::parseColor(strvalue));
  84. break;
  85. default:
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. void SEQVis::splineGetPoint(spline_struct *s, float frame, float *out)
  91. {
  92. int i, i_1, i0, i1, i2;
  93. float time1, time2, time3;
  94. float t1, t2, t3, t4, u1, u2, u3, u4, v1, v2, v3;
  95. float a, b, c, d;
  96. float *keys = s->keys;
  97. a = (1 - s->tens) * (1 + s->cont) * (1 + s->bias);
  98. b = (1 - s->tens) * (1 - s->cont) * (1 - s->bias);
  99. c = (1 - s->tens) * (1 - s->cont) * (1 + s->bias);
  100. d = (1 - s->tens) * (1 + s->cont) * (1 - s->bias);
  101. v1 = t1 = -a / 2.0f; u1 = a;
  102. u2 = (-6 - 2 * a + 2 * b + c) / 2.0f; v2 = (a - b) / 2.0f; t2 = (4 + a - b - c) / 2.0f;
  103. t3 = (-4 + b + c - d) / 2.0f;
  104. u3 = (6 - 2 * b - c + d) / 2.0f;
  105. v3 = b / 2.0f;
  106. t4 = d / 2.0f; u4 = -t4;
  107. i0 = (int) frame;
  108. i_1 = i0 - 1;
  109. while (i_1 < 0) i_1 += s->numKeys;
  110. i1 = i0 + 1;
  111. while (i1 >= s->numKeys) i1 -= s->numKeys;
  112. i2 = i0 + 2;
  113. while (i2 >= s->numKeys) i2 -= s->numKeys;
  114. time1 = frame - (float)((int) frame);
  115. time2 = time1 * time1;
  116. time3 = time2 * time1;
  117. i0 *= s->keyWidth;
  118. i1 *= s->keyWidth;
  119. i2 *= s->keyWidth;
  120. i_1 *= s->keyWidth;
  121. for (i = 0; i < s->keyWidth; i ++)
  122. {
  123. a = t1 * keys[i + i_1] + t2 * keys[i + i0] + t3 * keys[i + i1] + t4 * keys[i + i2];
  124. b = u1 * keys[i + i_1] + u2 * keys[i + i0] + u3 * keys[i + i1] + u4 * keys[i + i2];
  125. c = v1 * keys[i + i_1] + v2 * keys[i + i0] + v3 * keys[i + i1];
  126. *out++ = a * time3 + b * time2 + c * time1 + keys[i + i0];
  127. }
  128. }
  129. void SEQVis::DrawEQVis()
  130. {
  131. if (!shadedColors) return ;
  132. float keys[12] = {0};
  133. spline_struct spline = {keys, 1, 12, 0.0f, 0.0f, 0.1f};
  134. MEMSET(specData, 0, cur_w*cur_h*4);
  135. int ph = (int)((127 + WASABI_API_MEDIACORE->core_getEqPreamp(0)) * ((float)cur_h) / 256.0f);
  136. ph *= cur_w;
  137. for (int j = 0;j < cur_w;j++)
  138. specData[j + ph] = colorpreamp | 0xFF000000; // alpha :)
  139. {
  140. int x;
  141. int last_p = -1;
  142. for (x = 0; x < 10; x ++)
  143. keys[x + 1] = (127 - WASABI_API_MEDIACORE->core_getEqBand(0, x)) * ((float)cur_h) / 256.0f;
  144. keys[0] = keys[1];
  145. keys[11] = keys[10];
  146. for (x = 0; x < cur_w; x ++)
  147. {
  148. float p;
  149. int this_p;
  150. splineGetPoint(&spline, 1.0f + x / (cur_w*11.0f / 100.0f), &p);
  151. this_p = (int)p;
  152. if (this_p < 0) this_p = 0;
  153. if (this_p >= cur_h) this_p = cur_h - 1;
  154. if (last_p == -1 || this_p == last_p)
  155. specData[x + this_p*cur_w] = shadedColors[this_p];
  156. else
  157. {
  158. if (this_p < last_p)
  159. for (int j = 0;j < last_p - this_p + 1;j++)
  160. specData[x + (this_p + j)*cur_w] = shadedColors[this_p + j];
  161. else if (this_p > last_p)
  162. for (int j = 0;j < this_p - last_p + 1;j++)
  163. specData[x + (last_p + j)*cur_w] = shadedColors[last_p + j];
  164. }
  165. last_p = this_p;
  166. }
  167. }
  168. invalidate();
  169. invalidated = 1; // rerun filter
  170. }
  171. int SEQVis::onPaint(Canvas *canvas)
  172. {
  173. PaintCanvas paintcanvas;
  174. if (canvas == NULL)
  175. {
  176. if (!paintcanvas.beginPaint(this)) return 0;
  177. canvas = &paintcanvas;
  178. }
  179. SEQVIS_PARENT::onPaint(canvas);
  180. RECT s, r;
  181. getClientRect(&r);
  182. s.left = 0;
  183. s.top = 0;
  184. s.right = cur_w;
  185. s.bottom = cur_h;
  186. if (invalidated)
  187. {
  188. SkinBitmap *b = bc->getSkinBitmap();
  189. foreach(filters)
  190. filters.getfor()->filterBitmap((unsigned char *)bc->getBits(), b->getWidth(), b->getHeight(), 32, NULL, L"Vis/Eq");
  191. endfor;
  192. invalidated = 0;
  193. b->stretchToRectAlpha(canvas, &s, &r, getPaintingAlpha());
  194. }
  195. else
  196. {
  197. bc->stretchToRectAlpha(canvas, &s, &r, getPaintingAlpha());
  198. //SkinBitmap *b = bc->getSkinBitmap();
  199. //b->stretchToRectAlpha(canvas, &s, &r, getPaintingAlpha());
  200. }
  201. return 1;
  202. }
  203. int SEQVis::corecb_onEQBandChange(int b, int newval)
  204. {
  205. DrawEQVis();
  206. return 0;
  207. }
  208. int SEQVis::corecb_onEQPreampChange(int newval)
  209. {
  210. DrawEQVis();
  211. return 0;
  212. }
  213. void SEQVis::reloadResources()
  214. {
  215. SEQVIS_PARENT::reloadResources();
  216. DrawEQVis();
  217. invalidate();
  218. }
  219. int SEQVis::onResize()
  220. {
  221. SEQVIS_PARENT::onResize();
  222. RECT r;
  223. getClientRect(&r);
  224. cur_h = r.bottom - r.top;
  225. if (cur_h <= 0) cur_h = 1;
  226. cur_w = r.right - r.left;
  227. if (cur_w <= 0) cur_w = 1;
  228. delete bc;
  229. bc = new BltCanvas(cur_w, -cur_h);
  230. specData = (int *)bc->getBits();
  231. MEMSET(specData, 0, cur_w*cur_h*4);
  232. if (shadedColors) FREE(shadedColors);
  233. shadedColors = (int *)MALLOC(sizeof(int) * cur_h);
  234. int r1 = colortop & 0xff0000;
  235. int g1 = (colortop & 0x00ff00) << 8;
  236. int b1 = (colortop & 0x0000ff) << 16;
  237. int r2 = colormid & 0xff0000;
  238. int g2 = (colormid & 0x00ff00) << 8;
  239. int b2 = (colormid & 0x0000ff) << 16;
  240. int r3 = colorbottom & 0xff0000;
  241. int g3 = (colorbottom & 0x00ff00) << 8;
  242. int b3 = (colorbottom & 0x0000ff) << 16;
  243. int l = cur_h / 2;
  244. if (!l) l = 1;
  245. int i;
  246. for (i = 0;i < l;i++)
  247. {
  248. int r = r1 + ((r2 - r1) * i / l);
  249. int g = g1 + ((g2 - g1) * i / l);
  250. int b = b1 + ((b2 - b1) * i / l);
  251. shadedColors[i] = (r & 0xff0000);
  252. shadedColors[i] += ((g & 0xff0000) >> 8);
  253. shadedColors[i] += ((b & 0xff0000) >> 16);
  254. shadedColors[i] += 0xff000000; // alpha?
  255. }
  256. for (i = l;i < cur_h;i++)
  257. {
  258. int r = r2 + ((r3 - r2) * (i - l) / l);
  259. int g = g2 + ((g3 - g2) * (i - l) / l);
  260. int b = b2 + ((b3 - b2) * (i - l) / l);
  261. shadedColors[i] = (r & 0xff0000);
  262. shadedColors[i] += ((g & 0xff0000) >> 8);
  263. shadedColors[i] += ((b & 0xff0000) >> 16);
  264. shadedColors[i] += 0xff000000; // alpha?
  265. }
  266. DrawEQVis();
  267. return 1;
  268. }
  269. EqVisScriptController _eqvisController;
  270. EqVisScriptController *eqvisController = &_eqvisController;
  271. // -- Functions table -------------------------------------
  272. function_descriptor_struct EqVisScriptController::exportedFunction[] = {
  273. {L"fake", 0, (void*)SEQVis::script_vcpu_fake },
  274. };
  275. // --------------------------------------------------------
  276. const wchar_t *EqVisScriptController::getClassName()
  277. {
  278. return L"EqVis";
  279. }
  280. const wchar_t *EqVisScriptController::getAncestorClassName()
  281. {
  282. return L"GuiObject";
  283. }
  284. ScriptObject *EqVisScriptController::instantiate()
  285. {
  286. SEQVis *eqv = new SEQVis;
  287. ASSERT(eqv != NULL);
  288. return eqv->getScriptObject();
  289. }
  290. void EqVisScriptController::destroy(ScriptObject *o)
  291. {
  292. SEQVis *eqv = static_cast<SEQVis *>(o->vcpu_getInterface(eqvisGuid));
  293. ASSERT(eqv != NULL);
  294. delete eqv;
  295. }
  296. void *EqVisScriptController::encapsulate(ScriptObject *o)
  297. {
  298. return NULL; // no encapsulation for eqvis yet
  299. }
  300. void EqVisScriptController::deencapsulate(void *o)
  301. {}
  302. int EqVisScriptController::getNumFunctions()
  303. {
  304. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  305. }
  306. const function_descriptor_struct *EqVisScriptController::getExportedFunctions()
  307. {
  308. return exportedFunction;
  309. }
  310. GUID EqVisScriptController::getClassGuid()
  311. {
  312. return eqvisGuid;
  313. }
  314. // -----------------------------------------------------------------------
  315. scriptVar SEQVis::script_vcpu_fake(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  316. {
  317. SCRIPT_FUNCTION_INIT
  318. RETURN_SCRIPT_VOID;
  319. }