changeset 243:5b59c6f17cd5

Added the list of types and advantages to the hardcoded collection.
author Dylan Holmes <ocsenave@gmail.com>
date Sun, 25 Mar 2012 22:29:12 -0500
parents 2e751984b42d
children 27ca947084cf
files clojure/com/aurellem/gb/hxc.clj clojure/com/aurellem/music/midi_util.clj
diffstat 2 files changed, 188 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb/hxc.clj	Sun Mar 25 02:53:29 2012 -0500
     1.2 +++ b/clojure/com/aurellem/gb/hxc.clj	Sun Mar 25 22:29:12 2012 -0500
     1.3 @@ -57,8 +57,8 @@
     1.4    "The hardcoded place names in memory. List begins at
     1.5  ROM@71500. Cinnabar Mansion is dynamically calculated."
     1.6    (map character-codes->str
     1.7 -       (take-nth 2
     1.8 -                 (partition-by #(= % 0x50)
     1.9 +       (take-nth 2 
    1.10 +                (partition-by #(= % 0x50)
    1.11                                 (take 560
    1.12                                       (drop 0x71500
    1.13                                             (rom (root))))))))
    1.14 @@ -70,6 +70,82 @@
    1.15                            (drop (+ 0x98000)
    1.16                                  (rom (root))))))
    1.17  
    1.18 +
    1.19 +
    1.20 +
    1.21 +(def pkmn-types
    1.22 +  {0 :normal
    1.23 +   1 :fighting
    1.24 +   2 :flying
    1.25 +   3 :poison
    1.26 +   4 :ground
    1.27 +   5 :rock
    1.28 +   7 :bug
    1.29 +   8 :ghost
    1.30 +
    1.31 +   20 :fire
    1.32 +   21 :water
    1.33 +   22 :grass
    1.34 +   23 :electric
    1.35 +   24 :psychic
    1.36 +   25 :ice
    1.37 +   26 :dragon
    1.38 +   })
    1.39 +
    1.40 +
    1.41 +(def hxc-advantage
    1.42 +  (map
    1.43 +   (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
    1.44 +                         (get pkmn-types def (hex def))
    1.45 +                         (/ mult 10)])
    1.46 +   (partition 3
    1.47 +              (take-while (partial not= 0xFF)
    1.48 +                          (drop 0x3E62D (rom(root)))))))
    1.49 +  
    1.50 +
    1.51 +
    1.52 +;; (def pkmn-types
    1.53 +;;   [:normal
    1.54 +;;    :fighting
    1.55 +;;    :flying
    1.56 +;;    :poison
    1.57 +;;    :ground
    1.58 +;;    :rock
    1.59 +;;    :bird ;;?
    1.60 +;;    :bug
    1.61 +;;    :ghost
    1.62 +;;    :A
    1.63 +;;    :B
    1.64 +;;    :C
    1.65 +;;    :D
    1.66 +;;    :E
    1.67 +;;    :fire
    1.68 +;;    :water
    1.69 +;;    :grass
    1.70 +;;    :electric
    1.71 +;;    :psychic
    1.72 +;;    :ice
    1.73 +;;    :poision?])
    1.74 +
    1.75 +
    1.76 +
    1.77 +
    1.78 +
    1.79 +
    1.80 +
    1.81 +
    1.82 +
    1.83 +
    1.84 +
    1.85 +
    1.86 +
    1.87 +
    1.88 +
    1.89 +
    1.90 +
    1.91 +
    1.92 +
    1.93 +
    1.94  (def hxc-later
    1.95    "Running this code produces, e.g. hardcoded names NPCs give
    1.96  their pokemon. Will sort through it later."
     2.1 --- a/clojure/com/aurellem/music/midi_util.clj	Sun Mar 25 02:53:29 2012 -0500
     2.2 +++ b/clojure/com/aurellem/music/midi_util.clj	Sun Mar 25 22:29:12 2012 -0500
     2.3 @@ -28,11 +28,17 @@
     2.4     (File. path)))
     2.5  
     2.6  
     2.7 -(defn midi-play
     2.8 +
     2.9 +(def midi-play-file
    2.10    "Plays the MIDI file at the given location. The MIDI file runs in
    2.11  the current thread until it finishes or is cancelled."
    2.12 -  [path]
    2.13 -  (let [midi (midi-load path)]
    2.14 +  (comp midi-play-seq midi-load))
    2.15 +
    2.16 +
    2.17 +(defn midi-play-seq
    2.18 +  "Plays the MIDI Sequence. The MIDI runs in
    2.19 +the current thread until it finishes or is cancelled."
    2.20 +  [midi]
    2.21      (if (nil? midi) nil
    2.22          (let [song
    2.23                (doto
    2.24 @@ -46,8 +52,8 @@
    2.25                  (do
    2.26                    (Thread/sleep 10)
    2.27                    (recur))
    2.28 -                (throw (Exception. "Song stopped!"))))
    2.29 -            (finally (.close song)))))))
    2.30 +                ))
    2.31 +            (finally (.close song))))))
    2.32  
    2.33  
    2.34  (defn midi-test-1 []
    2.35 @@ -57,14 +63,15 @@
    2.36         (getTracks))
    2.37        
    2.38        (vec)
    2.39 -      (second)
    2.40 +      (nth 1)
    2.41  
    2.42        ((fn[trk]
    2.43           (map #(. trk (get %))
    2.44                (range 0 (. trk size)))))
    2.45  
    2.46        ((fn [evts]
    2.47 -         (map (juxt #(identity (.getMessage %)) #(vec (.getMessage (.getMessage %))) #(.getTick %) ) evts)
    2.48 +         (map (juxt #(.getTick %)  #(vec (.getMessage (.getMessage
    2.49 +         %))) #(.getMessage %) ) evts)
    2.50           )
    2.51        
    2.52        )))
    2.53 @@ -73,31 +80,83 @@
    2.54  
    2.55  
    2.56  
    2.57 +(defn midi-short
    2.58 +  "Creates a MIDI event containing a ShortMessage."
    2.59 +  [tick [status & ns]]
    2.60 +  (MidiEvent.
    2.61 +   (apply
    2.62 +    (fn
    2.63 +      ([x] (doto (ShortMessage.) (.setMessage x)))
    2.64 +      ([x y] (doto (ShortMessage.) (.setMessage x y 0)))
    2.65 +      ([x y z] (doto (ShortMessage.) (.setMessage x y z)))
    2.66 +      ([x y z w] (doto (ShortMessage.) (.setMessage x y z w))))
    2.67 +    status
    2.68 +    ns)
    2.69 +   tick))
    2.70  
    2.71 -(defn midi-event
    2.72 -  "Creates an event at the given tick, of the given msg-type,
    2.73 -  containing data in coll. msg-type can be :meta, :short. If :meta, x
    2.74 -  is the type of the msg. If :short, x is the status of the message."
    2.75 -  [tick msg-type x coll]
    2.76 +(defn midi-meta
    2.77 +  "Creates a MIDI event containing a MetaMessage"
    2.78 +  [tick type  ns]
    2.79 +  (MidiEvent.
    2.80 +   (doto (MetaMessage.)
    2.81 +     (.setMessage type
    2.82 +                 (byte-array (map byte ns))
    2.83 +                 (count ns)))
    2.84 +   tick))
    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 sign
    2.95 +  "Interpret the bits of n as a signed two's-complement byte"
    2.96 +  [n]
    2.97 +  (if (>= n 128) (- n 256)
    2.98 +      n))
    2.99 +
   2.100 +(defn unsign
   2.101 +  "Interpret the bits as an unsigned byte."
   2.102 +  [n]
   2.103 +  (if (neg? n) (+ n 256) n))
   2.104 +
   2.105 +
   2.106  
   2.107  
   2.108  (defn midi-test-2 []
   2.109 -  (let [sequence (Sequence. (float 30) 15)] ;; 30 fps, 15 frames per beat
   2.110 +  (let [sequence (Sequence. (float 30) 12)] ;; 30 fps, 10 frames per beat
   2.111      (doto (. sequence (createTrack))
   2.112 -      (.add (MidiEvent. (MetaMessage.) 0)))))
   2.113 +      (.add (midi-meta 0 3 [-1 3 33 79 114 105 103 105 110 97 108 32
   2.114 +      99 111 109 112 111 115 101 114 58 32 74 117 110 105 99 104 105 32 77
   2.115 +                            97 115 117 100 97]))
   2.116  
   2.117 +      (.add (midi-short 0 [-80 0 0])) ;; control change = -80
   2.118 +      (.add (midi-short 0 [-80 7 100])) ;; control change, volume, 100
   2.119 +      (.add (midi-short 0 [-80 10 64])) ;; control change, pan, 64 (middle?)
   2.120 +      (.add (midi-short 0 [-80 32 0]))  ;; ctrl chg, LSB ctrl 0 = bank 0
   2.121 +      (.add (midi-short 0 [-64 01]))  ;; program/instrument change = -64
   2.122 +      (.add (midi-short 0 [-80 101 0]))
   2.123 +      (.add (midi-short 1 [-80 100 0]))
   2.124 +      (.add (midi-short 2 [-80 6 2]))
   2.125 +      (.add (midi-short 3 [-80 38 0]))
   2.126 +      (.add (midi-short 3 [-32 0 56])) ;; pitch bend?! = -32
   2.127 +      (.add (midi-short 3 [-112 68 100])) ;; note on = -112
   2.128 +      (.add (midi-short 20 [-112 68 0]))
   2.129 +      (.add (midi-short 40 [-112 68 100]))
   2.130 +      (.add (midi-short 60 [-112 68 0]))
   2.131 +      (.add (midi-short 80 [-112 68 100]))
   2.132 +      (.add (midi-short 100 [-112 68 0]))
   2.133 +      (.add (midi-short 120 [-80 7 100])) ;; control change
   2.134 +      (.add (midi-short 120 [-112 76 100])) ;; note-on
   2.135 +      (.add (midi-short 181 [-80 7 90]))  ;; control change
   2.136 +      (.add (midi-short 209 [-80 7 80])) ;; control change
   2.137 +      (.add (midi-short 240 [-80 7 65])) ;; control change
   2.138 +      (.add (midi-short 270 [-80 7 50])) ;; control change
   2.139 +      (.add (midi-short 300 [-112 76 0])) ;; note on = -112
   2.140 +      (.add (midi-short 360 [-80 7 0])))
   2.141 +      ;;(.add (midi-short 360 [-1 47 0])) ;; system reset = -1
   2.142  
   2.143 +    sequence
   2.144 +    )))
   2.145  
   2.146 -            [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.147 -com.aurellem.music.midi-util> (midi-test-1)
   2.148 -([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.149 +
   2.150    
   2.151  
   2.152  ;;; ROM MUSIC MANIPULATION
   2.153 @@ -153,6 +212,11 @@
   2.154    "Returns the number represented by the bytes."
   2.155    [low high]
   2.156    (+ low (* 256  high)))
   2.157 +(defn high-low-format
   2.158 +  "Returns the number represented by the bytes."
   2.159 +  [high low]
   2.160 +  (+ low (* 256 high)))
   2.161 +
   2.162  
   2.163  (defn rom-tracks
   2.164    "Given a valid address to a music header, returns a list of the
   2.165 @@ -171,6 +235,7 @@
   2.166           1
   2.167           )]
   2.168  
   2.169 +    tracklist
   2.170      (map
   2.171       (fn [trk] (take-while #(not= 0xFF %) (drop trk rom)))
   2.172       tracklist)
   2.173 @@ -179,14 +244,31 @@
   2.174    
   2.175  
   2.176  
   2.177 -(defn parse-track
   2.178 +
   2.179 +(defn note?
   2.180 +  "Does the given byte correspond to a note?"
   2.181 +  [n]
   2.182 +  
   2.183 +(defn parse-ops
   2.184    "Consumes the list of opcodes, returning a runnable MIDI Sequence object."
   2.185 -  [track]
   2.186 -  (fn [midi track]
   2.187 -    (cond (empty? track) midi)))
   2.188 +  [ops]
   2.189 +  (
   2.190 +   (fn [midi ops]
   2.191 +     (let [x (first ops)]
   2.192 +       (cond (empty? ops) midi
   2.193 +             (= x 0xDA)
   2.194 +               ;; set tempo (high-low (nth ops 1)(nth ops 2))
   2.195 +             (recur (identity midi) (drop 3 ops))
   2.196 +             
   2.197 +             (note? x)
   2.198 +             
   2.199  
   2.200 +             )
   2.201  
   2.202 -
   2.203 +   (doto (Sequence. (float 30) 15) ;; 30 fps, 15 frames per beat
   2.204 +     (.createTrack))
   2.205 +   ops
   2.206 +   ))
   2.207  
   2.208  
   2.209