Installing Haskore (Haskell/CSound) under FreeBSD

I’ve spent most of today looking at Haskore, which appears to be the most well-developed kid on the block for doing Csound type things using Haskell (and indeed, doing music type things using Haskell). Here are my notes on finding/installing it, which experience wasn’t completely straightforward…

The Haskore home page is here but the code there appears to be superceded by the darcs stuff pointed at by the Haskore entry on the Haskell wiki, in particular http://darcs.haskell.org/haskore/. In general, a lot of the links online (including some top google links) seem to point to old versions, old versions of the tutorial, etc. So you have to be careful. Anyway, the darcs repo is surely the latest word, and it’s easily grabbed:

[gimbo@orb ~] mkdir haskore
[gimbo@orb ~] cd haskore 
[gimbo@orb haskore] darcs get http://darcs.haskell.org/haskore/
Copying patch 671 of 671... done!
Applying patch 671 of 671... done.
Finished getting.

The Readme in that repository, is right that installation is a little tricky, but with its help I managed OK. Interestingly, it targets MIDI, CSound, or Supercollider which isn’t in FreeBSD ports but might compile (says it’s “for Linux”). Hmmm, or maybe not if this June 2006 thread is anything to go by. I chose to avoid SuperCollider for now.

Haskore originates with Paul Hudak but Henning Thielemann seems to be Mr Haskore since about/at least 2004 (?). His research page includes an interesting-looking (2004) talk on audio processing using Haskell. It discusses Haskore and other Haskell libraries for, eg, signal processing.

Anyway, I seem to have managed to get Haskore to build using the Cabal route. As suggested in the Readme, I edited Haskore.cabal to remove the Hsc and SuperCollider dependencies, after which the normal Cabal process almost “just works”. The failure is that the Shell module is missing – in particular the line import Shell(launch), in several places, fails. This appears to want shell-haskell, whose purported home page is blank but which appears to now be maintained by Henning? (A good sign, that.) It’s quick and easy to build and install using cabal magic:

[gimbo@orb ~] darcs get http://darcs.haskell.org/shell-pipe/
Copying patch 6 of 6... done!
Applying patch 6 of 6... done.
Finished getting.
[gimbo@orb ~] cd shell-pipe 
[gimbo@orb shell-pipe] runghc Setup.lhs configure
  [snip]
[gimbo@orb shell-pipe] runghc Setup.lhs build    
  [snip]
[gimbo@orb shell-pipe] sudo runghc Setup.lhs install 
  [snip]

Alas, the Haskore cabal build still failed, with a permission error on Shell.hi. Easy enough to fix:

[gimbo@orb ~] cd /usr/local/lib/ShellPipe-0.0/ghc-6.6 
[gimbo@orb ghc-6.6] sudo chmod -R a+r * 

That done, building & installing of Haskore was as simple as:

[gimbo@orb haskore] runghc Setup.lhs configure
  [snip]
[gimbo@orb haskore] runghc Setup.lhs build
  [snip]
[gimbo@orb haskore] runghc Setup.lhs haddock
  [snip]
[gimbo@orb haskore] runghc Setup.lhs install
  [snip]

OMFG it actually worked. The haddock docs aren’t installed anywhere, just built in the source tree, and a bit hard to find (they were in dist/doc/html/ as it turned out). Now what about running examples? Oh right, so I also need to chmod everything in /usr/local/lib/Haskore-0.0/ghc-6.6 too! I wonder if this is a Cabal, FreeBSD, or gimbo problem? :-)

One thing the Readme doesn’t tell you, by the way, is that when building the tutorial (which is really more of a reference manual), it’s a good idea to issue gmake pdf several times, not just once, to get cross-references & contents included; it’s a shame the Makefile doesn’t do this for you. One thing I can’t figure out is whether a better introduction, or a red herring, is Matt Zamec’s Haskore CSound Tutorial, which seems to rely on the module presented in his Sounds Unlimited work. I’m not sure, though… This work is now apparently incorporated into Haskore, which doesn’t mean the tutorial isn’t useful, of course.

The first thing I’ve worked out to do now is how to play some examples – the MIDI ones. I was amazed this worked, because my understanding was that FreeBSD’s MIDI support was essentially nonexistent, but apparently having timidity installed (which I’d forgotten I did) is sufficient for Haskore.Interface.Midi.Play, and it Works For Me ™. Thus, for example, to run some of the examples, I created a little module haskore_eg.hs:

module Haskore_eg where

import Haskore.Interface.MIDI.Play (playSimple)

import qualified Haskore.Example.Guitar as Guitar
import qualified Haskore.Example.ChildSong6 as ChildSong6
import qualified Haskore.Example.SelfSim as SelfSim

Then loaded it into ghci and ran some examples:

[gimbo@orb ~] ghci haskore_eg.hs
   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base ... linking ... done.
[1 of 1] Compiling Haskore_eg       ( haskore_eg.hs, interpreted )
Ok, modules loaded: Haskore_eg.
*Haskore_eg> playSimple Guitar.legatoSongMIDI
Loading package mtl-1.0 ... linking ... done.
Loading package haskell98 ... linking ... done.
Loading package haskell-src-1.0 ... linking ... done.
Loading package parsec-2.0 ... linking ... done.
Loading package html-1.0 ... linking ... done.
Loading package network-2.0 ... linking ... done.
Loading package QuickCheck-1.0 ... linking ... done.
Loading package HUnit-1.1 ... linking ... done.
Loading package ShellPipe-0.0 ... linking ... done.
Loading package unix-1.0 ... linking ... done.
Loading package Haskore-0.0 ... linking ... done.
*Haskore_eg> playSimple Guitar.parallelSongMIDI
*Haskore_eg> playSimple ChildSong6.song
*Haskore_eg> playSimple SelfSim.ss

Hey pesto, after each playSimple issued, awful General MIDI sound emanates from my tinny speakers. Oh, glory! :-) Not all the examples can be directly rendered to MIDI so easily, and anyway I’m much more interested in the CSound side, but still, it’s nice to know that yes, Haskore really is installed, it really does work, it really does make sound!