Mercurial > vba-clojure
view clojure/com/aurellem/run/music.clj @ 427:fbccf46cf34d
sucessfully played third-kind.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 23 Apr 2012 08:26:23 -0500 |
parents | c03f28aa98d9 |
children | 476f7da175a4 |
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)118 (store (Integer/parseInt "00000000" 2) 0xFF11)119 (store (Integer/parseInt "00000000" 2) 0xFF12)120 (store (Integer/parseInt "00000000" 2) 0xFF13)121 (store (Integer/parseInt "00000000" 2) 0xFF14)123 (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)135 (store (Integer/parseInt "00000000" 2) 0xFF21)136 (store (Integer/parseInt "00000000" 2) 0xFF22)137 (store (Integer/parseInt "00000000" 2) 0xFF23)]))140 ;; mini-midi syntax142 ;; codes143 ;; note-code == 0x00144 ;; change-duty-code = 0x01145 ;; silence-code = 0x02147 ;; silence format148 ;; 2 bytes149 ;; [silence-code (0x02)]150 ;; [duration-8-bits]152 ;; note data format153 ;; 4 bytes154 ;; [note-code (0x00)]155 ;; [volume-4-bits 0 frequency-high-3-bits]156 ;; [frequengy-low-8-bits]157 ;; [duration-8-bits]159 ;; change-duty-format160 ;; 2 bytes161 ;; [change-duty-code (0x01)]162 ;; [new-duty]164 (def note-code 0x00)165 (def change-duty-code 0x01)166 (def silence-code 0x02)168 (defn do-message169 "Read the message which starts at the current value of HL and do170 what it says. Duration is left in A, and HL is advanced171 appropraitely."172 []173 (let [switch174 [0x2A ;; load message code into A, increment HL176 ;; switch on message177 0xFE178 note-code180 0x20181 :note-length]183 play-note184 [0x2A ;; load volume/frequency-high info185 0xF5 ;; push A186 0xE6187 (Integer/parseInt "11110000" 2) ;; volume mask188 0xE0189 0x17 ;; set volume190 0xF1 ;; pop A191 0xE6192 (Integer/parseInt "00000111" 2) ;; frequency-high mask193 0xE0194 0x19 ;; set frequency-high196 0x2A ;; load frequency low-bits197 0xE0198 0x18 ;; set frequency-low-bits200 0x2A]] ;; load duration201 (replace202 {:note-length (count play-note)}203 (concat switch play-note))))205 (defn play-note206 "Play the note referenced by HL in the appropiate channel.207 Leaves desired-duration in A."208 []209 [0x2A ;; load volume/frequency-high info210 0xF5 ;; push A211 0xE6212 (Integer/parseInt "11110000" 2) ;; volume mask213 0xE0214 0x17 ;; set volume215 0xF1 ;; pop A216 0xE6217 (Integer/parseInt "00000111" 2) ;; frequency-high mask218 0xE0219 0x19 ;; set frequency-high221 0x2A ;; load frequency low-bits222 0xE0223 0x18 ;; set frequency-low-bits225 0x2A ;; load duration226 ])228 (defn music-step []229 (flatten230 [231 0xF5 ;; push A232 0xF0233 0x05 ;; load current ticks234 0xB8 ;; B holds previous sub-ticks, subtract it from A235 ;; if A-B caused a carry, then (B > A) is true, and236 ;; A = current-sub-tics, B = previous-sub-ticks, so237 ;; current-sub-ticks < previous-sub-ticks, which means that the238 ;; timer counter HAS overflowed.239 0x30 ;; increment C only if last result caused carry240 0x01241 0x0C243 0x47 ;; update sub-ticks (A->B)245 0xF1 ;; pop AF, now A contains desired-ticks247 0xB9 ;; compare with current ticks249 ;; if desired-ticks = current ticks250 ;; go to next note ; set current set ticks to 0.252 0x20253 (+ (count (do-message)) 2)255 (do-message)257 0x0E258 0x00])) ;; 0->C (current-ticks)260 (defn music-kernel []261 (flatten262 [(clear-music-registers)264 0x21265 0x00266 0xD0 ;; set HL to 0xD000 == music-start267 0x0E268 0x00 ;; 0->C269 0x06270 0x00 ;; 0->B272 0x3E273 0x01274 0xE0275 0x06 ;; set TMA to 0277 0x3E278 (Integer/parseInt "00000110" 2)279 0xE0280 0x07 ;; set TAC to 65536 Hz and activate timer283 0xAF ;; initialiaze A to zero286 (music-step)287 0x18288 (->signed-8-bit (+ (- (count (music-step)))289 -2))]))291 (defn frequency-code->frequency292 [code]293 (assert (<= 0 code 2047))294 (/ 131072 (- 2048 code)))296 (defn clamp [x low high]297 (cond (> x high) high298 (< x low) low299 true x))301 (defn frequency->frequency-code302 [frequency]303 (clamp304 (Math/round305 (float306 (/ (- (* 2048 frequency) 131072) frequency)))307 0x00 2048))309 (defn note-codes [frequency volume duration]310 (assert (<= 0 volume 0xF))311 (assert (<= 0 duration 0xFF))312 (let [frequency-code313 (frequency->frequency-code frequency)314 volume&high-frequency315 (+ (bit-shift-left volume 4)316 (bit-shift-right frequency-code 8))317 low-frequency318 (bit-and 0xFF frequency-code)]319 [note-code320 volume&high-frequency321 low-frequency322 duration]))324 (defn midi-code->frequency325 [midi-code]326 (* 8.1757989156327 (Math/pow 2 (* (float (/ 12)) midi-code))))329 ;; division == clock-pulses / quarter-note330 ;; tempo == microseconds / quarter-note332 ;; have: clock-pulses333 ;; want: seconds338 (defn midi->mini-midi [#^File midi-file]339 (let [midi-events (parse-midi midi-file)341 note-on-events342 (filter #(= :Note_on_c (:command %)) midi-events)343 note-off-events344 (filter #(= :Note_off_c (:command %)) midi-events)346 channel-1-on347 (sort-by :time348 (filter #(= 1 (:channel (:args %)))349 note-on-events))350 channel-1-off351 (sort-by :time352 (filter #(= 1 (:channel (:args %)))353 note-off-events))356 tempo (:args (first (filter #(= :Tempo (:command %)) midi-events)))357 division (:division358 (:args (first (filter #(= :Header (:command %)) midi-events))))359 ]361 (map362 (fn [note-event]363 (note-codes (:frequency note-event)364 (:volume note-event)365 (int (* (:duration note-event) 0x100))))367 (map368 (fn [note-on note-off]369 {:frequency (midi-code->frequency (:note (:args note-on)))370 :duration371 (/ (* (/ tempo division)372 (- (:time note-off) (:time note-on)))373 1e6) ;; convert clock-pulses into seconds374 :volume (int (/ (:velocity (:args note-on)) 10))375 :time-stamp (/ (* (/ tempo division)376 (:time note-on)) 1e6)})377 channel-1-on channel-1-off))))385 (def C4 (partial note-codes 261.63))386 (def D4 (partial note-codes 293.66))387 (def E4 (partial note-codes 329.63))388 (def F4 (partial note-codes 349.23))389 (def G4 (partial note-codes 392))390 (def A4 (partial note-codes 440))391 (def B4 (partial note-codes 493.88))392 (def C5 (partial note-codes 523.3))394 (def scale395 (flatten396 [(C4 0xF 0x40)397 (D4 0xF 0x40)398 (E4 0xF 0x40)399 (F4 0xF 0x40)400 (G4 0xF 0x40)401 (A4 0xF 0x40)402 (B4 0xF 0x40)403 (C5 0xF 0x40)]))405 (defn play-music [music-bytes]406 (let [program-target 0xC000407 music-target 0xD000]408 (-> (set-memory-range (second (music-base))409 program-target (music-kernel))410 (set-memory-range music-target music-bytes)411 (PC! program-target))))414 (defn test-note [music-bytes]415 (-> (set-memory-range (second (music-base))416 0xC000 (concat (clear-music-registers)417 (play-note)418 (infinite-loop)))419 (set-memory-range 0xD000 music-bytes)420 (PC! 0xC000)421 (HL! 0xD000)422 ))425 (defn run-program426 ([program]427 (let [target 0xC000]428 (-> (set-memory-range (second (music-base))429 target program)430 (PC! target)))))432 (defn test-timer []433 (flatten434 [0x3E435 0x01436 0xE0437 0x06 ;; set TMA to 0439 0x3E440 (Integer/parseInt "00000100" 2)441 0xE0442 0x07 ;; set TAC to 16384 Hz and activate timer444 (repeat445 500446 [0xF0447 0x05])]))