Using Haskell for real-time audio streaming
Asked Answered
T

0

6

I would like to be able to play simple[1] audio in realtime from GHCI, starting and stopping independent oscillators and samples ("voices").

Using Bash on Linux, it's easy[2] to pipe data into a command that streams audio through your speakers.

In Haskell, I imagine something along these lines:

x <- realtimeAudioSink
createVoices x [VoiceName "sinewave",VoiceName "drums"]
play x (VoiceName "sinewave") $ sinewave (Hz 440)
play x (VoiceName "sinewave") $ sinewave (Hz 220)
  -- replace the earlier sinewave with a lower-frequency one
play x (VoiceName "drums")
  $ asSoonAs (theTime >= floor theTime)
  $ every (Seconds 10) $ soundfile "snare.wav"
  -- starting on the next second, play the snare drum sample every 10 seconds
destroyVoice x (VoiceName "drums")
  -- the drums stop, the sinewave keeps going

Haskell offers a lot of streaming libraries. Each looks hard to learn. Moreover the audio streaming problem is complicated by the need for realtime operation at a specific sample rate, and by the buffering problem[3].

Is this easy or hard?


[1] Four sinewaves and four samples concurrently seems like enough bandwidth to explore for a lifetime.

[2] If you use ALSA, for instance, cat /dev/urandom | aplay will play white noise. (Warning: It plays at maximum volume.)

[3] The buffering problem arises, I believe, from the following pair of opposing constraints: (1) If each sample is given its own calculate-and-stream cycle, it might overwhelm the processor. (Or maybe it wouldn't, if the generated audio is simple enough?) (2) If you calculate too many samples at a time before sending them out, you might not have finished calculating them all in time.

Tiger answered 24/3, 2018 at 18:25 Comment(4)
i would suggest you to look into outputting a binary stream on stdout and then pipe it into aplay like above. basicly smth like this should be passible haskell-app | aplayHerculaneum
How much Haskell do you know? As it stands this question is too broad.Chandrachandragupta
I think that library does NOT connect with the outside world, but its other siblings do. Check out the other packages.Appurtenance
@ThomasM.DuBuisson Is it specific enough now? Regarding myself, I know enough Haskell to have overhauled the Tidal music-making DSL. I'm good inside Haskell, but connecting it to other stuff always confounds me.Tiger

© 2022 - 2024 — McMap. All rights reserved.