4 category = "Instrument",
6 author = "Robin Gareus",
7 email = "robin@gareus.org",
8 site = "http://gareus.org",
9 description = [[An Example Synth for prototyping.]]
12 function dsp_ioconfig ()
15 -- { audio_in = 0, audio_out = -1}, -- any number of channels
16 -- { audio_in = 0, audio_out = 1}, -- values > 0, precisely N channels
17 { audio_in = 0, audio_out = 2}, -- values > 0, precisely N channels
18 { audio_in = 0, audio_out = 4}, -- values > 0, precisely N channels
19 { audio_in = 0, audio_out = 8}, -- values > 0, precisely N channels
20 -- { audio_in = 0, audio_out = -6}, -- values < -2, up to -N channels, here 1,..,6
24 function dsp_has_midi_input ()
30 local active_notes = {}
34 function dsp_init (rate)
36 note_table [n] = (440 / 32) * 2^((n - 10.0) / 12.0) / rate
41 function dsp_run (ins, outs, n_samples)
42 -- initialize output buffer
44 for s = 1, n_samples do a[s] = 0 end
47 -- very basic synth, simple sine, basic envelope
48 local function synth (s_start, s_end)
49 for n,v in pairs (active_notes) do
50 local vel = v["vel"] or 0
51 local tgt = v["tvel"];
52 for s = s_start,s_end do
53 local phase = phases[n] or 0
54 vel = vel + env * (tgt - vel)
55 a[s] = a[s] + math.sin(6.283185307 * phase) * vel / 167
56 phase = phase + note_table[n]
58 phases[n] = phase - 2.0
63 if vel < 1 and tgt == 0 then
66 active_notes[n]["vel"] = vel;
72 -- parse midi messages
73 assert (type(midiin) == "table") -- global table of midi events (for now)
74 for _,b in pairs (midiin) do
75 local t = b["time"] -- t = [ 1 .. n_samples ]
77 -- synth sound until event
81 local d = b["data"] -- get midi-event
82 -- we ignore the midi channel
83 if (#d == 3 and bit32.band (d[1], 240) == 144) then -- note on
85 active_notes[n] = active_notes[n] or {}
86 active_notes[n]["tvel"] = d[3]
88 if (#d == 3 and bit32.band (d[1], 240) == 128) then -- note off
90 active_notes[n] = active_notes[n] or {}
91 active_notes[n]["tvel"] = 0
93 if (#d == 3 and bit32.band (d[1], 240) == 176) then -- CC
94 if (d[2] == 120 or d[2] == 123) then -- panic
100 -- synth rest of cycle
101 synth(tme, n_samples)
105 outs[c]:set_table(a, n_samples)