# HG changeset patch # User Dylan Holmes # Date 1332662009 18000 # Node ID 2e751984b42df8ba76e1fb8f550fc4c4b4b39b6a # Parent 7984a084aa072cc8b81146a0d3b08140b633ca21 making interesting data insights. diff -r 7984a084aa07 -r 2e751984b42d clojure/com/aurellem/gb/assembly.clj --- a/clojure/com/aurellem/gb/assembly.clj Sun Mar 25 02:35:14 2012 -0500 +++ b/clojure/com/aurellem/gb/assembly.clj Sun Mar 25 02:53:29 2012 -0500 @@ -415,7 +415,7 @@ "A program for altering in-game memory by pressing buttons." [] [ - 0xF3 ; stop interrupts + ;; 0xF3 ; stop interrupts ;; --------- CLEANUP 0xAF ; zero A [D31E] 0x57 ; A->D; makes D=0. @@ -437,16 +437,16 @@ 0xF5 ;; push AF (vbprev) 0xA1 ;; A & C --> A. Now A_0 contains "increment?" - - 0xCB ;; test A_0. this result will be used twice. - 0x47 + + 0x4F + 0xCB ;; test bit 0 of C. sadly, this result can't be reused below. + 0x41 0x28 ;; end frame (JUMP) if A_0 = 0. 0xF3 ;; TODO: set jump length ;; -------- GET BUTTON INPUT - ;; btw, Z bit is now 1 ;; prepare to select bits 0x3E ;; load 0x20 into A, to measure dpad @@ -466,6 +466,9 @@ 0xB0 ;; A or B --> A + 0xCB ;; check bit 0 of C + 0x41 + 0x28 ;; JUMP forward if Z=0 0x08 @@ -475,11 +478,9 @@ 0x3E ;; load 0x10 into A, to measure btns 0x10 - - 0xBF ;; compare(A,A) sets Z=0 0x18 ;; JUMP back to "load A into [FF00]" - 0xEF + 0xF0 ;; ------ TAKE ACTION BASED ON USER INPUT @@ -542,8 +543,8 @@ 0x57 0x82 ;; add the mode to A 0xEA ;; store A into "thing to execute" - 0x74 - 0xD3 + 0x74 ;; ABSOLUTE LOCATION + 0xD3 ;; ABSOLUTE LOCATION 0x3E ;; load the constant 8 into A 0x08 diff -r 7984a084aa07 -r 2e751984b42d clojure/com/aurellem/music/midi_util.clj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clojure/com/aurellem/music/midi_util.clj Sun Mar 25 02:53:29 2012 -0500 @@ -0,0 +1,233 @@ +(ns com.aurellem.music.midi-util + ;;(:import javax.sound.sampled) + (:import (javax.sound.midi MidiSystem + Sequence + Track + MidiEvent + MetaMessage + + ShortMessage) + (com.sun.media.sound FastShortMessage) + (java.io File)) + + (:use (com.aurellem.gb saves util constants gb-driver vbm items assembly characters)) + (:use (com.aurellem.run title)) + (:use (com.aurellem.exp pokemon item-bridge)) + (:import [com.aurellem.gb.gb_driver SaveState])) + + +;;; PURE MIDI MANIPULATION + +(defn midi-load + "Takes a path to a MIDI file and returns a Sequence object." + [path] + ((fn [file] + (if (.exists file) + (MidiSystem/getSequence file) + nil)) + (File. path))) + + +(defn midi-play + "Plays the MIDI file at the given location. The MIDI file runs in +the current thread until it finishes or is cancelled." + [path] + (let [midi (midi-load path)] + (if (nil? midi) nil + (let [song + (doto + (MidiSystem/getSequencer) + (.open) + (.setSequence midi) + (.start))] + (try + (loop [] + (if (. song (isRunning)) + (do + (Thread/sleep 10) + (recur)) + (throw (Exception. "Song stopped!")))) + (finally (.close song))))))) + + +(defn midi-test-1 [] + (-> (. + (midi-load + "/home/ocsenave/bk_robert/sounds/sounds/www.vgmusic.com/console/nintendo/gameboy/PkmRB-Item.mid") + (getTracks)) + + (vec) + (second) + + ((fn[trk] + (map #(. trk (get %)) + (range 0 (. trk size))))) + + ((fn [evts] + (map (juxt #(identity (.getMessage %)) #(vec (.getMessage (.getMessage %))) #(.getTick %) ) evts) + ) + + ))) + + + + + + +(defn midi-event + "Creates an event at the given tick, of the given msg-type, + containing data in coll. msg-type can be :meta, :short. If :meta, x + is the type of the msg. If :short, x is the status of the message." + [tick msg-type x coll] + + (cond (= msg-type :meta) + (MetaMessage.) + (= msg-type :short) + (ShortMessage.)) + + ) + + +(defn midi-test-2 [] + (let [sequence (Sequence. (float 30) 15)] ;; 30 fps, 15 frames per beat + (doto (. sequence (createTrack)) + (.add (MidiEvent. (MetaMessage.) 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]) +com.aurellem.music.midi-util> (midi-test-1) +([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]) + + +;;; ROM MUSIC MANIPULATION + +(def songs;; music-headers + { + :pallet 0x822E + :pkmn-center 0x8237 + :gym 0x8240 + :city-1 0x8249 ;;virian, pewter, saffron + :city-2 0x8255 ;; cerulean, fuchsia + :celedon 0x825E + :cinnibar 0x8267 + :vermilion 0x8270 + :lavender 0x827C + :ss-anne 0x8288 + :meet-prof 0x8291 + :meet-blue 0x829A + :follow 0x82A3 + :safari 0x82AF + :sfx-heal 0x82BA + :route-1 0x82C1 ;; route 1,2 + :route-2 0x82CD ;; route 24, 25 + :route-3 0x82D9 ;; route 3-10,16-22 + :route-4 0x82E5 ;; route 11-15 + :route-5 0x82F1 ;; indigo plateau + + :title 0x7C249 + :credits 0x7C255 + :hall-of-fame 0x7C25E + :lab-prof 0x7C267 + :jigglypuff 0x7C270 + :bike 0x7C276 + :surfing 0x7C282 + :casino 0x7C28B + :intro-battle 0x7C294 + :power-plant 0x7C2A0 ;; power plant, unknown dungeon + :viridian-forest 0x7C2AC ;;viridian forest, seafoam islands + :victory-rd 0x7C2B8 ;;mt moon, rock tunnel, victory rd + :mansion 0x7C2C4 + :pkmn-tower 0x7C2D0 + :silph 0x7C2D9 + :trainer-bad 0x7C2E2 + :trainer-girl 0x7C2EB + :trainer-angry 0x7C2F4 + }) + + + }) + + +(defn low-high-format + "Returns the number represented by the bytes." + [low high] + (+ low (* 256 high))) + +(defn rom-tracks + "Given a valid address to a music header, returns a list of the +data tracks" + [address] + (let [rom (rom (root)) + tracklist + ((fn extract-tracklist [mem n] + (if (= (nth mem 2) n) + (cons (low-high-format (first mem) + (second mem)) + (extract-tracklist (drop 3 mem) (inc n))) + '())) + + (take 12 (drop (inc address) rom)) + 1 + )] + + (map + (fn [trk] (take-while #(not= 0xFF %) (drop trk rom))) + tracklist) + + )) + + + +(defn parse-track + "Consumes the list of opcodes, returning a runnable MIDI Sequence object." + [track] + (fn [midi track] + (cond (empty? track) midi))) + + + + + + + + + + +;; 8237-823F Pokecenter +;; 8240-8248 Gym +;; 8249-8254 Viridian / Pewter / Saffron +;; 8255-825D Cerulean / Fuchsia +;; 825E-8266 Celedon +;; 8267-826F Cinnibar +;; 8270-827B Vermilion +;; 827C-8287 Lavender +;; 8288-8290 S.S. Anne +;; 8291-8299 Meet Prof. Oak +;; 829A-82A2 Meet Rival +;; 82A3-82AE Guy Walks you to Museum +;; 82AF-82B7 Safari Zone +;; 82B8-82C0 Pokemon get healed +;; 82C1-82CC Routes 1 / 2 +;; 82CD-82D8 Routes 24 / 25 +;; 82D9-82E4 Routes 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10 / 16 / 17 / 18 / 19 / 20 / 21 / 22 +;; 82E5-82F0 Routes 11 / 12 / 13 / 14 / 15 +;; 82F1-82FD Route 23 / Indigo Plateau +;; 7C249-7C254 Title Screen +;; 7C255-7C25D Credits +;; 7C25E-7C266 Hall of FAme Registration +;; 7C267-7C26F PRof Oak's LAb +;; 7C270-7C275 Jigglypuff's Song +;; 7C276-7C281 Bike Riding +;; 7C282-7C28A Surfing +;; 7C28B-7C293 Casino +;; 7C294-7C29F Introduction Battle +;; 7C2A0-7C2AB Power Plant / Unknown Dungeon +;; 7C2AC-7C2B7 Viridian Forest / Seafoam Islands +;; 7C2B8-7C2C3 Mt. Moon / Rock Tunnel / Victory Road +;; 7C2C4-7C2CF Cinnibar Mansion +;; 7C2D0-7C2D8 Pokemon Tower +;; 7C2D9-7C2E1 Silph Co +;; 7C2E2-7C2EA Meet Bad Trainer +;; 7C2EB-7C2F3 Meet Girl Trainer +;; 7C2F4-7C2FC Meet Angry Trainer