Snap audio region to playhead (Ardour 7)

I need to snap audio regions to the playhead location. @x42 has shared a code to do this with a LUA script, but seems to work only in Ardour 6.0. (error: 28: bad argument #1 to ‘set_position’ (timepos_t expected, got number))
Is it possible to adapt this for the current Ardour 7? Could someone generously share how the code would be?

My situation is exactly as the user from the previous post. Using Ardour synced with Blender via Jack Transport.

Here is a copy of LUA script for Ardour 6:

~/.config/ardour6/scripts/move_regions_to_playhead.lua

ardour {
  ["type"]    = "EditorAction",
  name        = "Move Regions to Playhead",
  license     = "MIT",
  author      = "Ardour Team",
  description = "Move selected regions to playhead position"
}

function factory () return function ()

  local sel = Editor:get_selection () -- get current selection
  local sel_regions = sel.regions:regionlist() -- get selected regions

  local playhead = Session:transport_sample () -- get playhead position

  -- prepare undo operation
  Session:begin_reversible_command ("Move Regions to Playhead")

  -- iterate over selected regions
  for region in sel.regions:regionlist ():iter () do
      -- prepare for undo operation
      region:to_stateful ():clear_changes ()

      -- move region to playhead position
      region:set_position (playhead, 0)

      -- collect undo/redo diff
      Session:add_stateful_diff_command (region:to_statefuldestructible ())
  end

  -- all done, commit the combined Undo Operation (if any)
  if not Session:abort_empty_reversible_command () then
    Session:commit_reversible_command (nil)
  end

end end

The time representation was overhauled, and API changed in Ardour 7. There is no longer a 2nd argument for the bar/beat rounding. instead use

region:set_position (Temporal.timepos_t (playhead))

1 Like

Perfecto! It works.

I see also there’s an action “Snap/snap-to-timecode-frame”. I suppose it activates snapping to playhead regions dragged with the mouse. How would a script to activate it look like? I was trying the following without success.

ardour {
    ["type"]    = "EditorAction",
    name        = "Snap to playhead",
    license     = "MIT",
    author      = "Domingo Riesco",
    description = "Snap regions to playhead"
}

function factory () return function ()

    Editor:access_action("Snap","snap-to-timecode-frame")

end end

Strange enough, the script above works but it doesn’t move the selected region exactly to the playhead’s location. It always arrives around 2 frames after the playhead (see pic).

Screenshot from 2022-11-30 18-17-29

I ignore why this happens. Meanwhile I discovered the command “Align sync relative” which is activated by pressing the ‘x’ key and seems to do what I need.

Which of the above scripts do you refer to?

The code I referred to initially, from the other post, with the shared correction for it to work in Ardour 7 included. The code I tested (which doesn’t hit the playhead exactly on my system) goes as follows:

ardour {
  ["type"]    = "EditorAction",
  name        = "Move Regions to Playhead",
  license     = "MIT",
  author      = "Ardour Team",
  description = "Move selected regions to playhead position"
}

function factory () return function ()

  local sel = Editor:get_selection () -- get current selection
  local sel_regions = sel.regions:regionlist() -- get selected regions

  local playhead = Session:transport_sample () -- get playhead position

  -- prepare undo operation
  Session:begin_reversible_command ("Move Regions to Playhead")

  -- iterate over selected regions
  for region in sel.regions:regionlist ():iter () do
      -- prepare for undo operation
      region:to_stateful ():clear_changes ()

      -- move region to playhead position
      region:set_position (Temporal.timepos_t (playhead))

      -- collect undo/redo diff
      Session:add_stateful_diff_command (region:to_statefuldestructible ())
  end

  -- all done, commit the combined Undo Operation (if any)
  if not Session:abort_empty_reversible_command () then
    Session:commit_reversible_command (nil)
  end

end end

I have just checked this here and cannot reproduce this. The region snaps precisely to the playhead. I also tried to trim region start and lock it to music time, and it works reliably. I am using current ardour/git - so perhaps there was an issue with 7.0/7.1

Oddly enough in your screenshot neither region-fade, nor region-name is visible.

If you can reliably reproduce this with a given session, could you upload it and attach it to an issue on tracker.ardour.org? Thanks!

Done, submitted here: 0009132: Jack Transport: LUA script for snapping region to playhead behaves bizarre - MantisBT

I noticed that it only fails when transport master is set to JACK. When timecode is set to INT the script works fine, it seems to be more complex. The bug reported including that information.

Ah, I do not use JACK, so that can explain things.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.