dsp_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // Winamp test dsp library 0.9 for Winamp 2
  2. // Copyright (C) 1997, Justin Frankel/Nullsoft
  3. // Feel free to base any plugins on this "framework"...
  4. #include <windows.h>
  5. #include <commctrl.h>
  6. #include <math.h>
  7. #include "../Winamp/dsp.h"
  8. #include "resource.h"
  9. // avoid stupid CRT silliness
  10. //BOOL WINAPI _DllMainCRTStartup(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
  11. //{
  12. // return TRUE;
  13. //}
  14. // pitch value
  15. int g_pitch=100;
  16. // pitch control window
  17. HWND pitch_control_hwnd;
  18. // auxilary pitch buffer (for resampling from)
  19. short *pitch_buffer=NULL;
  20. int pitch_buffer_len=0;
  21. int quit_pitch=0;
  22. // module getter.
  23. winampDSPModule *getModule(int which);
  24. void config(struct winampDSPModule *this_mod);
  25. int init(struct winampDSPModule *this_mod);
  26. int initpitch(struct winampDSPModule *this_mod);
  27. void quit(struct winampDSPModule *this_mod);
  28. void quitpitch(struct winampDSPModule *this_mod);
  29. int modify_samples1(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate);
  30. int modify_samples2(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate);
  31. int modify_samples3(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate);
  32. int modify_samples_lopass(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate);
  33. int modify_samples_hipass(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate);
  34. static INT_PTR CALLBACK pitchProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam);
  35. // Module header, includes version, description, and address of the module retriever function
  36. typedef struct {
  37. int version; // DSP_HDRVER
  38. char *description; // description of library
  39. winampDSPModule* (*getModule)(int); // module retrieval function
  40. int (*sf)(int);
  41. } winampDSPHeaderEx;
  42. int sf(int v)
  43. {
  44. int res;
  45. res = v * (unsigned long)1103515245;
  46. res += (unsigned long)13293;
  47. res &= (unsigned long)0x7FFFFFFF;
  48. res ^= v;
  49. return res;
  50. }
  51. winampDSPHeaderEx hdr = { DSP_HDRVER+1, "Nullsoft DSP v0.35 for Winamp 2ARG", getModule, sf };
  52. // first module
  53. winampDSPModule mod =
  54. {
  55. "Nullsoft Echo v0.2",
  56. NULL, // hwndParent
  57. NULL, // hDllInstance
  58. config,
  59. init,
  60. modify_samples1,
  61. quit
  62. };
  63. // second module
  64. winampDSPModule mod2 =
  65. {
  66. "Nullsoft Stereo Voice Removal v0.2",
  67. NULL, // hwndParent
  68. NULL, // hDllInstance
  69. config,
  70. init,
  71. modify_samples2,
  72. quit
  73. };
  74. winampDSPModule mod3 =
  75. {
  76. "Nullsoft Pitch/Tempo Control v0.2",
  77. NULL, // hwndParent
  78. NULL, // hDllInstance
  79. config,
  80. initpitch,
  81. modify_samples3,
  82. quitpitch
  83. };
  84. winampDSPModule mod4 =
  85. {
  86. "Nullsoft Lowpass Filter v1.0",
  87. NULL, // hwndParent
  88. NULL, // hDllInstance
  89. config,
  90. init,
  91. modify_samples_lopass,
  92. quit
  93. };
  94. winampDSPModule mod5 =
  95. {
  96. "Nullsoft Highpass Filter v1.0",
  97. NULL, // hwndParent
  98. NULL, // hDllInstance
  99. config,
  100. init,
  101. modify_samples_hipass,
  102. quit
  103. };
  104. #ifdef __cplusplus
  105. extern "C" {
  106. #endif
  107. // this is the only exported symbol. returns our main header.
  108. __declspec( dllexport ) winampDSPHeaderEx *winampDSPGetHeader2()
  109. {
  110. return &hdr;
  111. }
  112. #ifdef __cplusplus
  113. }
  114. #endif
  115. // getmodule routine from the main header. Returns NULL if an invalid module was requested,
  116. // otherwise returns either mod1 or mod2 depending on 'which'.
  117. winampDSPModule *getModule(int which)
  118. {
  119. switch (which)
  120. {
  121. case 0: return &mod;
  122. case 1: return &mod2;
  123. case 2: return &mod3;
  124. // case 3: return &mod4;
  125. // case 4: return &mod5;
  126. default:return NULL;
  127. }
  128. }
  129. // configuration. Passed this_mod, as a "this" parameter. Allows you to make one configuration
  130. // function that shares code for all your modules (you don't HAVE to use it though, you can make
  131. // config1(), config2(), etc...)
  132. void config(struct winampDSPModule *this_mod)
  133. {
  134. MessageBox(this_mod->hwndParent,"This module is Copyright(C) 1997-1999, Nullsoft\n"
  135. "Notes:\n"
  136. " * 8 bit samples aren't supported.\n"
  137. " * Pitch control rules!\n"
  138. " * Voice removal sucks (works about 10% of the time)!\n"
  139. " * Echo isn't very good!\n"
  140. "etc... this is really just a test of the new\n"
  141. "DSP plug-in system. Nothing more.",
  142. "Configuration",MB_OK);
  143. }
  144. int init(struct winampDSPModule *this_mod)
  145. {
  146. return 0;
  147. }
  148. int initpitch(struct winampDSPModule *this_mod)
  149. {
  150. pitch_buffer_len=0;
  151. pitch_buffer=NULL;
  152. quit_pitch=0;
  153. ShowWindow((pitch_control_hwnd=CreateDialog(this_mod->hDllInstance,MAKEINTRESOURCE(IDD_DIALOG1),this_mod->hwndParent,pitchProc)),SW_SHOW);
  154. return 0;
  155. }
  156. // cleanup (opposite of init()). Destroys the window, unregisters the window class
  157. void quit(struct winampDSPModule *this_mod)
  158. {
  159. }
  160. void quitpitch(struct winampDSPModule *this_mod)
  161. {
  162. if (this_mod == &mod3)
  163. {
  164. if (pitch_buffer) GlobalFree(pitch_buffer);
  165. pitch_buffer_len=0;
  166. pitch_buffer=NULL;
  167. quit_pitch=1;
  168. if (pitch_control_hwnd)
  169. {
  170. DestroyWindow(pitch_control_hwnd);
  171. pitch_control_hwnd=0;
  172. }
  173. }
  174. }
  175. short echo_buf[65536], echo_buf2[65536];
  176. int modify_samples1(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate)
  177. {
  178. // echo doesn't support 8 bit right now cause I'm lazy.
  179. if (bps==16)
  180. {
  181. int x,s;
  182. s = numsamples*nch;
  183. memcpy(echo_buf2, echo_buf, s*2);
  184. memcpy(echo_buf, echo_buf+s, s*2);
  185. memcpy(echo_buf+s, echo_buf+s*2, s*2);
  186. memcpy(echo_buf+s*2,echo_buf+s*3, s*2);
  187. memcpy(echo_buf+s*3,samples, s*2);
  188. for (x = 0; x < s; x ++)
  189. {
  190. int s = samples[x]/2+echo_buf2[x]/2;
  191. samples[x] = (s>32767?32767:s<-32768?-32768:s);
  192. }
  193. }
  194. return numsamples;
  195. }
  196. int modify_samples3(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate)
  197. {
  198. int pitch=g_pitch;
  199. int rlen =numsamples*bps/8*nch;
  200. int index=0, x;
  201. int n;
  202. int dindex;
  203. if (quit_pitch || g_pitch==100) return numsamples;
  204. if (g_pitch > 200) g_pitch=200;
  205. if (g_pitch < 50) g_pitch=50;
  206. pitch = 100000/pitch;
  207. n=(numsamples*pitch)/1000;
  208. dindex=(numsamples<<11)/n;
  209. if (pitch_buffer_len < rlen)
  210. {
  211. pitch_buffer_len = rlen;
  212. GlobalFree(pitch_buffer);
  213. pitch_buffer=GlobalAlloc(GMEM_FIXED,rlen);
  214. }
  215. if (bps == 16 && nch == 2)
  216. {
  217. short *buf=pitch_buffer;
  218. memcpy(buf,samples,rlen);
  219. for (x = 0; x < n; x ++)
  220. {
  221. int p=(index>>11)<<1;
  222. index+=dindex;
  223. samples[0] = buf[p];
  224. samples[1] = buf[p+1];
  225. samples+=2;
  226. }
  227. return n;
  228. }
  229. else if (bps == 16 && nch == 1)
  230. {
  231. short *buf=pitch_buffer;
  232. memcpy(buf,samples,rlen);
  233. for (x = 0; x < n; x ++)
  234. {
  235. int p=(index>>11);
  236. index+=dindex;
  237. *samples++ = buf[p];
  238. }
  239. return n;
  240. }
  241. return numsamples;
  242. }
  243. int modify_samples2(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate)
  244. {
  245. int x = numsamples;
  246. if (bps == 16)
  247. {
  248. short *a = samples;
  249. if (nch == 2) while (x--)
  250. {
  251. int l, r;
  252. l = a[1]-a[0];
  253. r = a[0]-a[1];
  254. if (l < -32768) l = -32768;
  255. if (l > 32767) l = 32767;
  256. if (r < -32768) r = -32768;
  257. if (r > 32767) r = 32767;
  258. a[0] = l;
  259. a[1] = r;
  260. a+=2;
  261. }
  262. }
  263. return numsamples;
  264. }
  265. /*
  266. int modify_samples_lopass(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate)
  267. {
  268. if (bps==16)
  269. {
  270. static int lastspl=0,lastspl2=0;
  271. int x;
  272. x=numsamples;
  273. if (nch==1) while (x--)
  274. {
  275. int thisspl=*samples;
  276. *samples++=(lastspl+thisspl)/2;
  277. lastspl=thisspl;
  278. }
  279. else if (nch == 2) while (x--)
  280. {
  281. int thisspl=*samples;
  282. *samples++=(lastspl+thisspl)/2;
  283. lastspl=thisspl;
  284. thisspl=*samples;
  285. *samples++=(lastspl2+thisspl)/2;
  286. lastspl2=thisspl;
  287. }
  288. }
  289. return numsamples;
  290. }
  291. */
  292. enum FILTER_TYPE {
  293. lowpass,highpass,bandpass
  294. };
  295. typedef struct
  296. {
  297. float m_a0,m_a1;
  298. float m_b0,m_b1,m_b2;
  299. float m_d1,m_d2;
  300. float m_k;
  301. } filter;
  302. float Filter(filter *f, const float x )
  303. {
  304. float d0,y;
  305. d0 = f->m_k*x - f->m_a1*f->m_d1 - f->m_a0*f->m_d2;
  306. y = d0*f->m_b2 + f->m_d1*f->m_b1 + f->m_d2*f->m_b0;
  307. f->m_d2 = f->m_d1;
  308. f->m_d1 = d0;
  309. return y;
  310. }
  311. void makefilter( filter *f, int t , float sample_rate , float cutoff , float dampening )
  312. {
  313. float a2,c;
  314. c = (float)( 1.f / tan( 3.14159265359*cutoff / sample_rate ) );
  315. a2 = 1.f + c*(c+dampening);
  316. f->m_a1 = 2.f * (1.f - c*c) / a2;
  317. f->m_a0 = (1.f + c*(c-dampening)) / a2;
  318. f->m_d1 = f->m_d2 = 0.f;
  319. switch( t )
  320. {
  321. case lowpass:
  322. f->m_k = 1.f / a2;
  323. f->m_b1 = 2.f;
  324. f->m_b0 = 1.f;
  325. break;
  326. case highpass:
  327. f->m_k = c*c / a2;
  328. f->m_b1 = -2.f;
  329. f->m_b0 = 1.f;
  330. break;
  331. case bandpass:
  332. f->m_k = c*dampening / a2;
  333. f->m_b1 = 0.f;
  334. f->m_b0 = -1.f;
  335. break;
  336. }
  337. f->m_b2 = 1.f;
  338. }
  339. int modify_samples_lopass(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate)
  340. {
  341. static int i;
  342. static filter f1,f2;
  343. if (!i)
  344. {
  345. i=1;
  346. makefilter(&f1,bandpass,44100,1000.0,0.5);
  347. makefilter(&f2,lowpass,44100,1.0,1.0);
  348. }
  349. if (bps==16)
  350. {
  351. int x;
  352. x=numsamples;
  353. if (nch == 2) while (x--)
  354. {
  355. int t=(int)Filter(&f1,*samples);
  356. *samples++=min(max(t,-32768),32767);
  357. t=(int)Filter(&f2,*samples);
  358. *samples++=min(max(t,-32768),32767);
  359. }
  360. }
  361. return numsamples;
  362. }
  363. #define mulspl(a,b) _mulspl((int)(a),(int)((b)*65536.0))
  364. int _mulspl(int a, int b)
  365. {
  366. a *= b;
  367. a >>= 16;
  368. return a;
  369. }
  370. int modify_samples_hipass(struct winampDSPModule *this_mod, short int *samples, int numsamples, int bps, int nch, int srate)
  371. {
  372. if (bps==16 && nch==2)
  373. {
  374. static short splbuf[32768+2];
  375. short int *spls=splbuf+nch;
  376. int x;
  377. memcpy(spls,samples,numsamples*sizeof(short)*nch);
  378. x=numsamples;
  379. while (x--)
  380. {
  381. int ch;
  382. for (ch = 0; ch < nch; ch ++)
  383. {
  384. int r=mulspl(spls[0],0.93) + mulspl(spls[-nch],-0.93) + mulspl(samples[-nch],0.86);
  385. samples[0] = max(min(r,32767),-32768);
  386. spls++;
  387. samples++;
  388. }
  389. }
  390. memcpy(splbuf,&splbuf[numsamples*nch],nch*sizeof(short));
  391. }
  392. return numsamples;
  393. }
  394. static BOOL CALLBACK pitchProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  395. {
  396. if (uMsg == WM_INITDIALOG)
  397. {
  398. SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_SETRANGEMAX,0,50);
  399. SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_SETRANGEMIN,0,-50);
  400. SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_SETPOS,1,1);
  401. SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_SETPOS,1,0);
  402. {
  403. char str[123];
  404. wsprintf(str,"%s%d%%",g_pitch>=100?"+":"",g_pitch-100);
  405. SetDlgItemText(hwndDlg,IDC_BOOGA,str);
  406. }
  407. }
  408. if (uMsg == WM_VSCROLL)
  409. {
  410. HWND swnd = (HWND) lParam;
  411. if (swnd == GetDlgItem(hwndDlg,IDC_SLIDER1))
  412. {
  413. g_pitch = -SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_GETPOS,0,0)+100;
  414. {
  415. char str[123];
  416. wsprintf(str,"%s%d%%",g_pitch>=100?"+":"",g_pitch-100);
  417. SetDlgItemText(hwndDlg,IDC_BOOGA,str);
  418. }
  419. }
  420. }
  421. return 0;
  422. }