Ecasound Control Interface Guide
Kai Vehmanen
18112003
Table of Contents
1 Introduction
Idea behind the Ecasound Control Interface (ECI) is to take a subset of
functionality provided by libecasound, write a simple API for it, and
port it to various languages. At the moment, at least C++, C and
Python implementations of the ECI API are available and part of the
main Ecasound distribution. ECI is heavily based on ecasound's
interactive mode (EIAM), and the services it provides. See
ecasound-iam(1) manual page for
detailed EIAM documentation.
2 Document history
-
18.11.2003 - Typo fixes. Updated documentation to reflect the new
naming convention (ecasound refers to the binary,
Ecasound refers to the whole package).
- 26.10.2002 - Changed the C++ linking example.
- 24.10.2002 - Added ``Notes Concerning Standalone ECI Implementations''
section. Added compilation examples.
- 06.10.2002 - Added ``Application development'' section.
- 05.10.2002 - Changed the libecasoundc link path.
- 29.09.2002 - ``PHP'' section added.
- 25.04.2002 - Changed headers path from ``<ecasoundc/file.h>'' to
``<file.h>'' and added library version number to
link instructions.
- 21.10.2001 - Added this history section. Minor changes to
ECI examples.
3 General
ECI doesn't provide any routines that directly manipulate audio or
Ecasound objects. What is does provide is an easy and generic way
to issue EIAM (Ecasound Inter-Active Mode) commands, access to the
command return-values and error handling.
This approach has two benefits. First, it's 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 choice.
3.1 What's it good for?
Specific tasks ECI is aimed at:
-
1. automating (scripting in its traditional sense)
- 2. frontends (generic / specialized)
- 3. sound services to other apps
3.2 Services and behaviour
Here's a list of services provided by all ECI implementations:
-
command(string)
-
Issue an EIAM command.
- command_float_arg(string, float)
-
Issue an EIAM command. This function can be used instead of
command(string), if the command in question requires exactly one
numerical parameter. This way it's possible to avoid the extra
string -> float conversion, which would lead to lost precision.
3.2.2 Return values
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. Not all EIAM commands return a value (return type is void).
-
last_string()
-
Returns the last string return value.
- last_string_list()
-
Returns the last collection of strings (one or more strings).
- last_float()
-
Returns the last floating-point return value. Note! last_float()
doesn't refer to the C/C++ type 'float'. In most implementations,
floats are 64bit values (doubles in C/C++).
- last_integer()
-
Returns the last integer return value. This function is also
used to return boolean values, where non-zero means 'true'
and zero 'false'.
- last_long_integer()
-
Returns the last long integer return value. Long integers are
used to pass values like 'length_in_samples' and 'length_in_bytes'.
It's implementation specific whether there's any real difference
between integers and long integers.
-
error()
-
Returns true (!= 0) if error has occured during the execution
of last EIAM command. Otherwise returns false (= 0).
- last_error()
-
Returns a string describing the last error. If the last EIAM command
was executed succesfully, last_error() returns an empty string.
-
initialize()
-
Reserve resources.
- cleanup()
-
Free all reserved resources.
3.3 Porting to new environments
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 easist to use the C++ or C ECI
as the underlying implementation.
4 Implementations
4.1 General
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:
-
Setup ECI to read audio from file, apply a 100Hz lowpass filter, and
send it to the soundcard (/dev/dsp).
- 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.
- Stop the stream (if not already finished) and disconnect the
chainsetup. Print chain operator status info.
4.2 Notes Concerning Standalone ECI Implementations
The C implementation of ECI is not directly linked against the main
Ecasound libraries. Instead, the it launches the ecasound executable
on the background and uses command pipes 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 <eca-control-interface.h>
- create an instance of the ECA_CONTROL_INTERFACE class
and use its member functions
- link you app agains libecasoundc (-lecasoundc)
- compilation example: c++ -o ecidoc_example ecidoc_example.cpp `libecasoundc-config --cflags --libs`
#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 <ecasoundc.h>
- use the eci_* routines
- link your app against libecasoundc (-lecasoundc)
- compilation example: gcc -o ecidoc_example ecidoc_example.c `libecasoundc-config --cflags --libs`
#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);
}
4.5 Python
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/esteci.py. To use this instead
of the standard interface, just 'import eci' and you're set! :)
-
import pyeca
- create an instance of the ECA_CONTROL_INTERFACE class
and use its member functions
- python 'yourapp.py' and that's it :)
#!/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.
-
Obtain and build the Ecasound PHP extension
- Initialise Ecasound, eci_int();
- Issue EAM commands eg, eci_command("cs-add my_chain_setup");
- Free resources, eci_cleanup();
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();
?>
5 Application development
5.1 Tips for debugging
Here's a few tips what to do if the ECI app you have developed
is not working correctly.
-
Check your Ecasound installation. Try to run the ``ecasound''
console user-interface and verify that the basic functionality
is working (ie. something like ``ecasound -i foo.wav -o
/dev/dsp''.
- If developing in C or C++, check that your application
is correcly linked: ``ldd /path/to/myapp''. All the libraries
should be properly found.
- Check error conditions. You should remember to check
for errors in your ECI apps using the eci_error() and
eci_last_error() functions. Especially when intializing
ECI for the first time and after important commands
like ``cs-connect'', you should always check for errors.
- Launch Ecasound in interactive mode (``ecasound -c''),
and issue the commands your ECI application is using,
manually one-by-one and see what happens. If something
goes wrong, increase Ecasound's debug level (for instance
``-ddd'') and re-run the test.
This document was translated from LATEX by
HEVEA.