comparison clojure/com/aurellem/music/midi_util.clj @ 306:2873f50b7291

beginning to work on cry data
author Dylan Holmes <ocsenave@gmail.com>
date Sat, 31 Mar 2012 01:27:46 -0500
parents 5b59c6f17cd5
children e6a5dfd31230
comparison
equal deleted inserted replaced
305:7998b1cf18cf 306:2873f50b7291
11 (java.io File)) 11 (java.io File))
12 12
13 (:use (com.aurellem.gb saves util constants gb-driver vbm items assembly characters)) 13 (:use (com.aurellem.gb saves util constants gb-driver vbm items assembly characters))
14 (:use (com.aurellem.run title)) 14 (:use (com.aurellem.run title))
15 (:use (com.aurellem.exp pokemon item-bridge)) 15 (:use (com.aurellem.exp pokemon item-bridge))
16 (:use (com.aurellem.world practice))
16 (:import [com.aurellem.gb.gb_driver SaveState])) 17 (:import [com.aurellem.gb.gb_driver SaveState]))
17 18
18 19
19 ;;; PURE MIDI MANIPULATION 20 ;;; PURE MIDI MANIPULATION
20 21
25 (if (.exists file) 26 (if (.exists file)
26 (MidiSystem/getSequence file) 27 (MidiSystem/getSequence file)
27 nil)) 28 nil))
28 (File. path))) 29 (File. path)))
29 30
30
31
32 (def midi-play-file
33 "Plays the MIDI file at the given location. The MIDI file runs in
34 the current thread until it finishes or is cancelled."
35 (comp midi-play-seq midi-load))
36 31
37 32
38 (defn midi-play-seq 33 (defn midi-play-seq
39 "Plays the MIDI Sequence. The MIDI runs in 34 "Plays the MIDI Sequence. The MIDI runs in
40 the current thread until it finishes or is cancelled." 35 the current thread until it finishes or is cancelled."
52 (do 47 (do
53 (Thread/sleep 10) 48 (Thread/sleep 10)
54 (recur)) 49 (recur))
55 )) 50 ))
56 (finally (.close song)))))) 51 (finally (.close song))))))
52
53
54 (def midi-play-file
55 "Plays the MIDI file at the given location. The MIDI file runs in
56 the current thread until it finishes or is cancelled."
57 (comp midi-play-seq midi-load))
58
59
57 60
58 61
59 (defn midi-test-1 [] 62 (defn midi-test-1 []
60 (-> (. 63 (-> (.
61 (midi-load 64 (midi-load
152 (.add (midi-short 300 [-112 76 0])) ;; note on = -112 155 (.add (midi-short 300 [-112 76 0])) ;; note on = -112
153 (.add (midi-short 360 [-80 7 0]))) 156 (.add (midi-short 360 [-80 7 0])))
154 ;;(.add (midi-short 360 [-1 47 0])) ;; system reset = -1 157 ;;(.add (midi-short 360 [-1 47 0])) ;; system reset = -1
155 158
156 sequence 159 sequence
157 ))) 160 ))
158 161
159 162
160 163
164
165 (defn read-memory
166 ([mem start length]
167 (take length
168 (drop start
169 mem)))
170 ([start length]
171 (read-memory (rom(root)) start length)))
161 172
162 ;;; ROM MUSIC MANIPULATION 173 ;;; ROM MUSIC MANIPULATION
163 174
164 (def songs;; music-headers 175 (def songs;; music-headers
165 { 176 {
201 :silph 0x7C2D9 212 :silph 0x7C2D9
202 :trainer-bad 0x7C2E2 213 :trainer-bad 0x7C2E2
203 :trainer-girl 0x7C2EB 214 :trainer-girl 0x7C2EB
204 :trainer-angry 0x7C2F4 215 :trainer-angry 0x7C2F4
205 }) 216 })
206
207
208 })
209 217
210 218
211 (defn low-high-format 219 (defn low-high-format
212 "Returns the number represented by the bytes." 220 "Returns the number represented by the bytes."
213 [low high] 221 [low high]
243 )) 251 ))
244 252
245 253
246 254
247 255
248 (defn note? 256 ;; (defn note?
249 "Does the given byte correspond to a note?" 257 ;; "Does the given byte correspond to a note?"
250 [n] 258 ;; [n])
251 259
252 (defn parse-ops 260 ;; (comment defn parse-ops
253 "Consumes the list of opcodes, returning a runnable MIDI Sequence object." 261 ;; "Consumes the list of opcodes, returning a runnable MIDI Sequence object."
254 [ops] 262 ;; [ops]
255 ( 263 ;; (
256 (fn [midi ops] 264 ;; (fn [midi ops]
257 (let [x (first ops)] 265 ;; (let [x (first ops)]
258 (cond (empty? ops) midi 266 ;; (cond (empty? ops) midi
259 (= x 0xDA) 267 ;; (= x 0xDA)
260 ;; set tempo (high-low (nth ops 1)(nth ops 2)) 268 ;; ;; set tempo (high-low (nth ops 1)(nth ops 2))
261 (recur (identity midi) (drop 3 ops)) 269 ;; (recur (identity midi) (drop 3 ops))
262 270
263 (note? x) 271 ;; (note? x)
264 272
265 273
266 ) 274 ;; )
267 275
268 (doto (Sequence. (float 30) 15) ;; 30 fps, 15 frames per beat 276 ;; (doto (Sequence. (float 30) 15) ;; 30 fps, 15 frames per beat
269 (.createTrack)) 277 ;; (.createTrack))
270 ops 278 ;; ops
271 )) 279 ;; ))))
272 280
273 281
274 282
275 283
276 284