Ecasound Control Interface GuideKai Vehmanen, Brad Bowman, Tony Leake, Jan Weil, Mario Lang03062006 |
The idea behind the Ecasound Control Interface (ECI) is to take a subset of functionality provided by libecasound, provide a simple API for it, and port it to various languages. At the moment, implementations of the ECI API are available for C, C++, elisp, Python and Ruby. These all come by default with the Ecasound package. Additional implementations, for example for Perl and PHP, are distributed independently.
ECI is heavily based on Ecasound’s interactive mode (EIAM), and the services it provides. See ecasound-iam(1) manual page for a detailed EIAM documentation.
ECI doesn’t provide any routines that directly manipulate audio or Ecasound objects. What it does provide is an easy and generic way to issue EIAM (Ecasound Inter-Active Mode) commands, access the command return-values and perform error handling.
This approach has two benefits. First, it is possible to keep the API small, and thus make it easier to port ECI to new languages. Secondly, it’s possible to keep ECI relatively stable. Ecasound itself is a large, developing library. New features are added all the time, and from time to time, older parts of the library will get rewritten to better suit new uses. Now for application developers wanting to take advantage of libecasound, these constant changes are very annoying, especially if your specific app doesn’t need the latest new features. In these cases, ECI is the best platform for application development.
Specific tasks ECI is aimed at:
Here is a list of services provided by all ECI implementations:
Each EIAM command has exactly one return value type. After a command has been issued, only one last_type() functions returns a non-empty value. For example, last_float() only returns a valid value if last_type() == ’f’ holds true. Not all EIAM commands return a value (return type is void).
Porting ECI to new languages should be easy. All there is to do is to implement the services listed in the previous section to the target language. In most cases it’s to easiest to use the C++ or C ECI as the underlying implementation to build upon.
This section contains overview of how ECI is implemented in the discussed language (eg. as a single class, set of classes, set of routines, etc).
A quick tutorial to get you started.
Implementation of the following:
The C implementation of ECI is not directly linked against the main Ecasound libraries. Instead, the ecasound executable is launched on the background and command pipes are used to communicate with it.
The launched ecasound executable can be selected by using the ECASOUND environment variable. If it is not defined, the C ECI implementation will try to launch “ecasound” (ie. has to be somewhere in PATH).
In addition to the C implementation, this also affects all ECI implementations that are based on the C version. Currently this includes at least the Perl, PHP and Python ECI modules.
C++ implementation is based around the ECA_CONTROL_INTERFACE class. STL vector is used for representing collections of objects (last_string_list()).
#include <iostream> #include <unistd.h> #include <eca-control-interface.h> int main(int argc, char *argv[]) { double cutoff_inc = 500.0; ECA_CONTROL_INTERFACE e; e.command("cs-add play_chainsetup"); e.command("c-add 1st_chain"); e.command("ai-add some_file.wav"); e.command("ao-add /dev/dsp"); e.command("cop-add -efl:100"); e.command("cop-select 1"); e.command("copp-select 1"); e.command("cs-connect"); e.command("start"); while(1) { sleep(1); e.command("engine-status"); if (e.last_string() != "running") break; e.command("get-position"); double curpos = e.last_float(); if (curpos > 15.0) break; e.command("copp-get"); double next_cutoff = cutoff_inc + e.last_float(); e.command_float_arg("copp-set", next_cutoff); } e.command("stop"); e.command("cs-disconnect"); e.command("cop-status"); cerr << "Chain operator status: " << e.last_string() << endl; return(0); }
All C ECI functions are prefixed with "eci_". When returning string values, a const pointer to a null-terminated char array (const char*) is returned. It’s important to keep in mind that these are "borrowed" references. If you need to later use the data, you must copy it to application’s own buffers.
Returning a list of strings is implemented using two functions: eci_last_string_list_count() returns the number of strings available, and eci_last_string_list_item(int n) returns a pointer (const char*) to the string at index n.
Note! As of Ecasound 2.0.1, the C ECI implementation also provides reentrant access to the ECI API. These alternative routines are marked with ’_r’ postfix.
#include <stdio.h> #include <unistd.h> #include <ecasoundc.h> int main(int argc, char *argv[]) { double cutoff_inc = 500.0; eci_init(); eci_command("cs-add play_chainsetup"); eci_command("c-add 1st_chain"); eci_command("ai-add some_file.wav"); eci_command("ao-add /dev/dsp"); eci_command("cop-add -efl:100"); eci_command("cop-select 1"); eci_command("copp-select 1"); eci_command("cs-connect"); eci_command("start"); while(1) { double curpos, next_cutoff; sleep(1); eci_command("engine-status"); if (strcmp(eci_last_string(), "running") != 0) break; eci_command("get-position"); curpos = eci_last_float(); if (curpos > 15.0) break; eci_command("copp-get"); next_cutoff = cutoff_inc + eci_last_float(); eci_command_float_arg("copp-set", next_cutoff); } eci_command("stop"); eci_command("cs-disconnect"); eci_command("cop-status"); printf("Chain operator status: %s", eci_last_string()); eci_cleanup(); return(0); }
The Ecasound package comes with an ’Ecasound’ library for Emacs included. ecasound.el is a implementation of the ECI API for Emacs, as well as an interactive interface to Ecasound sessions implemented on top of that. Simply use "M-x ecasound RET" to fire up an interactive Ecasound session.
All Emacs Lisp ECI functions are prefixed with “eci”. ’ecasound.el’ is implemented in a high level manner which means that you won’t find most of the commands known from libecasoundc like last_string, last_float, etc. Instead of that every call to function "eci-command", which accepts all the well known IAM commands, returns ecasound’s response in an appropriate type automatically. If an error occurs, e. g. there’s a typo in a command or a file is not found, the function returns “nil”. In all other cases, either an automatically converted Lisp value is returned, or “t” in the case where there was no particular value returned.
Additionally, most of the available IAM commands have their own Emacs Lisp function including documentation and possibly a parameter list. All these functions are interactive, so you can use them in ecasound-iam-mode simply by invoking them via M-x or by pressing an assigned key combination. Emacs will prompt you for the required parameters, providing completion wherever possible.
As a convention, "eci-command" and its variants do take a buffer or process as an optional last argument. If this is “nil”, the current buffer is assumed to be the ecasound session refered to by this call. This makes it possible to use several ECI sessions concurrently, dispatching on the buffer or process in use.
(require 'ecasound) (defun example (file &optional cutoff-increment session) (unless cutoff-increment (setq cutoff-increment 500.0)) (with-current-buffer (or session (eci-init)) (eci-cs-add "play_chainsetup") (eci-c-add "1st_chain") (eci-ai-add file) (eci-ao-add "/dev/dsp") (eci-cop-add "-efl:100") (eci-cop-select 1) (eci-copp-select 1) (eci-cs-connect) (eci-start) (sit-for 1) (while (and (string= (eci-engine-status) "running") (<= (eci-get-position) 15)) (eci-copp-set (+ cutoff-increment (eci-copp-get))) (sit-for 1)) (eci-command "stop") (when (eci-cs-disconnect) (destructuring-bind ((cop n1 (copp n2 val))) (cdr (assoc "1st_chain" (eci-cop-status))) (message "%s %s is now %f" cop copp val)))))
NOTE: function “eci-cop-status” is actually a very high level function which already converts the returned information to a nested list structure.
For more complex examples of the Emacs Lisp ECI implementation, see function “eci-example”, “ecasound-normalize” and “ecasound-signalview” in ecasound.el.
Python implementation is based around the ECA_CONTROL_INTERFACE class. Lists are used for representing collections of objects.
Note! Eric S. Tiedemann has written an alternative Python interface to ECI. You’ll find this interface included in the main Ecasound packege, in “pyecasound/eci.py”. To use this instead of the standard interface, just ’import eci’ and you’re set! :)
#!/usr/local/bin/python import time from pyeca import * e = ECA_CONTROL_INTERFACE() e.command("cs-add play_chainsetup") e.command("c-add 1st_chain") e.command("ai-add some_file.wav") e.command("ao-add /dev/dsp") e.command("cop-add -efl:100") e.command("cop-select 1") e.command("copp-select 1") e.command("cs-connect") e.command("start") cutoff_inc = 500.0 while 1: time.sleep(1) e.command("engine-status") if e.last_string() != "running": break e.command("get-position") curpos = e.last_float() if curpos > 15: break e.command("copp-get") next_cutoff = cutoff_inc + e.last_float() e.command_float_arg("copp-set", next_cutoff) e.command("stop") e.command("cs-disconnect") e.command("cop-status") print "Chain operator status: ", e.last_string()
Audio::Ecasound provides perl bindings to the Ecasound control interface of the Ecasound program. You can use perl to automate or interact with Ecasound so you don’t have to turn you back on the adoring masses packed into Wembly Stadium.
Audio::Ecasound was written by Brad Bowman. At the moment this module is not distributed with Ecasound. To get the latest version, check the following CPAN link.
See the below example. For more info, here’s another CPAN link.
use Audio::Ecasound qw(:simple);
eci("cs-add play_chainsetup"); eci("c-add 1st_chain"); eci("ai-add some_file.wav"); eci("ao-add /dev/dsp"); # multiple \n separated commands eci("cop-add -efl:100 # with comments cop-select 1 copp-select 1 cs-connect"); eci("start"); my $cutoff_inc = 500.0; while (1) { sleep(1); last if eci("engine-status") ne "running"; my $curpos = eci("get-position"); last if $curpos > 15; my $next_cutoff = $cutoff_inc + eci("copp-get"); # Optional float argument eci("copp-set", $next_cutoff); } eci("stop"); eci("cs-disconnect"); print "Chain operator status: ", eci("cop-status");
This PHP extension provides bindings to the Ecasound control interface. It is useful both for scripting Ecasound and for writing graphical audio applications with PHP Gtk.
The PHP Ecasound extension was written by Tony Leake. At the moment this module is not distributed with Ecasound. The latest version and example scripts, are available from http://www.webwise-data.co.uk/php_audio/php_audio_extension.html.
Implementation of the following: 1. Setup ECI to read audio from file, apply a 100Hz lowpass filter, and send it to the soundcard (/dev/dsp). 2. Every second, check the current position. If the stream has been running for over 15 seconds, exit immediately. Also, every second, increase the lowpass filter's cutoff frequency by 500Hz. 3. Stop the stream (if not already finished) and disconnect the chainsetup. Print chain operator status info <?php $cutoff_inc = 500.0; $curpos=0; $next_cutoff=0; eci_init(); eci_command("cs-add play_chainsetup"); eci_command("c-add 1st_chain"); eci_command("ai-add /tmp/somefile.wav"); eci_command("ao-add /dev/dsp"); eci_command("cop-add -efl:10"); eci_command("cop-select 1"); eci_command("copp-select 1"); eci_command("cs-connect"); eci_command("start"); while(1) { sleep(1); eci_command("engine-status"); if (eci_last_string() !="running"){ break; } eci_command("get-position"); $curpos = eci_last_float(); if ($curpos > 15.0){ break; } eci_command("copp-get"); $next_cutoff = $cutoff_inc + eci_last_float(); eci_command_float_arg("copp-set",$next_cutoff); } eci_command("stop"); eci_command("cs-disconnect"); eci_command("cop-status"); printf("Chain operator status: %s", eci_last_string()); eci_cleanup(); ?>
The Ecasound package comes with an ’Ecasound’ module for Ruby included. If ruby is detected during the installation process it is installed automatically (assuming you are installing ecasound from source code). The module contains the class definition of a native ecasound control interface called "ControlInterface".
’Ecasound::ControlInterface’ is implemented in a high level manner which means that you won’t find most of the commands known from libecasoundc like last_string, last_float, etc. Instead of that every call to the instance method "command", which accepts all the well known IAM commands, returns ecasound’s response in an appropriate type automatically. If an error occurs, e. g. there’s a typo in a command or a file is not found, an exception of type EcasoundError is raised.
#!/usr/bin/env ruby require "ecasound" SOME_FILE = "path/to/file.wav" e = Ecasound::ControlInterface.new() e.command("cs-add play_chainsetup") e.command("c-add 1st_chain") e.command("ai-add #{SOME_FILE}") e.command("ao-add /dev/dsp") e.command("cop-add -efl:100") e.command("cop-select 1") e.command("copp-select 1") e.command("cs-connect") e.command("start") cutoff_inc = 500.0 loop do sleep(1) break if e.command("engine-status") != "running" break if e.command("get-position") > 15 e.command("copp-set #{cutoff_inc + e.command('copp-get')}") end e.command("stop") e.command("cs-disconnect") $stdout << "Chain operator status: " + e.command("cop-status") + "\n"
Here’s a few tips what to do if the ECI app you have developed is not working correctly.
This document was translated from LATEX by HEVEA.