-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_loader.lua
473 lines (360 loc) · 12.7 KB
/
music_loader.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
local GENERIC_DEFS = require "defs.generic_defs"
local io_utils = require "io_utils.io_utils"
local randomchoice = require "randomchoice"
local gauntlet_data = require "gauntlet_data"
local base_dir = "song_extractor_cs/out/"
local patch_ext = ".songpatch"
local transpose_offset_ext = ".transposeoffsets"
local bpm_offset_ext = ".bpmoffsets"
local transpose_range = 8
local bpm_shift_range = 20
local use_fixed_music = 0
GENERIC_DEFS.ENABLE_MUSIC_PATCHING = 1
--function file_exists(name)
--print(io.popen"cd":read'*l')
-- print("Trying to open file: " .. name)
-- local f=io.open(name,"r")
-- if f~=nil then io.close(f) return true else return false end
--end
function try_open_file(path, mode)
local file = io.open(path, mode)
print("Trying to open file: " .. path)
if file == nil then
file = io.open("Lua/mmbn3_draft_gauntlet/" .. path, mode)
end
if file == nil then
file = io.open("Lua/mmbn3-draft-gauntlet-master/" .. path, mode)
end
if file == nil then
file = io.open("Lua/mmbn3_draft_gauntlet/" .. path, mode)
end
if file == nil then
file = io.open("../lua/" .. path, mode)
end
if file == nil then
file = io.open("../bizhawk/" .. path, mode)
end
if file == nil then
file = io.open("../mmbn3_draft_gauntlet/" .. path, mode)
end
if file == nil then
file = io.open("../mmbn3-draft-gauntlet-master/" .. path, mode)
end
if file == nil then
print("Could not read input file " .. path .. "!")
return nil
end
return file
end
local BattleMusicList =
{
"mmbn/0", -- main theme
"mmbn/8", -- fire field
"mmbn/9", -- boundless network
"mmbn/12", -- elecrical crisis
"mmbn/14", -- battle theme
"mmbn2/0", -- main theme
"mmbn2/12", -- decision is inside of me
"mmbn2/18", -- smoky field
"mmbn2/19", -- time limit
"mmbn2/23", -- you can't go back
"mmbn2/24", -- internet world
"mmbn2/27", -- virus busting
"mmbn3/0", -- main theme
"mmbn3/10", -- n1 grand prix
"mmbn3/19", -- save a life
"mmbn3/21", -- final transmission
"mmbn3/22", -- network is spreading
"mmbn3/23", -- dangerous black
"mmbn3/24", -- shine in the dark
"mmbn3/25", -- shooting enemy
"mmbn3/37", -- navi customizer
"mmbn4/0", -- main theme
"mmbn4/15", -- invisible wing
"mmbn4/17", -- save our planet
"mmbn4/18", -- global network
"mmbn4/20", -- cyber battle
"mmbn4_5/64", -- life in the network
"mmbn4_5/98", -- battlefield
"mmbn5/1", -- main theme
"mmbn5/18", -- a total war
"mmbn5/21", -- battle start!
"mmbn5/34", -- liberate mission
"mmbn6/1", -- main theme
"mmbn6/10", -- break the storm
"mmbn6/14", -- hero!
"mmbn6/16", -- blast speed
"mmbn6/19", -- digital strider
"mmbn6/21", -- battle field
"mmbn6/32", -- two of braves
--"mmbcc/1", -- main theme
--"mmbcc/12", -- battle bgm 1
--"mmbcc/13", -- battle bgm 2
"mm_and_bass/0", -- opening stage
--"mm_and_bass/1", -- stage start
--"mm_and_bass/2", -- stage select
--"mm_and_bass/3", -- player select
--"mm_and_bass/4", -- data select
--"mm_and_bass/5", -- title
"mm_and_bass/6", -- dynamo man stage
"mm_and_bass/7", -- cold man stage
"mm_and_bass/8", -- ground man stage
"mm_and_bass/9", -- tengu man stage
"mm_and_bass/10", -- astro man stage
--"mm_and_bass/11", -- pirate man stage -> this glitches out in the second part, not sure why
"mm_and_bass/12", -- burner man stage
"mm_and_bass/13", -- magic man stage
"mm_and_bass/16", -- crystal gate stage
--"mm_and_bass/18", -- get a weapon
--"mm_and_bass/19", -- stage clear
--"mm_and_bass/20", -- king castle
"mm_and_bass/21", -- shop
"mm_and_bass/22", -- data base
--"mm_and_bass/23", -- game over
--"mm_and_bass/24", -- ending
}
local FixedMusicList =
{
"mmbn3/25",
"mmbn3/25",
"mmbn3/25",
"mmbn3/25",
"mmbn3/26",
"mmbn2/27",
"mmbn4/21",
"mmbn4/17",
"mmbn4/17",
"poke_emerald/511",
}
local FixedMusicTransposeRange =
{
0,
0,
0,
0,
0,
0,
0,
0,
5,
0,
}
local FixedMusicBPMRange =
{
0,
0,
0,
0,
0,
0,
0,
0,
30,
0,
}
---local BattleMusicList =
--{
-- "poke_emerald/299", -- cool battle theme (Battle 6)
--}
local BossMusicList =
{
"mmbn/15", -- boss battle theme
"mmbn/16", -- vs. life virus
"mmbn2/28", -- battle spirit
"mmbn2/29", -- vs. gospel
"mmbn3/26", -- boss battle
"mmbn3/27", -- vs. alpha
"mmbn3/28", -- great battlers
"mmbn4/21", -- fighting oneself
"mmbn4/22", -- vs. duo
"mmbn4/23", -- battle pressure
"mmbn4_5/89", -- tournament battle
"mmbn4_5/99", -- the fighter's soul
"mmbn5/22", -- powerful enemy
"mmbn5/23", -- vs. nebula gray
"mmbn6/22", -- surge of power!
"mmbn6/23", -- decisive battle, cyber beasts!
"mmbn6/34", -- ??? (seriously, I didn't find this in the rockman.exe sound box)
"mm_and_bass/14", -- king stage
"mm_and_bass/15", -- wily stage
"mm_and_bass/17", -- king/wily battle
"mm_and_bass/25", -- boss battle
--"mmbcc/14", -- battle bgm 3
}
local MusicLoader =
{
FinishedLoading = 0,
StartedLoading = 0,
BlockSize = 8000,
transpose_offset = 1,
bpm_offset = 3,
transpose = 4,
bpm_shift = 10,
offset = 0,
patch_str = nil
}
function mysplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function MusicLoader.LoadRandomFile(current_round)
if GENERIC_DEFS.ENABLE_MUSIC_PATCHING == 0 then
MusicLoader.FinishedLoading = 1
return
end
if MusicLoader.StartedLoading == 1 then
return
end
local chosen_file = ""
if current_round % 5 == 0 then
chosen_file = BossMusicList[gauntlet_data.math.random_music(1, #BossMusicList)]--randomchoice(BossMusicList)
else
chosen_file = BattleMusicList[gauntlet_data.math.random_music(1, #BattleMusicList)]--randomchoice(BattleMusicList)
end
if use_fixed_music == 1 then
if FixedMusicList[current_round] ~= nil then
chosen_file = FixedMusicList[current_round]
transpose_range = FixedMusicTransposeRange[current_round]
bpm_shift_range = FixedMusicBPMRange[current_round]
else
transpose_range = 8
bpm_shift_range = 20
end
end
print("Loading music for round " .. tostring(current_round) .. ": " .. chosen_file)
MusicLoader.FinishedLoading = 0
MusicLoader.offset = 0
MusicLoader.patch_str = nil
local patch_file_name = base_dir .. chosen_file .. patch_ext
local transpose_offset_file_name = base_dir .. chosen_file .. transpose_offset_ext
local bpm_offset_file_name = base_dir .. chosen_file .. bpm_offset_ext
local patch_file = try_open_file(patch_file_name, "rb")
local transpose_offset_file = try_open_file(transpose_offset_file_name, "r")
local bpm_offset_file = try_open_file(bpm_offset_file_name, "r")
if patch_file == nil or
transpose_offset_file == nil or
bpm_offset_file == nil or
GLOBAL_DISABLE_MUSIC_PATCHING == true then
print ("Turning off Music Loader, file: " .. chosen_file .. " could not be opened!")
GENERIC_DEFS.ENABLE_MUSIC_PATCHING = 0
MusicLoader.FinishedLoading = 1
return
end
-- Check if our ROM was patched
local memory_size = memory.getmemorydomainsize("ROM")
local required_music_memory_size = (GENERIC_DEFS.MUSIC_PATCH_ADDRESS - 0x08000000) + 0x00200000
--print("Memory size: " .. memory_size)
--print("required MUSIC_PATCH_ADDRESS size: " .. required_music_memory_size)
if memory_size < required_music_memory_size then
print ("Turning off Music Loader, ROM was not patched to be large enough!")
GENERIC_DEFS.ENABLE_MUSIC_PATCHING = 0
MusicLoader.FinishedLoading = 1
return
end
--local patch_file = assert(io.open(patch_file_name, "rb"))
MusicLoader.patch_str = patch_file:read("*all")
--print(#MusicLoader.patch_str)
patch_file:close()
--local transpose_offset_file = assert(io.open(transpose_offset_file_name, "r"))
local transpose_offset_string = ""
repeat
local str = transpose_offset_file:read(20*1024)
if str then
transpose_offset_string = transpose_offset_string .. str
end
until not str
transpose_offset_file:close()
transpose_offsets_split = mysplit(transpose_offset_string, '\n')
transpose_offsets_split[#transpose_offsets_split + 1] = 2147483647
--local bpm_offset_file = assert(io.open(bpm_offset_file_name, "r"))
local bpm_offset_string = ""
repeat
local str = bpm_offset_file:read(20*1024)
if str then
bpm_offset_string = bpm_offset_string .. str
end
until not str
bpm_offset_file:close()
bpm_offsets_split = mysplit(bpm_offset_string, '\n')
bpm_offsets_split[#bpm_offsets_split + 1] = 2147483647
--print(transpose_offsets_split)
--print(bpm_offsets_split)
MusicLoader.transpose = gauntlet_data.math.random_music(-transpose_range, transpose_range)--math.random(-transpose_range, transpose_range)
MusicLoader.bpm_shift = gauntlet_data.math.random_music(-bpm_shift_range, bpm_shift_range)--math.random(-bpm_shift_range, bpm_shift_range)
print("Music transpose / bpm shifts: " .. tostring(MusicLoader.transpose) .. " / " .. tostring(MusicLoader.bpm_shift))
MusicLoader.transpose_offsets = {}
MusicLoader.bpm_offsets = {}
for k, offset_str in ipairs(transpose_offsets_split) do
local offset = tonumber(offset_str)
MusicLoader.transpose_offsets[offset] = 1
end
for k, offset_str in ipairs(bpm_offsets_split) do
local offset = tonumber(offset_str)
MusicLoader.bpm_offsets[offset] = 1
end
MusicLoader.StartedLoading = 1
end
function MusicLoader.LoadBlock()
if GENERIC_DEFS.ENABLE_MUSIC_PATCHING == 0 then
MusicLoader.FinishedLoading = 1
return
end
if MusicLoader.FinishedLoading == 1 then
return
end
if MusicLoader.patch_str == nil then
return
end
if MusicLoader.offset == 0 then
print("Starting to load music!")
end
local info_interval = math.floor((#MusicLoader.patch_str / 25) + 0.5)
local yield_interval = MusicLoader.BlockSize
--MusicLoader.BlockSize = yield_interval
--print("Interval: " .. tostring(info_interval))
for i = 1,MusicLoader.BlockSize do
if MusicLoader.offset >= #MusicLoader.patch_str then
print("Loading done!")
MusicLoader.FinishedLoading = 1
MusicLoader.StartedLoading = 0
return 1
end
c = string.byte(MusicLoader.patch_str:sub(MusicLoader.offset + 1, MusicLoader.offset + 1))
--print(c)
--emu.yield()
if MusicLoader.bpm_offsets[MusicLoader.offset] ~= nil then
--print("BPM change " .. tostring(offset))
--print(c)
--print(c + bpm_shift)
io_utils.writebyte(GENERIC_DEFS.MUSIC_PATCH_ADDRESS + MusicLoader.offset, c + MusicLoader.bpm_shift)
--print("BPM changed. " .. tostring(offset))
elseif MusicLoader.transpose_offsets[MusicLoader.offset] ~= nil then
--print("Transpose change " .. tostring(offset))
--print(c)
--print(c + transpose)
io_utils.writebyte(GENERIC_DEFS.MUSIC_PATCH_ADDRESS + MusicLoader.offset, c + MusicLoader.transpose)
--print("Transpose changed. " .. tostring(offset))
else
io_utils.writebyte(GENERIC_DEFS.MUSIC_PATCH_ADDRESS + MusicLoader.offset, c)
end
--io_utils.writebyte(GENERIC_DEFS.MUSIC_PATCH_ADDRESS + offset, c)
MusicLoader.offset = MusicLoader.offset + 1
if (MusicLoader.offset + 1) % info_interval == 0 then
print("Music loading " .. tostring(math.floor((MusicLoader.offset / (1.0 * #MusicLoader.patch_str) * 100.0) + 0.5)) .. "% done.")
end
end
if MusicLoader.offset >= #MusicLoader.patch_str then
print("Loading done!")
MusicLoader.FinishedLoading = 1
MusicLoader.StartedLoading = 0
return 1
end
end
return MusicLoader