玩哈斯克尔wav文件 [英] Play a wav file with Haskell

查看:150
本文介绍了玩哈斯克尔wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有使用一些图书馆和可能这样的,我玩一次多声音?

Is there a simple, direct way to play a WAV file from Haskell using some library and possibly such that I play many sounds at once?

我知道了OpenAL的,但我不写了一些先进的音频合成节目,我只是想打一些声音一点点戏的事。理想情况下,API可能是这样的:

I'm aware of OpenAL but I'm not writing some advanced audio synthesis program, I just want to play some sounds for a little play thing. Ideally the API might be something like:

readWavFile :: FilePath -> IO Wave
playWave :: Wave -> IO ()
playWaveNonBlocking :: Wave -> IO ()

我的这个的接近只是推出的mplayer什么的。或直接试图猫WAV到/ dev / SND /或诸如此类。

I'm this close to merely launching mplayer or something. Or trying to cat the wav directly to /dev/snd/ or somesuch.

推荐答案

这是如何同时在多频道播放多个声音与SDL。我想这个答案的标准。 WAV文件,操作简单,哈斯克尔,多种渠道。

This is how to play multiple sounds on multiple channels at once with SDL. I think this answers the question criteria. WAV files, simple, Haskell, multiple channels.

import Control.Monad
import Control.Monad.Fix
import Graphics.UI.SDL as SDL
import Graphics.UI.SDL.Mixer as Mix

main = do
  SDL.init [SDL.InitAudio]
  result <- openAudio audioRate audioFormat audioChannels audioBuffers
  classicJungle <- Mix.loadWAV "/home/chris/Samples/ClassicJungle/A4.wav"
  realTech      <- Mix.loadWAV "/home/chris/Samples/RealTech/A4.wav"
  ch1 <- Mix.playChannel anyChannel classicJungle 0
  SDL.delay 1000
  ch2 <- Mix.playChannel anyChannel realTech 0
  fix $ \loop -> do
    SDL.delay 50
    stillPlaying <- numChannelsPlaying
    when (stillPlaying /= 0) loop
  Mix.closeAudio
  SDL.quit

  where audioRate     = 22050
        audioFormat   = Mix.AudioS16LSB
        audioChannels = 2
        audioBuffers  = 4096
        anyChannel    = (-1)

这篇关于玩哈斯克尔wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆