another tweak to maybe-fix crash in Playlist::fade_range()
[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 midi_region
10         local audio_regions = {}
11         local start_time = Session:current_end_frame ()
12         local end_time = Session:current_start_frame ()
13         for r in sel.regions:regionlist ():iter () do
14                 if r:to_midiregion():isnil() then
15                         local st = r:position()
16                         local ln = r:length()
17                         local et = st + ln
18                         if st < start_time then
19                                 start_time = st
20                         end
21                         if et > end_time then
22                                 end_time = et
23                         end
24                         table.insert(audio_regions, r) 
25                 else
26                         midi_region = r:to_midiregion()
27                 end
28         end
29         assert (audio_regions and midi_region)
30         midi_region:set_initial_position(start_time)
31         midi_region:set_length(end_time - start_time, 0)
32
33         for i,ar in pairs(audio_regions) do
34                 local a_off = ar:position ()
35                 local b_off = midi_region:quarter_note () - midi_region:start_beats ()
36
37                 vamp:analyze (ar:to_readable (), 0, nil)
38                 local fl = vamp:plugin ():getRemainingFeatures ():at (0)
39                 if fl and fl:size() > 0 then
40                         local mm = midi_region:midi_source(0):model()
41                         local midi_command = mm:new_note_diff_command ("Audio2Midi")
42                         for f in fl:iter () do
43                                 local ft = Vamp.RealTime.realTime2Frame (f.timestamp, sr)
44                                 local fd = Vamp.RealTime.realTime2Frame (f.duration, sr)
45                                 local fn = f.values:at (0)
46
47                                 local bs = tm:exact_qn_at_frame (a_off + ft, 0)
48                                 local be = tm:exact_qn_at_frame (a_off + ft + fd, 0)
49
50                                 local pos = Evoral.Beats (bs - b_off)
51                                 local len = Evoral.Beats (be - bs)
52                                 local note = ARDOUR.LuaAPI.new_noteptr (1, pos, len, fn + 1, 0x7f)
53                                 midi_command:add (note)
54                         end
55                         mm:apply_command (Session, midi_command)
56                 end
57         end
58 end end