rlm@417: (ns com.aurellem.run.music rlm@417: (:use (com.aurellem.gb saves gb-driver util constants rlm@417: items vbm characters money rlm@417: rlm-assembly)) rlm@417: (:use (com.aurellem.run util title save-corruption rlm@417: bootstrap-0 bootstrap-1)) rlm@426: (:require clojure.string) rlm@426: (:import [com.aurellem.gb.gb_driver SaveState]) rlm@426: (:import java.io.File)) rlm@417: rlm@427: (def third-kind rlm@427: (File. "/home/r/proj/midi/third-kind.mid")) rlm@417: rlm@455: (def pony rlm@455: (File. "/home/r/proj/vba-clojure/music/pony-title.mid")) rlm@455: rlm@455: (def sync-test rlm@455: (File. "/home/r/proj/vba-clojure/music/sync-test.mid")) rlm@455: rlm@454: rlm@427: (defn raw-midi-text [#^File midi-file] rlm@427: (:out rlm@427: (clojure.java.shell/sh rlm@427: "midicsv" rlm@427: (.getCanonicalPath midi-file) rlm@427: "-"))) rlm@427: rlm@427: (def command-line #"^(\d+), (\d+), ([^,]+)(.*)$") rlm@427: rlm@427: (defmulti parse-command :command) rlm@427: rlm@427: (defn discard-args [command] (dissoc command :args)) rlm@427: rlm@427: (defmethod parse-command :Start_track rlm@427: [command] (discard-args command)) rlm@427: rlm@427: (defmethod parse-command :End_track rlm@427: [command] (discard-args command)) rlm@427: rlm@427: (defmethod parse-command :default rlm@427: [command] command) rlm@427: rlm@427: (defn parse-number-list rlm@427: [number-list-str] rlm@427: (map #(Integer/parseInt %) rlm@427: (clojure.string/split number-list-str #", "))) rlm@427: rlm@427: (defmethod parse-command :Tempo rlm@427: [command] rlm@427: (update-in command [:args] #(Integer/parseInt %))) rlm@427: rlm@427: (defn parse-midi-note-list rlm@427: [midi-note-list-str] rlm@427: (let [[channel note velocity] rlm@427: (parse-number-list midi-note-list-str)] rlm@427: {:channel channel :note note :velocity velocity})) rlm@427: rlm@427: (defmethod parse-command :Note_on_c rlm@427: [command] rlm@427: (update-in command [:args] parse-midi-note-list)) rlm@427: rlm@427: (defmethod parse-command :Note_off_c rlm@427: [command] rlm@427: (update-in command [:args] parse-midi-note-list)) rlm@427: rlm@427: (defmethod parse-command :Header rlm@427: [command] rlm@427: (let [args (:args command) rlm@427: [format num-tracks division] (parse-number-list args)] rlm@427: (assoc command :args rlm@427: {:format format rlm@427: :num-tracks num-tracks rlm@427: :division division}))) rlm@427: rlm@427: (defmethod parse-command :Program_c rlm@427: [command] rlm@427: (let [args (:args command) rlm@427: [channel program-num] (parse-number-list args)] rlm@427: (assoc command :args rlm@427: {:channel channel rlm@427: :program-num program-num}))) rlm@427: rlm@427: (defn parse-midi [#^File midi-file] rlm@427: (map rlm@427: (comp parse-command rlm@427: (fn [line] rlm@427: (let [[[_ channel time command args]] rlm@427: (re-seq command-line line)] rlm@427: {:channel (Integer/parseInt channel) rlm@427: :time (Integer/parseInt time) rlm@427: :command (keyword command) rlm@427: :args (apply str (drop 2 args))}))) rlm@427: (drop-last rlm@427: (clojure.string/split-lines rlm@427: (raw-midi-text midi-file))))) rlm@427: rlm@417: (def music-base new-kernel) rlm@417: rlm@417: (defn store [n address] rlm@417: (flatten rlm@417: [0xF5 rlm@417: 0xE5 rlm@417: rlm@417: 0x3E rlm@417: n rlm@417: rlm@417: 0x21 rlm@417: (reverse (disect-bytes-2 address)) rlm@417: rlm@417: 0x77 rlm@417: rlm@417: 0xE1 rlm@417: 0xF1])) rlm@417: rlm@417: (defn infinite-loop [] rlm@417: [0x18 0xFE]) rlm@417: rlm@417: (def divider-register 0xFF04) rlm@417: rlm@417: (defrecord Bit-Note [frequency volume duration duty]) rlm@417: rlm@417: (defn clear-music-registers [] rlm@417: (flatten rlm@433: [(store (Integer/parseInt "00000000" 2) 0xFF10) ;; sweep rlm@433: (store (Integer/parseInt "00000000" 2) 0xFF11) ;; pattern duty rlm@433: (store (Integer/parseInt "00000000" 2) 0xFF12) ;; volume rlm@433: (store (Integer/parseInt "00000000" 2) 0xFF13) ;; frequency-low rlm@433: (store (Integer/parseInt "00000000" 2) 0xFF14) ;; frequency-high rlm@417: rlm@417: (store (Integer/parseInt "00000000" 2) 0xFF16) ;; pattern duty 000000 rlm@417: (store (Integer/parseInt "00000000" 2) 0xFF17) ;; volume 0000 rlm@417: (store (Integer/parseInt "00000000" 2) 0xFF18) ;; frequency-low rlm@417: (store (Integer/parseInt "00000000" 2) 0xFF19) ;; 00000 frequency-high rlm@417: rlm@417: (store (Integer/parseInt "00000000" 2) 0xFF1A) rlm@417: (store (Integer/parseInt "00000000" 2) 0xFF1B) rlm@417: (store (Integer/parseInt "00000000" 2) 0xFF1C) rlm@417: (store (Integer/parseInt "00000000" 2) 0xFF1D) rlm@417: (store (Integer/parseInt "00000000" 2) 0xFF1E) rlm@417: rlm@433: (store (Integer/parseInt "00000000" 2) 0xFF20) ;; length rlm@433: (store (Integer/parseInt "00000000" 2) 0xFF21) ;; volume rlm@433: (store (Integer/parseInt "00000000" 2) 0xFF22) ;; noise-frequency rlm@433: (store (Integer/parseInt "00000000" 2) 0xFF23) ;; control rlm@433: ])) rlm@417: rlm@424: rlm@424: ;; mini-midi syntax rlm@424: rlm@424: ;; codes rlm@424: ;; note-code == 0x00 rlm@424: ;; change-duty-code = 0x01 rlm@424: ;; silence-code = 0x02 rlm@424: rlm@424: ;; silence format rlm@424: ;; 2 bytes rlm@424: ;; [silence-code (0x02)] rlm@424: ;; [duration-8-bits] rlm@424: rlm@424: ;; note data format rlm@424: ;; 4 bytes rlm@424: ;; [note-code (0x00)] rlm@424: ;; [volume-4-bits 0 frequency-high-3-bits] rlm@424: ;; [frequengy-low-8-bits] rlm@424: ;; [duration-8-bits] rlm@424: rlm@424: ;; change-duty-format rlm@424: ;; 2 bytes rlm@424: ;; [change-duty-code (0x01)] rlm@424: ;; [new-duty] rlm@424: rlm@425: (def note-code 0x00) rlm@425: (def change-duty-code 0x01) rlm@425: (def silence-code 0x02) rlm@425: rlm@424: (defn do-message rlm@424: "Read the message which starts at the current value of HL and do rlm@424: what it says. Duration is left in A, and HL is advanced rlm@424: appropraitely." rlm@433: ([] (do-message 0x16)) rlm@433: ([sound-base-address] rlm@433: (let [switch rlm@433: [0x2A ;; load message code into A, increment HL rlm@433: rlm@433: ;; switch on message rlm@433: 0xFE rlm@433: note-code rlm@433: rlm@433: 0x20 rlm@433: :note-length] rlm@424: rlm@433: play-note rlm@433: [0x2A ;; load volume/frequency-high info rlm@433: 0xF5 ;; push A rlm@433: 0xE6 rlm@433: (Integer/parseInt "11110000" 2) ;; volume mask rlm@433: 0xE0 rlm@433: (inc sound-base-address) ;;0x17 ;; set volume rlm@433: 0xF1 ;; pop A rlm@433: 0xE6 rlm@433: (Integer/parseInt "00000111" 2) ;; frequency-high mask rlm@433: 0xE0 rlm@433: (+ 3 sound-base-address) ;;0x19 ;; set frequency-high rlm@433: rlm@433: 0x2A ;; load frequency low-bits rlm@433: 0xE0 rlm@433: (+ 2 sound-base-address) ;;0x18 ;; set frequency-low-bits rlm@433: 0x2A]] ;; load duration rlm@433: (replace rlm@433: {:note-length (count play-note)} rlm@433: (concat switch play-note))))) rlm@424: rlm@433: ;; (defn play-note rlm@433: ;; "Play the note referenced by HL in the appropiate channel. rlm@433: ;; Leaves desired-duration in A." rlm@433: rlm@433: ;; [0x2A ;; load volume/frequency-high info rlm@433: ;; 0xF5 ;; push A rlm@433: ;; 0xE6 rlm@433: ;; (Integer/parseInt "11110000" 2) ;; volume mask rlm@433: ;; 0xE0 rlm@433: ;; 0x17 ;; set volume rlm@433: ;; 0xF1 ;; pop A rlm@433: ;; 0xE6 rlm@433: ;; (Integer/parseInt "00000111" 2) ;; frequency-high mask rlm@433: ;; 0xE0 rlm@433: ;; 0x19 ;; set frequency-high rlm@417: rlm@433: ;; 0x2A ;; load frequency low-bits rlm@433: ;; 0xE0 rlm@433: ;; 0x18 ;; set frequency-low-bits rlm@417: rlm@433: ;; 0x2A ;; load duration rlm@433: ;; ]) rlm@417: rlm@434: (defn music-step [sound-base-address] rlm@432: ;; C == current-ticks rlm@432: ;; A == desired-ticks rlm@435: rlm@435: (flatten rlm@435: [;; restore variables from stack rlm@435: 0xE1 ;; pop HL rlm@435: 0xC1 ;; pop CB rlm@435: 0xF1 ;; pop AF rlm@432: rlm@435: rlm@435: 0xF5 ;; push A rlm@417: 0xF0 rlm@432: 0x05 ;; load current ticks from 0xF005 rlm@431: 0xB8 ;; rlm@417: 0x30 ;; increment C only if last result caused carry rlm@417: 0x01 rlm@418: 0x0C rlm@417: rlm@417: 0x47 ;; update sub-ticks (A->B) rlm@417: rlm@417: 0xF1 ;; pop AF, now A contains desired-ticks rlm@417: rlm@417: 0xB9 ;; compare with current ticks rlm@417: rlm@417: ;; if desired-ticks = current ticks rlm@417: ;; go to next note ; set current set ticks to 0. rlm@417: rlm@417: 0x20 rlm@434: (+ (count (do-message 0)) 2) rlm@417: rlm@434: (do-message sound-base-address) rlm@417: rlm@417: 0x0E rlm@435: 0x00 ;; 0->C (current-ticks) rlm@417: rlm@435: ;; save variables to stack rlm@435: 0xF5 ;; push AF rlm@435: 0xC5 ;; push CB rlm@435: 0xE5 ;; push HL rlm@435: rlm@435: rlm@435: ])) rlm@435: rlm@435: (def music-1 0x11) rlm@434: (def music-2 0x16) rlm@434: rlm@417: (defn music-kernel [] rlm@417: (flatten rlm@432: [;; global initilization section rlm@432: (clear-music-registers) rlm@432: rlm@417: 0x3E rlm@418: 0x01 rlm@417: 0xE0 rlm@417: 0x06 ;; set TMA to 0 rlm@417: rlm@417: 0x3E rlm@423: (Integer/parseInt "00000110" 2) rlm@417: 0xE0 rlm@423: 0x07 ;; set TAC to 65536 Hz and activate timer rlm@417: rlm@435: ;; initialize frame 1 rlm@432: 0x21 rlm@432: 0x00 rlm@437: 0xA0 ;; set HL to 0xA000 == music-start 1 rlm@432: 0x0E rlm@432: 0x00 ;; 0->C rlm@432: 0x06 rlm@432: 0x00 ;; 0->B rlm@435: rlm@433: 0xAF ;; 0->A rlm@432: rlm@435: 0xF5 ;; push AF rlm@435: 0xC5 ;; push CB rlm@435: 0xE5 ;; push HL rlm@432: rlm@435: ;; initialize frame 2 rlm@436: 0x21 rlm@436: 0x00 rlm@437: 0xB0 ;; set HL to 0xB000 == music-start 2 rlm@436: rlm@436: 0xF5 ;; push AF rlm@436: 0xC5 ;; push CB rlm@436: 0xE5 ;; push HL rlm@436: rlm@436: rlm@437: ;; main music loop rlm@437: rlm@437: 0xE8 ;; SP + 6; activate frame 1 rlm@437: 6 rlm@437: (music-step music-1) rlm@437: ;;(repeat (count (music-step music-1)) 0x00) rlm@435: rlm@437: 0xE8 ;; SP - 6; activate frame 2 rlm@437: (->signed-8-bit -6) rlm@437: ;;(repeat (count (music-step music-2)) 0x00) rlm@437: (music-step music-2) rlm@433: rlm@424: rlm@417: 0x18 rlm@437: (->signed-8-bit (+ rlm@437: ;; two music-steps rlm@437: (- (* 2 (count (music-step 0)))) rlm@437: -2 ;; this jump instruction rlm@437: -2 ;; activate frame 1 rlm@437: -2 ;; activate frame 2 rlm@437: ))])) rlm@417: rlm@424: (defn frequency-code->frequency rlm@424: [code] rlm@424: (assert (<= 0 code 2047)) rlm@424: (/ 131072 (- 2048 code))) rlm@424: rlm@424: (defn clamp [x low high] rlm@424: (cond (> x high) high rlm@424: (< x low) low rlm@424: true x)) rlm@424: rlm@424: (defn frequency->frequency-code rlm@424: [frequency] rlm@424: (clamp rlm@424: (Math/round rlm@424: (float rlm@424: (/ (- (* 2048 frequency) 131072) frequency))) rlm@424: 0x00 2048)) rlm@424: rlm@424: (defn note-codes [frequency volume duration] rlm@424: (assert (<= 0 volume 0xF)) rlm@430: (if (<= duration 0xFF) rlm@430: (let [frequency-code rlm@430: (frequency->frequency-code frequency) rlm@430: volume&high-frequency rlm@430: (+ (bit-shift-left volume 4) rlm@430: (bit-shift-right frequency-code 8)) rlm@430: low-frequency rlm@430: (bit-and 0xFF frequency-code)] rlm@430: [note-code rlm@430: volume&high-frequency rlm@430: low-frequency rlm@430: duration]) rlm@430: (vec rlm@430: (flatten rlm@430: [(note-codes frequency volume 0xFF) rlm@430: (note-codes frequency volume (- duration 0xFF))])))) rlm@430: rlm@424: rlm@427: (defn midi-code->frequency rlm@427: [midi-code] rlm@427: (* 8.1757989156 rlm@427: (Math/pow 2 (* (float (/ 12)) midi-code)))) rlm@427: rlm@427: ;; division == clock-pulses / quarter-note rlm@427: ;; tempo == microseconds / quarter-note rlm@427: rlm@427: ;; have: clock-pulses rlm@427: ;; want: seconds rlm@427: rlm@427: rlm@428: (defn silence [length] rlm@428: {:frequency 1 rlm@428: :duration length rlm@428: :volume 0}) rlm@427: rlm@438: (defn commands rlm@438: "return all events where #(= (:command %) command)" rlm@438: [command s] rlm@438: (filter #(= command (:command %)) s)) rlm@438: rlm@438: (defn midi-track->mini-midi [#^File midi-file track-num] rlm@427: (let [midi-events (parse-midi midi-file) rlm@427: rlm@438: note-on-events (commands :Note_on_c midi-events) rlm@438: note-off-events (commands :Note_off_c midi-events) rlm@427: rlm@438: select-channel rlm@438: (fn [n s] rlm@438: (sort-by :time (filter #(= n (:channel (:args %))) s))) rlm@438: rlm@438: channel-on (select-channel track-num note-on-events) rlm@427: rlm@438: channel-off (select-channel track-num note-off-events) rlm@427: rlm@438: rlm@438: tempo (:args (first (commands :Tempo midi-events))) rlm@438: division rlm@438: (:division (:args (first (commands :Header midi-events)))) rlm@428: rlm@428: notes rlm@428: (map rlm@428: (fn [note-on note-off] rlm@428: {:frequency (midi-code->frequency (:note (:args note-on))) rlm@428: :duration rlm@428: (/ (* (/ tempo division) rlm@428: (- (:time note-off) (:time note-on))) rlm@428: 1e6) ;; convert clock-pulses into seconds rlm@428: :volume (int (/ (:velocity (:args note-on)) 10)) rlm@428: :time-stamp (/ (* (/ tempo division) rlm@428: (:time note-on)) 1e6)}) rlm@438: channel-on channel-off) rlm@428: rlm@428: silences rlm@428: (map (fn [note-1 note-2] rlm@428: (let [note-1-space (- (:time-stamp note-2) rlm@428: (:time-stamp note-1)) rlm@428: note-1-length (:duration note-1)] rlm@428: (silence (- note-1-space note-1-length)))) rlm@428: ;; to handle silence at the beginning. rlm@428: (concat [(assoc (silence 0) rlm@428: :time-stamp 0)] notes) rlm@428: notes) rlm@428: rlm@428: notes-with-silence rlm@456: (concat rlm@456: (filter (comp not zero? :duration) rlm@456: (interleave silences notes)) rlm@456: [(silence 3)])] rlm@456: rlm@456: (map rlm@456: (fn [note-event] rlm@456: (note-codes (:frequency note-event) rlm@456: (:volume note-event) rlm@456: (int (* (:duration note-event) 0x100)))) rlm@456: notes-with-silence))) rlm@456: rlm@428: rlm@438: (defn midi->mini-midi [#^File midi-file] rlm@455: {:track-1 (flatten (midi-track->mini-midi midi-file 0)) rlm@455: :track-2 (flatten (midi-track->mini-midi midi-file 1))}) rlm@438: rlm@438: (defn play-midi [#^File midi-file] rlm@438: (let [track-1-target 0xA000 rlm@438: track-2-target 0xB000 rlm@438: program-target 0xC000 rlm@438: mini-midi (midi->mini-midi midi-file) rlm@438: long-silence (flatten (note-codes 20 0 9001))] rlm@438: rlm@438: (-> (second (music-base)) rlm@438: (set-memory-range track-1-target long-silence) rlm@438: (set-memory-range track-2-target long-silence) rlm@438: (set-memory-range track-1-target (:track-1 mini-midi)) rlm@438: (set-memory-range track-2-target (:track-2 mini-midi)) rlm@438: (set-memory-range program-target (music-kernel)) rlm@438: (PC! program-target)))) rlm@438: rlm@424: (def C4 (partial note-codes 261.63)) rlm@424: (def D4 (partial note-codes 293.66)) rlm@424: (def E4 (partial note-codes 329.63)) rlm@424: (def F4 (partial note-codes 349.23)) rlm@424: (def G4 (partial note-codes 392)) rlm@424: (def A4 (partial note-codes 440)) rlm@424: (def B4 (partial note-codes 493.88)) rlm@424: (def C5 (partial note-codes 523.3)) rlm@424: rlm@430: (def scale rlm@430: (flatten rlm@430: [(C4 0xF 0x40) rlm@430: (D4 0xF 0x40) rlm@430: (E4 0xF 0x40) rlm@430: (F4 0xF 0x40) rlm@430: (G4 0xF 0x40) rlm@430: (A4 0xF 0x40) rlm@430: (B4 0xF 0x40) rlm@430: (C5 0xF 0x40)])) rlm@424: rlm@418: (defn play-music [music-bytes] rlm@417: (let [program-target 0xC000 rlm@437: music-target 0xA000] rlm@417: (-> (set-memory-range (second (music-base)) rlm@417: program-target (music-kernel)) rlm@417: (set-memory-range music-target music-bytes) rlm@417: (PC! program-target)))) rlm@417: rlm@417: rlm@438: rlm@437: ;; (defn test-note [music-bytes] rlm@437: ;; (-> (set-memory-range (second (music-base)) rlm@437: ;; 0xC000 (concat (clear-music-registers) rlm@437: ;; (play-note) rlm@437: ;; (infinite-loop))) rlm@437: ;; (set-memory-range 0xD000 music-bytes) rlm@437: ;; (PC! 0xC000) rlm@437: ;; (HL! 0xD000) rlm@437: ;; )) rlm@417: rlm@417: rlm@417: (defn run-program rlm@418: ([program] rlm@417: (let [target 0xC000] rlm@417: (-> (set-memory-range (second (music-base)) rlm@417: target program) rlm@417: (PC! target))))) rlm@417: rlm@424: (defn test-timer [] rlm@424: (flatten rlm@424: [0x3E rlm@424: 0x01 rlm@424: 0xE0 rlm@424: 0x06 ;; set TMA to 0 rlm@424: rlm@424: 0x3E rlm@424: (Integer/parseInt "00000100" 2) rlm@424: 0xE0 rlm@424: 0x07 ;; set TAC to 16384 Hz and activate timer rlm@424: rlm@424: (repeat rlm@424: 500 rlm@424: [0xF0 rlm@424: 0x05])])) rlm@426: rlm@426: