example fluidsynth lua script (currently: hardcoded .sf2)
[ardour.git] / scripts / _fluidsynth1.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "Lua Fluid 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                 { midi_in = 1, audio_in = 0, audio_out =  2},
14         }
15 end
16
17 fluidsynth = nil
18
19 function dsp_init (rate)
20         fluidsynth = ARDOUR.FluidSynth (rate, 32)
21         assert (fluidsynth:load_sf2 ("/usr/share/sounds/sf2/FluidR3_GM.sf2"))
22 end
23
24 function dsp_run (ins, outs, n_samples)
25         local tme = 1
26         assert (#outs == 2)
27
28         -- parse midi messages
29         assert (type(midiin) == "table") -- global table of midi events (for now)
30         for _, e in pairs (midiin) do
31                 local t = e["time"] -- t = [ 1 .. n_samples ]
32
33                 -- synth sound until event
34                 if t > tme then
35                         local off = tme - 1
36                         local len = t - tme
37                         fluidsynth:synth (outs[1]:offset (off), outs[2]:offset (off), len)
38                 end
39
40                 tme = t + 1
41
42                 fluidsynth:midi_event (e["bytes"], e["size"]) -- parse midi event
43                 end
44
45         -- synth rest of cycle
46         if tme <= n_samples then
47                 local off = tme - 1
48                 local len = 1 + n_samples - tme
49                 fluidsynth:synth (outs[1]:offset (off), outs[2]:offset (off), len)
50         end
51 end