-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_v3.idr
250 lines (187 loc) · 5.43 KB
/
Main_v3.idr
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
module Main
import Data.Buffer
import System
import Printf
%default total
{- Type aliases -}
Hz : Type
Hz = Double
Seconds : Type
Seconds = Double
Samples : Type
Samples = Int
Volume : Type
Volume = Double
Bytes : Type
Bytes = Int
Semitone : Type
Semitone = Int
MidiNote : Type
MidiNote = Int
BPM : Type
BPM = Double
{- Constants -}
sampleRate : Hz
sampleRate = 48000.0
sampleSize : Bytes
sampleSize = 8
a440 : Hz
a440 = 440.0
midiA4 : Int
midiA4 = 69
tempo : BPM
tempo = 104.0
outFilePath : String
outFilePath = "wave.bin"
playCommand : String
playCommand = printf "ffplay -showmode 1 -f f64le -ar %f %s" sampleRate outFilePath
{- Derived Constants -}
quarterNote : Seconds
quarterNote = 60.0 / tempo
halfNote : Seconds
halfNote = quarterNote * 2.0
wholeNote : Seconds
wholeNote = halfNote * 2.0
eigthNote : Seconds
eigthNote = quarterNote * 0.5
sixteenthNote : Seconds
sixteenthNote = eigthNote * 0.5
dottedQuarterNote : Seconds
dottedQuarterNote = quarterNote + eigthNote
dottedEigthNote : Seconds
dottedEigthNote = eigthNote + sixteenthNote
dottedSixteenthNote : Seconds
dottedSixteenthNote = sixteenthNote + (sixteenthNote * 0.5)
tripletQuarterNote : Seconds
tripletQuarterNote = quarterNote - (quarterNote * (1/3))
tripletEigthNote : Seconds
tripletEigthNote = eigthNote - (eigthNote * (1/3))
tripletSixteenthNote : Seconds
tripletSixteenthNote = sixteenthNote - (sixteenthNote * (1/3))
{- Data types -}
record Event where
constructor MkEvent
midiNote : MidiNote
volume : Volume
duration : Seconds
Show Event where
show (MkEvent midiNote volume duration) =
printf "Note: %d Vol: %f Dur: %f" midiNote volume duration
{- Data -}
sequence : List Event
sequence = [
{- Bar 1 -}
MkEvent 67 0.5 eigthNote,
MkEvent 0 0.0 eigthNote,
MkEvent 67 0.5 eigthNote,
MkEvent 0 0.0 eigthNote,
MkEvent 67 0.5 eigthNote,
MkEvent 0 0.0 eigthNote,
MkEvent 63 0.3 dottedEigthNote,
MkEvent 70 0.6 sixteenthNote,
{- Bar 2 -}
MkEvent 67 0.5 eigthNote,
MkEvent 0 0.0 eigthNote,
MkEvent 63 0.3 dottedEigthNote,
MkEvent 70 0.6 sixteenthNote,
MkEvent 67 0.5 quarterNote,
MkEvent 0 0.0 quarterNote,
{- Bar 3 -}
MkEvent 74 0.2 eigthNote,
MkEvent 0 0.0 eigthNote,
MkEvent 74 0.2 eigthNote,
MkEvent 0 0.0 eigthNote,
MkEvent 74 0.2 eigthNote,
MkEvent 0 0.0 eigthNote,
MkEvent 75 0.7 dottedEigthNote,
MkEvent 70 0.6 sixteenthNote,
{- Bar 4 -}
MkEvent 66 0.5 eigthNote,
MkEvent 0 0.0 eigthNote,
MkEvent 63 0.7 dottedEigthNote,
MkEvent 70 0.6 sixteenthNote,
MkEvent 67 0.5 quarterNote,
MkEvent 0 0.0 quarterNote
]
{- Helpers -}
-- formula for converting a semitone distance from A4
noteFrequency : Semitone -> Hz
noteFrequency n = a440 * (pow a (the Double (cast n)))
where
a : Double
a = pow 2 (1 / 12)
-- midi note to note frequency
midiFrequency : MidiNote -> Hz
midiFrequency note = noteFrequency (note - midiA4)
-- calculate the number of samples for the given duration
durationSamples : Seconds -> Samples
durationSamples duration = cast (sampleRate * duration)
-- calculate the size in bytes for the given duration
durationSize : Seconds -> Bytes
durationSize duration = (durationSamples duration) * sampleSize
-- calculate the total duration of the given sequence
calcDuration : List Event -> Seconds
calcDuration [] = 0.0
calcDuration ((MkEvent _ _ duration) :: xs) = duration + calcDuration xs
{- Major functions -}
-- fill a buffer with PCM data for a note event
wave : Buffer -> (start : Seconds) -> Event -> IO ()
wave buf start event@(MkEvent midiNote volume duration) =
do
putStrLn $ printf "Rendering @ %f: %s" start (show event)
-- fill the buffer with samples (count down)
makeWave sampleCount buf
where
sampleCount : Samples
sampleCount = durationSamples duration
offset : Bytes
offset = durationSize start
pitch : Hz
pitch = midiFrequency midiNote
step : Double
step = (pitch * 2.0 * pi) / sampleRate
makeWave : (cur : Samples) ->
Buffer ->
IO ()
makeWave 0 buf = pure ()
makeWave cur buf =
do
let delta : Int = sampleCount - cur
let sample : Double = ((* volume) . sin . (* step) . cast) delta
let loc : Int = offset + (delta * sampleSize)
setDouble buf loc sample
makeWave (assert_smaller cur (cur - 1)) buf
-- render the sequence of note events into the buffer at the start time
render : List Event -> Buffer -> (start : Seconds) -> IO ()
render [] buf _ = pure ()
render (event@(MkEvent midiNote volume duration) :: xs) buf start =
do
wave buf start event
render xs buf (start + duration)
-- save the buffer to the file path
saveBuffer : Buffer -> (filePath : String) -> IO (Either FileError Buffer)
saveBuffer buf filePath =
do
Right file <- openFile filePath WriteTruncate
| Left err => pure (Left err)
size <- rawSize buf
newBuf <- writeBufferToFile file buf size
pure (Right newBuf)
{- Main -}
main : IO ()
main = do
-- calculate the total duration of the sequence
let totalDuration : Seconds = calcDuration sequence
-- convert
let bufferSize : Bytes = durationSize totalDuration
-- allocate buffer
Just buf <- newBuffer bufferSize
| putStrLn "Failed to allocate buffer"
-- render the event sequence into the buffer
render sequence buf 0.0
-- save the buffer to a file
Right _ <- saveBuffer buf outFilePath
| Left err => putStrLn (show err)
-- play the sound file
v <- system playCommand
pure ()