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