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