fix issue with initialization of a BBT_Time variable.
[ardour.git] / scripts / _rawmidi.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "Midi Passthru",
4         category    = "Example",
5         license     = "MIT",
6         author      = "Ardour Lua Task Force",
7         description = [[An Example Audio/MIDI Passthrough Plugin using Buffer Pointers]]
8 }
9
10 -- return possible audio i/o configurations
11 function dsp_ioconfig ()
12         -- -1, -1 = any number of channels as long as input and output count matches
13         -- require 1 MIDI in, 1 MIDI out.
14         return { { midi_in = 1, midi_out = 1, audio_in = -1, audio_out = -1}, }
15 end
16
17 -- "dsp_runmap" uses Ardour's internal processor API, eqivalent to
18 -- 'connect_and_run()". There is no overhead (mapping, translating buffers).
19 -- The lua implementation is responsible to map all the buffers directly.
20 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
21
22         -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:ChanMapping
23
24         local ib = in_map:get (ARDOUR.DataType ("midi"), 0) -- get index of the 1st mapped midi input buffer
25
26         if ib ~= ARDOUR.ChanMapping.Invalid then
27                 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:MidiBuffer
28                 local mb = bufs:get_midi (ib) -- get the mapped buffer
29                 local events = mb:table () -- copy event list into a lua table
30
31                 -- iterate over all MIDI events
32                 for _, e in pairs (events) do
33                         -- e is-a http://manual.ardour.org/lua-scripting/class_reference/#Evoral:MidiEvent
34
35                         -- do something with the event e.g.
36                         print (e:channel (), e:time (), e:size (), e:buffer ():array ()[1], e:buffer ():get_table (e:size ())[1])
37                 end
38         end
39
40         ----
41         -- The following code is needed with "dsp_runmap" to work for arbitrary pin connections
42         -- this passes though all audio/midi data unprocessed.
43
44         ARDOUR.DSP.process_map (bufs, in_map, out_map, n_samples, offset, ARDOUR.DataType ("audio"))
45         ARDOUR.DSP.process_map (bufs, in_map, out_map, n_samples, offset, ARDOUR.DataType ("midi"))
46
47         -- equivalent lua code.
48         -- NOTE: the lua implementation below is intended for io-config [-1,-1].
49         -- It only works for actually mapped channels due to in_map:count() out_map:count()
50         -- being identical to the i/o pin count in this case.
51         --
52         -- Plugins that have multiple possible configurations will need to implement
53         -- dsp_configure() and remember the actual channel count.
54         --
55         -- ARDOUR.DSP.process_map() does iterate over the mapping itself and works generally.
56         -- Still the lua code below does lend itself as elaborate example.
57         --
58         --[[
59
60         local audio_ins = in_map:count (): n_audio () -- number of mapped audio input buffers
61         local audio_outs = out_map:count (): n_audio () -- number of mapped audio output buffers
62         assert (audio_outs, audio_ins) -- ioconfig [-1, -1]: must match
63
64         -- copy audio data if any
65         for c = 1, audio_ins do
66                 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1) -- get index of mapped input buffer
67                 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1) -- get index of mapped output buffer
68                 if ib ~= ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid and ib ~= ob then
69                         -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
70                         -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:AudioBuffer
71                         ARDOUR.DSP.copy_vector (bufs:get_audio (ob):data (offset), bufs:get_audio (ib):data (offset), n_samples)
72                 end
73         end
74         -- Clear unconnected output buffers.
75         -- In case we're processing in-place some buffers may be identical,
76         -- so this must be done *after* copying relvant data from that port.
77         for c = 1, audio_outs do
78                 local ib = in_map:get (ARDOUR.DataType ("audio"), c - 1)
79                 local ob = out_map:get (ARDOUR.DataType ("audio"), c - 1)
80                 if ib == ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid then
81                         bufs:get_audio (ob):silence (n_samples, offset)
82                 end
83         end
84
85         -- copy midi data
86         local midi_ins = in_map:count (): n_midi () -- number of midi input buffers
87         local midi_outs = out_map:count (): n_midi () -- number of midi input buffers
88
89         -- with midi_in=1, midi_out=1 in dsp_ioconfig
90         -- the following will always be true
91         assert (midi_ins == 1)
92         assert (midi_outs == 1)
93
94         for c = 1, midi_ins do
95                 local ib = in_map:get (ARDOUR.DataType ("midi"), c - 1)
96                 local ob = out_map:get (ARDOUR.DataType ("midi"), c - 1)
97                 if ib ~= ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid and ib ~= ob then
98                         bufs:get_midi (ob):copy (bufs:get_midi (ib))
99                 end
100         end
101         -- silence unused midi outputs
102         for c = 1, midi_outs do
103                 local ib = in_map:get (ARDOUR.DataType ("midi"), c - 1)
104                 local ob = out_map:get (ARDOUR.DataType ("midi"), c - 1)
105                 if ib == ARDOUR.ChanMapping.Invalid and ob ~= ARDOUR.ChanMapping.Invalid then
106                         bufs:get_midi (ob):silence (n_samples, offset)
107                 end
108         end
109         --]]
110 end