这是一个简短的Haskell程序,可产生440 Hz的声音。它使用pulseaudio作为音频后端。
import GHC.Float
import Control.Arrow
import Sound.Pulse.Simple
import qualified Data.List.Stream as S
import Data.List
type Time = Double
type Frequency = Double
type Sample = Double
type CV = Double
chunksize = 441 * 2
sampleRate :: (Fractional a) => a
sampleRate = 44100
integral :: [Double] -> [Double]
integral = scanl1 (\acc x -> acc + x / sampleRate)
chunks :: Int -> [a] -> [[a]]
chunks n = S.takeWhile (not . S.null) . S.unfoldr (Just . S.splitAt n)
pulseaudioOutput :: [Sample] -> IO ()
pulseaudioOutput sx = do
pa <- simpleNew Nothing "Synths" Play Nothing "Synths PCM output"
(SampleSpec (F32 LittleEndian) 44100 1) Nothing Nothing
mapM_ (simpleWrite pa . S.map double2Float) $ chunks 1000 sx
simpleDrain pa
simpleFree pa
oscSine :: Frequency -> [CV] -> [Sample]
oscSine f = S.map sin <<< integral <<< S.map ((2 * pi * f *) . (2**))
music ::[Sample]
music = oscSine 440 (S.repeat 0)
main = do
pulseaudioOutput music
如果我编译并运行它,我会看到CPU消耗不断增长。
如果我改变&#34; S.splitAt&#34; to&#34; splitAt&#34;在&#34; chunks&#34;的定义中,一切都很好。
有人可以猜到为什么会这样吗?
谢谢。
在以下代码中,所有三个版本的块都可以产生上述行为:
import GHC.Float
import Control.Arrow
import Sound.Pulse.Simple
import Data.List.Stream
import Prelude hiding ( unfoldr
, map
, null
, scanl1
, takeWhile
, repeat
, splitAt
, drop
, take
)
type Time = Double
type Frequency = Double
type Sample = Double
type CV = Double
chunksize = 441 * 2
sampleRate :: (Fractional a) => a
sampleRate = 44100
integral :: [Double] -> [Double]
integral = scanl1 (\acc x -> acc + x / sampleRate)
chunks :: Int -> [a] -> [[a]]
--chunks n = takeWhile (not . null) . unfoldr (Just . splitAt n)
--chunks n xs = take n xs : chunks n (drop n xs)
chunks n xs = h : chunks n t
where
(h, t) = splitAt n xs
pulseaudioOutput :: [Sample] -> IO ()
pulseaudioOutput sx = do
pa <- simpleNew Nothing "Synths" Play Nothing "Synths PCM output"
(SampleSpec (F32 LittleEndian) 44100 1) Nothing Nothing
mapM_ (simpleWrite pa . map double2Float) $ chunks 1000 sx
simpleDrain pa
simpleFree pa
oscSine :: Frequency -> [CV] -> [Sample]
oscSine f = map sin <<< integral <<< map ((2 * pi * f *) . (2**))
music ::[Sample]
music = oscSine 440 (repeat 0)
main = do
pulseaudioOutput music
我清理了代码以避免混合普通旧列表和流融合列表。内存/ CPU泄漏仍然存在。要查看代码是否在旧列表上工作,只需删除Prelude import和&#34; .Stream&#34;在&#34; Data.List&#34;。
之后答案 0 :(得分:2)
由融合规则(http://hackage.haskell.org/package/stream-fusion-0.1.2.5/docs/Data-Stream.html#g:12)替换的流上的splitAt
具有以下签名:
splitAt :: Int -> Stream a -> ([a], [a])
由此可以看出,由于它产生列表而不是流,因此阻碍了进一步融合。我认为,正确的做法是生成一个splitAt
来生成流,或者更好地直接在流上编写chunks
函数,并使用列表版本中的相应融合规则。
以下是关于流的splitAt
我认为应该是好的。您当然需要将它与splitAt
列表中的相应重写规则配对,如果这些重写规则变得棘手,可能直接编写chunks
函数,尽管看起来有点棘手所以:
splitAt :: Int -> Stream a -> (Stream a, Stream a)
splitAt n0 (Stream next s0)
| n0 < 0 = (nilStream, (Stream next s0))
| otherwise = loop_splitAt n0 s0
where
nilStream = Stream (const Done) s0
loop_splitAt 0 !s = (nilStream, (Stream next s))
loop_splitAt !n !s = case next s of
Done -> (nilStream, nilStream)
Skip s' -> loop_splitAt n s'
Yield x s' -> (cons x xs', xs'')
where
(xs', xs'') = loop_splitAt (n-1) s'