Mercurial > vba-clojure
view clojure/com/aurellem/run/music.clj @ 460:497b47dd180e
added trill and extended last note.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 04 May 2012 02:41:45 -0500 |
parents | 9c192737034d |
children | a2ae4213deb7 |
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 (def pony15 (File. "/home/r/proj/vba-clojure/music/pony-title.mid"))17 (def sync-test18 (File. "/home/r/proj/vba-clojure/music/sync-test.mid"))21 (defn raw-midi-text [#^File midi-file]22 (:out23 (clojure.java.shell/sh24 "midicsv"25 (.getCanonicalPath midi-file)26 "-")))28 (def command-line #"^(\d+), (\d+), ([^,]+)(.*)$")30 (defmulti parse-command :command)32 (defn discard-args [command] (dissoc command :args))34 (defmethod parse-command :Start_track35 [command] (discard-args command))37 (defmethod parse-command :End_track38 [command] (discard-args command))40 (defmethod parse-command :default41 [command] command)43 (defn parse-number-list44 [number-list-str]45 (map #(Integer/parseInt %)46 (clojure.string/split number-list-str #", ")))48 (defmethod parse-command :Tempo49 [command]50 (update-in command [:args] #(Integer/parseInt %)))52 (defn parse-midi-note-list53 [midi-note-list-str]54 (let [[channel note velocity]55 (parse-number-list midi-note-list-str)]56 {:channel channel :note note :velocity velocity}))58 (defmethod parse-command :Note_on_c59 [command]60 (update-in command [:args] parse-midi-note-list))62 (defmethod parse-command :Note_off_c63 [command]64 (update-in command [:args] parse-midi-note-list))66 (defmethod parse-command :Header67 [command]68 (let [args (:args command)69 [format num-tracks division] (parse-number-list args)]70 (assoc command :args71 {:format format72 :num-tracks num-tracks73 :division division})))75 (defmethod parse-command :Program_c76 [command]77 (let [args (:args command)78 [channel program-num] (parse-number-list args)]79 (assoc command :args80 {:channel channel81 :program-num program-num})))83 (defn parse-midi [#^File midi-file]84 (map85 (comp parse-command86 (fn [line]87 (let [[[_ channel time command args]]88 (re-seq command-line line)]89 {:channel (Integer/parseInt channel)90 :time (Integer/parseInt time)91 :command (keyword command)92 :args (apply str (drop 2 args))})))93 (drop-last94 (clojure.string/split-lines95 (raw-midi-text midi-file)))))97 (def music-base new-kernel)99 (defn store [n address]100 (flatten101 [0xF5102 0xE5104 0x3E105 n107 0x21108 (reverse (disect-bytes-2 address))110 0x77112 0xE1113 0xF1]))115 (defn infinite-loop []116 [0x18 0xFE])118 (def divider-register 0xFF04)120 (defrecord Bit-Note [frequency volume duration duty])122 (defn clear-music-registers []123 (flatten124 [(store (Integer/parseInt "00000000" 2) 0xFF10) ;; sweep125 (store (Integer/parseInt "00000000" 2) 0xFF11) ;; pattern duty126 (store (Integer/parseInt "00000000" 2) 0xFF12) ;; volume127 (store (Integer/parseInt "00000000" 2) 0xFF13) ;; frequency-low128 (store (Integer/parseInt "00000000" 2) 0xFF14) ;; frequency-high130 (store (Integer/parseInt "00000000" 2) 0xFF16) ;; pattern duty 000000131 (store (Integer/parseInt "00000000" 2) 0xFF17) ;; volume 0000132 (store (Integer/parseInt "00000000" 2) 0xFF18) ;; frequency-low133 (store (Integer/parseInt "00000000" 2) 0xFF19) ;; 00000 frequency-high135 (store (Integer/parseInt "00000000" 2) 0xFF1A)136 (store (Integer/parseInt "00000000" 2) 0xFF1B)137 (store (Integer/parseInt "00000000" 2) 0xFF1C)138 (store (Integer/parseInt "00000000" 2) 0xFF1D)139 (store (Integer/parseInt "00000000" 2) 0xFF1E)141 (store (Integer/parseInt "00000000" 2) 0xFF20) ;; length142 (store (Integer/parseInt "00000000" 2) 0xFF21) ;; volume143 (store (Integer/parseInt "00000000" 2) 0xFF22) ;; noise-frequency144 (store (Integer/parseInt "00000000" 2) 0xFF23) ;; control145 ]))148 ;; mini-midi syntax150 ;; codes151 ;; note-code == 0x00152 ;; change-duty-code = 0x01153 ;; silence-code = 0x02155 ;; silence format156 ;; 2 bytes157 ;; [silence-code (0x02)]158 ;; [duration-8-bits]160 ;; note data format161 ;; 4 bytes162 ;; [note-code (0x00)]163 ;; [volume-4-bits 0 frequency-high-3-bits]164 ;; [frequengy-low-8-bits]165 ;; [duration-8-bits]167 ;; change-duty-format168 ;; 2 bytes169 ;; [change-duty-code (0x01)]170 ;; [new-duty]172 (def note-code 0x00)173 (def change-duty-code 0x01)174 (def silence-code 0x02)176 (defn do-message177 "Read the message which starts at the current value of HL and do178 what it says. Duration is left in A, and HL is advanced179 appropraitely."180 ([] (do-message 0x16))181 ([sound-base-address]182 (let [switch183 [0x2A ;; load message code into A, increment HL185 ;; switch on message186 0xFE187 note-code189 0x20190 :note-length]192 play-note193 [0x2A ;; load volume/frequency-high info194 0xF5 ;; push A195 0xE6196 (Integer/parseInt "11110000" 2) ;; volume mask197 0xE0198 (inc sound-base-address) ;;0x17 ;; set volume199 0xF1 ;; pop A200 0xE6201 (Integer/parseInt "00000111" 2) ;; frequency-high mask202 0xE0203 (+ 3 sound-base-address) ;;0x19 ;; set frequency-high205 0x2A ;; load frequency low-bits206 0xE0207 (+ 2 sound-base-address) ;;0x18 ;; set frequency-low-bits208 0x2A]] ;; load duration209 (replace210 {:note-length (count play-note)}211 (concat switch play-note)))))213 ;; (defn play-note214 ;; "Play the note referenced by HL in the appropiate channel.215 ;; Leaves desired-duration in A."217 ;; [0x2A ;; load volume/frequency-high info218 ;; 0xF5 ;; push A219 ;; 0xE6220 ;; (Integer/parseInt "11110000" 2) ;; volume mask221 ;; 0xE0222 ;; 0x17 ;; set volume223 ;; 0xF1 ;; pop A224 ;; 0xE6225 ;; (Integer/parseInt "00000111" 2) ;; frequency-high mask226 ;; 0xE0227 ;; 0x19 ;; set frequency-high229 ;; 0x2A ;; load frequency low-bits230 ;; 0xE0231 ;; 0x18 ;; set frequency-low-bits233 ;; 0x2A ;; load duration234 ;; ])236 (defn music-step [sound-base-address]237 ;; C == current-ticks238 ;; A == desired-ticks240 (flatten241 [;; restore variables from stack242 0xE1 ;; pop HL243 0xC1 ;; pop CB244 0xF1 ;; pop AF247 0xF5 ;; push A248 0xF0249 0x05 ;; load current ticks from 0xF005250 0xB8 ;;251 0x30 ;; increment C only if last result caused carry252 0x01253 0x0C255 0x47 ;; update sub-ticks (A->B)257 0xF1 ;; pop AF, now A contains desired-ticks259 0xB9 ;; compare with current ticks261 ;; if desired-ticks = current ticks262 ;; go to next note ; set current set ticks to 0.264 0x20265 (+ (count (do-message 0)) 2)267 (do-message sound-base-address)269 0x0E270 0x00 ;; 0->C (current-ticks)272 ;; save variables to stack273 0xF5 ;; push AF274 0xC5 ;; push CB275 0xE5 ;; push HL278 ]))280 (def music-1 0x11)281 (def music-2 0x16)283 (defn music-kernel []284 (flatten285 [;; global initilization section286 (clear-music-registers)288 0x3E289 0x01290 0xE0291 0x06 ;; set TMA to 0293 0x3E294 (Integer/parseInt "00000110" 2)295 0xE0296 0x07 ;; set TAC to 65536 Hz and activate timer298 ;; initialize frame 1299 0x21300 0x00301 0xA0 ;; set HL to 0xA000 == music-start 1302 0x0E303 0x00 ;; 0->C304 0x06305 0x00 ;; 0->B307 0xAF ;; 0->A309 0xF5 ;; push AF310 0xC5 ;; push CB311 0xE5 ;; push HL313 ;; initialize frame 2314 0x21315 0x00316 0xB0 ;; set HL to 0xB000 == music-start 2318 0xF5 ;; push AF319 0xC5 ;; push CB320 0xE5 ;; push HL323 ;; main music loop325 0xE8 ;; SP + 6; activate frame 1326 6327 (music-step music-1)328 ;;(repeat (count (music-step music-1)) 0x00)330 0xE8 ;; SP - 6; activate frame 2331 (->signed-8-bit -6)332 ;;(repeat (count (music-step music-2)) 0x00)333 (music-step music-2)336 0x18337 (->signed-8-bit (+338 ;; two music-steps339 (- (* 2 (count (music-step 0))))340 -2 ;; this jump instruction341 -2 ;; activate frame 1342 -2 ;; activate frame 2343 ))]))345 (defn frequency-code->frequency346 [code]347 (assert (<= 0 code 2047))348 (/ 131072 (- 2048 code)))350 (defn clamp [x low high]351 (cond (> x high) high352 (< x low) low353 true x))355 (defn frequency->frequency-code356 [frequency]357 (clamp358 (Math/round359 (float360 (/ (- (* 2048 frequency) 131072) frequency)))361 0x00 2048))363 (defn note-codes [frequency volume duration]364 (assert (<= 0 volume 0xF))365 (if (<= duration 0xFF)366 (let [frequency-code367 (frequency->frequency-code frequency)368 volume&high-frequency369 (+ (bit-shift-left volume 4)370 (bit-shift-right frequency-code 8))371 low-frequency372 (bit-and 0xFF frequency-code)]373 [note-code374 volume&high-frequency375 low-frequency376 duration])377 (vec378 (flatten379 [(note-codes frequency volume 0xFF)380 (note-codes frequency volume (- duration 0xFF))]))))383 (defn midi-code->frequency384 [midi-code]385 (* 8.1757989156386 (Math/pow 2 (* (float (/ 12)) midi-code))))388 ;; division == clock-pulses / quarter-note389 ;; tempo == microseconds / quarter-note391 ;; have: clock-pulses392 ;; want: seconds395 (defn silence [length]396 {:frequency 1397 :duration length398 :volume 0})400 (defn commands401 "return all events where #(= (:command %) command)"402 [command s]403 (filter #(= command (:command %)) s))405 (defn midi-track->mini-midi [#^File midi-file track-num]406 (let [midi-events (parse-midi midi-file)408 note-on-events (commands :Note_on_c midi-events)409 note-off-events (commands :Note_off_c midi-events)411 select-channel412 (fn [n s]413 (sort-by :time (filter #(= n (:channel (:args %))) s)))415 channel-on (select-channel track-num note-on-events)417 channel-off (select-channel track-num note-off-events)420 tempo (:args (first (commands :Tempo midi-events)))421 division422 (:division (:args (first (commands :Header midi-events))))424 notes425 (map426 (fn [note-on note-off]427 {:frequency (midi-code->frequency (:note (:args note-on)))428 :duration429 (/ (* (/ tempo division)430 (- (:time note-off) (:time note-on)))431 1e6) ;; convert clock-pulses into seconds432 :volume (int (/ (:velocity (:args note-on)) 10))433 :time-stamp (/ (* (/ tempo division)434 (:time note-on)) 1e6)})435 channel-on channel-off)437 silences438 (map (fn [note-1 note-2]439 (let [note-1-space (- (:time-stamp note-2)440 (:time-stamp note-1))441 note-1-length (:duration note-1)]442 (silence (- note-1-space note-1-length))))443 ;; to handle silence at the beginning.444 (concat [(assoc (silence 0)445 :time-stamp 0)] notes)446 notes)448 notes-with-silence449 (concat450 (filter (comp not zero? :duration)451 (interleave silences notes))452 [(silence 3)])]454 (map455 (fn [note-event]456 (note-codes (:frequency note-event)457 (:volume note-event)458 (int (* (:duration note-event) 0x100))))459 notes-with-silence)))462 (defn midi->mini-midi [#^File midi-file]463 {:track-1 (flatten (midi-track->mini-midi midi-file 0))464 :track-2 (flatten (midi-track->mini-midi midi-file 1))})466 (defn play-midi [#^File midi-file]467 (let [track-1-target 0xA000468 track-2-target 0xB000469 program-target 0xC000470 mini-midi (midi->mini-midi midi-file)471 long-silence (flatten (note-codes 20 0 9001))]473 (-> (second (music-base))474 (set-memory-range track-1-target long-silence)475 (set-memory-range track-2-target long-silence)476 (set-memory-range track-1-target (:track-1 mini-midi))477 (set-memory-range track-2-target (:track-2 mini-midi))478 (set-memory-range program-target (music-kernel))479 (PC! program-target))))481 (def C4 (partial note-codes 261.63))482 (def D4 (partial note-codes 293.66))483 (def E4 (partial note-codes 329.63))484 (def F4 (partial note-codes 349.23))485 (def G4 (partial note-codes 392))486 (def A4 (partial note-codes 440))487 (def B4 (partial note-codes 493.88))488 (def C5 (partial note-codes 523.3))490 (def scale491 (flatten492 [(C4 0xF 0x40)493 (D4 0xF 0x40)494 (E4 0xF 0x40)495 (F4 0xF 0x40)496 (G4 0xF 0x40)497 (A4 0xF 0x40)498 (B4 0xF 0x40)499 (C5 0xF 0x40)]))501 (defn play-music [music-bytes]502 (let [program-target 0xC000503 music-target 0xA000]504 (-> (set-memory-range (second (music-base))505 program-target (music-kernel))506 (set-memory-range music-target music-bytes)507 (PC! program-target))))511 ;; (defn test-note [music-bytes]512 ;; (-> (set-memory-range (second (music-base))513 ;; 0xC000 (concat (clear-music-registers)514 ;; (play-note)515 ;; (infinite-loop)))516 ;; (set-memory-range 0xD000 music-bytes)517 ;; (PC! 0xC000)518 ;; (HL! 0xD000)519 ;; ))522 (defn run-program523 ([program]524 (let [target 0xC000]525 (-> (set-memory-range (second (music-base))526 target program)527 (PC! target)))))529 (defn test-timer []530 (flatten531 [0x3E532 0x01533 0xE0534 0x06 ;; set TMA to 0536 0x3E537 (Integer/parseInt "00000100" 2)538 0xE0539 0x07 ;; set TAC to 16384 Hz and activate timer541 (repeat542 500543 [0xF0544 0x05])]))