Skip to content

Commit

Permalink
perform music volume lookup by lump number instead of name.
Browse files Browse the repository at this point in the history
This way any setting will work for both short and long file names of the same content.
  • Loading branch information
coelckers committed Dec 27, 2023
1 parent 9583035 commit eb2f263
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
53 changes: 43 additions & 10 deletions src/common/audio/music/music.cpp
Expand Up @@ -75,14 +75,12 @@ float saved_relative_volume = 1.0f; // this could be used to implement an ACS Fa
MusicVolumeMap MusicVolumes;
MidiDeviceMap MidiDevices;

static FileReader DefaultOpenMusic(const char* fn)
static int DefaultFindMusic(const char* fn)
{
// This is the minimum needed to make the music system functional.
FileReader fr;
fr.OpenFile(fn);
return fr;
return -1;
}
static MusicCallbacks mus_cb = { nullptr, DefaultOpenMusic };

MusicCallbacks mus_cb = { nullptr, DefaultFindMusic };


// PUBLIC DATA DEFINITIONS -------------------------------------------------
Expand All @@ -98,10 +96,44 @@ CVAR(Bool, mus_usereplaygain, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) // changi

// CODE --------------------------------------------------------------------

//==========================================================================
//
// OpenMusic
//
// opens a FileReader for the music - used as a callback to keep
// implementation details out of the core player.
//
//==========================================================================

static FileReader OpenMusic(const char* musicname)
{
FileReader reader;
if (!FileExists(musicname))
{
int lumpnum;
lumpnum = mus_cb.FindMusic(musicname);
if (lumpnum == -1) lumpnum = fileSystem.CheckNumForName(musicname, FileSys::ns_music);
if (lumpnum == -1)
{
Printf("Music \"%s\" not found\n", musicname);
}
else if (fileSystem.FileLength(lumpnum) != 0)
{
reader = fileSystem.ReopenFileReader(lumpnum);
}
}
else
{
// Load an external file.
reader.OpenFile(musicname);
}
return reader;
}

void S_SetMusicCallbacks(MusicCallbacks* cb)
{
mus_cb = *cb;
if (mus_cb.OpenMusic == nullptr) mus_cb.OpenMusic = DefaultOpenMusic; // without this we are dead in the water.
if (mus_cb.FindMusic == nullptr) mus_cb.FindMusic = DefaultFindMusic; // without this we are dead in the water.
}

int MusicEnabled() // int return is for scripting
Expand Down Expand Up @@ -521,7 +553,7 @@ static void CheckReplayGain(const char *musicname, EMidiDevice playertype, const
mod_dumb_mastervolume->Callback();
if (!mus_usereplaygain) return;

FileReader reader = mus_cb.OpenMusic(musicname);
FileReader reader = OpenMusic(musicname);
if (!reader.isOpen()) return;
int flength = (int)reader.GetLength();
auto mreader = GetMusicReader(reader); // this passes the file reader to the newly created wrapper.
Expand Down Expand Up @@ -693,7 +725,7 @@ bool S_ChangeMusic(const char* musicname, int order, bool looping, bool force)
}

// opening the music must be done by the game because it's different depending on the game's file system use.
FileReader reader = mus_cb.OpenMusic(musicname);
FileReader reader = OpenMusic(musicname);
if (!reader.isOpen()) return false;
auto m = reader.Read();
reader.Seek(0, FileReader::SeekSet);
Expand All @@ -718,7 +750,8 @@ bool S_ChangeMusic(const char* musicname, int order, bool looping, bool force)
}
else
{
auto volp = MusicVolumes.CheckKey(musicname);
int lumpnum = mus_cb.FindMusic(musicname);
auto volp = MusicVolumes.CheckKey(lumpnum);
if (volp)
{
mus_playing.musicVolume = *volp;
Expand Down
6 changes: 4 additions & 2 deletions src/common/audio/music/s_music.h
Expand Up @@ -24,7 +24,7 @@ void S_PauseAllCustomStreams(bool on);
struct MusicCallbacks
{
FString(*LookupFileName)(const char* fn, int &order);
FileReader(*OpenMusic)(const char* fn);
int(*FindMusic)(const char* fn);
};
void S_SetMusicCallbacks(MusicCallbacks* cb);

Expand Down Expand Up @@ -69,15 +69,17 @@ struct MidiDeviceSetting
};

typedef TMap<FName, MidiDeviceSetting> MidiDeviceMap;
typedef TMap<FName, float> MusicVolumeMap;
typedef TMap<int, float> MusicVolumeMap;

extern MidiDeviceMap MidiDevices;
extern MusicVolumeMap MusicVolumes;
extern MusicCallbacks mus_cb;

struct MusPlayingInfo
{
FString name;
ZMusic_MusicStream handle;
int lumpnum;
int baseorder;
float musicVolume;
bool loop;
Expand Down
4 changes: 2 additions & 2 deletions src/sound/s_advsound.cpp
Expand Up @@ -1034,7 +1034,7 @@ static void S_AddSNDINFO (int lump)

case SI_MusicVolume: {
sc.MustGetString();
FName musname (sc.String);
int lumpnum = mus_cb.FindMusic(sc.String);
if (!sc.CheckFloat())
{
sc.MustGetString();
Expand All @@ -1043,7 +1043,7 @@ static void S_AddSNDINFO (int lump)
if (!stricmp(p, "db")) sc.Float = dBToAmplitude((float)sc.Float);
else sc.ScriptError("Bad value for music volume: %s", sc.String);
}
MusicVolumes[musname] = (float)sc.Float;
if (lumpnum >= 0) MusicVolumes[lumpnum] = (float)sc.Float;
}
break;

Expand Down
33 changes: 7 additions & 26 deletions src/sound/s_doomsound.cpp
Expand Up @@ -177,36 +177,17 @@ static FString LookupMusic(const char* musicname, int& order)

//==========================================================================
//
// OpenMusic
// FindMusic
//
// opens a FileReader for the music - used as a callback to keep
// implementation details out of the core player.
// loops up a music resource according to the engine's rules
//
//==========================================================================

static FileReader OpenMusic(const char* musicname)
static int FindMusic(const char* musicname)
{
FileReader reader;
if (!FileExists(musicname))
{
int lumpnum;
lumpnum = fileSystem.CheckNumForFullName(musicname);
if (lumpnum == -1) lumpnum = fileSystem.CheckNumForName(musicname, FileSys::ns_music);
if (lumpnum == -1)
{
Printf("Music \"%s\" not found\n", musicname);
}
else if (fileSystem.FileLength(lumpnum) != 0)
{
reader = fileSystem.ReopenFileReader(lumpnum);
}
}
else
{
// Load an external file.
reader.OpenFile(musicname);
}
return reader;
int lumpnum = fileSystem.CheckNumForFullName(musicname);
if (lumpnum == -1) lumpnum = fileSystem.CheckNumForName(musicname, FileSys::ns_music);
return lumpnum;
}

//==========================================================================
Expand All @@ -220,7 +201,7 @@ static FileReader OpenMusic(const char* musicname)
void S_Init()
{
// Hook up the music player with the engine specific customizations.
static MusicCallbacks cb = { LookupMusic, OpenMusic };
static MusicCallbacks cb = { LookupMusic, FindMusic };
S_SetMusicCallbacks(&cb);

// Must be up before I_InitSound.
Expand Down

0 comments on commit eb2f263

Please sign in to comment.