Wednesday, April 13, 2011

Lean and Mean Marsyas by Graham Percival

Marsyas can (sometimes with some modifications) be used to write very efficient code that consumes little resources. It has been used in embedded systems and some mutant versions are even running on guitar effect pedals. The following text was provided by Graham Percival and describes how to avoid the startup cost of the MarSystemManager (a factory for creating MarSystems at run-time). This is also important in web application in which the same processing might be initiated every time there is a request and the startup cost needs to be minimized. This initial startup cost is typically not improtant in batch processing applications as it only takes place at the start of processing.

We all know that "premature optimization is the root of all evil"and the old "1. make it work, 2. make it right, 3. make it fast". But sometimes -- very occasionally, but sometimes -- optimization is not premature. Or else you just feel like being naughty. :) On a more serious note, if you are trying to use Marsyas on a system with limited resources such as an embedded system or mobile phone, it may necessary to optimize before deployment. Most Marsyas examples begin with:

MarSystemManager mng;
This initializes one copy of every MarSystem in Marsyas. This "caching" can save resources if you need to create many copies of certain MarSystems, but if you only need a few MarSystems, it means that your program needs to create (and store) a bunch of unnecessary data. Here's a simple example, contrasting the MarSystemManager way to the "lean and mean" way: you can create individual MarSystems as needed, avoiding the MarSystemManager entirely. Note that you need to include the headers you need yourself! The "lean and mean" technique is particularly useful if you want to statically link your application. You can easily contrast the two methods by commenting out the #define.

------------
#define LEAN

#ifndef LEAN
#include "marsyas/MarSystemManager.h"
#else
#include "marsyas/Series.h"
#include "marsyas/SoundFileSource.h"
#include "marsyas/Rms.h"
#include "marsyas/Annotator.h"
#include "marsyas/WekaSink.h"
#endif

using namespace Marsyas;

int main() {
#ifndef LEAN
 MarSystemManager mng;
 MarSystem *net =  mng.create("Series", "extract");
 net->addMarSystem(mng.create("SoundFileSource", "src"));
 net->addMarSystem(mng.create("Rms", "rms"));
 net->addMarSystem(mng.create("Annotator", "annotate"));
 net->addMarSystem(mng.create("WekaSink", "dest"));
#else
 MarSystem *net =   new Series("extract");
 net->addMarSystem( new SoundFileSource("src"));
 net->addMarSystem( new Rms("rms"));
 net->addMarSystem( new Annotator("annotate"));
 net->addMarSystem( new WekaSink("dest"));
#endif

// update controls and run the process loop 
} 

Wednesday, February 9, 2011

Logging in Marsyas

In this post I discuss the use of logging in Marsyas as an alternative to temporary print statements and/or debugging. Most developers are marginally aware of the existing facilities and I want to make a push to make them more widely used. It is my hope that after reading this post code in MarSystems like : 
  •    cout << type_ <<  "/" << name_ << "/mrs_real/gain = " << gainValue << endl;
 will be replaced with: 
  •     MRSDIAG(type_ << "/" << name_ << "/mrs_real/gain = " << gainValue);
Notice that the logging macros fully support the C++ stream syntax and therefore can include much richer information than just simply a text message (for example you could output a realvec or a counter variable). 

Marsyas is frequently used as a library/framework linked to external applications such as GUIs and command line tools as well as a back end to web servers. In addition it is also designed to support real-time interactive music generation and analysis as well as batch processing of collections with thousands of audio files. These different usage scenarios result into sometimes contradictory design requirements. For example in real-time scenarios it is desirable that unexpected behavior does not stop the data flowing through the system. As an example the SoundFileSource MarSystem outputs audio buffers with zeroes if the provided file-name can not be opened rather than doing something more drastic like exiting which could be a desired behavior in batch processing of files.
 
Even though a lot can be accomplished by using and combining the existing MarSystems sooner or later most developers have to write their own MarSystems to extend the provided functionality.  A frequent process used by the current developers (including me) is to write the new MarSystem, add various cout statements to help understand whether it is working or not and when everything looks good remove or comment out the cout statements from the source code. That way the resulting MarSystem does not produce any output that would clutter the screen as well as possibly confuse things like web-servers. Later if something goes wrong the cout statements are re-introduced and the cycle continues. Because of the heavy computational requirements of audio processing such print statements can have a significant slow down effect.

An alternative for understanding the run time behavior of a program is the use of a debugger. There are two main issues that potentially limit the use of a debugger when used with Marsyas. The first is that in multi-threaded applications (such as GUIs) debugging can not be used reliably. The second is that compiling in Debug mode can significantly slow down the computational time.

Logging is an important technique for better understanding the behavior of a program during run time.
It consists of recording information about the program while it is running at different levels of detail and controlling where that information is stored. The Marsyas logging system was designed with the considerations discussed above in mind. It is mostly configured at compile time so that there is no performance penalty when it is not used (even a small run-time check would result in performance effects given the heavy computation requirements for audio).  It supports three different output streams (standard output, standard error and a marsyas log file). When logging to file is enabled log messages are appended to the log file allowing a single log to be used for multiple invocations of the code something useful in parallel batch processing jobs.

There are several different types of log messages supported. They are: 
  1. MRSMSG: Messages are high level information about the running of the system. They should only be used in MarSystem and applications that need to display output. For example the ClassificationReport.cpp MarSystem should be using instead of the current cout statements (excellent homework for any of the readers).
  2. MRSWARN: Warnings provide information about things that might be a cause of concern but ignoring them will not affect the operation of the system. For example if a sound file can not be opened then a warning message is logged.
  3. MRSERR: Errors are more severe than warnings and indicate that something serious is going wrong. In certain cases they are followed by exiting the program.
  4. MRSDIAG: Diagnostics provide detailed information about the operation of the system. They can be turned on and off arbitrarily at run-time (when enabled) allowing fine control of the information displayed. For example ideally each MarSystem should have diagnostic messages for the myProcess and myUpdate methods and the values of the controls (more homework for all of us)
  5. MRSDEBUG: Debug messages are specific and detailed and trying to help with debugging the code. They are similar to diagnostic messages but more about what went wrong rather than what is going right. 
Typically Marsyas is configured to display only the first 3 messages types (messages, warnings and errors) in Release and all of them in Debug. By appropriately configuring CMake it is possible to have logging to any combination of standard output, standard input and log file.


Tuesday, February 8, 2011

Marsyas is 10 years old - time to start a blog


Marsyas (http://marsyas.info) is a free software framework for audio processing with specific emphasis on music analysis, synthesis and retrieval. has been under development for over 10 years now. It has been used for a variety of projects in academia and industry. The documentation although far from complete has a lot of information and there is a lot of source code to explore. Although the Marsyas developer community is small and the mailing lists provide a good forum for answer questions in the past 2 or 3 years I have realized that Marsyas has grown to the point that many developers are not aware of all the available functionality. My goal for this blog is to highlight different aspects of working with the Marsyas. The main target audience is Marsyas developers and users but anyone interested in audio and music processing software might find it interesting. I plan to post about once every two weeks and I might increase the frequency if there is enough interest.