Saturday, June 22, 2013

Real time audio programming languages

Introduction

Over the last couple of years I've written various real-time audio programs. Its difficult to adhere to real-time regulations: you've got to get threading and memory management right.

C++ is the often concidered the obvious choice language for large real-time audio programs: its a compiled language, and is deterministic in time if used carefully. This is necessary for real time (RT) work, and rules out VM based languages like Python for any low-latency work.

In C++ there's many a way to achieve real-time, one of which I have detailed here: https://github.com/harryhaaren/fypRealtimeCppPrograming

Other languages

C++ is one way to go, but in recent times there are various other programming languages which are becoming increasingly attractive to the real-time audio programmer. Particularly these two languages have caught my eye recently:

Rust : http://www.rust-lang.org
Iolanguage: http://iolanguage.org

Both of these languages have certain characteristics which make them possible candidates for RT programming.

Rust

Language Overview

Rust is a language that focuses on "blocks", using boundaries. Integrity, availability and concurrency are its main goals. It uses lightweight tasks with message passing for concurrency, no shared memory.

The Interesting Stuff

I'm most intrigued by the memory management of the language: everything is static unless declared "mut" (or "mutable"), and ownership of objects is very strict. This means that managing resources in a real-time safe way is well defined, and hence the code will be maintainable.

Three different "pointer" types exist, as well as new concepts like owned boxes and managed boxes... these new concepts may aid memory allocation troubles, but perhaps it complicates them too, I don't have much experience yet with it, so only time will tell...

Learning It

Most of what I know comes straight from their homepage or tutorial:
Homepage: www.rust-lang.org
Tutorial: http://static.rust-lang.org/doc/0.6/tutorial.html

 

Conclusion

A cool language, and if the memory concepts prove useful, it could be an awesome new language to learn for the audio-programming enthusiast.

 

IOlanguage

Language Overview

This is a smalltalk inspired language, while also incorporating various different elements from other languages together. Actors based concurrency is used (a la Act1), while it is also kept small for embeddable purposes. Runs in a small VM.

The Interesting Stuff

Intensive inspecting of object instances / program state (like LISP) aids debugging significantly. Extensive concurrency possibilities: co-routines, actors, futures and yield statements allow for flexible "time" programming.

 

Learning It

Extensive documentation and example code here:
http://iolanguage.org/scm/io/docs/IoGuide.html#Introduction

Conclusion

Cool language, unfortunately probably not fully real-time safe / deterministic due to running in a VM.

Sum Up

"So what language will I use for my next project?" I hear you ask: well I'm staying with the tried and tested C++ for a while. I've dabbled with Vala previously ( see ValaLooper and Prehear ), but they're not quite suitable to RT work in my opinion.

Although its nice to work with a slightly higher level language, its hard to determine if the generated code is genuinely real-time safe.

The perfect real-time safe code for me is code that is so simple, that proving its real-time safe under any conditions is trivial. Then the code is maintainable and readable.

Know of any RT capable language I've left out? Get in touch: I'm interested in hearing about it!