fix unused variable warnings and code tidy
[ardour.git] / scripts / midigenerator.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "Midi Generator",
4         category    = "Example", -- "Utility"
5         license     = "MIT",
6         author      = "Ardour Lua Task Force",
7         description = [[An Example Midi Generator for prototyping.]]
8 }
9
10 function dsp_ioconfig ()
11         return { { audio_in = 0, audio_out = 0}, }
12 end
13
14 function dsp_has_midi_output () return true end
15
16 local tme = 0 -- sample-counter
17 local seq = 1 -- sequence-step
18 local spb = 0 -- samples per beat
19
20 local midi_sequence = {
21         { 0x90, 64, 127 },
22         { 0x80, 64,   0 },
23 }
24
25 function dsp_init (rate)
26         local bpm = 120
27         spb = rate * 60 / bpm
28         if spb < 2 then spb = 2 end
29 end
30
31 function dsp_run (_, _, n_samples)
32         assert (type(midiout) == "table")
33         assert (spb > 1)
34         local m = 1
35
36         for time = 1,n_samples do -- not very efficient
37                 -- TODO, timestamp the sequence in beats, calc/skip to next event
38                 tme = tme + 1
39
40                 if tme >= spb then
41                         midiout[m] = {}
42                         midiout[m]["time"] = time
43                         midiout[m]["data"] = midi_sequence[seq]
44
45                         tme = 0
46                         m = m + 1
47                         if seq == #midi_sequence then seq = 1 else seq = seq + 1 end
48                 end
49         end
50 end