Mercurial > vba-clojure
view clojure/com/aurellem/run/music.clj @ 436:3171cbe077f3
created basic frame system for multiple voices.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 25 Apr 2012 12:41:30 -0500 |
parents | 3939ad680681 |
children | 20a9d5faf47c |
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 (def storage-start 0xC999)231 (defn music-step [sound-base-address]232 ;; C == current-ticks233 ;; A == desired-ticks235 (flatten236 [;; restore variables from stack237 0xE1 ;; pop HL238 0xC1 ;; pop CB239 0xF1 ;; pop AF242 0xF5 ;; push A243 0xF0244 0x05 ;; load current ticks from 0xF005245 0xB8 ;;246 0x30 ;; increment C only if last result caused carry247 0x01248 0x0C250 0x47 ;; update sub-ticks (A->B)252 0xF1 ;; pop AF, now A contains desired-ticks254 0xB9 ;; compare with current ticks256 ;; if desired-ticks = current ticks257 ;; go to next note ; set current set ticks to 0.259 0x20260 (+ (count (do-message 0)) 2)262 (do-message sound-base-address)264 0x0E265 0x00 ;; 0->C (current-ticks)267 ;; save variables to stack268 0xF5 ;; push AF269 0xC5 ;; push CB270 0xE5 ;; push HL273 ]))275 (def music-1 0x11)276 (def music-2 0x16)278 (defn music-kernel []279 (flatten280 [;; global initilization section281 (clear-music-registers)283 0x3E284 0x01285 0xE0286 0x06 ;; set TMA to 0288 0x3E289 (Integer/parseInt "00000110" 2)290 0xE0291 0x07 ;; set TAC to 65536 Hz and activate timer293 ;; initialize frame 1294 0x21295 0x00296 0xD0 ;; set HL to 0xD000 == music-start 1297 0x0E298 0x00 ;; 0->C299 0x06300 0x00 ;; 0->B302 0xAF ;; 0->A304 0xF5 ;; push AF305 0xC5 ;; push CB306 0xE5 ;; push HL308 ;; initialize frame 2309 0x21310 0x00311 0xC0 ;; set HL to 0xC000 == music-start 2313 0xF5 ;; push AF314 0xC5 ;; push CB315 0xE5 ;; push HL319 ;; init-2 (0->A,B,C), 0xC000 -> HL321 ;; push to stack323 0xE8 ;; SP + 8324 6325 ;; pop from stack326 (music-step music-1)327 ;; save to stack330 ;; SP + 5331 ;; pop from stack332 ;;(music-step music-2)333 ;; save to stack335 0x18336 (->signed-8-bit (+ (- (count (music-step 0)))337 -2))]))339 (defn frequency-code->frequency340 [code]341 (assert (<= 0 code 2047))342 (/ 131072 (- 2048 code)))344 (defn clamp [x low high]345 (cond (> x high) high346 (< x low) low347 true x))349 (defn frequency->frequency-code350 [frequency]351 (clamp352 (Math/round353 (float354 (/ (- (* 2048 frequency) 131072) frequency)))355 0x00 2048))357 (defn note-codes [frequency volume duration]358 (assert (<= 0 volume 0xF))359 (if (<= duration 0xFF)360 (let [frequency-code361 (frequency->frequency-code frequency)362 volume&high-frequency363 (+ (bit-shift-left volume 4)364 (bit-shift-right frequency-code 8))365 low-frequency366 (bit-and 0xFF frequency-code)]367 [note-code368 volume&high-frequency369 low-frequency370 duration])371 (vec372 (flatten373 [(note-codes frequency volume 0xFF)374 (note-codes frequency volume (- duration 0xFF))]))))377 (defn midi-code->frequency378 [midi-code]379 (* 8.1757989156380 (Math/pow 2 (* (float (/ 12)) midi-code))))382 ;; division == clock-pulses / quarter-note383 ;; tempo == microseconds / quarter-note385 ;; have: clock-pulses386 ;; want: seconds389 (defn silence [length]390 {:frequency 1391 :duration length392 :volume 0})394 (defn midi->mini-midi [#^File midi-file]395 (let [midi-events (parse-midi midi-file)397 note-on-events398 (filter #(= :Note_on_c (:command %)) midi-events)399 note-off-events400 (filter #(= :Note_off_c (:command %)) midi-events)402 channel-1-on403 (sort-by :time404 (filter #(= 1 (:channel (:args %)))405 note-on-events))406 channel-1-off407 (sort-by :time408 (filter #(= 1 (:channel (:args %)))409 note-off-events))412 tempo (:args (first (filter #(= :Tempo (:command %)) midi-events)))413 division (:division414 (:args (first (filter #(= :Header (:command %)) midi-events))))416 notes417 (map418 (fn [note-on note-off]419 {:frequency (midi-code->frequency (:note (:args note-on)))420 :duration421 (/ (* (/ tempo division)422 (- (:time note-off) (:time note-on)))423 1e6) ;; convert clock-pulses into seconds424 :volume (int (/ (:velocity (:args note-on)) 10))425 :time-stamp (/ (* (/ tempo division)426 (:time note-on)) 1e6)})427 channel-1-on channel-1-off)429 silences430 (map (fn [note-1 note-2]431 (let [note-1-space (- (:time-stamp note-2)432 (:time-stamp note-1))433 note-1-length (:duration note-1)]434 (silence (- note-1-space note-1-length))))435 ;; to handle silence at the beginning.436 (concat [(assoc (silence 0)437 :time-stamp 0)] notes)438 notes)440 notes-with-silence441 (filter (comp not zero? :duration) (interleave silences notes))442 ]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)))452 (def C4 (partial note-codes 261.63))453 (def D4 (partial note-codes 293.66))454 (def E4 (partial note-codes 329.63))455 (def F4 (partial note-codes 349.23))456 (def G4 (partial note-codes 392))457 (def A4 (partial note-codes 440))458 (def B4 (partial note-codes 493.88))459 (def C5 (partial note-codes 523.3))461 (def scale462 (flatten463 [(C4 0xF 0x40)464 (D4 0xF 0x40)465 (E4 0xF 0x40)466 (F4 0xF 0x40)467 (G4 0xF 0x40)468 (A4 0xF 0x40)469 (B4 0xF 0x40)470 (C5 0xF 0x40)]))472 (defn play-music [music-bytes]473 (let [program-target 0xC000474 music-target 0xD000]475 (-> (set-memory-range (second (music-base))476 program-target (music-kernel))477 (set-memory-range music-target music-bytes)478 (PC! program-target))))481 (defn test-note [music-bytes]482 (-> (set-memory-range (second (music-base))483 0xC000 (concat (clear-music-registers)484 (play-note)485 (infinite-loop)))486 (set-memory-range 0xD000 music-bytes)487 (PC! 0xC000)488 (HL! 0xD000)489 ))492 (defn run-program493 ([program]494 (let [target 0xC000]495 (-> (set-memory-range (second (music-base))496 target program)497 (PC! target)))))499 (defn test-timer []500 (flatten501 [0x3E502 0x01503 0xE0504 0x06 ;; set TMA to 0506 0x3E507 (Integer/parseInt "00000100" 2)508 0xE0509 0x07 ;; set TAC to 16384 Hz and activate timer511 (repeat512 500513 [0xF0514 0x05])]))