5 author = "Robin Gareus",
6 email = "robin@gareus.org",
7 site = "http://gareus.org",
8 description = [[An Example Synth for prototyping.]]
11 function dsp_ioconfig ()
14 { audio_in = 0, audio_out = -1}, -- any number of channels
15 -- { audio_in = 0, audio_out = 4}, -- values > 0, precisely N channels
16 -- { audio_in = 0, audio_out = -6}, -- values < -2, up to -N channels, here 1,..,6
20 function dsp_midi_input ()
26 local active_notes = {}
30 function dsp_init (rate)
32 note_table [n] = (440 / 32) * 2^((n - 10.0) / 12.0) / rate
37 function dsp_run (ins, outs, n_samples)
38 -- initialize output buffer
40 for s = 1, n_samples do a[s] = 0 end
43 -- very basic synth, simple sine, basic envelope
44 local function synth (s_start, s_end)
45 for n,v in pairs (active_notes) do
46 local vel = v["vel"] or 0
47 local tgt = v["tvel"];
48 for s = s_start,s_end do
49 local phase = phases[n] or 0
50 vel = vel + env * (tgt - vel)
51 a[s] = a[s] + math.sin(6.283185307 * phase) * vel / 167
52 phase = phase + note_table[n]
54 phases[n] = phase - 2.0
59 if vel < 1 and tgt == 0 then
62 active_notes[n]["vel"] = vel;
68 -- parse midi messages
69 assert (type(mididata) == "table") -- global table of midi events (for now)
70 for _,b in pairs (mididata) do
71 local t = b["time"] -- t = [ 1 .. n_samples ]
73 -- synth sound until event
77 local d = b["data"] -- get midi-event
78 -- we ignore the midi channel
79 if (#d == 3 and bit32.band (d[1], 240) == 144) then -- note on
81 active_notes[n] = active_notes[n] or {}
82 active_notes[n]["tvel"] = d[3]
84 if (#d == 3 and bit32.band (d[1], 240) == 128) then -- note off
86 active_notes[n] = active_notes[n] or {}
87 active_notes[n]["tvel"] = 0
89 if (#d == 3 and bit32.band (d[1], 240) == 176) then -- CC
90 if (d[2] == 120 or d[2] == 123) then -- panic
96 -- synth rest of cycle
101 outs[c]:set_table(a, n_samples)