Can i route tracks with lua?

Hello everybody,
i have a script that adds 3 Tracks and one Bus to the session. Is it possible to automaticaly route the tracks to the bus?
here is the script:

function factory () return function ()
	local created_tracks = {}
soundbus = Session:new_audio_route(2, 2, nil, 1, "SoundBus", ARDOUR.PresentationInfo.Flag.AudioBus, ARDOUR.PresentationInfo.max_order)
tr = Session:new_audio_track (2, 2, nil, 1, "Sound", -1, ARDOUR.TrackMode.Normal, true)
tr = Session:new_audio_track (2, 2, nil, 1, "Sound", -2, ARDOUR.TrackMode.Normal, true)
tr = Session:new_audio_track (2, 2, nil, 1, "Sound", -3, ARDOUR.TrackMode.Normal, true)
Editor:access_action("Editor", "select-none")

end
end

Also i wonder how could i automatically pan the three tracks to be hard left, center and hard right. I am happy about any help. :slight_smile:

You absolutely can. I can upload my script later that autoroutes tracks based on their group assignment.

2 Likes
    local function connectAudioRoutes(source, destination)
        assert(source)
        assert(destination)

        if source:name() ~= destination:name()
        then
            for i = 1, source:n_outputs():n_audio() do
                local source_port = source:output():audio(i - 1)
                local destination_port = destination:input():audio(i - 1)

                source_port:disconnect_all() -- FIXME: Check if output port is connected to the port already and disconnect all other ports
                source_port:connect(destination_port:name())
            end
        end
    end

Here’s my take on the connection part. You should call that function after each Session:new_audio_track with tr as source and soundBus as destination.

Pretty sure it’ll work only on stereo tracks tho! And there is probably room for improvement with the disconnect_all call. But for me, it does the job :slight_smile:

1 Like

Sorry but i am a noob… :see_no_evil: This is how i tried to add your function but it does not work. The script now only adds 1 Bus and 1 Track. There is no Routing and i can’t call the script again.


local function connectAudioRoutes(source, destination)
        assert(source)
        assert(destination)

        if source:name() ~= destination:name()
        then
            for i = 1, source:n_outputs():n_audio() do
                local source_port = source:output():audio(i - 1)
                local destination_port = destination:input():audio(i - 1)

                source_port:disconnect_all() -- FIXME: Check if output port is connected to the port already and disconnect all other ports
                source_port:connect(destination_port:name())
            end
        end
    end

function factory () return function ()
soundbus = Session:new_audio_route(2, 2, nil, 1, "SoundBus", ARDOUR.PresentationInfo.Flag.AudioBus, ARDOUR.PresentationInfo.max_order)
tr = Session:new_audio_track (2, 2, nil, 1, "Sound", -1, ARDOUR.TrackMode.Normal, true)
connectAudioRoutes(tr, soundbus)
tr = Session:new_audio_track (2, 2, nil, 1, "Sound", -2, ARDOUR.TrackMode.Normal, true)
connectAudioRoutes(tr, soundbus)
tr = Session:new_audio_track (2, 2, nil, 1, "Sound", -3, ARDOUR.TrackMode.Normal, true)
connectAudioRoutes(tr, soundbus)
Editor:access_action("Editor", "select-none")

end
end

Thanks for sharing your function! :slight_smile:

Change the name of the second and third track, ardour doesn’t support tracks with identical names (afaik).

When you run your script in the scripting window (not sure what’s it called in ardour) it should print errors there.

The script worked fine and the tracks got called Sound 1, Sound 2, etc…
Good tip with the scripting window!
The Error i got is:
LuaException: [string "ardour {..."]:13: attempt to call a nil value (method 'name')
I think this refers to the if statement. When i delete that i get:
LuaException: [string "ardour {..."]:15: attempt to call a nil value (method 'n_outputs')
Seems like my ardour does not know these methods?

Found the issue: Session:new_audio_track returns a RouteList, not a Route (due to the fact that you can create many identical routes in one function call), so you should put :front() after your function call to get the first (and in your case only) route from the list. Needed for both the bus and the route.

Not sure about your system, but the script runs for me only in Ardour 8.12. 9.2 crashes when I try to create a bus (midi or audio).

ardour {
    ["type"]    = "EditorAction",
    name        = "Add Bus and Tracks",
    license     = "MIT",
    author      = "author",
    description = [[Add Bus and Tracks]]
}

function factory()
    return function()
        local function connectAudioRoutes(source, destination)
            assert(source)
            assert(destination)

            if source:name() ~= destination:name()
            then
                for i = 1, source:n_outputs():n_audio() do
                    local source_port = source:output():audio(i - 1)
                    local destination_port = destination:input():audio(i - 1)

                    source_port:disconnect_all() -- FIXME: Check if output port is connected to the port already and disconnect all other ports
                    source_port:connect(destination_port:name())
                end
            end
        end

        soundbus = Session:new_audio_route(2, 2, nil, 1, "SoundBus", ARDOUR.PresentationInfo.Flag.AudioBus,
            ARDOUR.PresentationInfo.max_order):front()
        tr = Session:new_audio_track(2, 2, nil, 1, "Sound", -1, ARDOUR.TrackMode.Normal, true):front()
        connectAudioRoutes(tr, soundbus)
        tr = Session:new_audio_track(2, 2, nil, 1, "Sound", -2, ARDOUR.TrackMode.Normal, true):front()
        connectAudioRoutes(tr, soundbus)
        tr = Session:new_audio_track(2, 2, nil, 1, "Sound", -3, ARDOUR.TrackMode.Normal, true):front()
        connectAudioRoutes(tr, soundbus)
        Editor:access_action("Editor", "select-none")
    end
end

Works in Ardour 8.12 as you intended.

1 Like

It works! (ardour 8.6) Thanks a lot! I am doing this process everyday multiple times. This will save me lots of time and keep me in the flow. :slight_smile:

I never did the panning so far, can’t help you with that…

But glad that the rest of the script works :heart: