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