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 = 1}, -- values > 0, precisely N channels
16 { audio_in = 0, audio_out = 2}, -- values > 0, precisely N channels
17 { audio_in = 0, audio_out = 4}, -- values > 0, precisely N channels
18 { audio_in = 0, audio_out = 8}, -- values > 0, precisely N channels
19 -- { audio_in = 0, audio_out = -6}, -- values < -2, up to -N channels, here 1,..,6
23 function dsp_midi_input ()
29 local active_notes = {}
33 function dsp_init (rate)
35 note_table [n] = (440 / 32) * 2^((n - 10.0) / 12.0) / rate
40 function dsp_run (ins, outs, n_samples)
41 -- initialize output buffer
43 for s = 1, n_samples do a[s] = 0 end
46 -- very basic synth, simple sine, basic envelope
47 local function synth (s_start, s_end)
48 for n,v in pairs (active_notes) do
49 local vel = v["vel"] or 0
50 local tgt = v["tvel"];
51 for s = s_start,s_end do
52 local phase = phases[n] or 0
53 vel = vel + env * (tgt - vel)
54 a[s] = a[s] + math.sin(6.283185307 * phase) * vel / 167
55 phase = phase + note_table[n]
57 phases[n] = phase - 2.0
62 if vel < 1 and tgt == 0 then
65 active_notes[n]["vel"] = vel;
71 -- parse midi messages
72 assert (type(mididata) == "table") -- global table of midi events (for now)
73 for _,b in pairs (mididata) do
74 local t = b["time"] -- t = [ 1 .. n_samples ]
76 -- synth sound until event
80 local d = b["data"] -- get midi-event
81 -- we ignore the midi channel
82 if (#d == 3 and bit32.band (d[1], 240) == 144) then -- note on
84 active_notes[n] = active_notes[n] or {}
85 active_notes[n]["tvel"] = d[3]
87 if (#d == 3 and bit32.band (d[1], 240) == 128) then -- note off
89 active_notes[n] = active_notes[n] or {}
90 active_notes[n]["tvel"] = 0
92 if (#d == 3 and bit32.band (d[1], 240) == 176) then -- CC
93 if (d[2] == 120 or d[2] == 123) then -- panic
99 -- synth rest of cycle
100 synth(tme, n_samples)
104 outs[c]:set_table(a, n_samples)