From b6d625b202f65c965066fff9b5e8611bea8cf85a Mon Sep 17 00:00:00 2001 From: hendricks266 Date: Sun, 25 Jan 2015 12:17:59 +0000 Subject: [PATCH] Modularize .mid --> .ogg --> .flac code to apply to sounds as well, and various cleanup in music/sound filename-handling code. git-svn-id: https://svn.eduke32.com/eduke32@4948 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/include/common.h | 7 +++ polymer/eduke32/source/common.c | 38 +++++++++++++ polymer/eduke32/source/common_game.h | 6 ++ polymer/eduke32/source/game.c | 68 +++-------------------- polymer/eduke32/source/gameexec.c | 2 +- polymer/eduke32/source/lunatic/defs.ilua | 2 +- polymer/eduke32/source/premap.c | 29 ++++------ polymer/eduke32/source/savegame.c | 2 +- polymer/eduke32/source/sector.h | 2 +- polymer/eduke32/source/sounds.c | 64 +++++---------------- polymer/eduke32/source/sounds.h | 8 +-- polymer/eduke32/source/sounds_mapster32.c | 7 ++- polymer/eduke32/source/sounds_mapster32.h | 2 +- 13 files changed, 99 insertions(+), 138 deletions(-) diff --git a/polymer/eduke32/build/include/common.h b/polymer/eduke32/build/include/common.h index 9454c5b59..4eb661d16 100644 --- a/polymer/eduke32/build/include/common.h +++ b/polymer/eduke32/build/include/common.h @@ -79,6 +79,13 @@ static inline char *dup_filename(const char *fn) return Bstrncpyz(buf, fn, BMAX_PATH); } +static inline void realloc_copy(char **fn, const char *buf) +{ + uint8_t len = Bstrlen(buf) + 1; + *fn = (char *)Xrealloc(*fn, len); + Bstrncpy(*fn, buf, len); +} + int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens); int32_t G_CheckCmdSwitch(int32_t argc, const char **argv, const char *str); diff --git a/polymer/eduke32/source/common.c b/polymer/eduke32/source/common.c index 5c60ef567..a8f3d7c5b 100644 --- a/polymer/eduke32/source/common.c +++ b/polymer/eduke32/source/common.c @@ -1062,3 +1062,41 @@ int32_t G_LoadLookups(void) return 0; } + +#if defined HAVE_FLAC || defined HAVE_VORBIS +int32_t S_UpgradeFormat(const char *fn, char searchfirst) +{ + char *testfn, *extension; + int32_t fp = -1; + + testfn = (char *)Xmalloc(Bstrlen(fn) + 6); + Bstrcpy(testfn, fn); + extension = Bstrrchr(testfn, '.'); + + if (extension) + { +#ifdef HAVE_FLAC + Bstrcpy(extension, ".flac"); + fp = kopen4loadfrommod(testfn, searchfirst); + if (fp >= 0) + { + Bfree(testfn); + return fp; + } +#endif + +#ifdef HAVE_VORBIS + Bstrcpy(extension, ".ogg"); + fp = kopen4loadfrommod(testfn, searchfirst); + if (fp >= 0) + { + Bfree(testfn); + return fp; + } +#endif + } + + Bfree(testfn); + return -1; +} +#endif diff --git a/polymer/eduke32/source/common_game.h b/polymer/eduke32/source/common_game.h index ca78d6c1f..2ee3b07cb 100644 --- a/polymer/eduke32/source/common_game.h +++ b/polymer/eduke32/source/common_game.h @@ -123,6 +123,12 @@ extern uint8_t *basepaltable[BASEPALCOUNT]; extern int32_t G_LoadLookups(void); +////////// + +#if defined HAVE_FLAC || defined HAVE_VORBIS +int32_t S_UpgradeFormat(const char *fn, char searchfirst); +#endif + #ifdef __cplusplus } #endif diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c index 2a3f920c9..711012fed 100644 --- a/polymer/eduke32/source/game.c +++ b/polymer/eduke32/source/game.c @@ -8598,10 +8598,9 @@ void G_StartMusic(void) Bassert(MapInfo[i].musicfn != NULL); { - int32_t res = S_PlayMusic(MapInfo[i].musicfn, i); + S_PlayMusic(MapInfo[i].musicfn); - Bsnprintf(ScriptQuotes[QUOTE_MUSIC], MAXQUOTELEN, "Playing %s", - res ? MapInfo[i].ext_musicfn : MapInfo[i].musicfn); + Bsnprintf(ScriptQuotes[QUOTE_MUSIC], MAXQUOTELEN, "Playing %s", MapInfo[i].musicfn); P_DoQuote(QUOTE_MUSIC, g_player[myconnectindex].ps); } } @@ -9054,9 +9053,7 @@ FAKE_F3: KB_ClearKeyDown(sc_F5); - if (map->ext_musicfn != NULL) - Bstrncpyz(qmusic, map->ext_musicfn, MAXQUOTELEN); - else if (map->musicfn != NULL) + if (map->musicfn != NULL) Bsnprintf(qmusic, MAXQUOTELEN, "%s. Use SHIFT-F5 to change.", map->musicfn); else @@ -9258,36 +9255,13 @@ static void G_ShowDebugHelp(void) #endif } -static char *S_OggifyFilename(char *outputname, const char *newname, const char *origname) -{ - if (!origname) - return outputname; - - outputname = (char *)Xrealloc(outputname, Bstrlen(newname) + Bstrlen(origname) + 1); - - Bstrcpy(outputname, *newname ? newname : origname); - - // a special case for specifying a prefix directory - if (*newname && newname[Bstrlen(newname)-1] == '/') - { - while (*origname == '/') - origname++; - Bstrcat(outputname, origname); - } - - if (Bstrchr(outputname, '.') == NULL) - Bstrcat(outputname, ".ogg"); - - return outputname; -} - static int32_t S_DefineSound(int32_t ID, const char *name) { if ((unsigned)ID >= MAXSOUNDS) return 1; - g_sounds[ID].filename1 = S_OggifyFilename(g_sounds[ID].filename1, name, g_sounds[ID].filename); -// initprintf("(%s)(%s)(%s)\n",g_sounds[ID].filename1,name,g_sounds[ID].filename); + realloc_copy(&g_sounds[ID].filename, name); + // S_LoadSound(ID); return 0; @@ -9296,7 +9270,6 @@ static int32_t S_DefineSound(int32_t ID, const char *name) // Returns: // 0: all OK // -1: ID declaration was invalid: -// -2: map has no .musicfn (and hence will not be considered even if it has an .alt_musicfn) static int32_t S_DefineMusic(const char *ID, const char *name) { int32_t sel = MUS_FIRST_SPECIAL; @@ -9322,29 +9295,9 @@ static int32_t S_DefineMusic(const char *ID, const char *name) return -1; } - ID = MapInfo[sel].musicfn; + realloc_copy(&MapInfo[sel].musicfn, name); - { - map_t *map = &MapInfo[sel]; - const int special = (sel >= MUS_FIRST_SPECIAL); - - map->ext_musicfn = S_OggifyFilename(map->ext_musicfn, name, ID); - - // If we are given a music file name for a proper level that has no - // primary music defined, set it up as both. - if (map->ext_musicfn == NULL && !special && ID == 0 && name) - { - map->musicfn = dup_filename(name); - map->ext_musicfn = dup_filename(name); - } - - -// initprintf("%-15s | ",ID); -// initprintf("%3d %2d %2d | %s\n",sel,ep,lev,MapInfo[sel].alt_musicfn); -// S_PlayMusic(ID,sel); - - return map->musicfn || special ? 0 : -2; - } + return 0; } static int32_t parsedefinitions_game(scriptfile *script, int32_t preload); @@ -9491,9 +9444,6 @@ static int32_t parsedefinitions_game(scriptfile *script, int32_t preload) if (res == -1) initprintf("Error: invalid music ID on line %s:%d\n", script->filename, scriptfile_getlinum(script,tinttokptr)); - else if (res == -2) - initprintf("Error: %s has no primary (CON) music on line %s:%d\n", - ID, script->filename, scriptfile_getlinum(script,tinttokptr)); } } break; @@ -10488,7 +10438,7 @@ static void G_DisplayLogo(void) if (logoflags & LOGO_PLAYMUSIC) { g_musicIndex = MUS_INTRO; - S_PlayMusic(MapInfo[g_musicIndex].musicfn, g_musicIndex); + S_PlayMusic(MapInfo[g_musicIndex].musicfn); } if (!NAM) @@ -10676,7 +10626,6 @@ static void G_Cleanup(void) if (MapInfo[i].name != NULL) Bfree(MapInfo[i].name); if (MapInfo[i].filename != NULL) Bfree(MapInfo[i].filename); if (MapInfo[i].musicfn != NULL) Bfree(MapInfo[i].musicfn); - if (MapInfo[i].ext_musicfn != NULL) Bfree(MapInfo[i].ext_musicfn); G_FreeMapState(i); } @@ -10696,7 +10645,6 @@ static void G_Cleanup(void) for (i=MAXSOUNDS-1; i>=0; i--) { if (g_sounds[i].filename != NULL) Bfree(g_sounds[i].filename); - if (g_sounds[i].filename1 != NULL) Bfree(g_sounds[i].filename1); } #if !defined LUNATIC if (label != NULL && label != (char *)&sprite[0]) Bfree(label); diff --git a/polymer/eduke32/source/gameexec.c b/polymer/eduke32/source/gameexec.c index 571907728..86f1bb906 100644 --- a/polymer/eduke32/source/gameexec.c +++ b/polymer/eduke32/source/gameexec.c @@ -1105,7 +1105,7 @@ int32_t G_StartTrack(int32_t level) { // Only set g_musicIndex on success. g_musicIndex = musicIndex; - S_PlayMusic(MapInfo[musicIndex].musicfn, g_musicIndex); + S_PlayMusic(MapInfo[musicIndex].musicfn); return 0; } diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index ca2bc6260..d19907927 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -595,7 +595,7 @@ typedef struct { typedef struct { int32_t partime, designertime; - char *name, *filename, *musicfn, *alt_musicfn; + char *name, *filename, *musicfn; void *savedstate; } map_t; ]]) diff --git a/polymer/eduke32/source/premap.c b/polymer/eduke32/source/premap.c index 34b05a347..ce29e22e3 100644 --- a/polymer/eduke32/source/premap.c +++ b/polymer/eduke32/source/premap.c @@ -286,9 +286,13 @@ static int32_t G_CacheSound(uint32_t num) if (num >= MAXSOUNDS || ud.config.SoundToggle == 0) return 0; if (ud.config.FXDevice < 0) return 0; - if (!g_sounds[num].filename && !g_sounds[num].filename1) return 0; - if (g_sounds[num].filename1) fp = kopen4loadfrommod(g_sounds[num].filename1,g_loadFromGroupOnly); - if (fp == -1) fp = kopen4loadfrommod(g_sounds[num].filename,g_loadFromGroupOnly); + if (EDUKE32_PREDICT_FALSE(!g_sounds[num].filename)) return 0; + +#if defined HAVE_FLAC || defined HAVE_VORBIS + fp = S_UpgradeFormat(g_sounds[num].filename, g_loadFromGroupOnly); + if (fp == -1) +#endif + fp = kopen4loadfrommod(g_sounds[num].filename,g_loadFromGroupOnly); if (fp == -1) { // OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n",g_sounds[num].filename,num); @@ -439,7 +443,7 @@ void G_CacheMapData(void) if (MapInfo[MUS_LOADING].musicfn) { S_StopMusic(); - S_PlayMusic(MapInfo[MUS_LOADING].musicfn, MUS_LOADING); + S_PlayMusic(MapInfo[MUS_LOADING].musicfn); } starttime = getticks(); @@ -1356,7 +1360,7 @@ void G_NewGame(int32_t vn, int32_t ln, int32_t sk) if (ln == 0 && vn == 3 && (!g_netServer && ud.multimode < 2) && ud.lockout == 0 && (G_GetLogoFlags() & LOGO_NOE4CUTSCENE)==0) { - S_PlayMusic(MapInfo[MUS_BRIEFING].musicfn, MUS_BRIEFING); + S_PlayMusic(MapInfo[MUS_BRIEFING].musicfn); flushperms(); setview(0,0,xdim-1,ydim-1); @@ -1723,14 +1727,6 @@ static void G_LoadMapHack(char *outbuf, const char *filename) } } -static void G_ReallocCopyMusicName(int32_t level_number, const char *levnamebuf, int32_t altp) -{ - char **musfn = altp ? &MapInfo[level_number].ext_musicfn : &MapInfo[level_number].musicfn; - uint8_t len = Bstrlen(levnamebuf) + 1; - *musfn = (char *)Xrealloc(*musfn, len); - Bstrncpy(*musfn, levnamebuf, len); -} - // levnamebuf should have at least size BMAX_PATH void G_SetupFilenameBasedMusic(char *levnamebuf, const char *boardfilename, int32_t level_number) { @@ -1763,13 +1759,12 @@ void G_SetupFilenameBasedMusic(char *levnamebuf, const char *boardfilename, int3 if ((fil = kopen4loadfrommod(levnamebuf, 0)) != -1) { kclose(fil); - G_ReallocCopyMusicName(level_number, levnamebuf, i < (ARRAY_SIZE(exts) - 1)); + realloc_copy(&MapInfo[level_number].musicfn, levnamebuf); return; } } - DO_FREE_AND_NULL(MapInfo[level_number].ext_musicfn); - G_ReallocCopyMusicName(level_number, "dethtoll.mid", 0); + realloc_copy(&MapInfo[level_number].musicfn, "dethtoll.mid"); } static inline int G_HaveUserMap(void) @@ -1918,7 +1913,7 @@ int32_t G_EnterLevel(int32_t g) { g_musicIndex = mii; if (MapInfo[g_musicIndex].musicfn != NULL) - S_PlayMusic(MapInfo[g_musicIndex].musicfn, g_musicIndex); + S_PlayMusic(MapInfo[g_musicIndex].musicfn); } if (g & (MODE_GAME|MODE_EOL)) diff --git a/polymer/eduke32/source/savegame.c b/polymer/eduke32/source/savegame.c index 83bea0949..2a61dbbb2 100644 --- a/polymer/eduke32/source/savegame.c +++ b/polymer/eduke32/source/savegame.c @@ -1995,7 +1995,7 @@ static void postloadplayer(int32_t savegamep) S_StopMusic(); g_musicIndex = musicIdx; - S_PlayMusic(MapInfo[g_musicIndex].musicfn, g_musicIndex); + S_PlayMusic(MapInfo[g_musicIndex].musicfn); } S_PauseMusic(0); diff --git a/polymer/eduke32/source/sector.h b/polymer/eduke32/source/sector.h index e92065b8c..9871d1bdf 100644 --- a/polymer/eduke32/source/sector.h +++ b/polymer/eduke32/source/sector.h @@ -102,7 +102,7 @@ extern void G_RestoreMapState(); typedef struct { int32_t partime, designertime; - char *name, *filename, *musicfn, *ext_musicfn; + char *name, *filename, *musicfn; mapstate_t *savedstate; } map_t; diff --git a/polymer/eduke32/source/sounds.c b/polymer/eduke32/source/sounds.c index 82bb3bd4d..01edc0d42 100644 --- a/polymer/eduke32/source/sounds.c +++ b/polymer/eduke32/source/sounds.c @@ -36,6 +36,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "osd.h" #include "sounds.h" +#include "common_game.h" + #ifdef _WIN32 #include "winlayer.h" #endif @@ -173,11 +175,11 @@ void S_RestartMusic(void) if (ud.recstat != 2 && g_player[myconnectindex].ps->gm&MODE_GAME) { if (MapInfo[g_musicIndex].musicfn != NULL) - S_PlayMusic(MapInfo[g_musicIndex].musicfn, g_musicIndex); + S_PlayMusic(MapInfo[g_musicIndex].musicfn); } else if (MapInfo[MUS_INTRO].musicfn != 0) { - S_PlayMusic(MapInfo[MUS_INTRO].musicfn, MUS_INTRO); + S_PlayMusic(MapInfo[MUS_INTRO].musicfn); } } @@ -208,59 +210,21 @@ void S_MenuSound(void) S_PlaySound(menusnds[SoundNum++ % ARRAY_SIZE(menusnds)]); } -int32_t S_PlayMusic(const char *fn, const int32_t sel) +int32_t S_PlayMusic(const char *fn) { - const char *const ofn = fn; - char *testfn, *extension; int32_t fp, MusicLen; - const char *alt = 0; if (ud.config.MusicToggle == 0) return 0; if (ud.config.MusicDevice < 0) return 0; - if (MapInfo[sel].ext_musicfn != NULL) - alt = fn = MapInfo[sel].ext_musicfn; - if (fn == NULL) return 0; - testfn = (char *)Xmalloc(strlen(fn) + 6); - strcpy(testfn, fn); - extension = strrchr(testfn, '.'); - - do - { - if (extension && !Bstrcasecmp(extension, ".mid")) - { - // we've been asked to load a .mid file, but first let's see - // if there's a flac or an ogg with the same base name lying around - strcpy(extension, ".flac"); - fp = kopen4loadfrommod(testfn, 0); - if (fp >= 0) - { - Bfree(testfn); - break; - } - - strcpy(extension, ".ogg"); - fp = kopen4loadfrommod(testfn, 0); - if (fp >= 0) - { - Bfree(testfn); - break; - } - } - - Bfree(testfn); - - // just use what we've been given +#if defined HAVE_FLAC || defined HAVE_VORBIS + if ((fp = S_UpgradeFormat(fn, 0)) < 0) +#endif fp = kopen4loadfrommod(fn, 0); - if (alt && fp < 0) - fp = kopen4loadfrommod(ofn, 0); - } - while (0); - if (EDUKE32_PREDICT_FALSE(fp < 0)) { OSD_Printf(OSD_ERROR "S_PlayMusic(): error: can't open \"%s\" for playback!\n",fn); @@ -308,7 +272,7 @@ int32_t S_PlayMusic(const char *fn, const int32_t sel) MusicIsWaveform = 1; } - return (alt != 0); + return 0; } int32_t S_GetMusicPosition(void) @@ -420,16 +384,16 @@ int32_t S_LoadSound(uint32_t num) if (num > (unsigned)g_maxSoundPos || ud.config.SoundToggle == 0 || ud.config.FXDevice < 0) return 0; - if (EDUKE32_PREDICT_FALSE(g_sounds[num].filename == NULL && g_sounds[num].filename1 == NULL)) + if (EDUKE32_PREDICT_FALSE(g_sounds[num].filename == NULL)) { OSD_Printf(OSD_ERROR "Sound (#%d) not defined!\n",num); return 0; } - if (g_sounds[num].filename1) - fp = kopen4loadfrommod(g_sounds[num].filename1,g_loadFromGroupOnly); - +#if defined HAVE_FLAC || defined HAVE_VORBIS + fp = S_UpgradeFormat(g_sounds[num].filename, g_loadFromGroupOnly); if (fp == -1) +#endif { fp = kopen4loadfrommod(g_sounds[num].filename,g_loadFromGroupOnly); @@ -767,7 +731,7 @@ int32_t S_PlaySound(int32_t num) if (ud.config.FXDevice < 0) return -1; if (ud.config.SoundToggle==0) return -1; - if ((unsigned)num > (unsigned)g_maxSoundPos || (g_sounds[num].filename == NULL && g_sounds[num].filename1 == NULL)) + if ((unsigned)num > (unsigned)g_maxSoundPos || g_sounds[num].filename == NULL) { OSD_Printf("WARNING: invalid sound #%d\n",num); return -1; diff --git a/polymer/eduke32/source/sounds.h b/polymer/eduke32/source/sounds.h index 747dea19e..23a497bdb 100644 --- a/polymer/eduke32/source/sounds.h +++ b/polymer/eduke32/source/sounds.h @@ -60,17 +60,17 @@ int32_t EnumAudioDevs(struct audioenumdrv **wave, struct audioenumdev **midi, st typedef struct { - int16_t voice; - int16_t ow; uint32_t sndist; uint32_t clock; + int16_t voice; + int16_t ow; } SOUNDOWNER; typedef struct { + char *filename, *ptr; // 8b/16b int32_t length, num, soundsiz; // 12b - char *filename, *ptr, *filename1; // 12b/24b SOUNDOWNER SoundOwner[MAXSOUNDINSTANCES]; // 64b int16_t ps,pe,vo; // 6b char pr,m; // 2b @@ -95,7 +95,7 @@ void S_MusicStartup(void); void S_MusicVolume(int32_t volume); void S_RestartMusic(void); void S_PauseMusic(int32_t onf); -int32_t S_PlayMusic(const char *fn,const int32_t sel); +int32_t S_PlayMusic(const char *fn); int32_t S_PlaySound(int32_t num); int32_t S_PlaySound3D(int32_t num,int32_t i,const vec3_t *pos); void S_SoundShutdown(void); diff --git a/polymer/eduke32/source/sounds_mapster32.c b/polymer/eduke32/source/sounds_mapster32.c index 480e9bc77..2db30bcc5 100644 --- a/polymer/eduke32/source/sounds_mapster32.c +++ b/polymer/eduke32/source/sounds_mapster32.c @@ -137,8 +137,11 @@ int32_t S_LoadSound(uint32_t num) return 0; } - if (g_sounds[num].filename1) fp = kopen4loadfrommod(g_sounds[num].filename1,0);//pathsearchmode - if (fp == -1) fp = kopen4loadfrommod(g_sounds[num].filename,0); +#if defined HAVE_FLAC || defined HAVE_VORBIS + fp = S_UpgradeFormat(g_sounds[num].filename, 0); + if (fp == -1) +#endif + fp = kopen4loadfrommod(g_sounds[num].filename,0); if (fp == -1) { OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n",g_sounds[num].filename,num); diff --git a/polymer/eduke32/source/sounds_mapster32.h b/polymer/eduke32/source/sounds_mapster32.h index d6ec03bfd..8cccd87ef 100644 --- a/polymer/eduke32/source/sounds_mapster32.h +++ b/polymer/eduke32/source/sounds_mapster32.h @@ -35,8 +35,8 @@ typedef struct { } SOUNDOWNER; typedef struct { + char *filename, *ptr; int32_t length, num, soundsiz; - char *filename, *ptr, *filename1; SOUNDOWNER SoundOwner[4]; int16_t ps,pe,vo; char pr,m;