gpt4 book ai didi

haskell - 使用 Haskell 绑定(bind)保存脉冲音频流

转载 作者:行者123 更新时间:2023-12-02 22:22:59 26 4
gpt4 key购买 nike

使用 pulse-simple包我做了以下代码:

main=do
s<-simpleNew Nothing "example" Record Nothing "this is an example application"
(SampleSpec (F32 LittleEndian) 44100 1) Nothing Nothing
xs<-simpleRead s $ 44100*10 :: IO [Float]
simpleFree s
play xs

play :: [Float] -> IO ()
play d = do
s<-simpleNew Nothing "example" Play Nothing "this is an example application"
(SampleSpec (F32 LittleEndian) 44100 1) Nothing Nothing
simpleWrite s d
simpleDrain s
simpleFree s

这可以工作并记录 10 秒的音频,然后播放。

我现在想编码并保存音频文件(作为 .wav rr 无论如何),以便它可以在另一个程序中播放或转换。我认为这更像是一个音频问题,所以对于非 Haskell 的人来说,我基本上有一个 44100*10 长的浮点数组。我怀疑我需要一些其他的图书馆。

提前致谢。

最佳答案

好像是 Data.WAVE来自 WAVE package 的模块应该做的工作。

转换您的 [Float][Int32]这构成了waveSamples WAVE 的字段记录。填写 WAVEHeader 的其余部分并使用 putWAVEFile 写入文件.

import Data.WAVE

writeWaveFile :: String -> [Float] -> IO ()
writeWaveFile path floats = putWAVEFile path wave
where wave = WAVE wHeader wSamples
wHeader = WAVEHeader { waveNumChannels = 1
, waveFrameRate = 44100
, waveBitsPerSample = 32 -- or maybe 8?
, waveFrames = Nothing
}
wSamples = [[ ... | x <- floats ]]

填写 ...与合适的 Float -> Int32功能。

关于haskell - 使用 Haskell 绑定(bind)保存脉冲音频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31397655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com