Haskell audio output on OS X?

こ雲淡風輕ζ 提交于 2019-12-10 13:04:23

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/2223866/haskell-audio-output-on-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!