% ghc sicp.hs && ./a.out > /dev/dsp (or | aplay for real men) module Main where
import Data.Char
makeU8 x freq = (((sin (x * (2 * pi) * (freq / 8000))) + 1) * 128.0)
tone freq length amplitude x =
if x >= length
then []
else (floor ((makeU8 x freq) * amplitude)) : tone freq length (0.99925 * amplitude) (x + 1)
tones = [("c2", 65.406), ("d2", 73.416), ("e2", 82.407), ("f2", 87.307), ("g2", 97.999), ("a2", 110.00), ("b2", 123.47), ("c3", 130.81), ("d3", 146.83), ("e3", 164.81), ("f3", 174.61), ("g3", 196.00), ("a3", 220.00), ("b3", 246.94), ("c4", 261.63), ("d4", 293.66), ("e4", 329.63), ("f4", 349.23), ("g4", 392.00)]
t x = case lookup x tones of
Just y -> (tone y 2000 0.75 0) :: [Int]
Nothing -> []
main = do
putStr $ map chr $ concatMap t ["g3", "a3", "b3", "d4", "c4", "c4", "e4", "d4", "d4", "g4", "f4", "g4", "d4", "c4", "g3", "a3", "b3", "c4", "d4", "e4", "d4", "c4", "b3", "a3", "b3", "g3", "f3", "g3", "a3", "d3", "f3", "a3", "c4", "b3", "a3", "b3", "g3", "a3", "b3", "d4", "c4", "c4", "e4", "d4", "d4", "g4", "f4", "g4", "d4", "b3", "g3", "a3", "b3", "e3", "d4", "c4"]
Name:
Anonymous2008-04-29 15:38
>>40
You mean: IT SOUNDS PRETTY GODLY! *FLASH OF LIGHTNING*
Name:
Anonymous2008-04-29 19:57
>>34
It's designed to be ioctl'd, but >>35 is right
Name:
Anonymous2008-04-29 20:16
>>1
Can someone be kind enough to translate this to something more natural? I can't stand haskell. I fucking hate it.
Scheme, maybe?
Name:
Anonymous2008-04-29 23:26
>>43
Pretty simple really. You have a makeU8(x,freq) function, read it yourself.
Then tone(freq,amplitute,length) = map (lambda (x) -> floor(makeU8(x,freq)*amplitude), (0,1,2,3,..,length))
There is a map of notes to frequency. Send notes/frequencies to (tone freq 2000 0.75 0) one by one. You get a list of integers for a note. Concatenate the list. Integers to characters. Output.
Why not do the string → frequency mapping algorithmically instead of using a list lookup? Seems like it'd be more flexible and reduce the amount of magic numbers present in the code. I'm too lazy to do it, but it should be simple.
Name:
Anonymous2008-05-08 15:34
>>48
Give me the algorithm and I'll do it -- I couldn't find it, however, and these numbers are standardized by some committee.
Name:
Anonymous2008-05-08 15:43
>>48
Give me the algorithm and I'll do it -- I couldn't find it, however, and these numbers are standardized by some committee.
Just plotted it with gnuplot... doesn't make sense.
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy