fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
f7324a4
settings-overhaul
1local music ={ 2PLAYLIST ={} , 3PLAYLIST_ID =- 1 , 4PLAYING =nil , 5FINISHED =true , 6LOOP =false , 7USE_WEIGHTS =false , 8TRACK_ID ={} , 9RNG_SEED =nil , 10} 11 12local log =require ( "log" ) 13local melee =require ( "melee" ) 14local memory =require ( "memory" ) 15local notification =require ( "notification" ) 16local overlay =require ( "overlay" ) 17local wav =require ( "wav" ) 18 19require ( "extensions.math" ) 20 21-- Given a list of {element, weight} pairs, do a weighted random sample 22-- of the elements (returns an index, not the element itself) 23local function weightedRandomChoice ( list ) 24if not list or # list == 0 then return nil end 25if # list == 1 then return 1 end 26 27local sum =0 28for _ , v in pairs ( list ) do 29sum =sum + v . WEIGHT 30end 31 32local choice =math . random ( 1 , sum ) 33for k , v in pairs ( list ) do 34choice =choice - v . WEIGHT 35if choice <= 0 then 36return k 37end 38end 39return choice 40end 41 42local function moveFolderContentsTo ( from , to ) 43local info =love . filesystem . getInfo ( from ) 44 45if info and info . type == "directory" then 46-- Upgrade to new folder layout 47local configdir =love . filesystem . getSaveDirectory () 48 49local files =love . filesystem . getDirectoryItems ( from ) 50for k , file in ipairs ( files ) do 51os . rename (( "%s/%s/%s" ): format ( configdir , from , file ), ( "%s/%s/%s" ): format ( configdir , to , file )) 52end 53 54love . filesystem . remove ( from ) 55end 56end 57 58function music . update () 59if not music . PLAYING or music . FINISHED then return end 60 61local duration =music . PLAYING . STREAM : getDuration ( "samples" ) 62local position =music . PLAYING . STREAM : tell ( "samples" ) 63 64if music . LOOP then 65local finished =not music . PLAYING . STREAM : isPlaying () 66 67if finished then 68-- If the song is no longer playing, that means it reached the end 69-- Immediately play it again 70log . debug ( "[MUSIC] End of song reached, replaying" ) 71music . PLAYING . STREAM : play () 72end 73 74local info =music . PLAYING . WAV 75 76if info then 77for k , loop in pairs ( info . loops ) do 78if finished or position >= loop . sample_end then 79log . debug ( "[MUSIC] Loop point reached, seeking to %d" , loop . sample_start ) 80-- If the song finished playing or reached the looping point, loop back to the start? 81music . PLAYING . STREAM : seek ( loop . sample_start , "samples" ) 82break -- Only handle the first loop for now.. 83end 84end 85end 86else 87if not music . PLAYING . STREAM : isPlaying () or position >= duration then 88-- We mark that the song has completed, allowing the next game frame hook to play the next song in the playlist 89music . FINISHED =true 90end 91end 92end 93 94function music . init () 95love . filesystem . createDirectory ( "Melee" ) 96 97local info =love . filesystem . getInfo ( "Stage Music" ) 98 99if info and info . type == "directory" then 100-- Upgrade to new folder layout 101local configdir =love . filesystem . getSaveDirectory () 102os . rename (( "%s/Stage Music" ): format ( configdir ), ( "%s/Melee/Stage Music" ): format ( configdir )) 103end 104 105local info =love . filesystem . getInfo ( "Melee/Stage Music/Menu" ) 106 107if info and info . type == "directory" then 108-- Upgrade to new folder layout 109local configdir =love . filesystem . getSaveDirectory () 110os . rename (( "%s/Melee/Stage Music/Menu" ): format ( configdir ), ( "%s/Melee/Menu Music" ): format ( configdir )) 111else 112love . filesystem . createDirectory ( "Melee/Menu Music" ) 113end 114 115-- VS mode folders 116 117for stageid , name in pairs ( melee . getAllStages ()) do 118love . filesystem . createDirectory (( "Melee/Stage Music/%s" ): format ( name )) 119end 120moveFolderContentsTo ( "Melee/Stage Music/All" , "Melee/Stage Music" ) 121 122-- Single player folders 123 124love . filesystem . createDirectory ( "Melee/Single Player Music" ) 125 126for stageid , name in pairs ( melee . getSinglePlayerStages ()) do 127love . filesystem . createDirectory (( "Melee/Single Player Music/%s" ): format ( name )) 128end 129 130-- Series folders 131 132for stageid , series in pairs ( melee . getAllStageSeries ()) do 133love . filesystem . createDirectory (( "Melee/Series Music/%s" ): format ( series )) 134end 135moveFolderContentsTo ( "Melee/Series Music/All" , "Melee/Series Music" ) 136 137love . filesystem . createDirectory ( "Melee/Single Player Music/Break the Targets" ) 138 139for stageid , name in pairs ( melee . getAkaneiaStages ()) do 140love . filesystem . createDirectory (( "Melee/Akaneia Stage Music/%s" ): format ( name )) 141end 142for stageid , name in pairs ( melee . getBeyondMeleeStages ()) do 143love . filesystem . createDirectory (( "Melee/Beyond Melee Stage Music/%s" ): format ( name )) 144end 145end 146 147function music . kill () 148if music . PLAYING then 149if music . PLAYING . STREAM then 150if music . PLAYING . STREAM : isPlaying () then 151music . PLAYING . STREAM : stop () 152music . FINISHED =true 153end 154music . PLAYING . STREAM : release () 155end 156music . PLAYING =nil 157end 158end 159 160local LOOPING_OFF =1 161local LOOPING_MENU =2 162local LOOPING_STAGE =3 163local LOOPING_ALL =4 164 165-- Set to true when the announcer says 'GAME!' 166local MATCH_SOFT_END =false 167 168function music . shouldPlayMusic () 169return melee . isInMenus () or ( melee . isInGame () and not melee . matchFinsihed () and not MATCH_SOFT_END ) 170end 171 172function music . onLoopChange ( mode ) 173if music . PLAYING and music . PLAYING . STREAM : isPlaying () then 174local loop =false 175-- Handle the different loop settings properly 176if mode == LOOPING_MENU and melee . isInMenus () then 177loop =true 178elseif mode == LOOPING_STAGE and melee . isInGame () then 179loop =true 180elseif mode == LOOPING_ALL then 181loop =true 182end 183--music.PLAYING.STREAM:setLooping(loop) 184music . LOOP =loop 185end 186end 187 188function music . getVolume () 189return PANEL_SETTINGS : GetVolume () 190end 191 192local LOADED =false 193local ALLOW_INGAME_VOLUME =false 194 195memory . hook ( "OnGameClosed" , "Set state unloaded" , function () 196LOADED =false 197end ) 198 199memory . hook ( "menu.major" , "Load Initial Volume" , function ( menu ) 200if memory . isMelee () and ALLOW_INGAME_VOLUME and not LOADED and menu == MENU_MAIN_MENU then 201LOADED =true 202-- Set the games music value to our value 203music . setVolume ( music . getVolume ()) 204end 205end ) 206 207memory . hook ( "volume.slider" , "Ingame Volume Adjust" , function ( volume ) 208if memory . isMelee () and ALLOW_INGAME_VOLUME and LOADED then 209PANEL_SETTINGS : SetVolume ( 100 - volume ) 210end 211end ) 212 213function music . refreshRNGseed () 214local getSeed =function () 215if not music . RNG_SEED or not memory . isMelee () then return os . time (), "the system time" end 216if melee . isInMenus () then return music . RNG_SEED + 1 , "the previous seed" end 217if melee . isNetplayGame () then return memory . online . rng_offset , "Slippi" end 218return memory . rng . seed , "Melee" 219end 220 221local seed , source =getSeed () 222math . randomseed ( seed ) 223music . RNG_SEED =seed 224log . debug ( "[RANDOM] Obtained seed \"0x%X\" from %s" , seed , source ) 225end 226 227function music . setVolume ( vol ) 228if ALLOW_INGAME_VOLUME and memory . isMelee () then 229-- Melee's slider goes in increments of 5 230-- It seems to add +5 or -5 no matter what, but it can actually take numbers in between and still work 231--local nearest5 = math.round((100-vol) / 5)*5 232 233-- volume.slider adjust (0-100) 234memory . writeByte ( 0x8045C384 , math . round ( 100 - vol )) 235 236-- volume.music adjust (0-127) 237memory . writeByte ( 0x804D3887 , ( vol / 100 ) * 127 ) 238end 239 240if music . PLAYING and music . PLAYING . STREAM : isPlaying () then 241music . PLAYING . STREAM : setVolume (( vol / 100 ) * ( memory . match . paused and 0.35 or 1 )) 242end 243end 244 245do 246local function getNextTrack ( songs ) 247if music . USE_WEIGHTS then 248local track_id =weightedRandomChoice ( songs ) 249return track_id , track_id 250end 251 252music . TRACK_ID [ music . PLAYLIST_ID ] =(( music . TRACK_ID [ music . PLAYLIST_ID ] or - 1 ) + 1 ) % # songs 253local track_id =music . TRACK_ID [ music . PLAYLIST_ID ] + 1 254 255-- Only shuffle if we have more than 2 songs.. 256if # songs > 2 then 257-- Every time we play a song, we randomly place it towards the start of the playlist 258-- This keeps the playlist in a constantly shuffled order 259local track =table . remove ( songs , track_id ) 260local newpos =math . random ( 1 , track_id ) 261table . insert ( songs , newpos , track ) 262return track_id , newpos 263end 264 265return track_id , track_id 266end 267 268local function loadTrack ( songs , index ) 269local track_info =songs [ index ] 270 271local filepath =track_info . FILEPATH 272 273local success , source =pcall ( love . audio . newSource , filepath , "stream" ) 274if not ( success and source ) then 275local err =( "invalid music file %q" ): format ( filepath ) 276log . error ( "[MUSIC] %s" , err ) 277notification . error ( err ) 278table . remove ( songs , index ) 279return nil 280end 281 282return { STREAM =source , WAV =track_info . IS_WAV and wav . parse ( filepath ) or nil } 283end 284 285function music . playNextTrack () 286if not memory . isMelee () or not PANEL_SETTINGS : PlayStageMusic () then return end 287if music . PLAYING ~= nil and not music . FINISHED then return end 288if not music . PLAYLIST_ID then return end 289if not music . shouldPlayMusic () then return end 290 291local songs =music . PLAYLIST 292 293if # songs > 0 then 294local track_id , track_index =getNextTrack ( songs ) 295 296if not track_index then return end 297 298music . PLAYING =loadTrack ( songs , track_index ) 299 300if music . PLAYING then 301if music . PLAYLIST_ID == 0x0 then 302log . info ( "[MUSIC] Playing track #%d for menu" , track_id ) 303else 304log . info ( "[MUSIC] Playing track #%d for stage %q" , track_id , melee . getStageName ( music . PLAYLIST_ID )) 305end 306 307local loop =PANEL_SETTINGS : GetMusicLoopMode () 308 309if music . PLAYLIST_ID == 0 then 310music . LOOP =loop == LOOPING_MENU or loop == LOOPING_ALL 311else 312music . LOOP =loop == LOOPING_STAGE or loop == LOOPING_ALL 313end 314 315music . PLAYING . STREAM : setVolume (( PANEL_SETTINGS : GetVolume () / 100 ) * ( memory . match . paused and 0.35 or 1 )) 316music . PLAYING . STREAM : play () 317music . FINISHED =false 318end 319end 320end 321end 322 323function music . onStateChange () 324if melee . isInGame () then 325music . loadForStage ( memory . stage ) 326elseif melee . isInMenus () then 327music . loadForStage ( 0 ) 328end 329end 330 331memory . hook ( "frame" , "Melee - Music Think" , music . playNextTrack ) 332 333memory . hook ( "OnGameClosed" , "Dolphin - Game closed" , function () 334music . kill () 335end ) 336 337memory . hook ( "menu.major" , "Melee - Menu state" , function ( menu ) 338if melee . isInMenus () then 339music . loadForStage ( 0 ) 340elseif not melee . isInGame () then 341music . kill () 342end 343end ) 344 345memory . hook ( "menu.minor" , "Melee - Menu state" , function ( menu ) 346if melee . isInMenus () then 347music . loadForStage ( 0 ) 348elseif melee . isInGame () then 349MATCH_SOFT_END =false 350music . loadForStage ( memory . stage ) 351else 352music . kill () 353end 354end ) 355 356memory . hook ( "stage" , "Melee - Stage loaded" , function ( stage ) 357if melee . isInGame () then 358music . loadForStage ( stage ) 359end 360end ) 361 362memory . hook ( "match.playing" , "Melee - Classic Mode Master Hand Fix?" , function ( playing ) 363-- In classic mode, when you kill masterhand the match result is never set, but the "playing" flag is set to false. 364-- Mute the music right when masterhand is killed. 365if not playing and ( memory . menu . major == MENU_CLASSIC_MODE and memory . stage == 0x25 ) then 366MATCH_SOFT_END =true 367music . kill () 368end 369end ) 370 371memory . hook ( "match.paused" , "Melee - Pause volume" , function ( paused ) 372music . setVolume ( music . getVolume ()) 373end ) 374 375memory . hook ( "match.finished" , "Melee - GAME kill music" , function ( finished ) 376if finished then 377-- The match ended, so no matter what the music should be killed 378music . kill () 379end 380end ) 381 382memory . hook ( "match.result" , "Melee - GAME kill music" , function ( result ) 383-- LRAB+Start/Saltyrunback causes a result of MATCH_NO_CONTEST.. We don't want to end the music in this case only. 384-- The match.finished hook will end the music instead when actually exiting the match using normal LRA+Start. 385-- If the match actually ends normally, we kill the music early when it announces the end of the game. 386 387if result ~= MATCH_NO_RESULT and ( memory . menu . major ~= MENU_VS_MODE or ( memory . menu . major == MENU_VS_MODE and result ~= MATCH_NO_CONTEST )) then 388MATCH_SOFT_END =true 389music . kill () 390end 391end ) 392 393memory . hook ( "controller.*.buttons.pressed" , "Melee - Music skipper" , function ( port , pressed ) 394if PANEL_SETTINGS : IsBinding () or PANEL_SETTINGS : IsSlippiReplay () then return end -- Don't skip when the user is setting a button combination or when watching a replay 395local mask =PANEL_SETTINGS : GetMusicSkipMask () 396if mask ~= 0x0 and port == overlay . getPort () and pressed == mask and # music . PLAYLIST > 1 then 397log . debug ( "[MUSIC] [MASK = 0x%X] Button combo pressed, stopping music." , mask ) 398music . kill () 399end 400end ) 401 402local valid_music_ext ={ 403[ "mp3" ] =true , 404[ "ogg" ] =true , 405[ "wav" ] =true , 406[ "flac" ] =true 407} 408 409function music . loadPlaylistForStage ( stageid , name ) 410local found =0 411local files =love . filesystem . getDirectoryItems ( name ) 412table . sort ( files ) -- Sort our list of files alphabetically, giving our table a deterministic state 413for k , file in ipairs ( files ) do 414local filepath =( "%s/%s" ): format ( name , file ) 415local info =love . filesystem . getInfo ( filepath ) 416if info then 417if info . type == "file" then 418local ext =string . getFileExtension ( file ): lower () 419 420if valid_music_ext [ ext ] then 421found =found + 1 422 423-- Insert the newly found track into a random position in the playlist 424local pos =math . random ( 1 , # music . PLAYLIST ) 425local prob =tonumber ( string . match ( filepath , ".-_(%d+)%.%w+$" )) or 1 426 427if prob > 1 then 428music . USE_WEIGHTS =true 429end 430 431table . insert ( music . PLAYLIST , pos , { FILEPATH =filepath , WEIGHT =prob , IS_WAV =ext == "wav" } ) 432end 433end 434else 435log . warn ( "[MUSIC] Unable to get file information for file %q" , filepath ) 436end 437end 438if found > 0 then 439log . info ( "[MUSIC] Found %d songs in %q" , found , name ) 440end 441end 442 443function music . loadForStage ( stageid ) 444if music . PLAYLIST_ID == stageid then return end 445 446music . kill () 447if not memory . isMelee () or not PANEL_SETTINGS : PlayStageMusic () then return end 448 449music . PLAYLIST_ID =stageid 450music . PLAYLIST ={} 451music . USE_WEIGHTS =false 452 453music . refreshRNGseed () 454 455music . loadPlaylistForStage ( stageid , "Melee" ) 456 457if stageid == 0x0 then 458music . loadPlaylistForStage ( stageid , "Melee/Menu Music" ) 459return 460elseif melee . isBTTStage ( stageid ) then 461music . loadPlaylistForStage ( stageid , "Melee/Single Player Music/Break the Targets" ) 462return 463end 464 465local name =melee . getStageName ( stageid ) 466local series =melee . getStageSeries ( stageid ) 467local sp =melee . isSinglePlayerStage ( stageid ) 468local aka =melee . isAkaneiaStage ( stageid ) 469local bm =melee . isBeyondMeleeStage ( stageid ) 470 471if not name then music . PLAYLIST_ID =nil return end 472 473if sp then 474music . loadPlaylistForStage ( stageid , ( "Melee/Single Player Music/%s" ): format ( name )) -- Load everything in the stage specific folder 475music . loadPlaylistForStage ( stageid , "Melee/Single Player Music" ) -- Load everything that's not in a stage folder as well 476elseif aka then 477music . loadPlaylistForStage ( stageid , ( "Melee/Akaneia Stage Music/%s" ): format ( name )) -- Load everything in the stage specific folder 478music . loadPlaylistForStage ( stageid , "Melee/Akaneia Stage Music" ) -- Load everything in the akaneia folder 479music . loadPlaylistForStage ( stageid , "Melee/Stage Music" ) -- Load everything in the stage folder 480elseif bm then 481music . loadPlaylistForStage ( stageid , ( "Melee/Beyond Melee Stage Music/%s" ): format ( name )) -- Load everything in the stage specific folder 482music . loadPlaylistForStage ( stageid , "Melee/Beyond Melee Stage Music" ) -- Load everything in the akaneia folder 483music . loadPlaylistForStage ( stageid , "Melee/Stage Music" ) -- Load everything in the stage folder 484else 485music . loadPlaylistForStage ( stageid , ( "Melee/Stage Music/%s" ): format ( name )) -- Load everything in the stage specific folder 486music . loadPlaylistForStage ( stageid , "Melee/Stage Music" ) -- Load everything in the stage folder 487end 488 489if series then 490music . loadPlaylistForStage ( stageid , ( "Melee/Series Music/%s" ): format ( series )) -- Load everything in the series folder 491music . loadPlaylistForStage ( stageid , "Melee/Series Music" ) -- Load everything that's not in a stage folder as well 492end 493end 494 495return music