views:

171

answers:

1

I'd like to be able to output audio from Haskell. I'm currently using GHC 6.10 on OS X (Snow Leopard). I've tried building the jack library (using JackOSX) and the PortAudio library, but neither of them seemed effective. Is there a relatively simple way to do live audio output from a Haskell program on a Mac?

Edit: Clarity

+2  A: 

I've been using PortAudio successfully.

I took some excerpts from my toy program to make a very simple "echo" example, below:

(run with headphones. this is a feedback loop from the mic to the speakers and may become very loud after a few feedback rounds)

import Control.Monad (forever)
import Data.Int (Int16)
import Foreign.Ptr (nullPtr)
import Sound.PortAudio

initPortAudio :: Int -> IO (PaStream Int16)
initPortAudio blockSize = do
  Right NoError <- initialize
  Just micDevIdx <- getDefaultInputDevice
  Just spkDevIdx <- getDefaultOutputDevice
  Right paStream <-
    openStream
    (Just (StreamParameters micDevIdx 1 PaInt16 0.1 nullPtr))
    (Just (StreamParameters spkDevIdx 1 PaInt16 0.1 nullPtr))
    44100 blockSize
    :: IO (Either String (PaStream Int16))
  Right NoError <- startStream paStream
  let zeroBlock = replicate blockSize [0]
  Right NoError <- writeStream paStream zeroBlock blockSize
  return paStream

main :: IO ()
main = do
  paStream <- initPortAudio blockSize
  forever $ do
    Right numSampsAvail <- getStreamReadAvailable paStream
    Right curSamps <- readStream paStream 1 numSampsAvail
    Right NoError <- writeStream paStream curSamps numSampsAvail
    return ()
  where
    blockSize = 0x800

Works here in Leopard with GHC 6.10.4.

My own toy program actually only uses audio input, and it outputs zeros to audio output (without doing that PortAudio complains).

yairchu
How did you install the portaudio library itself? I installed it via macports and now the haskell library is complaining that the portaudio library is the wrong architecture.
Edward Amsden
So apparently this is related to Snow Leopard being all 64bit by default, but GHC still being 32 bit. Trying to build portaudio as 32bit also fails.
Edward Amsden
@Edward Amsden: I didn't use macports. I did `configure` .. `make install` for the stable version v19_20071207. Good luck.
yairchu
Macports failed building the 32 bit library, so I also did configure .. make install. I had to set CFLAGS and LDFLAGS when running configure to force the 32 bit build.
Edward Amsden
Might I request that you report this problem to the maintainer for that port?
Matt Kane