mpegutil.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "DecodeThread.h"
  2. #include "main.h"
  3. // benski> cut some old shit
  4. // this code would take the filterbank coefficients to get an approximate spectrum
  5. // and modify the coefficients to do a quick&easy EQ
  6. // it's been off by default for quite a few versions (i think starting with 5.12)
  7. // but not I just yanked it out. cause none of us have 486SX's any more
  8. static float eq_lookup1[64]={
  9. 4.000000f,3.610166f,3.320019f,3.088821f,2.896617f,
  10. 2.732131f,2.588368f,2.460685f,2.345845f,2.241498f,
  11. 2.145887f,2.057660f,1.975760f,1.899338f,1.827707f,
  12. 1.760303f,1.696653f,1.636363f,1.579094f,1.524558f,
  13. 1.472507f,1.422724f,1.375019f,1.329225f,1.285197f,
  14. 1.242801f,1.201923f,1.162456f,1.124306f,1.087389f,
  15. 1.051628f,1.016951f,0.983296f,0.950604f,0.918821f,
  16. 0.887898f,0.857789f,0.828454f,0.799853f,0.771950f,
  17. 0.744712f,0.718108f,0.692110f,0.666689f,0.641822f,
  18. 0.617485f,0.593655f,0.570311f,0.547435f,0.525008f,
  19. 0.503013f,0.481433f,0.460253f,0.439458f,0.419035f,
  20. 0.398970f,0.379252f,0.359868f,0.340807f,0.322060f,
  21. 0.303614f,0.285462f,0.267593f,0.250000
  22. };
  23. static float eq_lookup2[64] = {
  24. 2.00000000f, 1.96825397f, 1.93650794f, 1.90476191f, 1.87301588f,
  25. 1.84126985f, 1.80952382f, 1.77777779f, 1.74603176f, 1.71428573f,
  26. 1.68253970f, 1.65079367f, 1.61904764f, 1.58730161f, 1.55555558f,
  27. 1.52380955f, 1.49206352f, 1.46031749f, 1.42857146f, 1.39682543f,
  28. 1.36507940f, 1.33333337f, 1.30158734f, 1.26984131f, 1.23809528f,
  29. 1.20634925f, 1.17460322f, 1.14285719f, 1.11111116f, 1.07936513f,
  30. 1.04761910f, 1.01587307f, 0.98412699f, 0.95238096f, 0.92063493f,
  31. 0.88888890f, 0.85714287f, 0.82539684f, 0.79365081f, 0.76190478f,
  32. 0.73015875f, 0.69841272f, 0.66666669f, 0.63492066f, 0.60317463f,
  33. 0.57142860f, 0.53968257f, 0.50793654f, 0.47619048f, 0.44444445f,
  34. 0.41269842f, 0.38095239f, 0.34920636f, 0.31746033f, 0.28571430f,
  35. 0.25396827f, 0.22222222f, 0.19047619f, 0.15873016f, 0.12698413f,
  36. 0.09523810f, 0.06349207f, 0.03174603f, 0.00000000
  37. };
  38. unsigned char eq_preamp = 0;
  39. unsigned char eq_enabled = 0;
  40. unsigned char eq_tab[10] = {0};
  41. float g_vis_table[2][2][32][18] = {0};
  42. void mp3GiveVisData(float vistable[2][32][18],int gr, int nch)
  43. {
  44. if (g_vis_enabled)
  45. {
  46. memcpy(&g_vis_table[gr][0][0][0],&vistable[0][0][0],sizeof(float)*32*18*nch);
  47. }
  48. }
  49. void mp2Equalize(float *xr, int nch, int srate, int nparts)
  50. {
  51. if (!g_eq_ok || !(mod.UsesOutputPlug & 2)) return;
  52. if (!eq_enabled) return;
  53. float *eq_lookup = (config_eqmode&1)?eq_lookup2:eq_lookup1;
  54. float preamp = eq_lookup[eq_preamp];
  55. int offs[11] = { 0,1,2,3,4,5,6,9,15,18,32};
  56. int scale_offs[11] = {0};
  57. int x;
  58. unsigned char eqt[12] = {0};
  59. memcpy(eqt+1,eq_tab,10);
  60. eqt[0]=eqt[1];
  61. eqt[11]=eqt[10];
  62. for (x = 0; x < 11; x ++)
  63. {
  64. scale_offs[x] = float_to_long( ((float) offs[x] / (float) srate * 44100.0));
  65. if (scale_offs[x] > 32) scale_offs[x] = 32;
  66. }
  67. {
  68. if (nch == 1)
  69. {
  70. register int i;
  71. for (i = 0; i < 10; i ++)
  72. {
  73. register int x=scale_offs[i];
  74. register int t=scale_offs[i+1];
  75. float d = eq_lookup[(int)eqt[i]]*preamp;
  76. float dd = (eq_lookup[(int)eqt[i+1]]*preamp-d)/(float)(t-x);
  77. if (dd < 0.000000001f && dd > -0.000000001f) dd = 0.000000001f;
  78. while (x < t)
  79. {
  80. register float *p = xr+x;
  81. int e=nparts;
  82. while (e--)
  83. {
  84. *(p) *= d;
  85. p+=32;
  86. }
  87. p += 32*(18-nparts);
  88. d += dd;
  89. x++;
  90. }
  91. }
  92. }
  93. else
  94. {
  95. register int i;
  96. for (i = 0; i < 10; i ++)
  97. {
  98. register int x=scale_offs[i];
  99. register int t=scale_offs[i+1];
  100. float d = eq_lookup[(int)eqt[i]]*preamp;
  101. float dd = (eq_lookup[(int)eqt[i+1]]*preamp-d)/(float)(t-x);
  102. if (dd < 0.000000001f && dd > -0.000000001f) dd = 0.000000001f;
  103. while (x < t)
  104. {
  105. register float *p = xr+x;
  106. int e=nparts;
  107. while (e--)
  108. {
  109. *(p+32*18) *= d;
  110. *(p) *= d;
  111. p+=32;
  112. }
  113. p += 32*(18-nparts);
  114. d += dd;
  115. x++;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. void mp3Equalize(float *xr, int nch, int srate)
  122. {
  123. if (!g_eq_ok || !(mod.UsesOutputPlug & 2)) return;
  124. if (!eq_enabled) return;
  125. float *eq_lookup = (config_eqmode&1)?eq_lookup2:eq_lookup1;
  126. float preamp = eq_lookup[eq_preamp];
  127. static int scale_offs[11];
  128. static int lrate;
  129. unsigned char eqt[12] = {0};
  130. memcpy(eqt+1,eq_tab,10);
  131. eqt[0]=eqt[1];
  132. eqt[11]=eqt[10];
  133. if (lrate!=srate)
  134. {
  135. lrate=srate;
  136. for (int x = 0; x < 11; x ++)
  137. {
  138. int offs[11] = { 0,2,4, 9,16, 30, 63, 95,153,308, 576};
  139. scale_offs[x] = float_to_long( ((float) offs[x] / (float) srate * 44100.0));
  140. if (scale_offs[x] > 576) scale_offs[x] = 576;
  141. }
  142. }
  143. {
  144. if (nch == 1)
  145. {
  146. register float *p = xr;
  147. register float d = eq_lookup[eqt[0]]*preamp;
  148. register int i = 0;
  149. for (i = 0; i < 10; i ++)
  150. {
  151. register int x=scale_offs[i];
  152. register int t=scale_offs[i+1];
  153. register float dd = (eq_lookup[eqt[i+1]]*preamp-d)/(float)(t-x);
  154. while (x++ < t)
  155. {
  156. *(p++) *= d;
  157. d += dd;
  158. }
  159. }
  160. }
  161. else
  162. {
  163. register float *p = xr;
  164. register int i;
  165. for (i = 0; i < 10; i ++)
  166. {
  167. register int x=scale_offs[i];
  168. register int t=scale_offs[i+1];
  169. float d = eq_lookup[(int)eqt[i]]*preamp;
  170. float dd = (eq_lookup[(int)eqt[i+1]]*preamp-d)/(float)(t-x);
  171. if (dd < 0.000000001f && dd > -0.000000001f) dd = 0.000000001f;
  172. while (x++ < t)
  173. {
  174. *(p+32*18) *= d;
  175. *(p++) *= d;
  176. d += dd;
  177. }
  178. }
  179. }
  180. }
  181. }
  182. void genOsc(char *tempdata, short *samples, int len)
  183. {
  184. float d = 0.0f, dd = len/(75.0f*2.0f);
  185. short int *sbuf = samples;
  186. int x,y=0;
  187. signed char *c = (signed char *) tempdata;
  188. for (x = 0; x < 75; x ++)
  189. {
  190. float q=0;
  191. int td = float_to_long((d+=dd));
  192. for (; y < td; y ++)
  193. q += *sbuf++;
  194. q *= (32.0f/(dd*65536.0f));
  195. *c++ = (signed char) float_to_long(q);
  196. }
  197. }
  198. void genSpec(char *tempdata, float *xr, int nch)
  199. {
  200. static int offsets[76] =
  201. {
  202. 0,1,2,3,4,5,7,8,9,10,12,13,15,16,18,20,21,23,25,27,29,31,33,36,38,41,43,46,48,51,
  203. 54,57,60,64,67,71,74,78,82,86,91,95,100,105,109,115,120,126,131,137,144,150,157,
  204. 164,171,178,186,194,203,211,220,230,239,250,260,271,282,294,306,319,332,345,360,
  205. 374,389,576
  206. };
  207. {
  208. for (int x = 0; x < 75; x++)
  209. {
  210. float sum = 0.0;
  211. int z;
  212. int sx = offsets[x];
  213. int ex = offsets[x+1];
  214. if (nch == 2)
  215. {
  216. float *p = &xr[0];
  217. int w=32*18;
  218. for (z = sx; z < ex; z ++)
  219. {
  220. register float t1=p[z], t2=p[w+z];
  221. if (t1 <0.0) t1=-t1;
  222. if (t2<0.0f) t2=-t2;
  223. sum += (t1+t2) * 0.5f;
  224. }
  225. }
  226. else
  227. {
  228. float *p = &xr[0];
  229. for (z = sx; z < ex; z ++)
  230. {
  231. register float t=p[z];
  232. if (t < 0.0) t=-t;
  233. sum += t;
  234. }
  235. }
  236. sum *= 1.0f + (x) * 12.0f / (ex-sx) ;
  237. sum *= 1.8f/24000.0f;
  238. if (sum < 0.0) sum = 0.0;
  239. if (sum > 255.0) sum = 255.0;
  240. tempdata[x] = (unsigned char) float_to_long(sum);
  241. }
  242. }
  243. }
  244. void do_layer3_vis(short *samples, float *xr, int nch, int ts)
  245. {
  246. int vis_waveNch;
  247. int vis_specNch;
  248. int csa = mod.SAGetMode();
  249. int is_vis_running = mod.VSAGetMode(&vis_specNch,&vis_waveNch);
  250. static char tempdata[75*2];
  251. int len=32*18*nch;
  252. if (is_vis_running)
  253. {
  254. char data[576*4] = {0};
  255. int data_offs=0;
  256. int x;
  257. if (nch == 1 && vis_specNch > 0)
  258. {
  259. float *specdata = xr;
  260. int y;
  261. for (y=0; y < 576; y++)
  262. {
  263. float p = *specdata++ / 24.0f;
  264. if (p < 0.0) p = -p;
  265. if (p > 255.0) p = 255.0;
  266. data[data_offs++] = (unsigned char) float_to_long(p);
  267. }
  268. if (vis_specNch == 2)
  269. {
  270. memcpy(data+data_offs,data+data_offs-576,576);
  271. data_offs += 576;
  272. }
  273. }
  274. else if (vis_specNch == 2)
  275. {
  276. for (x = 0; x < 2; x ++)
  277. {
  278. float *specdata = &xr[x*32*18];
  279. for (int y=0; y < 576; y++)
  280. {
  281. float p = *specdata++ / 24.0f;
  282. if (p < 0.0) p = -p;
  283. if (p > 255.0) p = 255.0;
  284. data[data_offs++] = (unsigned char) float_to_long(p);
  285. }
  286. }
  287. }
  288. else if (vis_specNch == 1)
  289. {
  290. float *specdata = &xr[0];
  291. int y;
  292. for (y = 0; y < 576; y++)
  293. {
  294. register float p1=specdata[0],p2=specdata[32*18],p;
  295. if (p1 < 0.0) p1=-p1;
  296. if (p2 < 0.0) p2=-p2;
  297. p = (p1+p2)/ 48.0f;
  298. specdata++;
  299. if (p > 255.0) p = 255.0;
  300. data[data_offs++] = (unsigned char) float_to_long(p);
  301. }
  302. } // end of spectrum code
  303. if (nch == 1 && vis_waveNch > 0)
  304. {
  305. for (x = 0; x < 576; x++)
  306. {
  307. data[data_offs++] = ((samples[x])>>8);
  308. }
  309. if (vis_waveNch == 2)
  310. {
  311. memcpy(data+data_offs,data+data_offs-576,576);
  312. data_offs += 576;
  313. }
  314. }
  315. else if (vis_waveNch == 2)
  316. {
  317. for (x = 0; x < 2; x ++)
  318. {
  319. int y;
  320. for (y = 0; y < 576; y ++ )
  321. {
  322. data[data_offs++] = ((samples[y*2+x])>>8);
  323. }
  324. }
  325. }
  326. else if (vis_waveNch == 1)
  327. {
  328. int x;
  329. for (x = 0; x < 576; x ++)
  330. {
  331. data[data_offs++] = ((int)samples[x*2]+(int)samples[x*2+1])>>9;
  332. }
  333. }
  334. mod.VSAAdd(data,ts);
  335. }
  336. if (csa==4)
  337. {
  338. tempdata[0] = 0;
  339. tempdata[1] = 0;
  340. mod.SAAdd(tempdata,ts,4);
  341. }
  342. else if (csa == 3)
  343. {
  344. genSpec(tempdata,xr,nch);
  345. genOsc(tempdata+75,samples,len);
  346. mod.SAAdd(tempdata,ts,0x80000003);
  347. }
  348. else if (csa == 2)
  349. {
  350. genOsc(tempdata,samples,len);
  351. mod.SAAdd(tempdata,ts,2);
  352. }
  353. else if (csa == 1) {
  354. genSpec(tempdata,xr,nch);
  355. mod.SAAdd(tempdata,ts,1);
  356. }
  357. }