2020-04-12 06:07:48 +00:00
/*
* *
* * raze_music . cpp
* * music player for Build games
* *
* * Copyright 2019 - 2020 Christoph Oelckers
* *
* * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* *
* * Redistribution and use in source and binary forms , with or without
* * modification , are permitted provided that the following conditions
* * are met :
* *
* * 1. Redistributions of source code must retain the above copyright
* * notice , this list of conditions and the following disclaimer .
* * 2. Redistributions in binary form must reproduce the above copyright
* * notice , this list of conditions and the following disclaimer in the
* * documentation and / or other materials provided with the distribution .
* * 3. The name of the author may not be used to endorse or promote products
* * derived from this software without specific prior written permission .
* *
* * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ` ` AS IS ' ' AND ANY EXPRESS OR
* * IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
* * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED .
* * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT , INDIRECT ,
* * INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT
* * NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* * DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* * THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
* * THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
* * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* *
*/
# include "raze_music.h"
# include "s_music.h"
# include "c_cvars.h"
# include "cmdlib.h"
# include "filesystem.h"
# include "files.h"
# include "i_music.h"
2023-07-22 07:54:00 +00:00
# include "m_argv.h"
2020-04-12 06:07:48 +00:00
# include "gamecontrol.h"
# include "serializer.h"
static bool mus_blocked ;
static FString lastStartedMusic ;
2021-04-08 14:35:26 +00:00
TArray < FString > specialmusic ;
2020-04-12 06:07:48 +00:00
MusicAliasMap MusicAliases ;
CVAR ( Bool , printmusicinfo , false , 0 )
CVAR ( Bool , mus_extendedlookup , false , CVAR_ARCHIVE | CVAR_GLOBALCONFIG )
// Order is: streaming formats, module formats, emulated formats and MIDI formats - for external files the first one found wins so ambiguous names should be avoided
static const char * knownMusicExts [ ] = {
" OGG " , " FLAC " , " MP3 " , " MP2 " , " XA " , " XM " , " MOD " ,
" IT " , " S3M " , " MTM " , " STM " , " 669 " , " PTM " , " AMF " ,
" OKT " , " DSM " , " AMFF " , " SPC " , " VGM " , " VGZ " , " AY " ,
" GBS " , " GYM " , " HES " , " KSS " , " NSF " , " NSFE " , " SAP " ,
" MID " , " HMP " , " HMI " , " XMI " , " VOC "
} ;
//==========================================================================
//
// Music file name lookup
//
//==========================================================================
FString G_SetupFilenameBasedMusic ( const char * fn , const char * defmusic )
{
FString name = StripExtension ( fn ) ;
FString test ;
2021-12-26 12:10:03 +00:00
// Test if a real file with this name exists with all known extensions for music.
2020-04-12 06:07:48 +00:00
for ( auto & ext : knownMusicExts )
{
test . Format ( " %s.%s " , name . GetChars ( ) , ext ) ;
2021-12-26 12:10:03 +00:00
if ( FileExists ( test ) ) return test ;
# ifdef __unix__
test . Format ( " %s.%s " , name . GetChars ( ) , FString ( ext ) . MakeLower ( ) . GetChars ( ) ) ;
if ( FileExists ( test ) ) return test ;
# endif
2020-04-12 06:07:48 +00:00
}
return defmusic ;
}
FString MusicFileExists ( const char * fn )
{
if ( mus_extendedlookup ) return G_SetupFilenameBasedMusic ( fn , nullptr ) ;
if ( FileExists ( fn ) ) return fn ;
return FString ( ) ;
}
2020-09-05 09:58:19 +00:00
int LookupMusic ( const char * fn , bool onlyextended )
2020-04-12 06:07:48 +00:00
{
2020-09-05 09:58:19 +00:00
if ( mus_extendedlookup | | onlyextended )
2020-04-12 06:07:48 +00:00
{
FString name = StripExtension ( fn ) ;
2023-10-08 07:15:32 +00:00
int l = fileSystem . FindFileWithExtensions ( name . GetChars ( ) , knownMusicExts , countof ( knownMusicExts ) ) ;
2020-09-05 09:58:19 +00:00
if ( l > = 0 | | onlyextended ) return l ;
2020-04-12 06:07:48 +00:00
}
2023-08-23 18:36:19 +00:00
return fileSystem . CheckNumForFullName ( fn , true , FileSys : : ns_music ) ;
2020-04-12 06:07:48 +00:00
}
//==========================================================================
//
// Music lookup in various places.
//
//==========================================================================
FileReader OpenMusic ( const char * musicname )
{
FileReader reader ;
if ( ! mus_restartonload )
{
// If the currently playing piece of music is the same, do not restart. Note that there's still edge cases where this may fail to detect identities.
if ( mus_playing . handle ! = nullptr & & lastStartedMusic . CompareNoCase ( musicname ) = = 0 & & mus_playing . loop )
return reader ;
}
lastStartedMusic = musicname ; // remember the last piece of music that was requested to be played.
FString mus = MusicFileExists ( musicname ) ;
if ( mus . IsNotEmpty ( ) )
{
// Load an external file.
2023-10-08 07:15:32 +00:00
reader . OpenFile ( mus . GetChars ( ) ) ;
2020-04-12 06:07:48 +00:00
}
if ( ! reader . isOpen ( ) )
{
2020-09-05 09:58:19 +00:00
int lumpnum = LookupMusic ( musicname ) ;
2021-04-09 19:21:53 +00:00
if ( mus_extendedlookup | | lumpnum < 0 )
2020-04-12 06:07:48 +00:00
{
2021-04-09 19:21:53 +00:00
if ( lumpnum > = 0 )
{
// EDuke also looks in a subfolder named after the main game resource. Do this as well if extended lookup is active.
auto rfn = fileSystem . GetResourceFileName ( fileSystem . GetFileContainer ( lumpnum ) ) ;
auto rfbase = ExtractFileBase ( rfn ) ;
FStringf aliasMusicname ( " music/%s/%s " , rfbase . GetChars ( ) , musicname ) ;
2023-10-08 07:15:32 +00:00
int newlumpnum = LookupMusic ( aliasMusicname . GetChars ( ) ) ;
2021-04-09 19:21:53 +00:00
if ( newlumpnum > = 0 ) lumpnum = newlumpnum ;
}
2020-04-12 06:07:48 +00:00
// Always look in the 'music' subfolder as well. This gets used by multiple setups to store ripped CD tracks.
FStringf aliasMusicname ( " music/%s " , musicname ) ;
2023-10-08 07:15:32 +00:00
int newlumpnum = LookupMusic ( aliasMusicname . GetChars ( ) , lumpnum > = 0 ) ;
2021-04-09 19:21:53 +00:00
if ( newlumpnum > = 0 ) lumpnum = newlumpnum ;
2020-04-12 06:07:48 +00:00
}
2021-04-09 19:21:53 +00:00
2021-07-11 02:39:54 +00:00
if ( lumpnum = = - 1 & & isSWALL ( ) )
2020-04-12 06:07:48 +00:00
{
2021-04-08 15:46:53 +00:00
// Some Shadow Warrior distributions have the music in a subfolder named 'classic'. Check that, too.
2020-04-12 06:07:48 +00:00
FStringf aliasMusicname ( " classic/music/%s " , musicname ) ;
2023-10-08 07:15:32 +00:00
lumpnum = fileSystem . FindFile ( aliasMusicname . GetChars ( ) ) ;
2020-04-12 06:07:48 +00:00
}
if ( lumpnum > - 1 )
{
if ( fileSystem . FileLength ( lumpnum ) > = 0 )
{
reader = fileSystem . ReopenFileReader ( lumpnum ) ;
if ( ! reader . isOpen ( ) )
{
Printf ( TEXTCOLOR_RED " Unable to play music " TEXTCOLOR_WHITE " \" %s \" \n " , musicname ) ;
}
2023-08-20 00:25:12 +00:00
else if ( printmusicinfo ) Printf ( " Playing music from file system %s:%s \n " , fileSystem . GetResourceFileFullName ( fileSystem . GetFileContainer ( lumpnum ) ) , fileSystem . GetFileFullPath ( lumpnum ) . c_str ( ) ) ;
2020-04-12 06:07:48 +00:00
}
}
}
else if ( printmusicinfo ) Printf ( " Playing music from external file %s \n " , musicname ) ;
return reader ;
}
2020-09-05 09:58:19 +00:00
static FString LookupMusicCB ( const char * musicname , int & order )
2020-04-12 06:07:48 +00:00
{
// Now perform music aliasing. This also needs to be done before checking identities because multiple names can map to the same song.
FName * aliasp = MusicAliases . CheckKey ( musicname ) ;
if ( aliasp ! = nullptr )
{
if ( * aliasp = = NAME_None )
{
return true ; // flagged to be ignored
}
return aliasp - > GetChars ( ) ;
}
return musicname ;
}
2021-05-11 23:58:42 +00:00
int Mus_Play ( const char * fn , bool loop )
2020-04-12 06:07:48 +00:00
{
if ( mus_blocked ) return 1 ; // Caller should believe it succeeded.
return S_ChangeMusic ( fn , 0 , loop , true ) ;
}
bool Mus_IsPlaying ( )
{
return mus_playing . handle ! = nullptr ;
}
void Mus_Stop ( )
{
if ( mus_blocked ) return ;
S_StopMusic ( true ) ;
}
void Mus_SetPaused ( bool on )
{
if ( on ) S_PauseMusic ( ) ;
else S_ResumeMusic ( ) ;
}
void Mus_Serialize ( FSerializer & arc )
{
if ( arc . BeginObject ( " music " ) )
{
if ( arc . isWriting ( ) )
{
FString music = mus_playing . name ;
if ( music . IsEmpty ( ) ) music = mus_playing . LastSong ;
2023-10-08 07:15:32 +00:00
arc . AddString ( " music " , music . GetChars ( ) ) ;
2020-04-12 06:07:48 +00:00
}
else arc ( " music " , mus_playing . LastSong ) ;
arc ( " baseorder " , mus_playing . baseorder )
( " loop " , mus_playing . loop )
. EndObject ( ) ;
// this is to prevent scripts from resetting the music after it has been loaded from the savegame.
if ( arc . isReading ( ) ) mus_blocked = true ;
// Actual music resuming cannot be performed here, it must be done in the game code.
}
}
void Mus_ResumeSaved ( )
{
S_RestartMusic ( ) ;
}
void Mus_UpdateMusic ( )
{
mus_blocked = false ;
S_UpdateMusic ( ) ;
}
void Mus_InitMusic ( )
{
2023-07-22 07:54:00 +00:00
I_InitMusic ( Args - > CheckParm ( " -nomusic " ) | | Args - > CheckParm ( " -nosound " ) ) ;
2020-04-12 06:07:48 +00:00
static MusicCallbacks mus_cb =
{
2020-09-05 09:58:19 +00:00
LookupMusicCB ,
2020-04-12 06:07:48 +00:00
OpenMusic
} ;
S_SetMusicCallbacks ( & mus_cb ) ;
}