❓How can I create Ardour tracks from a JSON file using Lua?How can I create Ardour tracks from a JSON file using Lua?

Hi everyone,

I’m working on a project where I want to automatically create an Ardour session by reading track information from a JSON file.


:white_check_mark: Goal

I want to:

  • Read instrument data (like track name and type) from a JSON file.
  • Automatically create tracks in Ardour using Lua scripting.
  • Correctly handle mono vs stereo tracks based on the "Type" field.

:receipt: Example JSON Input

Here’s a sample of how my input JSON looks:

json

CopyEdit

[
    {
        "Channel": "Channel",
        "Description": "Instrument",
        "Type": "Classified Type"
    },
    {
        "Channel": "1",
        "Description": "Kick In",
        "Type": "Mono"
    },
    {
        "Channel": "13-14",
        "Description": "Lead Guitar DI",
        "Type": "Stereo"
    }
    // ... More entries ...
]

:x: Challenge

Ardour’s Lua scripting engine does not support require or external Lua modules (like json.lua) due to sandboxing. So I’m not sure how to parse JSON inside the Lua script in Ardour.


:thinking: What I Tried

  • I tried using require("json"), but it throws:
    attempt to call a nil value (global 'require')
  • Attempting to use a global JSON object fails as well: attempt to index a nil value (global 'JSON')

:pushpin: My Question

What’s the best way to work around Ardour’s limitations and load this JSON-like data in a Lua script?

Would converting the JSON into a Lua table (i.e., track_data.lua) and using dofile() be the recommended way?

And if so, could someone provide a clean example of:

  1. How the track_data.lua file should look?
  2. How to loop over it and create mono/stereo tracks dynamically inside the Ardour Lua script?

Any help or suggestions would be greatly appreciated! :pray:

Thanks! :question: How can I create Ardour tracks from a JSON file using Lua?

Hi everyone,

I’m working on a project where I want to automatically create an Ardour session by reading track information from a JSON file.


:white_check_mark: Goal

I want to:

  • Read instrument data (like track name and type) from a JSON file.
  • Automatically create tracks in Ardour using Lua scripting.
  • Correctly handle mono vs stereo tracks based on the "Type" field.

:receipt: Example JSON Input

Here’s a sample of how my input JSON looks:

json

CopyEdit

[
    {
        "Channel": "Channel",
        "Description": "Instrument",
        "Type": "Classified Type"
    },
    {
        "Channel": "1",
        "Description": "Kick In",
        "Type": "Mono"
    },
    {
        "Channel": "13-14",
        "Description": "Lead Guitar DI",
        "Type": "Stereo"
    }
    // ... More entries ...
]

:x: Challenge

Ardour’s Lua scripting engine does not support require or external Lua modules (like json.lua) due to sandboxing. So I’m not sure how to parse JSON inside the Lua script in Ardour.


:thinking: What I Tried

  • I tried using require("json"), but it throws:
    attempt to call a nil value (global 'require')
  • Attempting to use a global JSON object fails as well: attempt to index a nil value (global 'JSON')

:pushpin: My Question

What’s the best way to work around Ardour’s limitations and load this JSON-like data in a Lua script?

Would converting the JSON into a Lua table (i.e., track_data.lua) and using dofile() be the recommended way?

And if so, could someone provide a clean example of:

  1. How the track_data.lua file should look?
  2. How to loop over it and create mono/stereo tracks dynamically inside the Ardour Lua script?

Any help or suggestions would be greatly appreciated! :pray:
Could you provide code as well it is highly appreciated
Thanks!

If that is really your goal, there is absolutely no reason to do this inside Ardour.

An Ardour session is just an XML file in a folder. You can create that with any tools of your choice.

Ya but I was not able to genrate exact session using python
import pdfplumber
import re
import json
import os

def classify_audio_type(col1, col2):
“”“Classifies audio type based on the first and second column values.”“”
if col2 and “Stereo” in col2:
return “Stereo”
elif col2 and “Mono” in col2:
return “Mono”
elif re.fullmatch(r"\d+“, col1):
return “Mono”
elif re.fullmatch(r”\d±\d+", col1):
return “Stereo”
return “Unknown”

def extract_and_classify_tables(pdf_path):
classified_data = []
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
extracted_tables = page.extract_tables()
for table in extracted_tables:
for row in table:
if row and len(row) >= 2:
col1 = row[0].strip() if row[0] else “”
col2 = row[1].strip() if row[1] else “”

                    if not col2:
                        continue

                    if col1.lower() == "channel":
                        classified_data.append({
                            "Channel": col1,
                            "Description": col2,
                            "Type": "Classified Type"
                        })
                    else:
                        audio_type = classify_audio_type(col1, col2)
                        classified_data.append({
                            "Channel": col1,
                            "Description": col2,
                            "Type": audio_type
                        })
return classified_data

pdf_path = “D:/Scripting _ Practice/Tech Readers/TECH rider example.pdf”
classified_rows = extract_and_classify_tables(pdf_path)

Determine the output path (same directory, different extension)

output_dir = os.path.dirname(pdf_path)
output_file = os.path.splitext(os.path.basename(pdf_path))[0] + “_classified.json”
output_path = os.path.join(output_dir, output_file)

Save JSON to file

with open(output_path, “w”, encoding=“utf-8”) as f:
json.dump(classified_rows, f, indent=4, ensure_ascii=False)

print(f"\nJSON saved to: {output_path}")
this is my python code could you please help me to do this task in python
I was not able to create session from the data which I am getting from python

I don’t provide generalized help on programming. Perhaps someone else might help you. I would note that “saving JSON to file” is not related to Ardour, since Ardour does not use JSON. Our session files are XML.

okk
Thank You So Much
I will surely explore this way as well

The reason for that Ardoru Lua scripts are saved with the session. So it needs to be self-contained and not rely on external resources. Those may not be available when load the session on another machine, or after some updates to your system.

You could however just copy/paste the Lua JSON parser entirely into your script…

An entirely different approach would be to do this outside Ardour. Generate a script and send it as commands to interactive interpreter which then creates the session.

e.g. you can run the following script just using ardour-lua as interpreter, from a terminal:

#!/opt/Ardour-8.12.0/bin/ardour8-lua

-- setup audio-engine (global variable)
AudioEngine:set_backend("None (Dummy)", "", "")

-- create a test session (or load it)
s = create_session ("/tmp/luatest", "luatest", 48000)
if s == nil then s = load_session ("/tmp/luatest", "luatest") end

-- look up master-bus (channel count)
inputs = 1;
outputs = inputs
mst = s:master_out();
if not mst:isnil() then outputs = mst:n_inputs():n_audio() end
mst = nil -- drop reference
assert (outputs > 0)

-- create 16 tracks
s:new_audio_track (inputs, outputs, nil, 16, "",  ARDOUR.PresentationInfo.max_order, ARDOUR.TrackMode.Normal)

s:save_state("")
close_session()
quit()