moving singen and noisegen from mixbus repo to ardour + change singen display and...
[ardour.git] / scripts / noisegen.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "NoiseGen",
4         category    = "Instrument",
5         license     = "MIT",
6         author      = "Ardour Team",
7         description = [[Noise Generator (v-1.02)]]
8 }
9
10 function dsp_params ()
11         return
12         {
13                 { ["type"] = "input", name = "White/Pink", min = 0, max = 1, default = 0, toggled = true },
14                 { ["type"] = "input", name = "Gain", min = -60, 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 local ao = 0
23
24 function dsp_run (ins, outs, n_samples)
25
26         local a = {} -- init array
27         local ctrl = CtrlPorts:array ()
28         local noise = ctrl[1] or 0
29         local amplitude =  ARDOUR.DSP.dB_to_coefficient (ctrl[2]) or ARDOUR.DSP.dB_to_coefficient (-18)
30
31         local b0 = 0.0
32         local b1 = 0.0
33         local b2 = 0.0
34         local b3 = 0.0
35         local b4 = 0.0
36         local b5 = 0.0
37         local b6 = 0.0
38
39         --Pink noise generation courtesy of Paul Kellet's refined method
40         --http://www.musicdsp.org/files/pink.txt
41         --If 'white' consists of uniform random numbers,
42         --the pink noise will have an almost gaussian distribution.
43         for s = 1, n_samples do
44                 if noise == 0 then
45                         a[s] = amplitude * 2 * (math.random() - 0.5)
46                 end
47                 if noise == 1 then
48                         white = (amplitude * 0.25) * 2 * (math.random() - 0.5)
49                         b0 = 0.99886 * b0 + white * 0.0555179;
50                         b1 = 0.99332 * b1 + white * 0.0750759;
51                         b2 = 0.96900 * b2 + white * 0.1538520;
52                         b3 = 0.86650 * b3 + white * 0.3104856;
53                         b4 = 0.55000 * b4 + white * 0.5329522;
54                         b5 = -0.7616 * b5 - white * 0.0168980;
55                         b6 = white * 0.115926;
56                         a[s] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
57                 end
58         end
59
60         -- passes array a {} into buffer
61         for c = 1,#outs do
62                 outs[c]:set_table(a, n_samples)
63         end
64
65         if (a ~= ao) then
66                 self:queue_draw()
67         end
68         ao = amplitude
69 end
70
71 function render_inline (ctx, w, max_h) --inline display
72         local ctrl = CtrlPorts:array()
73         h = 30
74         p = 0
75         inc = 0
76         ycy = 0.5
77         pink = false
78         local amplitude = ARDOUR.DSP.dB_to_coefficient(ctrl[2])
79         if ctrl[1] == 1 then pink = true end
80         if pink then inc = 0.7/w end
81
82         --draw rectangle
83         ctx:rectangle(0, 0, w, h)
84         ctx:set_source_rgba(0, 0, 0, 1.0)
85         ctx:fill()
86         ctx:set_line_width(1.5)
87         ctx:set_source_rgba(0.8, 0.8, 0.8, 1.0)
88
89         l_x = 0
90         l_y = 0
91         for x = 0,w do
92                 if pink then ycy = 0.3 else ycy = 0.5 end --slant slightly like an actual pink noise spectrum
93                 y = math.log(20^amplitude) * (math.random() - 0.5) - p
94                 yc = ycy * h + ((-0.5 * h) * y)
95                 ctx:move_to (x, yc + 3)
96                 ctx:line_to (l_x, l_y + 3)
97                 l_x = x
98                 l_y = yc
99                 ctx:stroke()
100                 p = p + inc
101         end
102         return {w, h + 6}
103 end