Skip to main content

SCRIPT_TEMPLATE_AUP3

Constant SCRIPT_TEMPLATE_AUP3 

Source
const SCRIPT_TEMPLATE_AUP3: &str = "#!/usr/bin/env python3\n\"\"\"Open @@XIL_SHOW_LABEL@@ @@XIL_TAG@@ DAW layers in Audacity.\n\nRun this script while Audacity is open.  If mod-script-pipe is\nenabled the layer WAVs are imported automatically.  Otherwise the\nfile paths and manual import instructions are printed below.\n\nEnable mod-script-pipe in Audacity 3:\n  Edit > Preferences > Modules > mod-script-pipe \u{2192} Enabled\n  (restart Audacity after enabling)\n\nAudacity 4 ships no modules directory and has no scripting pipe, so\nthe import there is always manual \u{2014} the instructions below are the\nwhole story.\n\"\"\"\nimport os\nimport sys\n\nBASE_DIR = os.path.dirname(os.path.abspath(__file__))\nLAYERS = @@XIL_LAYERS_REPR@@\n\n\ndef try_pipe_import(layers):\n    \"\"\"Attempt Audacity import via mod-script-pipe named pipe.\"\"\"\n    import platform\n    import subprocess\n    # On WSL, Audacity\'s pipe is a Windows kernel object unreachable from Linux\n    # Python.  Re-invoke this script via Windows python.exe so it uses the\n    # Windows pipe paths.\n    if platform.system() != \"Windows\" and \"WSL_DISTRO_NAME\" in os.environ:\n        try:\n            win_script = subprocess.check_output(\n                [\"wslpath\", \"-w\", os.path.abspath(__file__)]\n            ).decode().strip()\n            result = subprocess.run([\"python.exe\", win_script])\n            return result.returncode == 0\n        except (subprocess.CalledProcessError, FileNotFoundError, OSError) as exc:\n            print(f\"[!] WSL re-invoke failed: {exc}\")\n            return False\n    if platform.system() == \"Windows\":\n        tofile = r\"\\\\.\\pipe\\ToSrvPipe\"\n        fromfile = r\"\\\\.\\pipe\\FromSrvPipe\"\n    else:\n        tofile = \"/tmp/audacity_script_pipe.to.app\"\n        fromfile = \"/tmp/audacity_script_pipe.from.app\"\n\n    try:\n        pipe_to = open(tofile, \"wb\", buffering=0)\n        pipe_from = open(fromfile, \"rb\", buffering=0)\n        try:\n            def send(cmd):\n                pipe_to.write((cmd + \"\\n\").encode(\"utf-8\"))\n                lines = []\n                while True:\n                    line = pipe_from.readline().decode(\"utf-8\")\n                    if line in (\"\", \"\\n\"):\n                        break\n                    lines.append(line.rstrip())\n                return lines\n\n            send(\"New:\")\n            wav_count = 0\n            for name, filename in layers:\n                if not filename.endswith(\".wav\"):\n                    continue\n                full_path = os.path.join(BASE_DIR, filename)\n                send(f\"Import2: Filename={full_path}\")\n                send(f\"SetTrackStatus: Name={name}\")\n                wav_count += 1\n            print(f\"[\u{2713}] Imported {wav_count} WAV tracks into Audacity.\")\n            aup3_path = os.path.join(BASE_DIR, \"@@XIL_TAG@@.aup3\")\n            send(f\"SaveProject2: Filename={aup3_path}\")\n            print(f\"[\u{2713}] Saved project: {aup3_path}\")\n        finally:\n            pipe_to.close()\n            pipe_from.close()\n        return True\n    except OSError as exc:\n        print(f\"[!] Pipe import failed: {exc}\")\n        return False\n\n\ndef print_instructions(layers):\n    \"\"\"Print manual import instructions.\"\"\"\n    wavs = [(n, f) for n, f in layers if f.endswith(\".wav\")]\n    labels = [(n, f) for n, f in layers if f.endswith(\".txt\")]\n    print()\n    print(f\"@@XIL_SHOW_LABEL@@ @@XIL_TAG@@ \u{2014} Audacity Layer Import\")\n    print(\"=\" * 45)\n    print()\n    print(f\"Import these {len(wavs)} WAV files into Audacity:\")\n    print(\"  File > Import > Audio...  (Ctrl+Shift+I on Windows/Linux)\")\n    print()\n    for i, (name, filename) in enumerate(wavs, 1):\n        full = os.path.join(BASE_DIR, filename)\n        print(f\"  {i}. {name:<12}  {full}\")\n    if labels:\n        print()\n        print(\"Optional label tracks (File > Import > Labels...):\")\n        for i, (name, filename) in enumerate(labels, 1):\n            full = os.path.join(BASE_DIR, filename)\n            print(f\"  {i}. {name:<20}  {full}\")\n        print()\n        print(\"  Audacity 4 does not name an imported label track after\")\n        print(\"  its file, so import them in the order above and rename\")\n        print(\"  each track to the name shown \u{2014} they are otherwise\")\n        print(\"  indistinguishable.\")\n    print()\n    print(\"After importing, all tracks are pre-aligned at t=0.\")\n    print(\"No repositioning needed \u{2014} just mix levels and export.\")\n    print()\n    print(\"Suggested track order (top to bottom in Audacity):\")\n    print(\"  1. Dialogue  \u{2014} speaking parts (phone filter already applied)\")\n    print(\"  2. Music     \u{2014} stings and themes\")\n    print(\"  3. SFX       \u{2014} one-shot effects\")\n    print(\"  4. Ambience  \u{2014} background loops\")\n    print()\n\n\nif __name__ == \"__main__\":\n    if not try_pipe_import(LAYERS):\n        print_instructions(LAYERS)\n";