Extra "ports" of juce-based plugins using the distrho build system
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.5KB

  1. --[[
  2. name: FFT sweeper
  3. description: testing
  4. author: osar.fr
  5. --]]
  6. require "include/protoplug"
  7. fftlib = script.ffiLoad("libfftw3.so.3", "libfftw3-3")
  8. ffi.cdef[[
  9. typedef enum {
  10. FFTW_R2HC=0, FFTW_HC2R=1, FFTW_DHT=2,
  11. FFTW_REDFT00=3, FFTW_REDFT01=4, FFTW_REDFT10=5, FFTW_REDFT11=6,
  12. FFTW_RODFT00=7, FFTW_RODFT01=8, FFTW_RODFT10=9, FFTW_RODFT11=10
  13. } fftw_r2r_kind;
  14. void *fftw_plan_r2r_1d(int n, double *in, double *out, fftw_r2r_kind kind, unsigned int flags);
  15. void fftw_execute(void *plan);
  16. ]]
  17. fftsz = 1024
  18. winsz = 1000
  19. fd = ffi.new("double[?]", fftsz) -- halfcomplex freq domain
  20. td1 = ffi.new("double[?]", fftsz) -- time domain
  21. td2 = ffi.new("double[?]", fftsz) -- time domain (alternating)
  22. hw = ffi.new("double[?]", fftsz) -- Hann window function
  23. plan1 = fftlib.fftw_plan_r2r_1d(fftsz, fd, td1, 1, 64)
  24. plan2 = fftlib.fftw_plan_r2r_1d(fftsz, fd, td2, 1, 64)
  25. -- prepare Hann window
  26. for i = 0,winsz-1 do
  27. hw[i] = 0.00003 * (1 - math.cos(2*math.pi*i/(winsz-1)));
  28. end
  29. function ApplyWindow (buf)
  30. for i = 0,winsz-1 do
  31. buf[i] = buf[i] * hw[i]
  32. end
  33. end
  34. cphase = 0
  35. gapper = 0
  36. function FillFD (buf)
  37. local center = math.sin(cphase)*10+15
  38. gapper = gapper+1
  39. if gapper > 1 then gapper = 0 end
  40. if gapper > 0 then
  41. --for i = 0,fftsz-1 do
  42. -- buf[i] = 0
  43. --end
  44. --return
  45. center = center*4
  46. end
  47. for i = 0,fftsz-1 do
  48. local sharm = math.sin(i-center)
  49. local x = i/(fftsz*0.0004883)
  50. buf[i] = 10000/((x-center)*(x-center)+10)*sharm*sharm
  51. buf[i] = buf[i] + 33457/((x-center*8)*(x-center*8)+1000)
  52. buf[i] = buf[i] + 34321/((x-center*12)*(x-center*12)+1000)
  53. --buf[i] = buf[i] + 100/((i-center*2)*(i-center*2)+1)
  54. --buf[i] = buf[i] + 10/((i-center*4)*(i-center*4)+1)
  55. --if i<center then buf[i] = 0 end
  56. if i%5==1 then buf[i] = buf[i]*0.1 end
  57. if i%3==1 then buf[i] = buf[i]*-1 end
  58. if buf[i] > 10 then buf[i] = 10 end
  59. end
  60. cphase = cphase + 0.01
  61. end
  62. FillFD(fd)
  63. fftlib.fftw_execute(plan1)
  64. fftlib.fftw_execute(plan2)
  65. ApplyWindow(td1)
  66. ApplyWindow(td2)
  67. tdpos1 = 0
  68. tdpos2 = winsz*0.5
  69. alt1 = false
  70. alt2 = false
  71. function plugin.processBlock(s, smax)
  72. for i = 0,smax do
  73. s[0][i] = td1[tdpos1] + td2[tdpos2]
  74. s[1][i] = td1[tdpos1] + td2[tdpos2]
  75. tdpos1 = tdpos1+1
  76. tdpos2 = tdpos2+1
  77. if tdpos1 >= winsz then
  78. tdpos1 = 0
  79. if alt1 then
  80. alt1 = false
  81. FillFD(fd)
  82. fftlib.fftw_execute(plan1)
  83. ApplyWindow(td1)
  84. else
  85. alt1 = true
  86. end
  87. end
  88. if tdpos2 >= winsz then
  89. tdpos2 = 0
  90. if alt2 then
  91. alt2 = false
  92. FillFD(fd)
  93. fftlib.fftw_execute(plan2)
  94. ApplyWindow(td2)
  95. else
  96. alt2 = true
  97. end
  98. end
  99. end
  100. return 1
  101. end