Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. ardour {
  2.     ["type"]    = "dsp",
  3.     name        = "SinGen",
  4.     category    = "Instrument",
  5.     license     = "MIT",
  6.     author      = "Nik",
  7.     description = [[Sine Wave Generator (v-1.2)]]
  8. }
  9.  
  10. function dsp_params ()
  11.     return
  12.     {
  13.         { ["type"] = "input", name = "Frequency", min = 20, max = 20000, default = 1000, unit="Hz", logarithmic = true },
  14.         { ["type"] = "input", name = "Gain", min = -90, max = 0, default = -18, unit="dB" },
  15.     }
  16. end
  17.  
  18. function dsp_ioconfig ()
  19.     return { [1] = { audio_in = -1, audio_out = -1}, }
  20. end
  21.  
  22. function dsp_init (rate)
  23.     r = rate
  24.     lpf  = 2048 / rate
  25. end
  26.  
  27. function low_pass_filter_param(old, new, limit)
  28.     if math.abs (old - new) < limit  then
  29.         return new
  30.     else
  31.         return old + lpf * (new - old)
  32.     end
  33. end
  34.  
  35. local p  = 0
  36. local fo = 0
  37. local ao = 0
  38.  
  39. function dsp_run (ins, outs, n_samples)
  40.     local ctrl = CtrlPorts:array() --call parameters
  41.    
  42.     local a = {} --init array
  43.     local f = ctrl[1] or 1000
  44.     local amp =  low_pass_filter_param(ao, ARDOUR.DSP.dB_to_coefficient(ctrl[2]), 0.02)
  45.     local inc = f / r
  46.  
  47.     for s = 1, n_samples do --fill table with fragments of a sine wave
  48.         p = p + inc
  49.         a[s] = amp * math.sin(p * (2 * math.pi))
  50.     end
  51.    
  52.     for c = 1,#outs do
  53.         outs[c]:set_table(a, n_samples) --passes array into buffer
  54.     end
  55.    
  56.     if f ~= fo then
  57.         self:queue_draw()
  58.     end
  59.     fo = f
  60.     ao = amp
  61. end
  62.  
  63. function render_inline (ctx, w, max_h) --inline display
  64.     local ctrl = CtrlPorts:array()
  65.     h = 30
  66.     p = 0
  67.     inc = 1/w
  68.     f = ctrl[1] / 1000
  69.    
  70.     if f < 0.5 then f = 0.5 end
  71.     if f > 8 then f  = 8 end
  72.    
  73.     --draw rectangle
  74.     ctx:rectangle(0, 0, w, h)
  75.     ctx:set_source_rgba(0, 0, 0, 1.0)
  76.     ctx:fill()
  77.     ctx:set_line_width(1.5)
  78.     ctx:set_source_rgba(0.8, 0.8, 0.8, 1.0)
  79.    
  80.     l_x = 0
  81.     l_y = 0
  82.     for x = 0,w do
  83.         y = math.sin(f * (2 * math.pi * (p)))
  84.         yc = 0.5 * h + ((-0.5 * h) * y)
  85.         ctx:move_to (x, yc + 3)
  86.         ctx:line_to (l_x, l_y + 3)
  87.         l_x = x
  88.         l_y = yc
  89.         ctx:stroke()
  90.         p = p + inc
  91.     end
  92.     return {w, h + 6}
  93. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement