Skip to content

Commit

Permalink
- cleanup of the sound init/exit code.
Browse files Browse the repository at this point in the history
Now there is only one single entry point for both, instead of previously 2 entry and 4 exit points.
This also eliminates the explicit shutdown of ZMusic. Timidity++'s two buffers have been put in containers that self-destruct on shutdown and calling dumb_exit is not necessary because the only feature requiring it is not used by any code in the music library.
  • Loading branch information
coelckers committed Oct 1, 2019
1 parent 86ab366 commit d2c1562
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 204 deletions.
51 changes: 28 additions & 23 deletions libraries/timidityplus/mblock.cpp
Expand Up @@ -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 */

Expand All @@ -50,16 +72,16 @@ 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;
p->block_size = MIN_MBLOCK_SIZE;
}
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;
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}

}
5 changes: 4 additions & 1 deletion libraries/timidityplus/resample.cpp
Expand Up @@ -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<float> gauss_table_data;
static float *gauss_table[(1 << FRACTION_BITS)] = { 0 }; /* don't need doubles */
static int gauss_n = DEFAULT_GAUSS_ORDER;

Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion libraries/zmusic/mididevices/mididevice.h
Expand Up @@ -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 -------------
Expand Down
6 changes: 0 additions & 6 deletions libraries/zmusic/mididevices/music_timiditypp_mididevice.cpp
Expand Up @@ -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();
}
15 changes: 0 additions & 15 deletions libraries/zmusic/zmusic/zmusic.cpp
Expand Up @@ -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();
}
2 changes: 0 additions & 2 deletions libraries/zmusic/zmusic/zmusic.h
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions src/d_main.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/posix/cocoa/i_system.mm
Expand Up @@ -88,9 +88,6 @@ void I_Init(void)
CheckCPUID(&CPU);
CalculateCPUSpeed();
DumpCPUInfo(&CPU);

atterm(I_ShutdownSound);
I_InitSound();
}


Expand Down
3 changes: 0 additions & 3 deletions src/posix/sdl/i_system.cpp
Expand Up @@ -97,9 +97,6 @@ void I_Init (void)
{
CheckCPUID (&CPU);
DumpCPUInfo (&CPU);

atterm(I_ShutdownSound);
I_InitSound ();
}

//
Expand Down
10 changes: 0 additions & 10 deletions src/sound/backend/i_sound.cpp
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/sound/backend/i_sound.h
Expand Up @@ -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);
Expand Down
29 changes: 0 additions & 29 deletions src/sound/music/i_music.cpp
Expand Up @@ -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<uint8_t> &newdata);

int nomusic = 0;
Expand Down Expand Up @@ -279,8 +275,6 @@ void I_InitMusic (void)
I_InitMusicWin32 ();
#endif // _WIN32

MusicDown = false;

Callbacks callbacks;

callbacks.Fluid_MessageFunc = Printf;
Expand All @@ -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();
}
}

//==========================================================================
//
//
Expand Down Expand Up @@ -380,7 +352,6 @@ ADD_STAT(music)
// Common loader for the dumpers.
//
//==========================================================================
extern MusPlayingInfo mus_playing;

static MIDISource *GetMIDISource(const char *fn)
{
Expand Down
1 change: 0 additions & 1 deletion src/sound/music/i_music.h
Expand Up @@ -41,7 +41,6 @@ struct FOptionValues;
// MUSIC I/O
//
void I_InitMusic ();
void I_ShutdownMusic (bool onexit = false);
void I_BuildMIDIMenuList (FOptionValues *);

// Volume.
Expand Down
2 changes: 0 additions & 2 deletions src/sound/music/music_config.cpp
Expand Up @@ -40,8 +40,6 @@
#include "s_music.h"
#include "zmusic/zmusic.h"

extern MusPlayingInfo mus_playing;

//==========================================================================
//
// ADL Midi device
Expand Down

0 comments on commit d2c1562

Please sign in to comment.