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},
18 function dsp_midi_input ()
24 local active_notes = {}
28 function dsp_init (rate)
30 note_table [n] = (440 / 32) * 2^((n - 10.0) / 12.0) / rate
35 function dsp_run (ins, outs, n_samples)
36 -- initialize output buffer
39 for s = 1, n_samples do a[s] = 0 end
42 -- very basic synth, simple sine, basic envelope
43 local function synth (s_start, s_end)
44 for n,v in pairs (active_notes) do
45 local vel = v["vel"] or 0
46 local tgt = v["tvel"];
47 for s = s_start,s_end do
48 local phase = phases[n] or 0
49 vel = vel + env * (tgt - vel)
50 a[s] = a[s] + math.sin(6.283185307 * phase) * vel / 167
51 phase = phase + note_table[n]
53 phases[n] = phase - 2.0
58 if vel < 1 and tgt == 0 then
61 active_notes[n]["vel"] = vel;
67 -- parse midi messages
68 assert (type(mididata) == "table") -- global table of midi events (for now)
69 for _,b in pairs (mididata) do
70 local t = b["time"] -- t = [ 1 .. n_samples ]
72 -- synth sound until event
76 local d = b["data"] -- get midi-event
77 -- we ignore the midi channel
78 if (#d == 3 and bit32.band (d[1], 240) == 144) then -- note on
80 active_notes[n] = active_notes[n] or {}
81 active_notes[n]["tvel"] = d[3]
83 if (#d == 3 and bit32.band (d[1], 240) == 128) then -- note off
85 active_notes[n] = active_notes[n] or {}
86 active_notes[n]["tvel"] = 0
88 if (#d == 3 and bit32.band (d[1], 240) == 176) then -- CC
89 if (d[2] == 120 or d[2] == 123) then -- panic
95 -- synth rest of cycle
99 outs[1]:set_table(a, n_samples)