Interpret start & length_beats properties as double rather than Evoral::Beats.
[ardour.git] / scripts / _vamp_audio_to_midi.lua
1 ardour { ["type"] = "EditorAction", name = "Vamp Audio to MIDI",
2 description = "analyze audio from selected audio region to selected midi region" }
3
4 function factory () return function ()
5         local sel = Editor:get_selection ()
6         local sr = Session:nominal_frame_rate ()
7         local tm = Session:tempo_map ()
8         local vamp = ARDOUR.LuaAPI.Vamp ("libardourvampplugins:qm-transcription", sr)
9         local ar, mr
10         for r in sel.regions:regionlist ():iter () do
11                 if r:to_midiregion():isnil() then
12                         ar = r
13                 else
14                         mr = r:to_midiregion()
15                 end
16         end
17         assert (ar and mr)
18
19         local a_off = ar:position ()
20         local b_off = 4.0 * mr:pulse () - mr:start_beats ()
21
22         vamp:analyze (ar:to_readable (), 0, nil)
23         local fl = vamp:plugin ():getRemainingFeatures ():at (0)
24         if fl and fl:size() > 0 then
25                 local mm = mr:midi_source(0):model()
26                 local midi_command = mm:new_note_diff_command ("Audio2Midi")
27                 for f in fl:iter () do
28                         local ft = Vamp.RealTime.realTime2Frame (f.timestamp, sr)
29                         local fd = Vamp.RealTime.realTime2Frame (f.duration, sr)
30                         local fn = f.values:at (0)
31
32                         local bs = tm:exact_qn_at_frame (a_off + ft, 0)
33                         local be = tm:exact_qn_at_frame (a_off + ft + fd, 0)
34
35                         local pos = Evoral.Beats (bs - b_off)
36                         local len = Evoral.Beats (be - bs)
37                         local note = ARDOUR.LuaAPI.new_noteptr (1, pos, len, fn + 1, 0x7f)
38                         midi_command:add (note)
39                 end
40                 mm:apply_command (Session, midi_command)
41         end
42 end end