How to import audio from sources with Lua

What would be the way to go to import an audio file from the Sources (in the Editor List) to an existing audio track in Lua?
Thanks in advance! :slight_smile:

Does this help?

ardour { ["type"] = "Snippet", name = "Add all Sources to a new Track" }

function factory () return function () 

  -- default track output channel count (= master bus input count)
  local n_chan_out  = 2
  if not Session:master_out():isnil() then
    n_chan_out = Session:master_out():n_inputs ():n_audio ()

  -- create a stereo track
  local newtracks = Session:new_audio_track (2, n_chan_out, nil, 1, "All Audio Sources", ARDOUR.PresentationInfo.max_order, ARDOUR.TrackMode.Normal, true)
  -- get playlist of the new track
  local playlist = newtracks:front():playlist()

  -- add 1st region at the start of the session
  local position = Temporal.timepos_t(0)

  -- get all regions in this session...
  local rl = ARDOUR.RegionFactory.regions()

  -- ...and iterate over them
  for _, r in rl:iter() do
    -- only look for "sources", which are represented by "whole file regions"
    -- and filter by audio-regions
    if r:whole_file() and not r:to_audioregion():isnil() then
      print (r:name())
      -- add region to the track's playlist
      playlist:add_region (r, position, 1, false)
      position = position + r:length ()

end end
1 Like

Thank you, mate, that’s a great help to me! :slight_smile:
I didn’t know, where to start.
there are still some uncertainties to me:

  1. What does the _, at the beginning of the for-loop do?
  2. I want to add a region with a specific name from the sources, so I changed the for-loop like this:
for _, r in rl:iter() do    
                -- Check regions for a specific name...
				if r:name() == audioTrackname then -- audioTrackname is a variable containing the search name
                -- ...and add it to the playlist
				playlist:add_region (r, Temporal.timepos_t(0), 1, false)

But in case, there are multiple regions with the same name, I need a second condition that picks the most current region with that name, which I then add to the playlist. Any hints for this?

  1. A bit off-topic, but is there a way to embed a Lua scrtipt into Ardours context menus?

EDIT: Okay, I figured out, that there are tables in Lua, which differ from arrays in regard of indexing their elements. So the undrescore represents the index of the regionlist table?