Mercurial > vba-clojure
view clojure/com/aurellem/run/music.clj @ 447:eea3b257ef59
removed some tracks.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 03 May 2012 06:22:34 -0500 |
parents | 067ea3f0d951 |
children | bf87b87a4ad7 |
line wrap: on
line source
1 (ns com.aurellem.run.music2 (:use (com.aurellem.gb saves gb-driver util constants3 items vbm characters money4 rlm-assembly))5 (:use (com.aurellem.run util title save-corruption6 bootstrap-0 bootstrap-1))7 (:require clojure.string)8 (:import [com.aurellem.gb.gb_driver SaveState])9 (:import java.io.File))11 (def third-kind12 (File. "/home/r/proj/midi/third-kind.mid"))14 (defn raw-midi-text [#^File midi-file]15 (:out16 (clojure.java.shell/sh17 "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_track28 [command] (discard-args command))30 (defmethod parse-command :End_track31 [command] (discard-args command))33 (defmethod parse-command :default34 [command] command)36 (defn parse-number-list37 [number-list-str]38 (map #(Integer/parseInt %)39 (clojure.string/split number-list-str #", ")))41 (defmethod parse-command :Tempo42 [command]43 (update-in command [:args] #(Integer/parseInt %)))45 (defn parse-midi-note-list46 [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_c52 [command]53 (update-in command [:args] parse-midi-note-list))55 (defmethod parse-command :Note_off_c56 [command]57 (update-in command [:args] parse-midi-note-list))59 (defmethod parse-command :Header60 [command]61 (let [args (:args command)62 [format num-tracks division] (parse-number-list args)]63 (assoc command :args64 {:format format65 :num-tracks num-tracks66 :division division})))68 (defmethod parse-command :Program_c69 [command]70 (let [args (:args command)71 [channel program-num] (parse-number-list args)]72 (assoc command :args73 {:channel channel74 :program-num program-num})))76 (defn parse-midi [#^File midi-file]77 (map78 (comp parse-command79 (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-last87 (clojure.string/split-lines88 (raw-midi-text midi-file)))))90 (def music-base new-kernel)92 (defn store [n address]93 (flatten94 [0xF595 0xE597 0x3E98 n100 0x21101 (reverse (disect-bytes-2 address))103 0x77105 0xE1106 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 (flatten117 [(store (Integer/parseInt "00000000" 2) 0xFF10) ;; sweep118 (store (Integer/parseInt "00000000" 2) 0xFF11) ;; pattern duty119 (store (Integer/parseInt "00000000" 2) 0xFF12) ;; volume120 (store (Integer/parseInt "00000000" 2) 0xFF13) ;; frequency-low121 (store (Integer/parseInt "00000000" 2) 0xFF14) ;; frequency-high123 (store (Integer/parseInt "00000000" 2) 0xFF16) ;; pattern duty 000000124 (store (Integer/parseInt "00000000" 2) 0xFF17) ;; volume 0000125 (store (Integer/parseInt "00000000" 2) 0xFF18) ;; frequency-low126 (store (Integer/parseInt "00000000" 2) 0xFF19) ;; 00000 frequency-high128 (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) ;; length135 (store (Integer/parseInt "00000000" 2) 0xFF21) ;; volume136 (store (Integer/parseInt "00000000" 2) 0xFF22) ;; noise-frequency137 (store (Integer/parseInt "00000000" 2) 0xFF23) ;; control138 ]))141 ;; mini-midi syntax143 ;; codes144 ;; note-code == 0x00145 ;; change-duty-code = 0x01146 ;; silence-code = 0x02148 ;; silence format149 ;; 2 bytes150 ;; [silence-code (0x02)]151 ;; [duration-8-bits]153 ;; note data format154 ;; 4 bytes155 ;; [note-code (0x00)]156 ;; [volume-4-bits 0 frequency-high-3-bits]157 ;; [frequengy-low-8-bits]158 ;; [duration-8-bits]160 ;; change-duty-format161 ;; 2 bytes162 ;; [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-message170 "Read the message which starts at the current value of HL and do171 what it says. Duration is left in A, and HL is advanced172 appropraitely."173 ([] (do-message 0x16))174 ([sound-base-address]175 (let [switch176 [0x2A ;; load message code into A, increment HL178 ;; switch on message179 0xFE180 note-code182 0x20183 :note-length]185 play-note186 [0x2A ;; load volume/frequency-high info187 0xF5 ;; push A188 0xE6189 (Integer/parseInt "11110000" 2) ;; volume mask190 0xE0191 (inc sound-base-address) ;;0x17 ;; set volume192 0xF1 ;; pop A193 0xE6194 (Integer/parseInt "00000111" 2) ;; frequency-high mask195 0xE0196 (+ 3 sound-base-address) ;;0x19 ;; set frequency-high198 0x2A ;; load frequency low-bits199 0xE0200 (+ 2 sound-base-address) ;;0x18 ;; set frequency-low-bits201 0x2A]] ;; load duration202 (replace203 {:note-length (count play-note)}204 (concat switch play-note)))))206 ;; (defn play-note207 ;; "Play the note referenced by HL in the appropiate channel.208 ;; Leaves desired-duration in A."210 ;; [0x2A ;; load volume/frequency-high info211 ;; 0xF5 ;; push A212 ;; 0xE6213 ;; (Integer/parseInt "11110000" 2) ;; volume mask214 ;; 0xE0215 ;; 0x17 ;; set volume216 ;; 0xF1 ;; pop A217 ;; 0xE6218 ;; (Integer/parseInt "00000111" 2) ;; frequency-high mask219 ;; 0xE0220 ;; 0x19 ;; set frequency-high222 ;; 0x2A ;; load frequency low-bits223 ;; 0xE0224 ;; 0x18 ;; set frequency-low-bits226 ;; 0x2A ;; load duration227 ;; ])229 (defn music-step [sound-base-address]230 ;; C == current-ticks231 ;; A == desired-ticks233 (flatten234 [;; restore variables from stack235 0xE1 ;; pop HL236 0xC1 ;; pop CB237 0xF1 ;; pop AF240 0xF5 ;; push A241 0xF0242 0x05 ;; load current ticks from 0xF005243 0xB8 ;;244 0x30 ;; increment C only if last result caused carry245 0x01246 0x0C248 0x47 ;; update sub-ticks (A->B)250 0xF1 ;; pop AF, now A contains desired-ticks252 0xB9 ;; compare with current ticks254 ;; if desired-ticks = current ticks255 ;; go to next note ; set current set ticks to 0.257 0x20258 (+ (count (do-message 0)) 2)260 (do-message sound-base-address)262 0x0E263 0x00 ;; 0->C (current-ticks)265 ;; save variables to stack266 0xF5 ;; push AF267 0xC5 ;; push CB268 0xE5 ;; push HL271 ]))273 (def music-1 0x11)274 (def music-2 0x16)276 (defn music-kernel []277 (flatten278 [;; global initilization section279 (clear-music-registers)281 0x3E282 0x01283 0xE0284 0x06 ;; set TMA to 0286 0x3E287 (Integer/parseInt "00000110" 2)288 0xE0289 0x07 ;; set TAC to 65536 Hz and activate timer291 ;; initialize frame 1292 0x21293 0x00294 0xA0 ;; set HL to 0xA000 == music-start 1295 0x0E296 0x00 ;; 0->C297 0x06298 0x00 ;; 0->B300 0xAF ;; 0->A302 0xF5 ;; push AF303 0xC5 ;; push CB304 0xE5 ;; push HL306 ;; initialize frame 2307 0x21308 0x00309 0xB0 ;; set HL to 0xB000 == music-start 2311 0xF5 ;; push AF312 0xC5 ;; push CB313 0xE5 ;; push HL316 ;; main music loop318 0xE8 ;; SP + 6; activate frame 1319 6320 (music-step music-1)321 ;;(repeat (count (music-step music-1)) 0x00)323 0xE8 ;; SP - 6; activate frame 2324 (->signed-8-bit -6)325 ;;(repeat (count (music-step music-2)) 0x00)326 (music-step music-2)329 0x18330 (->signed-8-bit (+331 ;; two music-steps332 (- (* 2 (count (music-step 0))))333 -2 ;; this jump instruction334 -2 ;; activate frame 1335 -2 ;; activate frame 2336 ))]))338 (defn frequency-code->frequency339 [code]340 (assert (<= 0 code 2047))341 (/ 131072 (- 2048 code)))343 (defn clamp [x low high]344 (cond (> x high) high345 (< x low) low346 true x))348 (defn frequency->frequency-code349 [frequency]350 (clamp351 (Math/round352 (float353 (/ (- (* 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-code360 (frequency->frequency-code frequency)361 volume&high-frequency362 (+ (bit-shift-left volume 4)363 (bit-shift-right frequency-code 8))364 low-frequency365 (bit-and 0xFF frequency-code)]366 [note-code367 volume&high-frequency368 low-frequency369 duration])370 (vec371 (flatten372 [(note-codes frequency volume 0xFF)373 (note-codes frequency volume (- duration 0xFF))]))))376 (defn midi-code->frequency377 [midi-code]378 (* 8.1757989156379 (Math/pow 2 (* (float (/ 12)) midi-code))))381 ;; division == clock-pulses / quarter-note382 ;; tempo == microseconds / quarter-note384 ;; have: clock-pulses385 ;; want: seconds388 (defn silence [length]389 {:frequency 1390 :duration length391 :volume 0})393 (defn commands394 "return all events where #(= (:command %) command)"395 [command s]396 (filter #(= command (:command %)) s))398 (defn midi-track->mini-midi [#^File midi-file track-num]399 (let [midi-events (parse-midi midi-file)401 note-on-events (commands :Note_on_c midi-events)402 note-off-events (commands :Note_off_c midi-events)404 select-channel405 (fn [n s]406 (sort-by :time (filter #(= n (:channel (:args %))) s)))408 channel-on (select-channel track-num note-on-events)410 channel-off (select-channel track-num note-off-events)413 tempo (:args (first (commands :Tempo midi-events)))414 division415 (:division (:args (first (commands :Header midi-events))))417 notes418 (map419 (fn [note-on note-off]420 {:frequency (midi-code->frequency (:note (:args note-on)))421 :duration422 (/ (* (/ tempo division)423 (- (:time note-off) (:time note-on)))424 1e6) ;; convert clock-pulses into seconds425 :volume (int (/ (:velocity (:args note-on)) 10))426 :time-stamp (/ (* (/ tempo division)427 (:time note-on)) 1e6)})428 channel-on channel-off)430 silences431 (map (fn [note-1 note-2]432 (let [note-1-space (- (:time-stamp note-2)433 (:time-stamp note-1))434 note-1-length (:duration note-1)]435 (silence (- note-1-space note-1-length))))436 ;; to handle silence at the beginning.437 (concat [(assoc (silence 0)438 :time-stamp 0)] notes)439 notes)441 notes-with-silence442 (filter (comp not zero? :duration)443 (interleave silences notes))]444 (map445 (fn [note-event]446 (note-codes (:frequency note-event)447 (:volume note-event)448 (int (* (:duration note-event) 0x100))))449 notes-with-silence)))451 (defn midi->mini-midi [#^File midi-file]452 {:track-1 (flatten (midi-track->mini-midi midi-file 1))453 :track-2 (flatten (midi-track->mini-midi midi-file 2))})455 (defn play-midi [#^File midi-file]456 (let [track-1-target 0xA000457 track-2-target 0xB000458 program-target 0xC000459 mini-midi (midi->mini-midi midi-file)460 long-silence (flatten (note-codes 20 0 9001))]462 (-> (second (music-base))463 (set-memory-range track-1-target long-silence)464 (set-memory-range track-2-target long-silence)465 (set-memory-range track-1-target (:track-1 mini-midi))466 (set-memory-range track-2-target (:track-2 mini-midi))467 (set-memory-range program-target (music-kernel))468 (PC! program-target))))473 (def C4 (partial note-codes 261.63))474 (def D4 (partial note-codes 293.66))475 (def E4 (partial note-codes 329.63))476 (def F4 (partial note-codes 349.23))477 (def G4 (partial note-codes 392))478 (def A4 (partial note-codes 440))479 (def B4 (partial note-codes 493.88))480 (def C5 (partial note-codes 523.3))482 (def scale483 (flatten484 [(C4 0xF 0x40)485 (D4 0xF 0x40)486 (E4 0xF 0x40)487 (F4 0xF 0x40)488 (G4 0xF 0x40)489 (A4 0xF 0x40)490 (B4 0xF 0x40)491 (C5 0xF 0x40)]))493 (defn play-music [music-bytes]494 (let [program-target 0xC000495 music-target 0xA000]496 (-> (set-memory-range (second (music-base))497 program-target (music-kernel))498 (set-memory-range music-target music-bytes)499 (PC! program-target))))503 ;; (defn test-note [music-bytes]504 ;; (-> (set-memory-range (second (music-base))505 ;; 0xC000 (concat (clear-music-registers)506 ;; (play-note)507 ;; (infinite-loop)))508 ;; (set-memory-range 0xD000 music-bytes)509 ;; (PC! 0xC000)510 ;; (HL! 0xD000)511 ;; ))514 (defn run-program515 ([program]516 (let [target 0xC000]517 (-> (set-memory-range (second (music-base))518 target program)519 (PC! target)))))521 (defn test-timer []522 (flatten523 [0x3E524 0x01525 0xE0526 0x06 ;; set TMA to 0528 0x3E529 (Integer/parseInt "00000100" 2)530 0xE0531 0x07 ;; set TAC to 16384 Hz and activate timer533 (repeat534 500535 [0xF0536 0x05])]))