diff --git a/libraries/timidityplus/mblock.cpp b/libraries/timidityplus/mblock.cpp index 5ad8fd658..9116a3676 100644 --- a/libraries/timidityplus/mblock.cpp +++ b/libraries/timidityplus/mblock.cpp @@ -30,7 +30,29 @@ namespace TimidityPlus { -static MBlockNode *free_mblock_list = NULL; +struct MBlock +{ + MBlockNode* free_mblock_list = NULL; + + ~MBlock() + { + int cnt; + + cnt = 0; + while (free_mblock_list) + { + MBlockNode* tmp; + + tmp = free_mblock_list; + free_mblock_list = free_mblock_list->next; + free(tmp); + cnt++; + } + } +}; + +static MBlock free_list; + #define ADDRALIGN 8 /* #define DEBUG */ @@ -50,7 +72,7 @@ static MBlockNode *new_mblock_node(size_t n) return NULL; p->block_size = n; } - else if (free_mblock_list == NULL) + else if (free_list.free_mblock_list == NULL) { if ((p = (MBlockNode *)safe_malloc(sizeof(MBlockNode) + MIN_MBLOCK_SIZE)) == NULL) return NULL; @@ -58,8 +80,8 @@ static MBlockNode *new_mblock_node(size_t n) } else { - p = free_mblock_list; - free_mblock_list = free_mblock_list->next; + p = free_list.free_mblock_list; + free_list.free_mblock_list = free_list.free_mblock_list->next; } p->offset = 0; @@ -115,8 +137,8 @@ static void reuse_mblock1(MBlockNode *p) free(p); else /* p->block_size <= MIN_MBLOCK_SIZE */ { - p->next = free_mblock_list; - free_mblock_list = p; + p->next = free_list.free_mblock_list; + free_list.free_mblock_list = p; } } @@ -149,21 +171,4 @@ char *strdup_mblock(MBlockList *mblock, const char *str) return p; } -int free_global_mblock(void) -{ - int cnt; - - cnt = 0; - while (free_mblock_list) - { - MBlockNode *tmp; - - tmp = free_mblock_list; - free_mblock_list = free_mblock_list->next; - free(tmp); - cnt++; - } - return cnt; -} - } \ No newline at end of file diff --git a/libraries/timidityplus/resample.cpp b/libraries/timidityplus/resample.cpp index c27e9d8d1..5cc1bfd1d 100644 --- a/libraries/timidityplus/resample.cpp +++ b/libraries/timidityplus/resample.cpp @@ -42,6 +42,7 @@ static float newt_coeffs[58][58]; static int sample_bounds_min, sample_bounds_max; /* min/max bounds for sample data */ #define DEFAULT_GAUSS_ORDER 25 +std::vector gauss_table_data; static float *gauss_table[(1 << FRACTION_BITS)] = { 0 }; /* don't need doubles */ static int gauss_n = DEFAULT_GAUSS_ORDER; @@ -209,7 +210,9 @@ void initialize_gauss_table(int n) zsin[i] = sin(i / (4 * M_PI)); x_inc = 1.0 / (1 << FRACTION_BITS); - gptr = (float*)safe_realloc(gauss_table[0], (n + 1) * sizeof(float)*(1 << FRACTION_BITS)); + + gauss_table_data.resize((n + 1) * sizeof(float) * (1 << FRACTION_BITS)); + gptr = gauss_table_data.data(); for (m = 0, x = 0.0; m < (1 << FRACTION_BITS); m++, x += x_inc) { xz = (x + n_half) / (4 * M_PI); diff --git a/libraries/zmusic/mididevices/mididevice.h b/libraries/zmusic/mididevices/mididevice.h index 80dff086e..f125e13fb 100644 --- a/libraries/zmusic/mididevices/mididevice.h +++ b/libraries/zmusic/mididevices/mididevice.h @@ -4,7 +4,6 @@ #include "zmusic/midiconfig.h" #include "zmusic/mididefs.h" -void TimidityPP_Shutdown(); typedef void(*MidiCallback)(void *); // A device that provides a WinMM-like MIDI streaming interface ------------- diff --git a/libraries/zmusic/mididevices/music_timiditypp_mididevice.cpp b/libraries/zmusic/mididevices/music_timiditypp_mididevice.cpp index bffebf4b1..57f45df71 100644 --- a/libraries/zmusic/mididevices/music_timiditypp_mididevice.cpp +++ b/libraries/zmusic/mididevices/music_timiditypp_mididevice.cpp @@ -225,9 +225,3 @@ MIDIDevice *CreateTimidityPPMIDIDevice(const char *Args, int samplerate) return new TimidityPPMIDIDevice(samplerate); } - -void TimidityPP_Shutdown() -{ - TimidityPlus::free_gauss_table(); - TimidityPlus::free_global_mblock(); -} diff --git a/libraries/zmusic/zmusic/zmusic.cpp b/libraries/zmusic/zmusic/zmusic.cpp index 2d1ab1d61..0fc6de6d5 100644 --- a/libraries/zmusic/zmusic/zmusic.cpp +++ b/libraries/zmusic/zmusic/zmusic.cpp @@ -306,18 +306,3 @@ MusInfo *ZMusic_OpenCDSong (int track, int id) return info; } -//========================================================================== -// -// -// -//========================================================================== - -void TimidityPP_Shutdown(); -extern "C" void dumb_exit(); - -void ZMusic_Shutdown() -{ - // free static data in the backends. - TimidityPP_Shutdown(); - dumb_exit(); -} diff --git a/libraries/zmusic/zmusic/zmusic.h b/libraries/zmusic/zmusic/zmusic.h index dedd1f196..2bd6fcdeb 100644 --- a/libraries/zmusic/zmusic/zmusic.h +++ b/libraries/zmusic/zmusic/zmusic.h @@ -159,8 +159,6 @@ void MIDIDumpWave(MIDISource* source, EMidiDevice devtype, const char* devarg, c MusInfo *ZMusic_OpenSong (MusicIO::FileInterface *reader, EMidiDevice device, const char *Args); MusInfo *ZMusic_OpenCDSong (int track, int cdid = 0); -void ZMusic_Shutdown(); - class MusInfo; // Configuration interface. The return value specifies if a music restart is needed. // RealValue should be written back to the CVAR or whatever other method the client uses to store configuration state. diff --git a/src/d_main.cpp b/src/d_main.cpp index 325201167..47a04a4a0 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -2494,7 +2494,6 @@ void D_DoomMain (void) if (!batchrun) Printf ("S_Init: Setting up sound.\n"); S_Init (); - S_InitMusic(); if (!batchrun) Printf ("ST_Init: Init startup screen.\n"); if (!restart) @@ -2803,7 +2802,6 @@ void D_Cleanup() R_DeinitTranslationTables(); // some tables are initialized from outside the translation code. gameinfo.~gameinfo_t(); new (&gameinfo) gameinfo_t; // Reset gameinfo - S_ShutdownMusic(); S_Shutdown(); // free all channels and delete playlist C_ClearAliases(); // CCMDs won't be reinitialized so these need to be deleted here DestroyCVarsFlagged(CVAR_MOD); // Delete any cvar left by mods diff --git a/src/posix/cocoa/i_system.mm b/src/posix/cocoa/i_system.mm index 5b89d7149..b05d7dc82 100644 --- a/src/posix/cocoa/i_system.mm +++ b/src/posix/cocoa/i_system.mm @@ -88,9 +88,6 @@ void I_Init(void) CheckCPUID(&CPU); CalculateCPUSpeed(); DumpCPUInfo(&CPU); - - atterm(I_ShutdownSound); - I_InitSound(); } diff --git a/src/posix/sdl/i_system.cpp b/src/posix/sdl/i_system.cpp index dd89c7bce..a9d563763 100644 --- a/src/posix/sdl/i_system.cpp +++ b/src/posix/sdl/i_system.cpp @@ -97,9 +97,6 @@ void I_Init (void) { CheckCPUID (&CPU); DumpCPUInfo (&CPU); - - atterm(I_ShutdownSound); - I_InitSound (); } // diff --git a/src/sound/backend/i_sound.cpp b/src/sound/backend/i_sound.cpp index 4bdc00b7e..1fc43341b 100644 --- a/src/sound/backend/i_sound.cpp +++ b/src/sound/backend/i_sound.cpp @@ -307,16 +307,6 @@ void I_CloseSound () GSnd = NULL; } -void I_ShutdownSound() -{ - I_ShutdownMusic(true); - if (GSnd != NULL) - { - S_StopAllChannels(); - I_CloseSound(); - } -} - const char *GetSampleTypeName(SampleType type) { switch(type) diff --git a/src/sound/backend/i_sound.h b/src/sound/backend/i_sound.h index bd10b11f7..eedcb18b2 100644 --- a/src/sound/backend/i_sound.h +++ b/src/sound/backend/i_sound.h @@ -174,7 +174,6 @@ extern bool nosfx; extern bool nosound; void I_InitSound (); -void I_ShutdownSound (); void S_ChannelEnded(FISoundChannel *schan); void S_ChannelVirtualChanged(FISoundChannel *schan, bool is_virtual); diff --git a/src/sound/music/i_music.cpp b/src/sound/music/i_music.cpp index 5a8d60722..c6d9f749c 100644 --- a/src/sound/music/i_music.cpp +++ b/src/sound/music/i_music.cpp @@ -62,13 +62,9 @@ void I_InitSoundFonts(); -extern MusPlayingInfo mus_playing; - EXTERN_CVAR (Int, snd_samplerate) EXTERN_CVAR (Int, snd_mididevice) -static bool MusicDown = true; - static bool ungzip(uint8_t *data, int size, std::vector &newdata); int nomusic = 0; @@ -279,8 +275,6 @@ void I_InitMusic (void) I_InitMusicWin32 (); #endif // _WIN32 - MusicDown = false; - Callbacks callbacks; callbacks.Fluid_MessageFunc = Printf; @@ -298,28 +292,6 @@ void I_InitMusic (void) } -//========================================================================== -// -// -// -//========================================================================== - -void I_ShutdownMusic(bool onexit) -{ - if (MusicDown) - return; - MusicDown = true; - if (mus_playing.handle) - { - S_StopMusic (true); - assert (mus_playing.handle == nullptr); - } - if (onexit) - { - ZMusic_Shutdown(); - } -} - //========================================================================== // // @@ -380,7 +352,6 @@ ADD_STAT(music) // Common loader for the dumpers. // //========================================================================== -extern MusPlayingInfo mus_playing; static MIDISource *GetMIDISource(const char *fn) { diff --git a/src/sound/music/i_music.h b/src/sound/music/i_music.h index 54502461b..af45ec1fe 100644 --- a/src/sound/music/i_music.h +++ b/src/sound/music/i_music.h @@ -41,7 +41,6 @@ struct FOptionValues; // MUSIC I/O // void I_InitMusic (); -void I_ShutdownMusic (bool onexit = false); void I_BuildMIDIMenuList (FOptionValues *); // Volume. diff --git a/src/sound/music/music_config.cpp b/src/sound/music/music_config.cpp index 2531be803..47486e8a3 100644 --- a/src/sound/music/music_config.cpp +++ b/src/sound/music/music_config.cpp @@ -40,8 +40,6 @@ #include "s_music.h" #include "zmusic/zmusic.h" -extern MusPlayingInfo mus_playing; - //========================================================================== // // ADL Midi device diff --git a/src/sound/s_music.cpp b/src/sound/s_music.cpp index 2aec2c853..5245a880b 100644 --- a/src/sound/s_music.cpp +++ b/src/sound/s_music.cpp @@ -103,8 +103,7 @@ static void S_ActivatePlayList(bool goBack); static bool MusicPaused; // whether music is paused MusPlayingInfo mus_playing; // music currently being played -static FString LastSong; // last music that was played -static FPlayList *PlayList; +static FPlayList PlayList; float relative_volume = 1.f; float saved_relative_volume = 1.0f; // this could be used to implement an ACS FadeMusic function @@ -114,47 +113,9 @@ DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, baseorder); DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, loop); // PUBLIC DATA DEFINITIONS ------------------------------------------------- -void S_ShutdownMusic (); // CODE -------------------------------------------------------------------- -//========================================================================== -// -// S_Init -// -// Initializes sound stuff, including volume. Sets channels, SFX and -// music volume, allocates channel buffer, and sets S_sfx lookup. -//========================================================================== - -void S_InitMusic () -{ - // no sounds are playing, and they are not paused - mus_playing.name = ""; - LastSong = ""; - mus_playing.handle = nullptr; - mus_playing.baseorder = 0; - MusicPaused = false; - atterm(S_ShutdownMusic); -} - -//========================================================================== -// -// S_Shutdown -// -//========================================================================== - -void S_ShutdownMusic () -{ - if (PlayList != nullptr) - { - delete PlayList; - PlayList = nullptr; - } - S_StopMusic (true); - mus_playing.name = ""; - LastSong = ""; -} - //========================================================================== // // @@ -279,9 +240,9 @@ void S_UpdateMusic () // playlist when the current song finishes. if (!mus_playing.handle->IsPlaying()) { - if (PlayList) + if (PlayList.GetNumSongs()) { - PlayList->Advance(); + PlayList.Advance(); S_ActivatePlayList(false); } else @@ -331,15 +292,14 @@ void S_ActivatePlayList (bool goBack) { int startpos, pos; - startpos = pos = PlayList->GetPosition (); + startpos = pos = PlayList.GetPosition (); S_StopMusic (true); - while (!S_ChangeMusic (PlayList->GetSong (pos), 0, false, true)) + while (!S_ChangeMusic (PlayList.GetSong (pos), 0, false, true)) { - pos = goBack ? PlayList->Backup () : PlayList->Advance (); + pos = goBack ? PlayList.Backup () : PlayList.Advance (); if (pos == startpos) { - delete PlayList; - PlayList = nullptr; + PlayList.Clear(); Printf ("Cannot play anything in the playlist.\n"); return; } @@ -394,7 +354,7 @@ bool S_StartMusic (const char *m_id) bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force) { if (nomusic) return false; // skip the entire procedure if music is globally disabled. - if (!force && PlayList) + if (!force && PlayList.GetNumSongs()) { // Don't change if a playlist is active return false; } @@ -418,7 +378,7 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force) // Don't choke if the map doesn't have a song attached S_StopMusic (true); mus_playing.name = ""; - LastSong = ""; + mus_playing.LastSong = ""; return true; } @@ -535,7 +495,7 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force) mus_playing.loop = looping; mus_playing.name = musicname; mus_playing.baseorder = order; - LastSong = musicname; + mus_playing.LastSong = musicname; return true; } @@ -561,7 +521,7 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force) mus_playing.loop = looping; mus_playing.name = musicname; mus_playing.baseorder = 0; - LastSong = ""; + mus_playing.LastSong = ""; if (mus_playing.handle != 0) { // play it @@ -600,10 +560,10 @@ DEFINE_ACTION_FUNCTION(DObject, S_ChangeMusic) void S_RestartMusic () { - if (!LastSong.IsEmpty()) + if (!mus_playing.LastSong.IsEmpty()) { - FString song = LastSong; - LastSong = ""; + FString song = mus_playing.LastSong; + mus_playing.LastSong = ""; S_ChangeMusic (song, mus_playing.baseorder, mus_playing.loop, true); } } @@ -680,7 +640,7 @@ void S_StopMusic (bool force) try { // [RH] Don't stop if a playlist is active. - if ((force || PlayList == nullptr) && !mus_playing.name.IsEmpty()) + if ((force || PlayList.GetNumSongs() == 0) && !mus_playing.name.IsEmpty()) { if (mus_playing.handle != nullptr) { @@ -690,8 +650,7 @@ void S_StopMusic (bool force) delete mus_playing.handle; mus_playing.handle = nullptr; } - LastSong = mus_playing.name; - mus_playing.name = ""; + mus_playing.LastSong = std::move(mus_playing.name); } } catch (const std::runtime_error& ) @@ -772,11 +731,7 @@ CCMD (changemus) { if (argv.argc() > 1) { - if (PlayList) - { - delete PlayList; - PlayList = nullptr; - } + PlayList.Clear(); S_ChangeMusic (argv[1], argv.argc() > 2 ? atoi (argv[2]) : 0); } else @@ -806,13 +761,9 @@ CCMD (changemus) CCMD (stopmus) { - if (PlayList) - { - delete PlayList; - PlayList = nullptr; - } + PlayList.Clear(); S_StopMusic (false); - LastSong = ""; // forget the last played song so that it won't get restarted if some volume changes occur + mus_playing.LastSong = ""; // forget the last played song so that it won't get restarted if some volume changes occur } //========================================================================== @@ -909,30 +860,25 @@ UNSAFE_CCMD (playlist) } else { - if (PlayList != nullptr) + if (PlayList.GetNumSongs() > 0) { - PlayList->ChangeList (argv[1]); + PlayList.ChangeList (argv[1]); } else { - PlayList = new FPlayList (argv[1]); + PlayList.ChangeList(argv[1]); } - if (PlayList->GetNumSongs () == 0) - { - delete PlayList; - PlayList = nullptr; - } - else + if (PlayList.GetNumSongs () > 0) { if (argc == 3) { if (stricmp (argv[2], "shuffle") == 0) { - PlayList->Shuffle (); + PlayList.Shuffle (); } else { - PlayList->SetPosition (atoi (argv[2])); + PlayList.SetPosition (atoi (argv[2])); } } S_ActivatePlayList (false); @@ -948,7 +894,7 @@ UNSAFE_CCMD (playlist) static bool CheckForPlaylist () { - if (PlayList == nullptr) + if (PlayList.GetNumSongs() == 0) { Printf ("No playlist is playing.\n"); return false; @@ -960,7 +906,7 @@ CCMD (playlistpos) { if (CheckForPlaylist() && argv.argc() > 1) { - PlayList->SetPosition (atoi (argv[1]) - 1); + PlayList.SetPosition (atoi (argv[1]) - 1); S_ActivatePlayList (false); } } @@ -975,7 +921,7 @@ CCMD (playlistnext) { if (CheckForPlaylist()) { - PlayList->Advance (); + PlayList.Advance (); S_ActivatePlayList (false); } } @@ -990,7 +936,7 @@ CCMD (playlistprev) { if (CheckForPlaylist()) { - PlayList->Backup (); + PlayList.Backup (); S_ActivatePlayList (true); } } @@ -1006,9 +952,9 @@ CCMD (playliststatus) if (CheckForPlaylist ()) { Printf ("Song %d of %d:\n%s\n", - PlayList->GetPosition () + 1, - PlayList->GetNumSongs (), - PlayList->GetSong (PlayList->GetPosition ())); + PlayList.GetPosition () + 1, + PlayList.GetNumSongs (), + PlayList.GetSong (PlayList.GetPosition ())); } } diff --git a/src/sound/s_music.h b/src/sound/s_music.h index b0fcf231a..f196a658f 100644 --- a/src/sound/s_music.h +++ b/src/sound/s_music.h @@ -37,7 +37,6 @@ void S_ParseMusInfo(); // void S_InitMusic (); -void S_ShutdownMusic (); void S_StartMusic (); @@ -86,8 +85,11 @@ struct MusPlayingInfo MusInfo* handle; int baseorder; bool loop; + FString LastSong; // last music that was played }; +extern MusPlayingInfo mus_playing; + extern float relative_volume, saved_relative_volume; diff --git a/src/sound/s_sound.cpp b/src/sound/s_sound.cpp index 7820f1cce..426d1602f 100644 --- a/src/sound/s_sound.cpp +++ b/src/sound/s_sound.cpp @@ -83,6 +83,7 @@ #include "vm.h" #include "g_game.h" #include "atterm.h" +#include "s_music.h" // MACROS ------------------------------------------------------------------ @@ -308,6 +309,7 @@ void S_Init () atterm(S_Shutdown); // Heretic and Hexen have sound curve lookup tables. Doom does not. + I_InitSound(); curvelump = Wads.CheckNumForName ("SNDCURVE"); if (curvelump >= 0) { @@ -349,13 +351,9 @@ void S_Shutdown () { FSoundChan *chan, *next; - chan = Channels; - while (chan != NULL) - { - next = chan->NextChan; - S_StopChannel(chan); - chan = next; - } + S_StopMusic(true); + mus_playing.LastSong = ""; // If this isn't reset here, the song would attempt resume at the most inpopportune time... + S_StopAllChannels(); GSnd->UpdateSounds(); for (chan = FreeChannels; chan != NULL; chan = next) @@ -364,8 +362,14 @@ void S_Shutdown () delete chan; } FreeChannels = NULL; + + if (GSnd != NULL) + { + I_CloseSound(); + } } + //========================================================================== // // S_Start @@ -2614,7 +2618,7 @@ CCMD (snd_reset) void S_SoundReset() { - I_ShutdownMusic(); + S_StopMusic(true); S_EvictAllChannels(); I_CloseSound(); I_InitSound(); diff --git a/src/utility/s_playlist.cpp b/src/utility/s_playlist.cpp index 979bbb67a..8b8be5f76 100644 --- a/src/utility/s_playlist.cpp +++ b/src/utility/s_playlist.cpp @@ -41,11 +41,6 @@ #include "v_text.h" #include "files.h" -FPlayList::FPlayList (const char *path) -{ - ChangeList (path); -} - FPlayList::~FPlayList () { } diff --git a/src/utility/s_playlist.h b/src/utility/s_playlist.h index e1f497879..5bd5fd55e 100644 --- a/src/utility/s_playlist.h +++ b/src/utility/s_playlist.h @@ -39,7 +39,6 @@ class FileReader; class FPlayList { public: - FPlayList (const char *path); ~FPlayList (); bool ChangeList (const char *path); @@ -51,11 +50,16 @@ public: int Backup (); void Shuffle (); const char *GetSong (int position) const; + void Clear() + { + Songs.Clear(); + Position = 0; + } private: static FString NextLine (FileReader &file); - unsigned int Position; + unsigned int Position = 0; TArray Songs; }; diff --git a/src/win32/i_system.cpp b/src/win32/i_system.cpp index 8d52903e6..ea60b07ae 100644 --- a/src/win32/i_system.cpp +++ b/src/win32/i_system.cpp @@ -336,9 +336,6 @@ void I_Init() CheckCPUID(&CPU); CalculateCPUSpeed(); DumpCPUInfo(&CPU); - - atterm(I_ShutdownSound); - I_InitSound (); }