rlm@417
|
1 (ns com.aurellem.run.music
|
rlm@417
|
2 (:use (com.aurellem.gb saves gb-driver util constants
|
rlm@417
|
3 items vbm characters money
|
rlm@417
|
4 rlm-assembly))
|
rlm@417
|
5 (:use (com.aurellem.run util title save-corruption
|
rlm@417
|
6 bootstrap-0 bootstrap-1))
|
rlm@426
|
7 (:require clojure.string)
|
rlm@426
|
8 (:import [com.aurellem.gb.gb_driver SaveState])
|
rlm@426
|
9 (:import java.io.File))
|
rlm@417
|
10
|
rlm@427
|
11 (def third-kind
|
rlm@427
|
12 (File. "/home/r/proj/midi/third-kind.mid"))
|
rlm@417
|
13
|
rlm@455
|
14 (def pony
|
rlm@455
|
15 (File. "/home/r/proj/vba-clojure/music/pony-title.mid"))
|
rlm@455
|
16
|
rlm@455
|
17 (def sync-test
|
rlm@455
|
18 (File. "/home/r/proj/vba-clojure/music/sync-test.mid"))
|
rlm@455
|
19
|
rlm@454
|
20
|
rlm@427
|
21 (defn raw-midi-text [#^File midi-file]
|
rlm@427
|
22 (:out
|
rlm@427
|
23 (clojure.java.shell/sh
|
rlm@427
|
24 "midicsv"
|
rlm@427
|
25 (.getCanonicalPath midi-file)
|
rlm@427
|
26 "-")))
|
rlm@427
|
27
|
rlm@427
|
28 (def command-line #"^(\d+), (\d+), ([^,]+)(.*)$")
|
rlm@427
|
29
|
rlm@427
|
30 (defmulti parse-command :command)
|
rlm@427
|
31
|
rlm@427
|
32 (defn discard-args [command] (dissoc command :args))
|
rlm@427
|
33
|
rlm@427
|
34 (defmethod parse-command :Start_track
|
rlm@427
|
35 [command] (discard-args command))
|
rlm@427
|
36
|
rlm@427
|
37 (defmethod parse-command :End_track
|
rlm@427
|
38 [command] (discard-args command))
|
rlm@427
|
39
|
rlm@427
|
40 (defmethod parse-command :default
|
rlm@427
|
41 [command] command)
|
rlm@427
|
42
|
rlm@427
|
43 (defn parse-number-list
|
rlm@427
|
44 [number-list-str]
|
rlm@427
|
45 (map #(Integer/parseInt %)
|
rlm@427
|
46 (clojure.string/split number-list-str #", ")))
|
rlm@427
|
47
|
rlm@427
|
48 (defmethod parse-command :Tempo
|
rlm@427
|
49 [command]
|
rlm@427
|
50 (update-in command [:args] #(Integer/parseInt %)))
|
rlm@427
|
51
|
rlm@427
|
52 (defn parse-midi-note-list
|
rlm@427
|
53 [midi-note-list-str]
|
rlm@427
|
54 (let [[channel note velocity]
|
rlm@427
|
55 (parse-number-list midi-note-list-str)]
|
rlm@427
|
56 {:channel channel :note note :velocity velocity}))
|
rlm@427
|
57
|
rlm@427
|
58 (defmethod parse-command :Note_on_c
|
rlm@427
|
59 [command]
|
rlm@427
|
60 (update-in command [:args] parse-midi-note-list))
|
rlm@427
|
61
|
rlm@427
|
62 (defmethod parse-command :Note_off_c
|
rlm@427
|
63 [command]
|
rlm@427
|
64 (update-in command [:args] parse-midi-note-list))
|
rlm@427
|
65
|
rlm@427
|
66 (defmethod parse-command :Header
|
rlm@427
|
67 [command]
|
rlm@427
|
68 (let [args (:args command)
|
rlm@427
|
69 [format num-tracks division] (parse-number-list args)]
|
rlm@427
|
70 (assoc command :args
|
rlm@427
|
71 {:format format
|
rlm@427
|
72 :num-tracks num-tracks
|
rlm@427
|
73 :division division})))
|
rlm@427
|
74
|
rlm@427
|
75 (defmethod parse-command :Program_c
|
rlm@427
|
76 [command]
|
rlm@427
|
77 (let [args (:args command)
|
rlm@427
|
78 [channel program-num] (parse-number-list args)]
|
rlm@427
|
79 (assoc command :args
|
rlm@427
|
80 {:channel channel
|
rlm@427
|
81 :program-num program-num})))
|
rlm@427
|
82
|
rlm@427
|
83 (defn parse-midi [#^File midi-file]
|
rlm@427
|
84 (map
|
rlm@427
|
85 (comp parse-command
|
rlm@427
|
86 (fn [line]
|
rlm@427
|
87 (let [[[_ channel time command args]]
|
rlm@427
|
88 (re-seq command-line line)]
|
rlm@427
|
89 {:channel (Integer/parseInt channel)
|
rlm@427
|
90 :time (Integer/parseInt time)
|
rlm@427
|
91 :command (keyword command)
|
rlm@427
|
92 :args (apply str (drop 2 args))})))
|
rlm@427
|
93 (drop-last
|
rlm@427
|
94 (clojure.string/split-lines
|
rlm@427
|
95 (raw-midi-text midi-file)))))
|
rlm@427
|
96
|
rlm@417
|
97 (def music-base new-kernel)
|
rlm@417
|
98
|
rlm@417
|
99 (defn store [n address]
|
rlm@417
|
100 (flatten
|
rlm@417
|
101 [0xF5
|
rlm@417
|
102 0xE5
|
rlm@417
|
103
|
rlm@417
|
104 0x3E
|
rlm@417
|
105 n
|
rlm@417
|
106
|
rlm@417
|
107 0x21
|
rlm@417
|
108 (reverse (disect-bytes-2 address))
|
rlm@417
|
109
|
rlm@417
|
110 0x77
|
rlm@417
|
111
|
rlm@417
|
112 0xE1
|
rlm@417
|
113 0xF1]))
|
rlm@417
|
114
|
rlm@417
|
115 (defn infinite-loop []
|
rlm@417
|
116 [0x18 0xFE])
|
rlm@417
|
117
|
rlm@417
|
118 (def divider-register 0xFF04)
|
rlm@417
|
119
|
rlm@417
|
120 (defrecord Bit-Note [frequency volume duration duty])
|
rlm@417
|
121
|
rlm@417
|
122 (defn clear-music-registers []
|
rlm@417
|
123 (flatten
|
rlm@433
|
124 [(store (Integer/parseInt "00000000" 2) 0xFF10) ;; sweep
|
rlm@433
|
125 (store (Integer/parseInt "00000000" 2) 0xFF11) ;; pattern duty
|
rlm@433
|
126 (store (Integer/parseInt "00000000" 2) 0xFF12) ;; volume
|
rlm@433
|
127 (store (Integer/parseInt "00000000" 2) 0xFF13) ;; frequency-low
|
rlm@433
|
128 (store (Integer/parseInt "00000000" 2) 0xFF14) ;; frequency-high
|
rlm@417
|
129
|
rlm@417
|
130 (store (Integer/parseInt "00000000" 2) 0xFF16) ;; pattern duty 000000
|
rlm@417
|
131 (store (Integer/parseInt "00000000" 2) 0xFF17) ;; volume 0000
|
rlm@417
|
132 (store (Integer/parseInt "00000000" 2) 0xFF18) ;; frequency-low
|
rlm@417
|
133 (store (Integer/parseInt "00000000" 2) 0xFF19) ;; 00000 frequency-high
|
rlm@417
|
134
|
rlm@417
|
135 (store (Integer/parseInt "00000000" 2) 0xFF1A)
|
rlm@417
|
136 (store (Integer/parseInt "00000000" 2) 0xFF1B)
|
rlm@417
|
137 (store (Integer/parseInt "00000000" 2) 0xFF1C)
|
rlm@417
|
138 (store (Integer/parseInt "00000000" 2) 0xFF1D)
|
rlm@417
|
139 (store (Integer/parseInt "00000000" 2) 0xFF1E)
|
rlm@417
|
140
|
rlm@433
|
141 (store (Integer/parseInt "00000000" 2) 0xFF20) ;; length
|
rlm@433
|
142 (store (Integer/parseInt "00000000" 2) 0xFF21) ;; volume
|
rlm@433
|
143 (store (Integer/parseInt "00000000" 2) 0xFF22) ;; noise-frequency
|
rlm@433
|
144 (store (Integer/parseInt "00000000" 2) 0xFF23) ;; control
|
rlm@433
|
145 ]))
|
rlm@417
|
146
|
rlm@424
|
147
|
rlm@424
|
148 ;; mini-midi syntax
|
rlm@424
|
149
|
rlm@424
|
150 ;; codes
|
rlm@424
|
151 ;; note-code == 0x00
|
rlm@424
|
152 ;; change-duty-code = 0x01
|
rlm@424
|
153 ;; silence-code = 0x02
|
rlm@424
|
154
|
rlm@424
|
155 ;; silence format
|
rlm@424
|
156 ;; 2 bytes
|
rlm@424
|
157 ;; [silence-code (0x02)]
|
rlm@424
|
158 ;; [duration-8-bits]
|
rlm@424
|
159
|
rlm@424
|
160 ;; note data format
|
rlm@424
|
161 ;; 4 bytes
|
rlm@424
|
162 ;; [note-code (0x00)]
|
rlm@424
|
163 ;; [volume-4-bits 0 frequency-high-3-bits]
|
rlm@424
|
164 ;; [frequengy-low-8-bits]
|
rlm@424
|
165 ;; [duration-8-bits]
|
rlm@424
|
166
|
rlm@424
|
167 ;; change-duty-format
|
rlm@424
|
168 ;; 2 bytes
|
rlm@424
|
169 ;; [change-duty-code (0x01)]
|
rlm@424
|
170 ;; [new-duty]
|
rlm@424
|
171
|
rlm@425
|
172 (def note-code 0x00)
|
rlm@425
|
173 (def change-duty-code 0x01)
|
rlm@425
|
174 (def silence-code 0x02)
|
rlm@425
|
175
|
rlm@424
|
176 (defn do-message
|
rlm@424
|
177 "Read the message which starts at the current value of HL and do
|
rlm@424
|
178 what it says. Duration is left in A, and HL is advanced
|
rlm@424
|
179 appropraitely."
|
rlm@461
|
180 ([] (do-message 0x16 1))
|
rlm@461
|
181 ([sound-base-address wave-duty]
|
rlm@461
|
182 (assert (<= 0 wave-duty 3))
|
rlm@433
|
183 (let [switch
|
rlm@433
|
184 [0x2A ;; load message code into A, increment HL
|
rlm@433
|
185
|
rlm@433
|
186 ;; switch on message
|
rlm@433
|
187 0xFE
|
rlm@433
|
188 note-code
|
rlm@433
|
189
|
rlm@433
|
190 0x20
|
rlm@433
|
191 :note-length]
|
rlm@424
|
192
|
rlm@433
|
193 play-note
|
rlm@461
|
194 [0x3E ;; set wave-duty
|
rlm@461
|
195 (bit-shift-left wave-duty 6)
|
rlm@461
|
196 0xE0
|
rlm@461
|
197 sound-base-address
|
rlm@461
|
198 0x2A ;; load volume/frequency-high info
|
rlm@433
|
199 0xF5 ;; push A
|
rlm@433
|
200 0xE6
|
rlm@433
|
201 (Integer/parseInt "11110000" 2) ;; volume mask
|
rlm@433
|
202 0xE0
|
rlm@433
|
203 (inc sound-base-address) ;;0x17 ;; set volume
|
rlm@433
|
204 0xF1 ;; pop A
|
rlm@433
|
205 0xE6
|
rlm@433
|
206 (Integer/parseInt "00000111" 2) ;; frequency-high mask
|
rlm@433
|
207 0xE0
|
rlm@433
|
208 (+ 3 sound-base-address) ;;0x19 ;; set frequency-high
|
rlm@433
|
209
|
rlm@433
|
210 0x2A ;; load frequency low-bits
|
rlm@433
|
211 0xE0
|
rlm@433
|
212 (+ 2 sound-base-address) ;;0x18 ;; set frequency-low-bits
|
rlm@433
|
213 0x2A]] ;; load duration
|
rlm@433
|
214 (replace
|
rlm@433
|
215 {:note-length (count play-note)}
|
rlm@433
|
216 (concat switch play-note)))))
|
rlm@424
|
217
|
rlm@466
|
218 (defn play-noise
|
rlm@466
|
219 "read [noise-code, duration] and play the noise. Duration is left in
|
rlm@466
|
220 A, and HL is advanced appropraitely."
|
rlm@466
|
221 ([]
|
rlm@466
|
222 [0x2A ;; load noise-code into A
|
rlm@466
|
223 0xE0
|
rlm@466
|
224 0x22 ;; write noise-code
|
rlm@466
|
225 0x2A] ;; load duration into A
|
rlm@466
|
226 ))
|
rlm@466
|
227
|
rlm@466
|
228
|
rlm@433
|
229 ;; (defn play-note
|
rlm@433
|
230 ;; "Play the note referenced by HL in the appropiate channel.
|
rlm@433
|
231 ;; Leaves desired-duration in A."
|
rlm@433
|
232
|
rlm@433
|
233 ;; [0x2A ;; load volume/frequency-high info
|
rlm@433
|
234 ;; 0xF5 ;; push A
|
rlm@433
|
235 ;; 0xE6
|
rlm@433
|
236 ;; (Integer/parseInt "11110000" 2) ;; volume mask
|
rlm@433
|
237 ;; 0xE0
|
rlm@433
|
238 ;; 0x17 ;; set volume
|
rlm@433
|
239 ;; 0xF1 ;; pop A
|
rlm@433
|
240 ;; 0xE6
|
rlm@433
|
241 ;; (Integer/parseInt "00000111" 2) ;; frequency-high mask
|
rlm@433
|
242 ;; 0xE0
|
rlm@433
|
243 ;; 0x19 ;; set frequency-high
|
rlm@417
|
244
|
rlm@433
|
245 ;; 0x2A ;; load frequency low-bits
|
rlm@433
|
246 ;; 0xE0
|
rlm@433
|
247 ;; 0x18 ;; set frequency-low-bits
|
rlm@417
|
248
|
rlm@433
|
249 ;; 0x2A ;; load duration
|
rlm@433
|
250 ;; ])
|
rlm@417
|
251
|
rlm@466
|
252 (defn music-step [sound-base-address wave-duty noise?]
|
rlm@432
|
253 ;; C == current-ticks
|
rlm@432
|
254 ;; A == desired-ticks
|
rlm@435
|
255
|
rlm@435
|
256 (flatten
|
rlm@435
|
257 [;; restore variables from stack
|
rlm@435
|
258 0xE1 ;; pop HL
|
rlm@435
|
259 0xC1 ;; pop CB
|
rlm@435
|
260 0xF1 ;; pop AF
|
rlm@432
|
261
|
rlm@435
|
262
|
rlm@435
|
263 0xF5 ;; push A
|
rlm@417
|
264 0xF0
|
rlm@432
|
265 0x05 ;; load current ticks from 0xF005
|
rlm@431
|
266 0xB8 ;;
|
rlm@417
|
267 0x30 ;; increment C only if last result caused carry
|
rlm@417
|
268 0x01
|
rlm@418
|
269 0x0C
|
rlm@417
|
270
|
rlm@417
|
271 0x47 ;; update sub-ticks (A->B)
|
rlm@417
|
272
|
rlm@417
|
273 0xF1 ;; pop AF, now A contains desired-ticks
|
rlm@417
|
274
|
rlm@417
|
275 0xB9 ;; compare with current ticks
|
rlm@417
|
276
|
rlm@417
|
277 ;; if desired-ticks = current ticks
|
rlm@417
|
278 ;; go to next note ; set current set ticks to 0.
|
rlm@417
|
279
|
rlm@466
|
280 (if noise?
|
rlm@466
|
281 [0x20
|
rlm@466
|
282 (+ 2 (count (play-noise)))
|
rlm@466
|
283 (play-noise)]
|
rlm@466
|
284
|
rlm@466
|
285 [0x20
|
rlm@466
|
286 (+ (count (do-message 0 0)) 2)
|
rlm@466
|
287 (do-message sound-base-address wave-duty)])
|
rlm@417
|
288
|
rlm@417
|
289 0x0E
|
rlm@435
|
290 0x00 ;; 0->C (current-ticks)
|
rlm@417
|
291
|
rlm@435
|
292 ;; save variables to stack
|
rlm@435
|
293 0xF5 ;; push AF
|
rlm@435
|
294 0xC5 ;; push CB
|
rlm@435
|
295 0xE5 ;; push HL
|
rlm@435
|
296
|
rlm@435
|
297
|
rlm@435
|
298 ]))
|
rlm@435
|
299
|
rlm@435
|
300 (def music-1 0x11)
|
rlm@434
|
301 (def music-2 0x16)
|
rlm@434
|
302
|
rlm@461
|
303 (defn music-kernel [wave-duty-1 wave-duty-2]
|
rlm@417
|
304 (flatten
|
rlm@432
|
305 [;; global initilization section
|
rlm@432
|
306 (clear-music-registers)
|
rlm@432
|
307
|
rlm@417
|
308 0x3E
|
rlm@418
|
309 0x01
|
rlm@417
|
310 0xE0
|
rlm@417
|
311 0x06 ;; set TMA to 0
|
rlm@417
|
312
|
rlm@417
|
313 0x3E
|
rlm@423
|
314 (Integer/parseInt "00000110" 2)
|
rlm@417
|
315 0xE0
|
rlm@423
|
316 0x07 ;; set TAC to 65536 Hz and activate timer
|
rlm@417
|
317
|
rlm@435
|
318 ;; initialize frame 1
|
rlm@432
|
319 0x21
|
rlm@432
|
320 0x00
|
rlm@437
|
321 0xA0 ;; set HL to 0xA000 == music-start 1
|
rlm@432
|
322 0x0E
|
rlm@432
|
323 0x00 ;; 0->C
|
rlm@432
|
324 0x06
|
rlm@432
|
325 0x00 ;; 0->B
|
rlm@435
|
326
|
rlm@433
|
327 0xAF ;; 0->A
|
rlm@432
|
328
|
rlm@435
|
329 0xF5 ;; push AF
|
rlm@435
|
330 0xC5 ;; push CB
|
rlm@435
|
331 0xE5 ;; push HL
|
rlm@432
|
332
|
rlm@435
|
333 ;; initialize frame 2
|
rlm@436
|
334 0x21
|
rlm@436
|
335 0x00
|
rlm@437
|
336 0xB0 ;; set HL to 0xB000 == music-start 2
|
rlm@436
|
337
|
rlm@436
|
338 0xF5 ;; push AF
|
rlm@436
|
339 0xC5 ;; push CB
|
rlm@436
|
340 0xE5 ;; push HL
|
rlm@436
|
341
|
rlm@436
|
342
|
rlm@466
|
343 ;; initialize frame 3 (noise)
|
rlm@466
|
344 0x21
|
rlm@466
|
345 0x00
|
rlm@466
|
346 0xA9 ;; 0xA9OO -> HL
|
rlm@466
|
347
|
rlm@466
|
348 0xF5 ;; push AF
|
rlm@466
|
349 0xC5 ;; push CB
|
rlm@466
|
350 0xE5 ;; push HL
|
rlm@466
|
351
|
rlm@437
|
352 ;; main music loop
|
rlm@437
|
353
|
rlm@466
|
354 0xE8 ;; SP + 12; activate frame 1
|
rlm@466
|
355 12
|
rlm@466
|
356 (music-step music-1 wave-duty-1 false)
|
rlm@435
|
357
|
rlm@437
|
358 0xE8 ;; SP - 6; activate frame 2
|
rlm@437
|
359 (->signed-8-bit -6)
|
rlm@466
|
360 (music-step music-2 wave-duty-2 false)
|
rlm@466
|
361
|
rlm@466
|
362 0xE8 ;; SP - 6; activate frame 3
|
rlm@466
|
363 (->signed-8-bit -6)
|
rlm@466
|
364 (music-step nil nil true)
|
rlm@424
|
365
|
rlm@417
|
366 0x18
|
rlm@437
|
367 (->signed-8-bit (+
|
rlm@437
|
368 ;; two music-steps
|
rlm@466
|
369 (- (* 2 (count (music-step 0 0 false))))
|
rlm@466
|
370 (- (count (music-step nil nil true)))
|
rlm@437
|
371 -2 ;; this jump instruction
|
rlm@437
|
372 -2 ;; activate frame 1
|
rlm@437
|
373 -2 ;; activate frame 2
|
rlm@466
|
374 -2 ;; activate frame 3
|
rlm@437
|
375 ))]))
|
rlm@417
|
376
|
rlm@424
|
377 (defn frequency-code->frequency
|
rlm@424
|
378 [code]
|
rlm@424
|
379 (assert (<= 0 code 2047))
|
rlm@424
|
380 (/ 131072 (- 2048 code)))
|
rlm@424
|
381
|
rlm@424
|
382 (defn clamp [x low high]
|
rlm@424
|
383 (cond (> x high) high
|
rlm@424
|
384 (< x low) low
|
rlm@424
|
385 true x))
|
rlm@424
|
386
|
rlm@424
|
387 (defn frequency->frequency-code
|
rlm@424
|
388 [frequency]
|
rlm@424
|
389 (clamp
|
rlm@424
|
390 (Math/round
|
rlm@424
|
391 (float
|
rlm@424
|
392 (/ (- (* 2048 frequency) 131072) frequency)))
|
rlm@424
|
393 0x00 2048))
|
rlm@424
|
394
|
rlm@424
|
395 (defn note-codes [frequency volume duration]
|
rlm@424
|
396 (assert (<= 0 volume 0xF))
|
rlm@430
|
397 (if (<= duration 0xFF)
|
rlm@430
|
398 (let [frequency-code
|
rlm@430
|
399 (frequency->frequency-code frequency)
|
rlm@430
|
400 volume&high-frequency
|
rlm@430
|
401 (+ (bit-shift-left volume 4)
|
rlm@430
|
402 (bit-shift-right frequency-code 8))
|
rlm@430
|
403 low-frequency
|
rlm@430
|
404 (bit-and 0xFF frequency-code)]
|
rlm@430
|
405 [note-code
|
rlm@430
|
406 volume&high-frequency
|
rlm@430
|
407 low-frequency
|
rlm@430
|
408 duration])
|
rlm@430
|
409 (vec
|
rlm@430
|
410 (flatten
|
rlm@430
|
411 [(note-codes frequency volume 0xFF)
|
rlm@430
|
412 (note-codes frequency volume (- duration 0xFF))]))))
|
rlm@430
|
413
|
rlm@424
|
414
|
rlm@427
|
415 (defn midi-code->frequency
|
rlm@427
|
416 [midi-code]
|
rlm@427
|
417 (* 8.1757989156
|
rlm@427
|
418 (Math/pow 2 (* (float (/ 12)) midi-code))))
|
rlm@427
|
419
|
rlm@427
|
420 ;; division == clock-pulses / quarter-note
|
rlm@427
|
421 ;; tempo == microseconds / quarter-note
|
rlm@427
|
422
|
rlm@427
|
423 ;; have: clock-pulses
|
rlm@427
|
424 ;; want: seconds
|
rlm@427
|
425
|
rlm@427
|
426
|
rlm@428
|
427 (defn silence [length]
|
rlm@428
|
428 {:frequency 1
|
rlm@428
|
429 :duration length
|
rlm@428
|
430 :volume 0})
|
rlm@427
|
431
|
rlm@466
|
432 (defn commands
|
rlm@466
|
433 "return all events where #(= (:command %) command)"
|
rlm@466
|
434 [command s]
|
rlm@466
|
435 (filter #(= command (:command %)) s))
|
rlm@466
|
436
|
rlm@462
|
437 (defn track-info [#^File midi-file]
|
rlm@462
|
438 (let [events (parse-midi midi-file)
|
rlm@462
|
439 track-titles (commands :Title_t events)
|
rlm@462
|
440 track-info
|
rlm@462
|
441 (map #(read-string (read-string (:args %))) track-titles)
|
rlm@462
|
442 track-map
|
rlm@462
|
443 (zipmap track-info track-titles)]
|
rlm@462
|
444 track-map))
|
rlm@462
|
445
|
rlm@462
|
446 (defn target-tracks
|
rlm@462
|
447 "return the track-numbers in the form [voice-0 voice-1 noise]"
|
rlm@462
|
448 [#^File midi-file]
|
rlm@462
|
449 (let [track-data (track-info midi-file)
|
rlm@462
|
450 track-order
|
rlm@462
|
451 (zipmap (map :out (keys track-data))
|
rlm@462
|
452 (vals track-data))
|
rlm@462
|
453 channel-nums (map (comp :channel track-order) (range 3))]
|
rlm@462
|
454 channel-nums))
|
rlm@438
|
455
|
rlm@438
|
456 (defn midi-track->mini-midi [#^File midi-file track-num]
|
rlm@427
|
457 (let [midi-events (parse-midi midi-file)
|
rlm@427
|
458
|
rlm@438
|
459 note-on-events (commands :Note_on_c midi-events)
|
rlm@438
|
460 note-off-events (commands :Note_off_c midi-events)
|
rlm@427
|
461
|
rlm@438
|
462 select-channel
|
rlm@438
|
463 (fn [n s]
|
rlm@462
|
464 (sort-by :time (filter #(= n (:channel %)) s)))
|
rlm@438
|
465
|
rlm@438
|
466 channel-on (select-channel track-num note-on-events)
|
rlm@427
|
467
|
rlm@438
|
468 channel-off (select-channel track-num note-off-events)
|
rlm@427
|
469
|
rlm@438
|
470
|
rlm@438
|
471 tempo (:args (first (commands :Tempo midi-events)))
|
rlm@438
|
472 division
|
rlm@438
|
473 (:division (:args (first (commands :Header midi-events))))
|
rlm@428
|
474
|
rlm@428
|
475 notes
|
rlm@428
|
476 (map
|
rlm@428
|
477 (fn [note-on note-off]
|
rlm@428
|
478 {:frequency (midi-code->frequency (:note (:args note-on)))
|
rlm@428
|
479 :duration
|
rlm@428
|
480 (/ (* (/ tempo division)
|
rlm@428
|
481 (- (:time note-off) (:time note-on)))
|
rlm@428
|
482 1e6) ;; convert clock-pulses into seconds
|
rlm@428
|
483 :volume (int (/ (:velocity (:args note-on)) 10))
|
rlm@428
|
484 :time-stamp (/ (* (/ tempo division)
|
rlm@428
|
485 (:time note-on)) 1e6)})
|
rlm@438
|
486 channel-on channel-off)
|
rlm@428
|
487
|
rlm@428
|
488 silences
|
rlm@428
|
489 (map (fn [note-1 note-2]
|
rlm@428
|
490 (let [note-1-space (- (:time-stamp note-2)
|
rlm@428
|
491 (:time-stamp note-1))
|
rlm@428
|
492 note-1-length (:duration note-1)]
|
rlm@428
|
493 (silence (- note-1-space note-1-length))))
|
rlm@428
|
494 ;; to handle silence at the beginning.
|
rlm@428
|
495 (concat [(assoc (silence 0)
|
rlm@428
|
496 :time-stamp 0)] notes)
|
rlm@428
|
497 notes)
|
rlm@428
|
498
|
rlm@428
|
499 notes-with-silence
|
rlm@456
|
500 (concat
|
rlm@456
|
501 (filter (comp not zero? :duration)
|
rlm@456
|
502 (interleave silences notes))
|
rlm@456
|
503 [(silence 3)])]
|
rlm@456
|
504
|
rlm@456
|
505 (map
|
rlm@456
|
506 (fn [note-event]
|
rlm@456
|
507 (note-codes (:frequency note-event)
|
rlm@456
|
508 (:volume note-event)
|
rlm@456
|
509 (int (* (:duration note-event) 0x100))))
|
rlm@456
|
510 notes-with-silence)))
|
rlm@456
|
511
|
rlm@438
|
512 (defn midi->mini-midi [#^File midi-file]
|
rlm@462
|
513 (let [targets (target-tracks midi-file)
|
rlm@462
|
514 get-track (fn [n]
|
rlm@462
|
515 (if (not (nil? n))
|
rlm@462
|
516 (midi-track->mini-midi midi-file n)
|
rlm@463
|
517 []))
|
rlm@463
|
518 duty-info (keys (track-info midi-file))]
|
rlm@463
|
519
|
rlm@462
|
520 {:voice-1 (get-track (nth targets 0))
|
rlm@462
|
521 :voice-2 (get-track (nth targets 1))
|
rlm@463
|
522 :noise (get-track (nth targets 2))
|
rlm@463
|
523 :duty (zipmap (map :out duty-info)
|
rlm@464
|
524 (map #(get % :duty 0) duty-info))}))
|
rlm@438
|
525
|
rlm@438
|
526 (defn play-midi [#^File midi-file]
|
rlm@438
|
527 (let [track-1-target 0xA000
|
rlm@438
|
528 track-2-target 0xB000
|
rlm@438
|
529 program-target 0xC000
|
rlm@438
|
530 mini-midi (midi->mini-midi midi-file)
|
rlm@461
|
531 long-silence (flatten (note-codes 20 0 9001))
|
rlm@463
|
532
|
rlm@463
|
533 voice-1 (flatten (:voice-1 mini-midi))
|
rlm@464
|
534 wave-duty-1 ((:duty mini-midi) 0 0)
|
rlm@463
|
535
|
rlm@463
|
536 voice-2 (flatten (:voice-2 mini-midi))
|
rlm@464
|
537 wave-duty-2 ((:duty mini-midi) 1 0)
|
rlm@466
|
538
|
rlm@466
|
539 noise (flatten (:noise mini-midi))
|
rlm@461
|
540 ]
|
rlm@438
|
541
|
rlm@438
|
542 (-> (second (music-base))
|
rlm@438
|
543 (set-memory-range track-1-target long-silence)
|
rlm@438
|
544 (set-memory-range track-2-target long-silence)
|
rlm@463
|
545 (set-memory-range track-1-target voice-1)
|
rlm@463
|
546 (set-memory-range track-2-target voice-2)
|
rlm@461
|
547 (set-memory-range
|
rlm@461
|
548 program-target
|
rlm@461
|
549 (music-kernel wave-duty-1 wave-duty-2))
|
rlm@438
|
550 (PC! program-target))))
|
rlm@438
|
551
|
rlm@424
|
552 (def C4 (partial note-codes 261.63))
|
rlm@424
|
553 (def D4 (partial note-codes 293.66))
|
rlm@424
|
554 (def E4 (partial note-codes 329.63))
|
rlm@424
|
555 (def F4 (partial note-codes 349.23))
|
rlm@424
|
556 (def G4 (partial note-codes 392))
|
rlm@424
|
557 (def A4 (partial note-codes 440))
|
rlm@424
|
558 (def B4 (partial note-codes 493.88))
|
rlm@424
|
559 (def C5 (partial note-codes 523.3))
|
rlm@424
|
560
|
rlm@430
|
561 (def scale
|
rlm@430
|
562 (flatten
|
rlm@430
|
563 [(C4 0xF 0x40)
|
rlm@430
|
564 (D4 0xF 0x40)
|
rlm@430
|
565 (E4 0xF 0x40)
|
rlm@430
|
566 (F4 0xF 0x40)
|
rlm@430
|
567 (G4 0xF 0x40)
|
rlm@430
|
568 (A4 0xF 0x40)
|
rlm@430
|
569 (B4 0xF 0x40)
|
rlm@430
|
570 (C5 0xF 0x40)]))
|
rlm@424
|
571
|
rlm@418
|
572 (defn play-music [music-bytes]
|
rlm@417
|
573 (let [program-target 0xC000
|
rlm@437
|
574 music-target 0xA000]
|
rlm@417
|
575 (-> (set-memory-range (second (music-base))
|
rlm@417
|
576 program-target (music-kernel))
|
rlm@417
|
577 (set-memory-range music-target music-bytes)
|
rlm@417
|
578 (PC! program-target))))
|
rlm@417
|
579
|
rlm@417
|
580 (defn run-program
|
rlm@418
|
581 ([program]
|
rlm@417
|
582 (let [target 0xC000]
|
rlm@417
|
583 (-> (set-memory-range (second (music-base))
|
rlm@417
|
584 target program)
|
rlm@417
|
585 (PC! target)))))
|
rlm@417
|
586
|
rlm@424
|
587 (defn test-timer []
|
rlm@424
|
588 (flatten
|
rlm@424
|
589 [0x3E
|
rlm@424
|
590 0x01
|
rlm@424
|
591 0xE0
|
rlm@424
|
592 0x06 ;; set TMA to 0
|
rlm@424
|
593
|
rlm@424
|
594 0x3E
|
rlm@424
|
595 (Integer/parseInt "00000100" 2)
|
rlm@424
|
596 0xE0
|
rlm@424
|
597 0x07 ;; set TAC to 16384 Hz and activate timer
|
rlm@424
|
598
|
rlm@424
|
599 (repeat
|
rlm@424
|
600 500
|
rlm@424
|
601 [0xF0
|
rlm@424
|
602 0x05])]))
|
rlm@426
|
603
|
rlm@426
|
604
|