view clojure/com/aurellem/run/music.clj @ 437:20a9d5faf47c

now I can play two voices at once!
author Robert McIntyre <rlm@mit.edu>
date Wed, 25 Apr 2012 13:09:06 -0500
parents 3171cbe077f3
children 067ea3f0d951
line wrap: on
line source
1 (ns com.aurellem.run.music
2 (:use (com.aurellem.gb saves gb-driver util constants
3 items vbm characters money
4 rlm-assembly))
5 (:use (com.aurellem.run util title save-corruption
6 bootstrap-0 bootstrap-1))
7 (:require clojure.string)
8 (:import [com.aurellem.gb.gb_driver SaveState])
9 (:import java.io.File))
11 (def third-kind
12 (File. "/home/r/proj/midi/third-kind.mid"))
14 (defn raw-midi-text [#^File midi-file]
15 (:out
16 (clojure.java.shell/sh
17 "midicsv"
18 (.getCanonicalPath midi-file)
19 "-")))
21 (def command-line #"^(\d+), (\d+), ([^,]+)(.*)$")
23 (defmulti parse-command :command)
25 (defn discard-args [command] (dissoc command :args))
27 (defmethod parse-command :Start_track
28 [command] (discard-args command))
30 (defmethod parse-command :End_track
31 [command] (discard-args command))
33 (defmethod parse-command :default
34 [command] command)
36 (defn parse-number-list
37 [number-list-str]
38 (map #(Integer/parseInt %)
39 (clojure.string/split number-list-str #", ")))
41 (defmethod parse-command :Tempo
42 [command]
43 (update-in command [:args] #(Integer/parseInt %)))
45 (defn parse-midi-note-list
46 [midi-note-list-str]
47 (let [[channel note velocity]
48 (parse-number-list midi-note-list-str)]
49 {:channel channel :note note :velocity velocity}))
51 (defmethod parse-command :Note_on_c
52 [command]
53 (update-in command [:args] parse-midi-note-list))
55 (defmethod parse-command :Note_off_c
56 [command]
57 (update-in command [:args] parse-midi-note-list))
59 (defmethod parse-command :Header
60 [command]
61 (let [args (:args command)
62 [format num-tracks division] (parse-number-list args)]
63 (assoc command :args
64 {:format format
65 :num-tracks num-tracks
66 :division division})))
68 (defmethod parse-command :Program_c
69 [command]
70 (let [args (:args command)
71 [channel program-num] (parse-number-list args)]
72 (assoc command :args
73 {:channel channel
74 :program-num program-num})))
76 (defn parse-midi [#^File midi-file]
77 (map
78 (comp parse-command
79 (fn [line]
80 (let [[[_ channel time command args]]
81 (re-seq command-line line)]
82 {:channel (Integer/parseInt channel)
83 :time (Integer/parseInt time)
84 :command (keyword command)
85 :args (apply str (drop 2 args))})))
86 (drop-last
87 (clojure.string/split-lines
88 (raw-midi-text midi-file)))))
90 (def music-base new-kernel)
92 (defn store [n address]
93 (flatten
94 [0xF5
95 0xE5
97 0x3E
98 n
100 0x21
101 (reverse (disect-bytes-2 address))
103 0x77
105 0xE1
106 0xF1]))
108 (defn infinite-loop []
109 [0x18 0xFE])
111 (def divider-register 0xFF04)
113 (defrecord Bit-Note [frequency volume duration duty])
115 (defn clear-music-registers []
116 (flatten
117 [(store (Integer/parseInt "00000000" 2) 0xFF10) ;; sweep
118 (store (Integer/parseInt "00000000" 2) 0xFF11) ;; pattern duty
119 (store (Integer/parseInt "00000000" 2) 0xFF12) ;; volume
120 (store (Integer/parseInt "00000000" 2) 0xFF13) ;; frequency-low
121 (store (Integer/parseInt "00000000" 2) 0xFF14) ;; frequency-high
123 (store (Integer/parseInt "00000000" 2) 0xFF16) ;; pattern duty 000000
124 (store (Integer/parseInt "00000000" 2) 0xFF17) ;; volume 0000
125 (store (Integer/parseInt "00000000" 2) 0xFF18) ;; frequency-low
126 (store (Integer/parseInt "00000000" 2) 0xFF19) ;; 00000 frequency-high
128 (store (Integer/parseInt "00000000" 2) 0xFF1A)
129 (store (Integer/parseInt "00000000" 2) 0xFF1B)
130 (store (Integer/parseInt "00000000" 2) 0xFF1C)
131 (store (Integer/parseInt "00000000" 2) 0xFF1D)
132 (store (Integer/parseInt "00000000" 2) 0xFF1E)
134 (store (Integer/parseInt "00000000" 2) 0xFF20) ;; length
135 (store (Integer/parseInt "00000000" 2) 0xFF21) ;; volume
136 (store (Integer/parseInt "00000000" 2) 0xFF22) ;; noise-frequency
137 (store (Integer/parseInt "00000000" 2) 0xFF23) ;; control
138 ]))
141 ;; mini-midi syntax
143 ;; codes
144 ;; note-code == 0x00
145 ;; change-duty-code = 0x01
146 ;; silence-code = 0x02
148 ;; silence format
149 ;; 2 bytes
150 ;; [silence-code (0x02)]
151 ;; [duration-8-bits]
153 ;; note data format
154 ;; 4 bytes
155 ;; [note-code (0x00)]
156 ;; [volume-4-bits 0 frequency-high-3-bits]
157 ;; [frequengy-low-8-bits]
158 ;; [duration-8-bits]
160 ;; change-duty-format
161 ;; 2 bytes
162 ;; [change-duty-code (0x01)]
163 ;; [new-duty]
165 (def note-code 0x00)
166 (def change-duty-code 0x01)
167 (def silence-code 0x02)
169 (defn do-message
170 "Read the message which starts at the current value of HL and do
171 what it says. Duration is left in A, and HL is advanced
172 appropraitely."
173 ([] (do-message 0x16))
174 ([sound-base-address]
175 (let [switch
176 [0x2A ;; load message code into A, increment HL
178 ;; switch on message
179 0xFE
180 note-code
182 0x20
183 :note-length]
185 play-note
186 [0x2A ;; load volume/frequency-high info
187 0xF5 ;; push A
188 0xE6
189 (Integer/parseInt "11110000" 2) ;; volume mask
190 0xE0
191 (inc sound-base-address) ;;0x17 ;; set volume
192 0xF1 ;; pop A
193 0xE6
194 (Integer/parseInt "00000111" 2) ;; frequency-high mask
195 0xE0
196 (+ 3 sound-base-address) ;;0x19 ;; set frequency-high
198 0x2A ;; load frequency low-bits
199 0xE0
200 (+ 2 sound-base-address) ;;0x18 ;; set frequency-low-bits
201 0x2A]] ;; load duration
202 (replace
203 {:note-length (count play-note)}
204 (concat switch play-note)))))
206 ;; (defn play-note
207 ;; "Play the note referenced by HL in the appropiate channel.
208 ;; Leaves desired-duration in A."
210 ;; [0x2A ;; load volume/frequency-high info
211 ;; 0xF5 ;; push A
212 ;; 0xE6
213 ;; (Integer/parseInt "11110000" 2) ;; volume mask
214 ;; 0xE0
215 ;; 0x17 ;; set volume
216 ;; 0xF1 ;; pop A
217 ;; 0xE6
218 ;; (Integer/parseInt "00000111" 2) ;; frequency-high mask
219 ;; 0xE0
220 ;; 0x19 ;; set frequency-high
222 ;; 0x2A ;; load frequency low-bits
223 ;; 0xE0
224 ;; 0x18 ;; set frequency-low-bits
226 ;; 0x2A ;; load duration
227 ;; ])
229 (defn music-step [sound-base-address]
230 ;; C == current-ticks
231 ;; A == desired-ticks
233 (flatten
234 [;; restore variables from stack
235 0xE1 ;; pop HL
236 0xC1 ;; pop CB
237 0xF1 ;; pop AF
240 0xF5 ;; push A
241 0xF0
242 0x05 ;; load current ticks from 0xF005
243 0xB8 ;;
244 0x30 ;; increment C only if last result caused carry
245 0x01
246 0x0C
248 0x47 ;; update sub-ticks (A->B)
250 0xF1 ;; pop AF, now A contains desired-ticks
252 0xB9 ;; compare with current ticks
254 ;; if desired-ticks = current ticks
255 ;; go to next note ; set current set ticks to 0.
257 0x20
258 (+ (count (do-message 0)) 2)
260 (do-message sound-base-address)
262 0x0E
263 0x00 ;; 0->C (current-ticks)
265 ;; save variables to stack
266 0xF5 ;; push AF
267 0xC5 ;; push CB
268 0xE5 ;; push HL
271 ]))
273 (def music-1 0x11)
274 (def music-2 0x16)
276 (defn music-kernel []
277 (flatten
278 [;; global initilization section
279 (clear-music-registers)
281 0x3E
282 0x01
283 0xE0
284 0x06 ;; set TMA to 0
286 0x3E
287 (Integer/parseInt "00000110" 2)
288 0xE0
289 0x07 ;; set TAC to 65536 Hz and activate timer
291 ;; initialize frame 1
292 0x21
293 0x00
294 0xA0 ;; set HL to 0xA000 == music-start 1
295 0x0E
296 0x00 ;; 0->C
297 0x06
298 0x00 ;; 0->B
300 0xAF ;; 0->A
302 0xF5 ;; push AF
303 0xC5 ;; push CB
304 0xE5 ;; push HL
306 ;; initialize frame 2
307 0x21
308 0x00
309 0xB0 ;; set HL to 0xB000 == music-start 2
311 0xF5 ;; push AF
312 0xC5 ;; push CB
313 0xE5 ;; push HL
316 ;; main music loop
318 0xE8 ;; SP + 6; activate frame 1
319 6
320 (music-step music-1)
321 ;;(repeat (count (music-step music-1)) 0x00)
323 0xE8 ;; SP - 6; activate frame 2
324 (->signed-8-bit -6)
325 ;;(repeat (count (music-step music-2)) 0x00)
326 (music-step music-2)
329 0x18
330 (->signed-8-bit (+
331 ;; two music-steps
332 (- (* 2 (count (music-step 0))))
333 -2 ;; this jump instruction
334 -2 ;; activate frame 1
335 -2 ;; activate frame 2
336 ))]))
338 (defn frequency-code->frequency
339 [code]
340 (assert (<= 0 code 2047))
341 (/ 131072 (- 2048 code)))
343 (defn clamp [x low high]
344 (cond (> x high) high
345 (< x low) low
346 true x))
348 (defn frequency->frequency-code
349 [frequency]
350 (clamp
351 (Math/round
352 (float
353 (/ (- (* 2048 frequency) 131072) frequency)))
354 0x00 2048))
356 (defn note-codes [frequency volume duration]
357 (assert (<= 0 volume 0xF))
358 (if (<= duration 0xFF)
359 (let [frequency-code
360 (frequency->frequency-code frequency)
361 volume&high-frequency
362 (+ (bit-shift-left volume 4)
363 (bit-shift-right frequency-code 8))
364 low-frequency
365 (bit-and 0xFF frequency-code)]
366 [note-code
367 volume&high-frequency
368 low-frequency
369 duration])
370 (vec
371 (flatten
372 [(note-codes frequency volume 0xFF)
373 (note-codes frequency volume (- duration 0xFF))]))))
376 (defn midi-code->frequency
377 [midi-code]
378 (* 8.1757989156
379 (Math/pow 2 (* (float (/ 12)) midi-code))))
381 ;; division == clock-pulses / quarter-note
382 ;; tempo == microseconds / quarter-note
384 ;; have: clock-pulses
385 ;; want: seconds
388 (defn silence [length]
389 {:frequency 1
390 :duration length
391 :volume 0})
393 (defn midi->mini-midi [#^File midi-file]
394 (let [midi-events (parse-midi midi-file)
396 note-on-events
397 (filter #(= :Note_on_c (:command %)) midi-events)
398 note-off-events
399 (filter #(= :Note_off_c (:command %)) midi-events)
401 channel-1-on
402 (sort-by :time
403 (filter #(= 1 (:channel (:args %)))
404 note-on-events))
405 channel-1-off
406 (sort-by :time
407 (filter #(= 1 (:channel (:args %)))
408 note-off-events))
411 tempo (:args (first (filter #(= :Tempo (:command %)) midi-events)))
412 division (:division
413 (:args (first (filter #(= :Header (:command %)) midi-events))))
415 notes
416 (map
417 (fn [note-on note-off]
418 {:frequency (midi-code->frequency (:note (:args note-on)))
419 :duration
420 (/ (* (/ tempo division)
421 (- (:time note-off) (:time note-on)))
422 1e6) ;; convert clock-pulses into seconds
423 :volume (int (/ (:velocity (:args note-on)) 10))
424 :time-stamp (/ (* (/ tempo division)
425 (:time note-on)) 1e6)})
426 channel-1-on channel-1-off)
428 silences
429 (map (fn [note-1 note-2]
430 (let [note-1-space (- (:time-stamp note-2)
431 (:time-stamp note-1))
432 note-1-length (:duration note-1)]
433 (silence (- note-1-space note-1-length))))
434 ;; to handle silence at the beginning.
435 (concat [(assoc (silence 0)
436 :time-stamp 0)] notes)
437 notes)
439 notes-with-silence
440 (filter (comp not zero? :duration) (interleave silences notes))
441 ]
443 (map
444 (fn [note-event]
445 (note-codes (:frequency note-event)
446 (:volume note-event)
447 (int (* (:duration note-event) 0x100))))
448 notes-with-silence)))
451 (def C4 (partial note-codes 261.63))
452 (def D4 (partial note-codes 293.66))
453 (def E4 (partial note-codes 329.63))
454 (def F4 (partial note-codes 349.23))
455 (def G4 (partial note-codes 392))
456 (def A4 (partial note-codes 440))
457 (def B4 (partial note-codes 493.88))
458 (def C5 (partial note-codes 523.3))
460 (def scale
461 (flatten
462 [(C4 0xF 0x40)
463 (D4 0xF 0x40)
464 (E4 0xF 0x40)
465 (F4 0xF 0x40)
466 (G4 0xF 0x40)
467 (A4 0xF 0x40)
468 (B4 0xF 0x40)
469 (C5 0xF 0x40)]))
471 (defn play-music [music-bytes]
472 (let [program-target 0xC000
473 music-target 0xA000]
474 (-> (set-memory-range (second (music-base))
475 program-target (music-kernel))
476 (set-memory-range music-target music-bytes)
477 (PC! program-target))))
480 ;; (defn test-note [music-bytes]
481 ;; (-> (set-memory-range (second (music-base))
482 ;; 0xC000 (concat (clear-music-registers)
483 ;; (play-note)
484 ;; (infinite-loop)))
485 ;; (set-memory-range 0xD000 music-bytes)
486 ;; (PC! 0xC000)
487 ;; (HL! 0xD000)
488 ;; ))
491 (defn run-program
492 ([program]
493 (let [target 0xC000]
494 (-> (set-memory-range (second (music-base))
495 target program)
496 (PC! target)))))
498 (defn test-timer []
499 (flatten
500 [0x3E
501 0x01
502 0xE0
503 0x06 ;; set TMA to 0
505 0x3E
506 (Integer/parseInt "00000100" 2)
507 0xE0
508 0x07 ;; set TAC to 16384 Hz and activate timer
510 (repeat
511 500
512 [0xF0
513 0x05])]))