开发者

Iterate over list in haskell?

开发者 https://www.devze.com 2022-12-20 09:12 出处:网络
I\'m writing an audio program in Haskell using Portaudio. I have a function that generates a list of samples I\'d like to play, and I\'m trying to play them using the following snippet inside main:

I'm writing an audio program in Haskell using Portaudio. I have a function that generates a list of samples I'd like to play, and I'm trying to play them using the following snippet inside main:

curSamps <- return (chunk 1 (sineWave 440 44100))
forever $ do
  Right numSampsAvail <- getStreamWriteAvailable paStream
  Right NoError <- writeStream paStream curSamps numSampsAvail
  curSamps <- return (drop numSampsAvail curSamps)

sineWave is a function I've created to generate an infinite list of Int16 samples of a sinewave at a specified frequency and sample rate.

When I debug this code, by replacing the audio output code with a putStrLn, it prints all 0s, which is the first开发者_JS百科 sample from the function.

How can I iterate over this list with the audio output functions? I don't think I can use recursion or a map.

Edit: Code copying mistake


Use recursion:

play []       = return ()
play curSamps = do Right numSampsAvail <- getStreamWriteAvailable paStream
                   Right NoError <- writeStream paStream curSamps numSamps
                   play (drop numSampsAvail curSamps)

main = do ...
          play (chunk 1 (sineWave 440 44100))
          ...


Consider using map's monadic cousins mapM/forM.


Using the same API functions one can do this:

let playSamples curSamps = do
      Right numSampsAvail <- getStreamWriteAvailable paStream
      Right NoError <- writeStream paStream curSamps numSampsAvail
      playSamples (drop numSampsAvail curSamps)
playSamples (chunk 1 (sineWave 440 44100))

I'm not familiar with the Portaudio API, so it might provide a more convenient, higher-level way of doing what you're trying to achieve.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号