[Haskell-cafe] Toy application advice wanted

Alastair Reid alastair at reid-consulting-uk.ltd.uk
Fri May 7 18:51:27 EDT 2004

> It could be fun to figure out ways of writing jack-clients and
> plugins in Haskell.  Would it be difficult, or stupid?   Are
> callbacks to Haskell from C a problem?

Callbacks to Haskell are very easy using the ffi extension supported by Hugs, 
GHC and NHC.

If components are standalone apps, that's all you need to do.

If components are more like libraries, you'll need to take care of 
initializing the Haskell runtime, etc. (The ffi has calls to do this though I 
think only GHC implements them.)

> I suppose an interesting jackable component written in Haskell
> would of necessity something more midi than audio oriented.

Because of raw performance or GC delays?

1) Performance 

If you take just a little care, GHC can generate some pretty fast code and 
generating 88 thousand samples per second (i.e., CD-rate stereo) isn't really 
that much on your typical PC hardware.

Or, if you want to be able to use Hugs or GHC just isn't fast enough, you 
could write the inner loop in C and use Haskell to control how it is invoked.  
(I'm assuming that most audio ops can be expressed in terms of a few generic 
convolution operators.)  We had good luck with this sort of approach at Yale 
when doing real-time visual tracking.  (The important things to know about 
visual tracking are that you process megabytes of data per second and the 
more frames per second you can process the more reliable the tracking.)  The 
'inner loop' was written in C or hand-bummed assembly code and the rest was 
written in Haskell.  Performance was something like 99.9% of equivalent C 

2) GC delays.

GC systems are usually tuned to minimise overhead but can usually be persuaded 
to minimise the average or worst delay by specifying different sizes of 
different allocation arenas and explicitly invoking the garbage collector.  
Again, this is something we did at Yale with the visual tracking.  You'd 
probably do very well with a simple policy like invoking the GC N-times per 
second of sound processed.

Alastair Reid

More information about the Haskell-Cafe mailing list