Automate Multitrack Audio with Ecasound Scripts

This article demonstrates how to automate multitrack audio processing, mixing, and recording tasks using shell scripts to control Ecasound. By interfacing directly with Ecasound’s command-line flags or feeding commands into its interactive control mode, a shell script can dynamically configure signal chains, manage multiple inputs and outputs, and execute precise audio routing without manual user intervention.

Understanding Ecasound Control Methods

Ecasound provides two primary ways to interface with shell scripts:

  1. Non-Interactive Batch Execution: Suitable for static render and mix jobs where all inputs, chains, and outputs are defined once as command-line arguments.
  2. Ecasound Interactive Access Mode (EIAM): Initiated with the -c flag, this mode reads text commands from standard input (stdin). This allows a shell script to send real-time control directives such as creating chains, starting playback, muting tracks, altering effect parameters, and safely stopping the engine.

Method 1: Static Multitrack Mixing via Bash

For offline mixing, format conversion, and chain processing, you can declare all parameters inside a single script execution using standard arguments:

#!/usr/bin/env bash

# Define input files
TRACK_1="drums.wav"
TRACK_2="bass.wav"
OUTPUT="final_mix.wav"

# Combine tracks into a stereo mix
ecasound \
  -a:1 -i:"$TRACK_1" -ea:120 \
  -a:2 -i:"$TRACK_2" -ea:90 \
  -a:1,2 -o:"$OUTPUT"

In this script:

Method 2: Dynamic Automation Using EIAM and HereDocs

When tasks require sequencing—such as arming tracks, running for an exact duration, adjusting parameters mid-session, and stopping cleanly—pipe EIAM commands directly into Ecasound via standard input:

#!/usr/bin/env bash

# Setup session parameters
SESSION_NAME="multitrack_recording"
DURATION=10

# Pass interactive commands sequentially via HereDoc
ecasound -c << EOF
cs-add $SESSION_NAME

# Setup Track 1 (Microphone input to file)
c-add vocal
ai-add /dev/dsp
ao-add vocals_take.wav

# Setup Track 2 (Backing track playback)
c-add backing
ai-add backing_track.wav
ao-add /dev/dsp

# Arm and record
cs-connect
start
! sleep $DURATION
stop
cs-disconnect
quit
EOF

Key commands utilized in the workflow:

Method 3: Bidirectional Automation with Named Pipes (FIFO)

For asynchronous, event-driven audio tasks—such as updating filter parameters in response to network requests or sensor inputs—use a Unix named pipe (FIFO) to send commands to a persistent background Ecasound daemon:

#!/usr/bin/env bash

FIFO="/tmp/ecasound_fifo"

# Create named pipe
[[ -p "$FIFO" ]] || mkfifo "$FIFO"

# Launch Ecasound in the background listening to the FIFO
ecasound -c < "$FIFO" > /dev/null 2>&1 &
ECASOUND_PID=$!

# Ensure cleanup on exit
trap 'kill "$ECASOUND_PID"; rm -f "$FIFO"' EXIT

# Helper function to send commands
send_cmd() {
  echo "$1" > "$FIFO"
}

# Configure and run session
send_cmd "cs-add live_stream"
send_cmd "c-add master"
send_cmd "ai-add alsa,default"
send_cmd "ao-add stream.wav"
send_cmd "cop-add -ef1:1000"  # Add low-pass filter at 1000Hz
send_cmd "start"

# Dynamically alter the filter parameter during execution
sleep 5
send_cmd "cop-set 1,1,2000"   # Open filter to 2000Hz

sleep 5
send_cmd "stop"
send_cmd "quit"

Using a FIFO enables the separation of Ecasound's digital signal engine from your script’s application logic, allowing dynamic adjustment of routing, levels, and multitrack transport states programmatically.