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