changeset 242:2e751984b42d

making interesting data insights.
author Dylan Holmes <ocsenave@gmail.com>
date Sun, 25 Mar 2012 02:53:29 -0500
parents 7984a084aa07
children 5b59c6f17cd5
files clojure/com/aurellem/gb/assembly.clj clojure/com/aurellem/music/midi_util.clj
diffstat 2 files changed, 244 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb/assembly.clj	Sun Mar 25 02:35:14 2012 -0500
     1.2 +++ b/clojure/com/aurellem/gb/assembly.clj	Sun Mar 25 02:53:29 2012 -0500
     1.3 @@ -415,7 +415,7 @@
     1.4    "A program for altering in-game memory by pressing buttons."
     1.5    []
     1.6    [
     1.7 -   0xF3 ; stop interrupts
     1.8 +   ;; 0xF3 ; stop interrupts
     1.9     ;; --------- CLEANUP
    1.10     0xAF ; zero A  [D31E]
    1.11     0x57 ; A->D; makes D=0.
    1.12 @@ -437,16 +437,16 @@
    1.13     0xF5 ;; push AF (vbprev)
    1.14     
    1.15     0xA1 ;; A & C --> A. Now A_0 contains "increment?"
    1.16 -   
    1.17 -   0xCB ;; test A_0. this result will be used twice.
    1.18 -   0x47
    1.19 +
    1.20 +   0x4F
    1.21 +   0xCB ;; test bit 0 of C. sadly, this result can't be reused below. 
    1.22 +   0x41
    1.23     
    1.24     0x28 ;; end frame (JUMP) if A_0 = 0.  
    1.25     0xF3 ;; TODO: set jump length
    1.26     
    1.27     ;; -------- GET BUTTON INPUT
    1.28     
    1.29 -        ;; btw, Z bit is now 1
    1.30          ;; prepare to select bits
    1.31   
    1.32     0x3E ;; load 0x20 into A, to measure dpad
    1.33 @@ -466,6 +466,9 @@
    1.34     
    1.35     0xB0 ;; A or B --> A
    1.36  
    1.37 +   0xCB ;; check bit 0 of C
    1.38 +   0x41
    1.39 +   
    1.40     0x28 ;; JUMP forward if Z=0
    1.41     0x08
    1.42  
    1.43 @@ -475,11 +478,9 @@
    1.44     
    1.45     0x3E ;; load 0x10 into A, to measure btns
    1.46     0x10
    1.47 -
    1.48 -   0xBF ;; compare(A,A) sets Z=0
    1.49     
    1.50     0x18 ;; JUMP back to "load A into [FF00]"
    1.51 -   0xEF
    1.52 +   0xF0
    1.53  
    1.54  
    1.55     ;; ------ TAKE ACTION BASED ON USER INPUT
    1.56 @@ -542,8 +543,8 @@
    1.57     0x57
    1.58     0x82 ;; add the mode to A
    1.59     0xEA ;; store A into "thing to execute"
    1.60 -   0x74
    1.61 -   0xD3
    1.62 +   0x74 ;; ABSOLUTE LOCATION
    1.63 +   0xD3 ;; ABSOLUTE LOCATION
    1.64  
    1.65     0x3E ;; load the constant 8 into A
    1.66     0x08
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/clojure/com/aurellem/music/midi_util.clj	Sun Mar 25 02:53:29 2012 -0500
     2.3 @@ -0,0 +1,233 @@
     2.4 +(ns com.aurellem.music.midi-util
     2.5 +  ;;(:import javax.sound.sampled)
     2.6 +  (:import (javax.sound.midi MidiSystem
     2.7 +                             Sequence
     2.8 +                             Track
     2.9 +                             MidiEvent
    2.10 +                             MetaMessage
    2.11 +                             
    2.12 +                             ShortMessage)
    2.13 +           (com.sun.media.sound FastShortMessage)
    2.14 +           (java.io File))
    2.15 +  
    2.16 + (:use (com.aurellem.gb saves util constants gb-driver vbm items assembly characters))
    2.17 + (:use (com.aurellem.run title))
    2.18 + (:use (com.aurellem.exp pokemon item-bridge))
    2.19 + (:import [com.aurellem.gb.gb_driver SaveState]))
    2.20 +
    2.21 +
    2.22 +;;; PURE MIDI MANIPULATION
    2.23 +
    2.24 +(defn midi-load
    2.25 +  "Takes a path to a MIDI file and returns a Sequence object."
    2.26 +  [path]
    2.27 +  ((fn [file]
    2.28 +    (if (.exists file)
    2.29 +      (MidiSystem/getSequence file)
    2.30 +      nil))
    2.31 +   (File. path)))
    2.32 +
    2.33 +
    2.34 +(defn midi-play
    2.35 +  "Plays the MIDI file at the given location. The MIDI file runs in
    2.36 +the current thread until it finishes or is cancelled."
    2.37 +  [path]
    2.38 +  (let [midi (midi-load path)]
    2.39 +    (if (nil? midi) nil
    2.40 +        (let [song
    2.41 +              (doto
    2.42 +                  (MidiSystem/getSequencer) 
    2.43 +                (.open)
    2.44 +                (.setSequence midi)
    2.45 +                (.start))]
    2.46 +          (try
    2.47 +            (loop []
    2.48 +              (if (. song (isRunning))
    2.49 +                (do
    2.50 +                  (Thread/sleep 10)
    2.51 +                  (recur))
    2.52 +                (throw (Exception. "Song stopped!"))))
    2.53 +            (finally (.close song)))))))
    2.54 +
    2.55 +
    2.56 +(defn midi-test-1 []
    2.57 +  (-> (.
    2.58 +       (midi-load
    2.59 +        "/home/ocsenave/bk_robert/sounds/sounds/www.vgmusic.com/console/nintendo/gameboy/PkmRB-Item.mid")
    2.60 +       (getTracks))
    2.61 +      
    2.62 +      (vec)
    2.63 +      (second)
    2.64 +
    2.65 +      ((fn[trk]
    2.66 +         (map #(. trk (get %))
    2.67 +              (range 0 (. trk size)))))
    2.68 +
    2.69 +      ((fn [evts]
    2.70 +         (map (juxt #(identity (.getMessage %)) #(vec (.getMessage (.getMessage %))) #(.getTick %) ) evts)
    2.71 +         )
    2.72 +      
    2.73 +      )))
    2.74 +
    2.75 +
    2.76 +
    2.77 +
    2.78 +
    2.79 +
    2.80 +(defn midi-event
    2.81 +  "Creates an event at the given tick, of the given msg-type,
    2.82 +  containing data in coll. msg-type can be :meta, :short. If :meta, x
    2.83 +  is the type of the msg. If :short, x is the status of the message."
    2.84 +  [tick msg-type x coll]
    2.85 +
    2.86 +    (cond (= msg-type :meta)
    2.87 +          (MetaMessage.)
    2.88 +          (= msg-type :short)
    2.89 +          (ShortMessage.))
    2.90 +    
    2.91 +  )
    2.92 +
    2.93 +
    2.94 +(defn midi-test-2 []
    2.95 +  (let [sequence (Sequence. (float 30) 15)] ;; 30 fps, 15 frames per beat
    2.96 +    (doto (. sequence (createTrack))
    2.97 +      (.add (MidiEvent. (MetaMessage.) 0)))))
    2.98 +
    2.99 +
   2.100 +
   2.101 +            [com.sun.media.sound.FastShortMessage [-80 0 0] 0] [com.sun.media.sound.FastShortMessage [-80 7 100] 0] [com.sun.media.sound.FastShortMessage [-80 10 64] 0] [com.sun.media.sound.FastShortMessage [-80 32 0] 0] [com.sun.media.sound.FastShortMessage [-64 80] 0] [com.sun.media.sound.FastShortMessage [-80 101 0] 0] [com.sun.media.sound.FastShortMessage [-80 100 0] 1] [com.sun.media.sound.FastShortMessage [-80 6 2] 2] [com.sun.media.sound.FastShortMessage [-80 38 0] 3] [com.sun.media.sound.FastShortMessage [-32 0 56] 3] [com.sun.media.sound.FastShortMessage [-112 68 100] 3] [com.sun.media.sound.FastShortMessage [-112 68 0] 20] [com.sun.media.sound.FastShortMessage [-112 68 100] 40] [com.sun.media.sound.FastShortMessage [-112 68 0] 60] [com.sun.media.sound.FastShortMessage [-112 68 100] 80] [com.sun.media.sound.FastShortMessage [-112 68 0] 100] [com.sun.media.sound.FastShortMessage [-80 7 100] 120] [com.sun.media.sound.FastShortMessage [-112 76 100] 120] [com.sun.media.sound.FastShortMessage [-80 7 90] 181] [com.sun.media.sound.FastShortMessage [-80 7 80] 209] [com.sun.media.sound.FastShortMessage [-80 7 65] 240] [com.sun.media.sound.FastShortMessage [-80 7 50] 270] [com.sun.media.sound.FastShortMessage [-112 76 0] 300] [com.sun.media.sound.FastShortMessage [-80 7 0] 360] [javax.sound.midi.Track$ImmutableEndOfTrack [-1 47 0] 360])
   2.102 +com.aurellem.music.midi-util> (midi-test-1)
   2.103 +([javax.sound.midi.MetaMessage [-1 3 33 79 114 105 103 105 110 97 108 32 99 111 109 112 111 115 101 114 58 32 74 117 110 105 99 104 105 32 77 97 115 117 100 97] 0] [com.sun.media.sound.FastShortMessage [-80 0 0] 0] [com.sun.media.sound.FastShortMessage [-80 7 100] 0] [com.sun.media.sound.FastShortMessage [-80 10 64] 0] [com.sun.media.sound.FastShortMessage [-80 32 0] 0] [com.sun.media.sound.FastShortMessage [-64 80] 0] [com.sun.media.sound.FastShortMessage [-80 101 0] 0] [com.sun.media.sound.FastShortMessage [-80 100 0] 1] [com.sun.media.sound.FastShortMessage [-80 6 2] 2] [com.sun.media.sound.FastShortMessage [-80 38 0] 3] [com.sun.media.sound.FastShortMessage [-32 0 56] 3] [com.sun.media.sound.FastShortMessage [-112 68 100] 3] [com.sun.media.sound.FastShortMessage [-112 68 0] 20] [com.sun.media.sound.FastShortMessage [-112 68 100] 40] [com.sun.media.sound.FastShortMessage [-112 68 0] 60] [com.sun.media.sound.FastShortMessage [-112 68 100] 80] [com.sun.media.sound.FastShortMessage [-112 68 0] 100] [com.sun.media.sound.FastShortMessage [-80 7 100] 120] [com.sun.media.sound.FastShortMessage [-112 76 100] 120] [com.sun.media.sound.FastShortMessage [-80 7 90] 181] [com.sun.media.sound.FastShortMessage [-80 7 80] 209] [com.sun.media.sound.FastShortMessage [-80 7 65] 240] [com.sun.media.sound.FastShortMessage [-80 7 50] 270] [com.sun.media.sound.FastShortMessage [-112 76 0] 300] [com.sun.media.sound.FastShortMessage [-80 7 0] 360] [javax.sound.midi.Track$ImmutableEndOfTrack [-1 47 0] 360])
   2.104 +  
   2.105 +
   2.106 +;;; ROM MUSIC MANIPULATION
   2.107 +
   2.108 +(def songs;; music-headers
   2.109 +  {
   2.110 +   :pallet 0x822E
   2.111 +   :pkmn-center 0x8237
   2.112 +   :gym 0x8240
   2.113 +   :city-1 0x8249 ;;virian, pewter, saffron
   2.114 +   :city-2 0x8255 ;; cerulean, fuchsia
   2.115 +   :celedon 0x825E
   2.116 +   :cinnibar 0x8267
   2.117 +   :vermilion 0x8270
   2.118 +   :lavender 0x827C
   2.119 +   :ss-anne 0x8288
   2.120 +   :meet-prof 0x8291
   2.121 +   :meet-blue 0x829A
   2.122 +   :follow 0x82A3
   2.123 +   :safari 0x82AF
   2.124 +   :sfx-heal 0x82BA
   2.125 +   :route-1 0x82C1 ;; route 1,2
   2.126 +   :route-2 0x82CD ;; route 24, 25
   2.127 +   :route-3 0x82D9 ;; route 3-10,16-22
   2.128 +   :route-4 0x82E5 ;; route 11-15
   2.129 +   :route-5 0x82F1 ;; indigo plateau
   2.130 +
   2.131 +   :title 0x7C249
   2.132 +   :credits 0x7C255
   2.133 +   :hall-of-fame 0x7C25E
   2.134 +   :lab-prof 0x7C267
   2.135 +   :jigglypuff 0x7C270
   2.136 +   :bike 0x7C276
   2.137 +   :surfing 0x7C282
   2.138 +   :casino 0x7C28B
   2.139 +   :intro-battle 0x7C294
   2.140 +   :power-plant 0x7C2A0 ;; power plant, unknown dungeon
   2.141 +   :viridian-forest 0x7C2AC ;;viridian forest, seafoam islands
   2.142 +   :victory-rd 0x7C2B8 ;;mt moon, rock tunnel, victory rd 
   2.143 +   :mansion 0x7C2C4
   2.144 +   :pkmn-tower 0x7C2D0
   2.145 +   :silph 0x7C2D9
   2.146 +   :trainer-bad 0x7C2E2
   2.147 +   :trainer-girl 0x7C2EB 
   2.148 +   :trainer-angry 0x7C2F4
   2.149 +   })
   2.150 +   
   2.151 +
   2.152 +   })
   2.153 +
   2.154 +
   2.155 +(defn low-high-format
   2.156 +  "Returns the number represented by the bytes."
   2.157 +  [low high]
   2.158 +  (+ low (* 256  high)))
   2.159 +
   2.160 +(defn rom-tracks
   2.161 +  "Given a valid address to a music header, returns a list of the
   2.162 +data tracks"
   2.163 +  [address]
   2.164 +  (let [rom (rom (root))
   2.165 +        tracklist
   2.166 +        ((fn extract-tracklist [mem n]
   2.167 +           (if (= (nth mem 2) n)
   2.168 +             (cons (low-high-format (first mem)
   2.169 +                       (second mem))
   2.170 +                   (extract-tracklist (drop 3 mem) (inc n)))              
   2.171 +             '()))
   2.172 +     
   2.173 +         (take 12 (drop (inc address) rom))
   2.174 +         1
   2.175 +         )]
   2.176 +
   2.177 +    (map
   2.178 +     (fn [trk] (take-while #(not= 0xFF %) (drop trk rom)))
   2.179 +     tracklist)
   2.180 +
   2.181 +    ))
   2.182 +  
   2.183 +
   2.184 +
   2.185 +(defn parse-track
   2.186 +  "Consumes the list of opcodes, returning a runnable MIDI Sequence object."
   2.187 +  [track]
   2.188 +  (fn [midi track]
   2.189 +    (cond (empty? track) midi)))
   2.190 +
   2.191 +
   2.192 +
   2.193 +
   2.194 +
   2.195 +
   2.196 +
   2.197 +
   2.198 +
   2.199 +  
   2.200 +;; 8237-823F Pokecenter 
   2.201 +;; 8240-8248 Gym 
   2.202 +;; 8249-8254 Viridian / Pewter / Saffron 
   2.203 +;; 8255-825D Cerulean / Fuchsia 
   2.204 +;; 825E-8266 Celedon 
   2.205 +;; 8267-826F Cinnibar 
   2.206 +;; 8270-827B Vermilion 
   2.207 +;; 827C-8287 Lavender 
   2.208 +;; 8288-8290 S.S. Anne 
   2.209 +;; 8291-8299 Meet Prof. Oak 
   2.210 +;; 829A-82A2 Meet Rival 
   2.211 +;; 82A3-82AE Guy Walks you to Museum 
   2.212 +;; 82AF-82B7 Safari Zone 
   2.213 +;; 82B8-82C0 Pokemon get healed 
   2.214 +;; 82C1-82CC Routes 1 / 2 
   2.215 +;; 82CD-82D8 Routes 24 / 25 
   2.216 +;; 82D9-82E4 Routes 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10 / 16 / 17 / 18 / 19 / 20 / 21 / 22 
   2.217 +;; 82E5-82F0 Routes 11 / 12 / 13 / 14 / 15 
   2.218 +;; 82F1-82FD Route 23 / Indigo Plateau 
   2.219 +;; 7C249-7C254 Title Screen 
   2.220 +;; 7C255-7C25D Credits 
   2.221 +;; 7C25E-7C266 Hall of FAme Registration 
   2.222 +;; 7C267-7C26F PRof Oak's LAb 
   2.223 +;; 7C270-7C275 Jigglypuff's Song 
   2.224 +;; 7C276-7C281 Bike Riding 
   2.225 +;; 7C282-7C28A Surfing 
   2.226 +;; 7C28B-7C293 Casino 
   2.227 +;; 7C294-7C29F Introduction Battle 
   2.228 +;; 7C2A0-7C2AB Power Plant / Unknown Dungeon 
   2.229 +;; 7C2AC-7C2B7 Viridian Forest / Seafoam Islands 
   2.230 +;; 7C2B8-7C2C3 Mt. Moon / Rock Tunnel / Victory Road 
   2.231 +;; 7C2C4-7C2CF Cinnibar Mansion 
   2.232 +;; 7C2D0-7C2D8 Pokemon Tower 
   2.233 +;; 7C2D9-7C2E1 Silph Co 
   2.234 +;; 7C2E2-7C2EA Meet Bad Trainer 
   2.235 +;; 7C2EB-7C2F3 Meet Girl Trainer 
   2.236 +;; 7C2F4-7C2FC Meet Angry Trainer