Querying Ecasound Sample Rate via ECI

Querying the current sample rate of an active session in Ecasound using the Ecasound Control Interface (ECI) involves issuing interactive commands to inspect the active chainsetup and reading the engine's response. Because ECI operates as a command-response messaging interface across C, Python, Perl, and shell environments, applications obtain audio configuration details by dispatching status queries and parsing the returned session parameters.

The ECI Command-Response Architecture

ECI communicates with the Ecasound engine using procedural functions that send Interactive Audio Mode (IAM) commands and retrieve typed outputs. To query any parameter, an application uses three primary steps:

  1. Send an interactive command using eci_command() (or the language binding equivalent, such as eci.command() in Python).
  2. Check for errors using eci_last_error().
  3. Retrieve the response data using the appropriate retrieval function, typically eci_last_string() for multi-line status reports.

Querying the Sample Rate via cs-status

In Ecasound, sample rate is tied to the chainsetup (cs) governing the session. The most direct method to extract the operational sample rate from a running session is via the cs-status command.

When an application sends cs-status, Ecasound returns a formatted text block detailing the current chainsetup state, including the audio format parameters (bit depth, channels, and sample rate).

Python Example

import pyecasound

# Initialize the ECI handle
eci = pyecasound.ECA_CONTROL_INTERFACE()

# Select the current chainsetup and query its status
eci.command("cs-status")
status_output = eci.last_string()

# Parse the sample rate from the format line
sample_rate = None
for line in status_output.splitlines():
    if "Format:" in line or "sample rate" in line.lower():
        # Ecasound reports format attributes like: "Format: s16_le, 2 channels, 44100 Hz"
        parts = line.split(",")
        for part in parts:
            if "Hz" in part:
                sample_rate = int(part.replace("Hz", "").strip())
                break

print(f"Current Sample Rate: {sample_rate} Hz")

C API Example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libecasound/eca-control-interface.h>

int main(void) {
    eci_init();

    // Query active chainsetup status
    eci_command("cs-status");
    const char *status = eci_last_string();

    // Search the output string for the sampling rate identifier
    const char *hz_pos = strstr(status, " Hz");
    if (hz_pos != NULL) {
        // Step backward to locate the beginning of the numerical rate
        const char *start = hz_pos;
        while (start > status && *(start - 1) >= '0' && *(start - 1) <= '9') {
            start--;
        }
        int sample_rate = atoi(start);
        printf("Running sample rate: %d Hz\n", sample_rate);
    }

    eci_cleanup();
    return 0;
}

Querying Audio Object Status Directly

If an application needs to verify the sample rate of a specific audio input or output rather than the global chainsetup, it can target the selected audio object directly using ai-status (audio input) or ao-status (audio output):

  1. Target the audio object: eci_command("ai-select 1") or eci_command("ao-select 1").
  2. Request the object status: eci_command("aio-status").
  3. Read the returned description with eci_last_string() to extract the hardware or file sample rate configured for that specific stream.

By inspecting either cs-status for the chainsetup processing rate or aio-status for target I/O rates, client applications can monitor and adapt to the running session's audio configuration dynamically.