-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
143 lines (106 loc) · 2.72 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module Main where
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Builder as B
import Data.Foldable
import System.Process
import Text.Printf
import Data.List
type Seconds = Float
type Samples = Float
type Hz = Float
type Pulse = Float
type Semitones = Float
type Beats = Float
outputFilePath :: FilePath
outputFilePath = "output.bin"
sampleRate :: Samples
sampleRate = 48000.0 -- samples per second
volume :: Float
volume = 0.2
bpm :: Beats
bpm = 240.0
pitchA :: Hz
pitchA = 440.0
sa :: Hz
sa = semitone pitchA 0
re' :: Hz
re' = semitone pitchA 1
re :: Hz
re = semitone pitchA 2
ga' :: Hz
ga' = semitone pitchA 3
ga :: Hz
ga = semitone pitchA 4
ma :: Hz
ma = semitone pitchA 5
ma' :: Hz
ma' = semitone pitchA 6
pa :: Hz
pa = semitone pitchA 7
dha' :: Hz
dha' = semitone pitchA 8
dha :: Hz
dha = semitone pitchA 9
ni' :: Hz
ni' = semitone pitchA 10
ni :: Hz
ni = semitone pitchA 11
silence :: Hz
silence = 0.0
beatDuration :: Seconds
beatDuration = 60.0 / bpm
semitone :: Hz -> Semitones -> Hz
semitone pitch n = pitch * (2 ** (1.0 / 12.0)) ** n
note :: Hz -> Semitones -> Beats -> [Pulse]
note pitch n beats = freq (semitone pitch n) beats
freq' :: [Hz] -> Beats -> [Pulse]
freq' hzs beats = concatMap (`freq` beats) hzs
freq :: Hz -> Beats -> [Pulse]
freq hz beats =
map (* volume) $
zipWith4 (\w x y z -> w * x * y * z) output attack decaySustain release
where
step = hz * 2 * pi / sampleRate
attack :: [Pulse]
attack = map (min 1.0) [0.0, 0.0005 ..]
decaySustain :: [Pulse]
decaySustain = let
attackLength = length $ takeWhile (/= 1) attack
paddingForAttack = replicate attackLength 1.0
in
concat [paddingForAttack, [1.0, 0.99985 .. 0.5], repeat 0.5]
release :: [Pulse]
release = reverse $ take (length output) attack
duration :: Seconds
duration = beats * beatDuration
output :: [Pulse]
output = map (sin . (* step)) [0.0 .. sampleRate * duration]
alankar :: [Hz]
alankar = [ sa, re, ga
, silence
, re, ga, ma
, silence
, ga, ma, pa
, silence
, ma, pa, dha
, silence
, pa, dha, ni
, silence
, dha, ni, sa * 2
]
maalkaunsAroha :: [Hz]
maalkaunsAroha = [ sa, ga', ma, dha', ni', sa * 2 ]
maalkaunsAwaroha :: [Hz]
maalkaunsAwaroha = reverse maalkaunsAroha
wave :: [Pulse]
wave = freq' (maalkaunsAroha ++ [silence] ++ maalkaunsAwaroha) beats
where beats = 2
save :: IO ()
save = B.writeFile outputFilePath . B.toLazyByteString $ foldMap B.floatLE wave
play :: IO ()
play = do
save
runCommand $ printf "ffplay -showmode 2 -f f32le -ar %f %s" sampleRate outputFilePath
return ()
main :: IO ()
main = putStrLn "Hello, world!"