Stevebook - [ languages_other_than_c_and_c_for_real-time_audio_computation ]

Languages other than C/C++ for real-time audio computation

While I do love programming in C, I'm much less enthralled lately by C++. In any case, I'm constantly finding myself stuck with these languages because I tend to do a lot of real-time program; audio and haptics requires strictly deterministic behaviour in terms of timing so that a simulation can maintain perceptual coherency. So I'm quite interested in the prospects of discovering new languages that are capable of behaving well in a real-time environment.

Many tasks, especially in audio, work fine as long as the audio buffers can be computed quickly enough; the timing doesn't have to be ultra precise, since that is taken care of for you by the FIFO hardware. If audio vectors are coming in fast enough, they'll be played at the right time.

Traditionally many non-C languages are considered “non-real-time” because they use non-deterministic memory handling (such as garbage collection) in order to provide the dynamic features that make using them easier, more pleasant, and less error-prone. There are good reasons for wanting deterministic memory handling, and C provides this. However, concerns related to pure speed are more often quoted when discussing this subject—on that topic, we often see claims, particularly for JIT-compiled languages like Java for instance, that benchmarked execution speeds are approaching C. So it would seem interesting to begin exploring whether dynamic languages can now be used in real-time systems.

Unfortunately these benchmarks are very rarely done for real-time needs. With nice dynamic languages like JavaScript getting faster and faster, I'm more and more interested in discovering new ways of doing audio computation, and doing away with this dependency on C.

Again, I don't mind programming in C; in fact, I like it. But I don't like being forced into it purely because of the type of programming I chose to do. Basically, I get jealous when I read about all the nice features of newer languages like Clojure or Haskell, for example. In some cases, like writing a simulator, one really needs to take advantage of object oriented features or generally better ways to encapsulate logic and data structures, and I don't particularly like being stuck with C++ for this.

Some day I'd like to do a proper benchmark rundown of various languages when used in an audio or haptic context. However, for now I intend to use this page to collect a list of links to interesting uses of non-C languages for real-time audio computing.

The HaXe language, used for decoding Vorbis audio.

It's interesting to see that Flash has incorporated audio vector computation into its API, essentially making it possible to create sound synthesizers in Flash. http://barelyfocused.net/blog/2008/10/03/flash-vorbis-player/

Here's an example of Karplus-Strong string synthesis written in HaXe and executed in Flash: http://yaxu.org/synth/synth.html

Speaking of Flash, apparently now you can just use C and C++ thanks to LLVM, so maybe learning a new language like HaXe isn't even necessary.

Embedded ML

I've just learned about Embedded ML. The interesting thing about languages designed for embedded environments is that they often have similar requirements to audio and other kinds of real-time programming. For instance, this compiler for SML, a functional language, generates static C code that can then be compiled to fast native code.

On that note, did you know that the popular FFTW library (often considered the fastest FFT routine) was generated from OCaml, a variant of ML? I've often thought of exploring more functional languages for static code generation. FAUST is one that is specifically designed for creating audio unit generators for various languages and environments such as Pure Data and JACK.

BitC

BitC is a language taking inspiration from both Standard ML and C. So it's a functional language with type inference, but it is intended to be used for “systems programming” — in other words, it's meant to be a C equivalent, but with modern, functional features. I haven't read about it being used in real-time contexts yet, but it looks like it could be something really worth exploring. It's not yet ready for release, however.

Haskell

There are at least two sound synthesis projects for Haskell. One is just called the “Synthesizer library”, and is available here. The page has some information on different approaches to avoiding speed issues that arise from Haskell's lazy lists and can get in the way of doing real-time DSP. Another, called YampaSynth, is based on a Haskell embedded DSL called Yampa designed for doing functional reactive programming.