annotate clojure/com/aurellem/music/midi_util.clj @ 284:57e0314e488d

script: bought 96 burn heals
author Robert McIntyre <rlm@mit.edu>
date Wed, 28 Mar 2012 05:08:24 -0500
parents 5b59c6f17cd5
children 2873f50b7291
rev   line source
ocsenave@242 1 (ns com.aurellem.music.midi-util
ocsenave@242 2 ;;(:import javax.sound.sampled)
ocsenave@242 3 (:import (javax.sound.midi MidiSystem
ocsenave@242 4 Sequence
ocsenave@242 5 Track
ocsenave@242 6 MidiEvent
ocsenave@242 7 MetaMessage
ocsenave@242 8
ocsenave@242 9 ShortMessage)
ocsenave@242 10 (com.sun.media.sound FastShortMessage)
ocsenave@242 11 (java.io File))
ocsenave@242 12
ocsenave@242 13 (:use (com.aurellem.gb saves util constants gb-driver vbm items assembly characters))
ocsenave@242 14 (:use (com.aurellem.run title))
ocsenave@242 15 (:use (com.aurellem.exp pokemon item-bridge))
ocsenave@242 16 (:import [com.aurellem.gb.gb_driver SaveState]))
ocsenave@242 17
ocsenave@242 18
ocsenave@242 19 ;;; PURE MIDI MANIPULATION
ocsenave@242 20
ocsenave@242 21 (defn midi-load
ocsenave@242 22 "Takes a path to a MIDI file and returns a Sequence object."
ocsenave@242 23 [path]
ocsenave@242 24 ((fn [file]
ocsenave@242 25 (if (.exists file)
ocsenave@242 26 (MidiSystem/getSequence file)
ocsenave@242 27 nil))
ocsenave@242 28 (File. path)))
ocsenave@242 29
ocsenave@242 30
ocsenave@243 31
ocsenave@243 32 (def midi-play-file
ocsenave@242 33 "Plays the MIDI file at the given location. The MIDI file runs in
ocsenave@242 34 the current thread until it finishes or is cancelled."
ocsenave@243 35 (comp midi-play-seq midi-load))
ocsenave@243 36
ocsenave@243 37
ocsenave@243 38 (defn midi-play-seq
ocsenave@243 39 "Plays the MIDI Sequence. The MIDI runs in
ocsenave@243 40 the current thread until it finishes or is cancelled."
ocsenave@243 41 [midi]
ocsenave@242 42 (if (nil? midi) nil
ocsenave@242 43 (let [song
ocsenave@242 44 (doto
ocsenave@242 45 (MidiSystem/getSequencer)
ocsenave@242 46 (.open)
ocsenave@242 47 (.setSequence midi)
ocsenave@242 48 (.start))]
ocsenave@242 49 (try
ocsenave@242 50 (loop []
ocsenave@242 51 (if (. song (isRunning))
ocsenave@242 52 (do
ocsenave@242 53 (Thread/sleep 10)
ocsenave@242 54 (recur))
ocsenave@243 55 ))
ocsenave@243 56 (finally (.close song))))))
ocsenave@242 57
ocsenave@242 58
ocsenave@242 59 (defn midi-test-1 []
ocsenave@242 60 (-> (.
ocsenave@242 61 (midi-load
ocsenave@242 62 "/home/ocsenave/bk_robert/sounds/sounds/www.vgmusic.com/console/nintendo/gameboy/PkmRB-Item.mid")
ocsenave@242 63 (getTracks))
ocsenave@242 64
ocsenave@242 65 (vec)
ocsenave@243 66 (nth 1)
ocsenave@242 67
ocsenave@242 68 ((fn[trk]
ocsenave@242 69 (map #(. trk (get %))
ocsenave@242 70 (range 0 (. trk size)))))
ocsenave@242 71
ocsenave@242 72 ((fn [evts]
ocsenave@243 73 (map (juxt #(.getTick %) #(vec (.getMessage (.getMessage
ocsenave@243 74 %))) #(.getMessage %) ) evts)
ocsenave@242 75 )
ocsenave@242 76
ocsenave@242 77 )))
ocsenave@242 78
ocsenave@242 79
ocsenave@242 80
ocsenave@242 81
ocsenave@242 82
ocsenave@243 83 (defn midi-short
ocsenave@243 84 "Creates a MIDI event containing a ShortMessage."
ocsenave@243 85 [tick [status & ns]]
ocsenave@243 86 (MidiEvent.
ocsenave@243 87 (apply
ocsenave@243 88 (fn
ocsenave@243 89 ([x] (doto (ShortMessage.) (.setMessage x)))
ocsenave@243 90 ([x y] (doto (ShortMessage.) (.setMessage x y 0)))
ocsenave@243 91 ([x y z] (doto (ShortMessage.) (.setMessage x y z)))
ocsenave@243 92 ([x y z w] (doto (ShortMessage.) (.setMessage x y z w))))
ocsenave@243 93 status
ocsenave@243 94 ns)
ocsenave@243 95 tick))
ocsenave@242 96
ocsenave@243 97 (defn midi-meta
ocsenave@243 98 "Creates a MIDI event containing a MetaMessage"
ocsenave@243 99 [tick type ns]
ocsenave@243 100 (MidiEvent.
ocsenave@243 101 (doto (MetaMessage.)
ocsenave@243 102 (.setMessage type
ocsenave@243 103 (byte-array (map byte ns))
ocsenave@243 104 (count ns)))
ocsenave@243 105 tick))
ocsenave@242 106
ocsenave@243 107
ocsenave@243 108
ocsenave@243 109 (defn sign
ocsenave@243 110 "Interpret the bits of n as a signed two's-complement byte"
ocsenave@243 111 [n]
ocsenave@243 112 (if (>= n 128) (- n 256)
ocsenave@243 113 n))
ocsenave@243 114
ocsenave@243 115 (defn unsign
ocsenave@243 116 "Interpret the bits as an unsigned byte."
ocsenave@243 117 [n]
ocsenave@243 118 (if (neg? n) (+ n 256) n))
ocsenave@243 119
ocsenave@243 120
ocsenave@242 121
ocsenave@242 122
ocsenave@242 123 (defn midi-test-2 []
ocsenave@243 124 (let [sequence (Sequence. (float 30) 12)] ;; 30 fps, 10 frames per beat
ocsenave@242 125 (doto (. sequence (createTrack))
ocsenave@243 126 (.add (midi-meta 0 3 [-1 3 33 79 114 105 103 105 110 97 108 32
ocsenave@243 127 99 111 109 112 111 115 101 114 58 32 74 117 110 105 99 104 105 32 77
ocsenave@243 128 97 115 117 100 97]))
ocsenave@242 129
ocsenave@243 130 (.add (midi-short 0 [-80 0 0])) ;; control change = -80
ocsenave@243 131 (.add (midi-short 0 [-80 7 100])) ;; control change, volume, 100
ocsenave@243 132 (.add (midi-short 0 [-80 10 64])) ;; control change, pan, 64 (middle?)
ocsenave@243 133 (.add (midi-short 0 [-80 32 0])) ;; ctrl chg, LSB ctrl 0 = bank 0
ocsenave@243 134 (.add (midi-short 0 [-64 01])) ;; program/instrument change = -64
ocsenave@243 135 (.add (midi-short 0 [-80 101 0]))
ocsenave@243 136 (.add (midi-short 1 [-80 100 0]))
ocsenave@243 137 (.add (midi-short 2 [-80 6 2]))
ocsenave@243 138 (.add (midi-short 3 [-80 38 0]))
ocsenave@243 139 (.add (midi-short 3 [-32 0 56])) ;; pitch bend?! = -32
ocsenave@243 140 (.add (midi-short 3 [-112 68 100])) ;; note on = -112
ocsenave@243 141 (.add (midi-short 20 [-112 68 0]))
ocsenave@243 142 (.add (midi-short 40 [-112 68 100]))
ocsenave@243 143 (.add (midi-short 60 [-112 68 0]))
ocsenave@243 144 (.add (midi-short 80 [-112 68 100]))
ocsenave@243 145 (.add (midi-short 100 [-112 68 0]))
ocsenave@243 146 (.add (midi-short 120 [-80 7 100])) ;; control change
ocsenave@243 147 (.add (midi-short 120 [-112 76 100])) ;; note-on
ocsenave@243 148 (.add (midi-short 181 [-80 7 90])) ;; control change
ocsenave@243 149 (.add (midi-short 209 [-80 7 80])) ;; control change
ocsenave@243 150 (.add (midi-short 240 [-80 7 65])) ;; control change
ocsenave@243 151 (.add (midi-short 270 [-80 7 50])) ;; control change
ocsenave@243 152 (.add (midi-short 300 [-112 76 0])) ;; note on = -112
ocsenave@243 153 (.add (midi-short 360 [-80 7 0])))
ocsenave@243 154 ;;(.add (midi-short 360 [-1 47 0])) ;; system reset = -1
ocsenave@242 155
ocsenave@243 156 sequence
ocsenave@243 157 )))
ocsenave@242 158
ocsenave@243 159
ocsenave@242 160
ocsenave@242 161
ocsenave@242 162 ;;; ROM MUSIC MANIPULATION
ocsenave@242 163
ocsenave@242 164 (def songs;; music-headers
ocsenave@242 165 {
ocsenave@242 166 :pallet 0x822E
ocsenave@242 167 :pkmn-center 0x8237
ocsenave@242 168 :gym 0x8240
ocsenave@242 169 :city-1 0x8249 ;;virian, pewter, saffron
ocsenave@242 170 :city-2 0x8255 ;; cerulean, fuchsia
ocsenave@242 171 :celedon 0x825E
ocsenave@242 172 :cinnibar 0x8267
ocsenave@242 173 :vermilion 0x8270
ocsenave@242 174 :lavender 0x827C
ocsenave@242 175 :ss-anne 0x8288
ocsenave@242 176 :meet-prof 0x8291
ocsenave@242 177 :meet-blue 0x829A
ocsenave@242 178 :follow 0x82A3
ocsenave@242 179 :safari 0x82AF
ocsenave@242 180 :sfx-heal 0x82BA
ocsenave@242 181 :route-1 0x82C1 ;; route 1,2
ocsenave@242 182 :route-2 0x82CD ;; route 24, 25
ocsenave@242 183 :route-3 0x82D9 ;; route 3-10,16-22
ocsenave@242 184 :route-4 0x82E5 ;; route 11-15
ocsenave@242 185 :route-5 0x82F1 ;; indigo plateau
ocsenave@242 186
ocsenave@242 187 :title 0x7C249
ocsenave@242 188 :credits 0x7C255
ocsenave@242 189 :hall-of-fame 0x7C25E
ocsenave@242 190 :lab-prof 0x7C267
ocsenave@242 191 :jigglypuff 0x7C270
ocsenave@242 192 :bike 0x7C276
ocsenave@242 193 :surfing 0x7C282
ocsenave@242 194 :casino 0x7C28B
ocsenave@242 195 :intro-battle 0x7C294
ocsenave@242 196 :power-plant 0x7C2A0 ;; power plant, unknown dungeon
ocsenave@242 197 :viridian-forest 0x7C2AC ;;viridian forest, seafoam islands
ocsenave@242 198 :victory-rd 0x7C2B8 ;;mt moon, rock tunnel, victory rd
ocsenave@242 199 :mansion 0x7C2C4
ocsenave@242 200 :pkmn-tower 0x7C2D0
ocsenave@242 201 :silph 0x7C2D9
ocsenave@242 202 :trainer-bad 0x7C2E2
ocsenave@242 203 :trainer-girl 0x7C2EB
ocsenave@242 204 :trainer-angry 0x7C2F4
ocsenave@242 205 })
ocsenave@242 206
ocsenave@242 207
ocsenave@242 208 })
ocsenave@242 209
ocsenave@242 210
ocsenave@242 211 (defn low-high-format
ocsenave@242 212 "Returns the number represented by the bytes."
ocsenave@242 213 [low high]
ocsenave@242 214 (+ low (* 256 high)))
ocsenave@243 215 (defn high-low-format
ocsenave@243 216 "Returns the number represented by the bytes."
ocsenave@243 217 [high low]
ocsenave@243 218 (+ low (* 256 high)))
ocsenave@243 219
ocsenave@242 220
ocsenave@242 221 (defn rom-tracks
ocsenave@242 222 "Given a valid address to a music header, returns a list of the
ocsenave@242 223 data tracks"
ocsenave@242 224 [address]
ocsenave@242 225 (let [rom (rom (root))
ocsenave@242 226 tracklist
ocsenave@242 227 ((fn extract-tracklist [mem n]
ocsenave@242 228 (if (= (nth mem 2) n)
ocsenave@242 229 (cons (low-high-format (first mem)
ocsenave@242 230 (second mem))
ocsenave@242 231 (extract-tracklist (drop 3 mem) (inc n)))
ocsenave@242 232 '()))
ocsenave@242 233
ocsenave@242 234 (take 12 (drop (inc address) rom))
ocsenave@242 235 1
ocsenave@242 236 )]
ocsenave@242 237
ocsenave@243 238 tracklist
ocsenave@242 239 (map
ocsenave@242 240 (fn [trk] (take-while #(not= 0xFF %) (drop trk rom)))
ocsenave@242 241 tracklist)
ocsenave@242 242
ocsenave@242 243 ))
ocsenave@242 244
ocsenave@242 245
ocsenave@242 246
ocsenave@243 247
ocsenave@243 248 (defn note?
ocsenave@243 249 "Does the given byte correspond to a note?"
ocsenave@243 250 [n]
ocsenave@243 251
ocsenave@243 252 (defn parse-ops
ocsenave@242 253 "Consumes the list of opcodes, returning a runnable MIDI Sequence object."
ocsenave@243 254 [ops]
ocsenave@243 255 (
ocsenave@243 256 (fn [midi ops]
ocsenave@243 257 (let [x (first ops)]
ocsenave@243 258 (cond (empty? ops) midi
ocsenave@243 259 (= x 0xDA)
ocsenave@243 260 ;; set tempo (high-low (nth ops 1)(nth ops 2))
ocsenave@243 261 (recur (identity midi) (drop 3 ops))
ocsenave@243 262
ocsenave@243 263 (note? x)
ocsenave@243 264
ocsenave@242 265
ocsenave@243 266 )
ocsenave@242 267
ocsenave@243 268 (doto (Sequence. (float 30) 15) ;; 30 fps, 15 frames per beat
ocsenave@243 269 (.createTrack))
ocsenave@243 270 ops
ocsenave@243 271 ))
ocsenave@242 272
ocsenave@242 273
ocsenave@242 274
ocsenave@242 275
ocsenave@242 276
ocsenave@242 277
ocsenave@242 278
ocsenave@242 279 ;; 8237-823F Pokecenter
ocsenave@242 280 ;; 8240-8248 Gym
ocsenave@242 281 ;; 8249-8254 Viridian / Pewter / Saffron
ocsenave@242 282 ;; 8255-825D Cerulean / Fuchsia
ocsenave@242 283 ;; 825E-8266 Celedon
ocsenave@242 284 ;; 8267-826F Cinnibar
ocsenave@242 285 ;; 8270-827B Vermilion
ocsenave@242 286 ;; 827C-8287 Lavender
ocsenave@242 287 ;; 8288-8290 S.S. Anne
ocsenave@242 288 ;; 8291-8299 Meet Prof. Oak
ocsenave@242 289 ;; 829A-82A2 Meet Rival
ocsenave@242 290 ;; 82A3-82AE Guy Walks you to Museum
ocsenave@242 291 ;; 82AF-82B7 Safari Zone
ocsenave@242 292 ;; 82B8-82C0 Pokemon get healed
ocsenave@242 293 ;; 82C1-82CC Routes 1 / 2
ocsenave@242 294 ;; 82CD-82D8 Routes 24 / 25
ocsenave@242 295 ;; 82D9-82E4 Routes 3 / 4 / 5 / 6 / 7 / 8 / 9 / 10 / 16 / 17 / 18 / 19 / 20 / 21 / 22
ocsenave@242 296 ;; 82E5-82F0 Routes 11 / 12 / 13 / 14 / 15
ocsenave@242 297 ;; 82F1-82FD Route 23 / Indigo Plateau
ocsenave@242 298 ;; 7C249-7C254 Title Screen
ocsenave@242 299 ;; 7C255-7C25D Credits
ocsenave@242 300 ;; 7C25E-7C266 Hall of FAme Registration
ocsenave@242 301 ;; 7C267-7C26F PRof Oak's LAb
ocsenave@242 302 ;; 7C270-7C275 Jigglypuff's Song
ocsenave@242 303 ;; 7C276-7C281 Bike Riding
ocsenave@242 304 ;; 7C282-7C28A Surfing
ocsenave@242 305 ;; 7C28B-7C293 Casino
ocsenave@242 306 ;; 7C294-7C29F Introduction Battle
ocsenave@242 307 ;; 7C2A0-7C2AB Power Plant / Unknown Dungeon
ocsenave@242 308 ;; 7C2AC-7C2B7 Viridian Forest / Seafoam Islands
ocsenave@242 309 ;; 7C2B8-7C2C3 Mt. Moon / Rock Tunnel / Victory Road
ocsenave@242 310 ;; 7C2C4-7C2CF Cinnibar Mansion
ocsenave@242 311 ;; 7C2D0-7C2D8 Pokemon Tower
ocsenave@242 312 ;; 7C2D9-7C2E1 Silph Co
ocsenave@242 313 ;; 7C2E2-7C2EA Meet Bad Trainer
ocsenave@242 314 ;; 7C2EB-7C2F3 Meet Girl Trainer
ocsenave@242 315 ;; 7C2F4-7C2FC Meet Angry Trainer