@mthines/reaper-mcp 0.93.0 → 0.93.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -787,6 +787,135 @@ local function write_markers(api, t, list)
787
787
  end
788
788
  end
789
789
 
790
+ -- Snapshot support: the stretch markers of every audio item on some tracks, and putting them back.
791
+ -- A track chunk can hold them too, but REAPER refused to apply a chunk (SetTrackStateChunk) on tracks
792
+ -- whose items carry Melodyne (ARA) take FX, so a snapshot keeps the markers on their own.
793
+ M.MAX_SNAPSHOT_ITEMS = 5000
794
+
795
+ local function num_text(n) return string.format("%.10g", n) end
796
+
797
+ local function item_guid(api, it)
798
+ if not api.GetSetMediaItemInfo_String then return "" end
799
+ local ok, guid = api.GetSetMediaItemInfo_String(it, "GUID", "", false)
800
+ if ok and type(guid) == "string" then return guid end
801
+ return ""
802
+ end
803
+
804
+ -- One entry per audio item (the active take): {trackIndex, guid, position, length, markers}, with the
805
+ -- markers as one flat string "pos,src,slope;pos,src,slope" in raw take time (what get_stretch_markers
806
+ -- lists). A flat string, because the bridge's JSON fallback cannot read nested arrays back. An item
807
+ -- with no markers is kept too (markers = ""), so a restore clears markers added since.
808
+ -- `trackIndices`: 0-based list, nil = every track.
809
+ function M.capture_stretch_markers(api, trackIndices)
810
+ local list = trackIndices
811
+ if not list then
812
+ list = {}
813
+ for i = 0, api.CountTracks(0) - 1 do list[#list + 1] = i end
814
+ end
815
+ local entries = {}
816
+ for _, ti in ipairs(list) do
817
+ local tr = api.GetTrack(0, ti)
818
+ if tr then
819
+ for ii = 0, api.CountTrackMediaItems(tr) - 1 do
820
+ local it = api.GetTrackMediaItem(tr, ii)
821
+ local take = it and api.GetActiveTake(it)
822
+ if take and not api.TakeIsMIDI(take) then
823
+ if #entries >= M.MAX_SNAPSHOT_ITEMS then
824
+ return nil, "more than " .. M.MAX_SNAPSHOT_ITEMS .. " audio items: pass trackIndices to scope the snapshot"
825
+ end
826
+ local parts = {}
827
+ for m = 0, api.GetTakeNumStretchMarkers(take) - 1 do
828
+ local _, pos, src = api.GetTakeStretchMarker(take, m)
829
+ local slope = api.GetTakeStretchMarkerSlope and api.GetTakeStretchMarkerSlope(take, m) or 0
830
+ parts[#parts + 1] = num_text(pos) .. "," .. num_text(src) .. "," .. num_text(slope)
831
+ end
832
+ entries[#entries + 1] = {
833
+ trackIndex = ti, guid = item_guid(api, it),
834
+ position = api.GetMediaItemInfo_Value(it, "D_POSITION"),
835
+ length = api.GetMediaItemInfo_Value(it, "D_LENGTH"),
836
+ markers = table.concat(parts, ";"),
837
+ }
838
+ end
839
+ end
840
+ end
841
+ end
842
+ return entries
843
+ end
844
+
845
+ local function parse_marker_text(text)
846
+ local list = {}
847
+ for pos, src, slope in tostring(text or ""):gmatch("([^,;]+),([^,;]+),([^,;]+)") do
848
+ local p, s, sl = tonumber(pos), tonumber(src), tonumber(slope)
849
+ if p and s then list[#list + 1] = { pos = p, src = s, slope = sl or 0 } end
850
+ end
851
+ return list
852
+ end
853
+
854
+ local function same_markers(api, take, list)
855
+ if api.GetTakeNumStretchMarkers(take) ~= #list then return false end
856
+ for i, m in ipairs(list) do
857
+ local _, pos, src = api.GetTakeStretchMarker(take, i - 1)
858
+ local slope = api.GetTakeStretchMarkerSlope and api.GetTakeStretchMarkerSlope(take, i - 1) or 0
859
+ if math.abs(pos - m.pos) > 1e-6 or math.abs(src - m.src) > 1e-6 or math.abs(slope - m.slope) > 1e-6 then
860
+ return false
861
+ end
862
+ end
863
+ return true
864
+ end
865
+
866
+ -- Puts the saved markers back on the items they were saved from: found by GUID, else by track, position
867
+ -- and length (an item split or re-created since has a new GUID). Items already as saved are left alone;
868
+ -- an item that is gone is counted in `missing`. No undo block of its own: the caller opens one.
869
+ function M.restore_stretch_markers(api, entries)
870
+ local by_guid, on_track = {}, {}
871
+ for ti = 0, api.CountTracks(0) - 1 do
872
+ local tr = api.GetTrack(0, ti)
873
+ local items = {}
874
+ for ii = 0, api.CountTrackMediaItems(tr) - 1 do
875
+ local it = api.GetTrackMediaItem(tr, ii)
876
+ local take = it and api.GetActiveTake(it)
877
+ if take and not api.TakeIsMIDI(take) then
878
+ local rec = { it = it, take = take }
879
+ items[#items + 1] = rec
880
+ local guid = item_guid(api, it)
881
+ if guid ~= "" then by_guid[guid] = rec end
882
+ end
883
+ end
884
+ on_track[ti] = items
885
+ end
886
+
887
+ local used = {}
888
+ local function find(e)
889
+ local rec = e.guid and e.guid ~= "" and by_guid[e.guid]
890
+ if rec and not used[rec] then return rec end
891
+ for _, r in ipairs(on_track[tonumber(e.trackIndex)] or {}) do
892
+ if not used[r] and math.abs(api.GetMediaItemInfo_Value(r.it, "D_POSITION") - tonumber(e.position)) < 0.001
893
+ and math.abs(api.GetMediaItemInfo_Value(r.it, "D_LENGTH") - tonumber(e.length)) < 0.001 then
894
+ return r
895
+ end
896
+ end
897
+ end
898
+
899
+ local restored, unchanged, missing = 0, 0, 0
900
+ for _, e in ipairs(entries or {}) do
901
+ local rec = find(e)
902
+ if not rec then
903
+ missing = missing + 1
904
+ else
905
+ used[rec] = true
906
+ local list = parse_marker_text(e.markers)
907
+ if same_markers(api, rec.take, list) then
908
+ unchanged = unchanged + 1
909
+ else
910
+ write_markers(api, { take = rec.take }, list)
911
+ api.UpdateItemInProject(rec.it)
912
+ restored = restored + 1
913
+ end
914
+ end
915
+ end
916
+ return { restored = restored, unchanged = unchanged, missing = missing }
917
+ end
918
+
790
919
  -- Edits existing markers by index: {index, position?, sourcePosition?, slope?}. Positions are item
791
920
  -- seconds. Every edit is checked against the whole list first (an edit that folds time, i.e. puts a
792
921
  -- marker out of order in position or source, refuses the call); markers are then rewritten in one undo point.