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