@mthines/reaper-mcp 0.93.1 → 0.94.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mthines/reaper-mcp",
3
- "version": "0.93.1",
3
+ "version": "0.94.0",
4
4
  "type": "module",
5
5
  "description": "MCP server for controlling REAPER DAW — real-time mixing, FX control, and frequency analysis for AI agents",
6
6
  "license": "MIT",
@@ -41,6 +41,7 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@modelcontextprotocol/sdk": "^1.12.0",
44
+ "@openfeature/server-sdk": "^1.23.0",
44
45
  "@opentelemetry/api": "^1.9.0",
45
46
  "@opentelemetry/api-logs": "^0.213.0",
46
47
  "@opentelemetry/auto-instrumentations-node": "^0.71.0",
@@ -371,5 +371,34 @@ function M.set_selected_items(api, items, mode)
371
371
  return { selected = selected }
372
372
  end
373
373
 
374
+ -- The items selected right now: where each sits (track and item index as list_media_items reports them),
375
+ -- its track's name, start, length and mute. Read-only.
376
+ function M.get_selected_items(api, offset, limit)
377
+ offset = math.max(0, math.floor(tonumber(offset) or 0))
378
+ limit = math.max(1, math.floor(tonumber(limit) or 200))
379
+ local items, total = {}, 0
380
+ for i = 0, api.CountTracks(0) - 1 do
381
+ local tr = api.GetTrack(0, i)
382
+ local _, track_name = api.GetTrackName(tr)
383
+ for j = 0, api.CountTrackMediaItems(tr) - 1 do
384
+ local it = api.GetTrackMediaItem(tr, j)
385
+ if api.IsMediaItemSelected(it) then
386
+ total = total + 1
387
+ if total > offset and #items < limit then
388
+ items[#items + 1] = {
389
+ trackIndex = i,
390
+ trackName = track_name,
391
+ itemIndex = j,
392
+ position = api.GetMediaItemInfo_Value(it, "D_POSITION"),
393
+ length = api.GetMediaItemInfo_Value(it, "D_LENGTH"),
394
+ muted = api.GetMediaItemInfo_Value(it, "B_MUTE") == 1,
395
+ }
396
+ end
397
+ end
398
+ end
399
+ end
400
+ return { total = total, offset = offset, hasMore = offset + #items < total, items = items }
401
+ end
402
+
374
403
  M.section_id = section_id
375
404
  return M
@@ -1520,6 +1520,17 @@ function handlers.snapshot_save(params)
1520
1520
  trackIndices = params.trackIndices,
1521
1521
  fullChunk = params.fullChunk,
1522
1522
  })
1523
+ -- Stretch markers of the audio items on the captured tracks, kept apart from the track chunk (REAPER
1524
+ -- refuses to apply a chunk on tracks whose items carry Melodyne take FX)
1525
+ local marker_items = 0
1526
+ if params.stretchMarkers == true then
1527
+ local captured = {}
1528
+ for _, t in ipairs(mixer_state.tracks) do captured[#captured + 1] = t.index end
1529
+ local entries, err = ext.mix.run("capture_stretch_markers", captured)
1530
+ if not entries then return nil, err end
1531
+ mixer_state.stretchMarkers = entries
1532
+ marker_items = #entries
1533
+ end
1523
1534
  -- Metadata first, mixerState last: a lister can read name/description/trackCount from the
1524
1535
  -- head of the file (mcp_snapshot_lib.lua's read_snapshot_meta reads only that prefix)
1525
1536
  local content = "{" .. table.concat({
@@ -1547,6 +1558,7 @@ function handlers.snapshot_save(params)
1547
1558
  scoped = mixer_state.scoped == true,
1548
1559
  fullChunk = mixer_state.fullChunk == true,
1549
1560
  tracksCaptured = #mixer_state.tracks,
1561
+ stretchMarkerItems = params.stretchMarkers == true and marker_items or nil,
1550
1562
  }
1551
1563
  end
1552
1564
 
@@ -1607,7 +1619,10 @@ function handlers.snapshot_restore(params)
1607
1619
  if reaper.SetTrackStateChunk(track, base64_decode(track_state.chunkB64), false) then
1608
1620
  restored = restored + 1
1609
1621
  else
1610
- warnings[#warnings + 1] = "Track " .. track_state.index .. ": SetTrackStateChunk failed"
1622
+ warnings[#warnings + 1] = "Track " .. track_state.index .. " '" .. tostring(track_state.name)
1623
+ .. "': REAPER refused the saved track state (SetTrackStateChunk failed), the track is unchanged"
1624
+ .. (state.stretchMarkers and params.restoreStretchMarkers ~= false and "; its stretch markers are restored separately" or
1625
+ "; tracks with Melodyne (ARA) take FX can do this: save with stretchMarkers for their item edits")
1611
1626
  end
1612
1627
  else
1613
1628
  -- Basic mixer state (all versions)
@@ -1685,6 +1700,25 @@ function handlers.snapshot_restore(params)
1685
1700
  end
1686
1701
  end
1687
1702
 
1703
+ -- Stretch markers saved with the snapshot (snapshot_save stretchMarkers): put back after the tracks,
1704
+ -- also on tracks whose chunk could not be applied
1705
+ local marker_result
1706
+ if params.restoreStretchMarkers ~= false and state.stretchMarkers then
1707
+ local entries = state.stretchMarkers
1708
+ if type(entries) == "string" then entries = json_decode_array(entries) end
1709
+ if type(entries) == "table" then
1710
+ local res, err = ext.mix.run("restore_stretch_markers", entries)
1711
+ if res then
1712
+ marker_result = res
1713
+ if res.missing > 0 then
1714
+ warnings[#warnings + 1] = res.missing .. " saved item(s) no longer exist, their stretch markers were not restored"
1715
+ end
1716
+ else
1717
+ warnings[#warnings + 1] = "Stretch markers not restored: " .. tostring(err)
1718
+ end
1719
+ end
1720
+ end
1721
+
1688
1722
  reaper.Undo_EndBlock("MCP: Restore snapshot '" .. name .. "'", -1)
1689
1723
 
1690
1724
  reaper.TrackList_AdjustWindows(false)
@@ -1696,6 +1730,7 @@ function handlers.snapshot_restore(params)
1696
1730
  timestamp = snapshot.timestamp,
1697
1731
  tracksRestored = restored,
1698
1732
  version = version,
1733
+ stretchMarkers = marker_result,
1699
1734
  }
1700
1735
  if #warnings > 0 then result.warnings = warnings end
1701
1736
  return result
@@ -3529,6 +3564,12 @@ function handlers.set_selected_tracks(params)
3529
3564
  end)
3530
3565
  end
3531
3566
 
3567
+ function handlers.get_selected_items(params)
3568
+ return with_actions_lib(function()
3569
+ return actions_lib.get_selected_items(reaper, params.offset, params.limit)
3570
+ end)
3571
+ end
3572
+
3532
3573
  function handlers.set_selected_items(params)
3533
3574
  return with_actions_lib(function()
3534
3575
  local items, err = decode_list(params.items, "items")
@@ -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.