show debug out when AU NSView doesnt get moved
[ardour.git] / scripts / synth1.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "Simple Synth",
4         license     = "MIT",
5         author      = "Robin Gareus",
6         email       = "robin@gareus.org",
7         site        = "http://gareus.org",
8         description = [[An Example Synth for prototyping.]]
9 }
10
11 function dsp_ioconfig ()
12         return
13         {
14         --      { audio_in = 0, audio_out = -1}, -- any number of channels
15         --      { audio_in = 0, audio_out =  1}, -- values >  0, precisely N channels
16                 { audio_in = 0, audio_out =  2}, -- values >  0, precisely N channels
17                 { audio_in = 0, audio_out =  4}, -- values >  0, precisely N channels
18                 { audio_in = 0, audio_out =  8}, -- values >  0, precisely N channels
19         --      { audio_in = 0, audio_out = -6}, -- values < -2, up to -N channels, here 1,..,6
20         }
21 end
22
23 function dsp_midi_input ()
24         return true
25 end
26
27
28 local note_table = {}
29 local active_notes = {}
30 local phases = {}
31 local env = .01;
32
33 function dsp_init (rate)
34         for n = 1,128 do
35                 note_table [n] = (440 / 32) * 2^((n - 10.0) / 12.0) / rate
36         end
37         env = 100 / rate
38 end
39
40 function dsp_run (ins, outs, n_samples)
41         -- initialize output buffer
42         local a = {}
43         for s = 1, n_samples do a[s] = 0 end
44
45
46         -- very basic synth, simple sine, basic envelope
47         local function synth (s_start, s_end)
48                 for n,v in pairs (active_notes) do
49                         local vel = v["vel"] or 0
50                         local tgt = v["tvel"];
51                         for s = s_start,s_end do
52                                 local phase = phases[n] or 0
53                                 vel = vel + env * (tgt - vel)
54                                 a[s] = a[s] + math.sin(6.283185307 * phase) * vel / 167
55                                 phase = phase + note_table[n]
56                                 if (phase > 1.0) then
57                                         phases[n] = phase - 2.0
58                                 else
59                                         phases[n] = phase
60                                 end
61                         end
62                         if vel < 1 and tgt == 0 then
63                                 active_notes[n] = nil
64                         else
65                                 active_notes[n]["vel"] = vel;
66                         end
67                 end
68         end
69
70         local tme = 1
71         -- parse midi messages
72         assert (type(mididata) == "table") -- global table of midi events (for now)
73         for _,b in pairs (mididata) do 
74                 local t = b["time"] -- t = [ 1 .. n_samples ]
75
76                 -- synth sound until event
77                 synth(tme, t)
78                 tme = t + 1
79
80                 local d = b["data"] -- get midi-event
81                 -- we ignore the midi channel
82                 if (#d == 3 and bit32.band (d[1], 240) == 144) then -- note on
83                         local n = 1 + d[2];
84                         active_notes[n] = active_notes[n] or {}
85                         active_notes[n]["tvel"] = d[3]
86                 end
87                 if (#d == 3 and bit32.band (d[1], 240) == 128) then -- note off
88                         local n = 1 + d[2];
89                         active_notes[n] = active_notes[n] or {}
90                         active_notes[n]["tvel"] = 0
91                 end
92                 if (#d == 3 and bit32.band (d[1], 240) == 176) then -- CC
93                         if (d[2] == 120 or d[2] == 123) then -- panic
94                                 active_notes = {}
95                         end
96                 end
97         end
98
99         -- synth rest of cycle
100         synth(tme, n_samples)
101
102         -- copy
103         for c = 1,#outs do
104                 outs[c]:set_table(a, n_samples)
105         end
106 end