Question

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

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top