4 category = "Instrument",
6 author = "Ardour Lua Task Force",
7 description = [[An Example Synth for Prototyping.]]
10 function dsp_ioconfig ()
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
22 function dsp_has_midi_input ()
28 local active_notes = {}
32 function dsp_init (rate)
34 note_table [n] = (440 / 32) * 2^((n - 10.0) / 12.0) / rate
39 function dsp_run (ins, outs, n_samples)
40 -- initialize output buffer
42 for s = 1, n_samples do a[s] = 0 end
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]
56 phases[n] = phase - 2.0
61 if vel < 1 and tgt == 0 then
64 active_notes[n]["vel"] = vel;
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 ]
75 -- synth sound until event
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
83 active_notes[n] = active_notes[n] or {}
84 active_notes[n]["tvel"] = d[3]
86 if (#d == 3 and bit32.band (d[1], 240) == 128) then -- note off
88 active_notes[n] = active_notes[n] or {}
89 active_notes[n]["tvel"] = 0
91 if (#d == 3 and bit32.band (d[1], 240) == 176) then -- CC
92 if (d[2] == 120 or d[2] == 123) then -- panic
98 -- synth rest of cycle
103 outs[c]:set_table(a, n_samples)