Control Audacity with Python Using Mod-Script-Pipe

Audacity's mod-script-pipe is a built-in module that opens an inter-process communication (IPC) channel, allowing external programming languages like Python to control the digital audio workstation. This article explains what mod-script-pipe is, how its underlying communication mechanism works, how to enable it within Audacity, and how Python scripts interact with it to automate complex audio editing and batch-processing tasks.

What is mod-script-pipe?

Mod-script-pipe is an optional dynamic library module included with Audacity. By default, Audacity is operated via its graphical user interface (GUI). Mod-script-pipe bypasses manual GUI interaction by creating a standard named-pipe interface on the host operating system. This interface exposes Audacity’s internal scripting commands to external processes, allowing users to automate virtually any action that can be executed via menus, shortcuts, or macros.

How the Named Pipe Mechanism Works

Once loaded, mod-script-pipe creates two unidirectional named pipes (FIFOs) in the operating system:

  1. ToSrvPipe (To Server Pipe): The external client (such as a Python script) writes text-based commands into this pipe to send instructions to Audacity.
  2. FromSrvPipe (From Server Pipe): Audacity writes command execution statuses, error codes, and returned data into this pipe for the client to read.

Communication is purely text-based. A command string is sent terminated by a newline character (\n), and Audacity processes the instruction and returns a response string (such as BatchCommand finished: OK or an error message) followed by a null character or newline to indicate completion.

Enabling mod-script-pipe in Audacity

To use the interface, it must first be activated within Audacity's settings:

  1. Launch Audacity.
  2. Navigate to Edit > Preferences (or Audacity > Preferences on macOS).
  3. Select the Modules tab from the left sidebar.
  4. Locate mod-script-pipe in the list and change its state from New or Disabled to Enabled.
  5. Click OK and restart Audacity.

Upon restarting, Audacity initializes the named pipes, making them available in the system's temporary directory or local pipe registry.

Interfacing with Python

Python connects to mod-script-pipe using standard file input/output operations or platform-specific pipe wrappers.

On Windows, the pipes are located under \\.\pipe\ToSrvPipe and \\.\pipe\FromSrvPipe. On macOS and Linux, they are standard FIFO files typically created in /tmp/audacity_script_pipe.to. and /tmp/audacity_script_pipe.from. (appended with the user's UID).

A basic Python automation routine follows this lifecycle:

  1. Establish Connection: The script opens the write pipe to send data and opens the read pipe to capture feedback.
  2. Send Commands: The script writes command strings to the write pipe. These commands include standard Audacity macro commands such as SelectTime: Start=0 End=10, Normalize:, or Export2: Filename="output.wav".
  3. Wait for Execution: Audacity handles the operation synchronously, writing back status lines to the read pipe.
  4. Read Output: The Python script reads from the read pipe until it receives an completion message (OK or Failed), ensuring synchronization before the next command is sent.
  5. Close Connection: The file descriptors are closed once the workflow completes.

Common Capabilities and Use Cases

Using Python via mod-script-pipe allows for headless operations, batch conversions, programmatically splitting tracks, applying effect chains to hundreds of files, analyzing audio metadata, and integrating Audacity into automated multimedia production pipelines.