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