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
This commit is contained in:
hendricks266 2015-01-25 12:17:59 +00:00
parent ef4a19bb50
commit b6d625b202
13 changed files with 99 additions and 138 deletions

View file

@ -79,6 +79,13 @@ static inline char *dup_filename(const char *fn)
return Bstrncpyz(buf, fn, BMAX_PATH); 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 getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens);
int32_t G_CheckCmdSwitch(int32_t argc, const char **argv, const char *str); int32_t G_CheckCmdSwitch(int32_t argc, const char **argv, const char *str);

View file

@ -1062,3 +1062,41 @@ int32_t G_LoadLookups(void)
return 0; 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

View file

@ -123,6 +123,12 @@ extern uint8_t *basepaltable[BASEPALCOUNT];
extern int32_t G_LoadLookups(void); 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 #ifdef __cplusplus
} }
#endif #endif

View file

@ -8598,10 +8598,9 @@ void G_StartMusic(void)
Bassert(MapInfo[i].musicfn != NULL); 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", Bsnprintf(ScriptQuotes[QUOTE_MUSIC], MAXQUOTELEN, "Playing %s", MapInfo[i].musicfn);
res ? MapInfo[i].ext_musicfn : MapInfo[i].musicfn);
P_DoQuote(QUOTE_MUSIC, g_player[myconnectindex].ps); P_DoQuote(QUOTE_MUSIC, g_player[myconnectindex].ps);
} }
} }
@ -9054,9 +9053,7 @@ FAKE_F3:
KB_ClearKeyDown(sc_F5); KB_ClearKeyDown(sc_F5);
if (map->ext_musicfn != NULL) if (map->musicfn != NULL)
Bstrncpyz(qmusic, map->ext_musicfn, MAXQUOTELEN);
else if (map->musicfn != NULL)
Bsnprintf(qmusic, MAXQUOTELEN, "%s. Use SHIFT-F5 to change.", Bsnprintf(qmusic, MAXQUOTELEN, "%s. Use SHIFT-F5 to change.",
map->musicfn); map->musicfn);
else else
@ -9258,36 +9255,13 @@ static void G_ShowDebugHelp(void)
#endif #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) static int32_t S_DefineSound(int32_t ID, const char *name)
{ {
if ((unsigned)ID >= MAXSOUNDS) if ((unsigned)ID >= MAXSOUNDS)
return 1; return 1;
g_sounds[ID].filename1 = S_OggifyFilename(g_sounds[ID].filename1, name, g_sounds[ID].filename); realloc_copy(&g_sounds[ID].filename, name);
// initprintf("(%s)(%s)(%s)\n",g_sounds[ID].filename1,name,g_sounds[ID].filename);
// S_LoadSound(ID); // S_LoadSound(ID);
return 0; return 0;
@ -9296,7 +9270,6 @@ static int32_t S_DefineSound(int32_t ID, const char *name)
// Returns: // Returns:
// 0: all OK // 0: all OK
// -1: ID declaration was invalid: // -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) static int32_t S_DefineMusic(const char *ID, const char *name)
{ {
int32_t sel = MUS_FIRST_SPECIAL; int32_t sel = MUS_FIRST_SPECIAL;
@ -9322,29 +9295,9 @@ static int32_t S_DefineMusic(const char *ID, const char *name)
return -1; return -1;
} }
ID = MapInfo[sel].musicfn; realloc_copy(&MapInfo[sel].musicfn, name);
{ return 0;
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;
}
} }
static int32_t parsedefinitions_game(scriptfile *script, int32_t preload); 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) if (res == -1)
initprintf("Error: invalid music ID on line %s:%d\n", initprintf("Error: invalid music ID on line %s:%d\n",
script->filename, scriptfile_getlinum(script,tinttokptr)); 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; break;
@ -10488,7 +10438,7 @@ static void G_DisplayLogo(void)
if (logoflags & LOGO_PLAYMUSIC) if (logoflags & LOGO_PLAYMUSIC)
{ {
g_musicIndex = MUS_INTRO; g_musicIndex = MUS_INTRO;
S_PlayMusic(MapInfo[g_musicIndex].musicfn, g_musicIndex); S_PlayMusic(MapInfo[g_musicIndex].musicfn);
} }
if (!NAM) if (!NAM)
@ -10676,7 +10626,6 @@ static void G_Cleanup(void)
if (MapInfo[i].name != NULL) Bfree(MapInfo[i].name); if (MapInfo[i].name != NULL) Bfree(MapInfo[i].name);
if (MapInfo[i].filename != NULL) Bfree(MapInfo[i].filename); if (MapInfo[i].filename != NULL) Bfree(MapInfo[i].filename);
if (MapInfo[i].musicfn != NULL) Bfree(MapInfo[i].musicfn); if (MapInfo[i].musicfn != NULL) Bfree(MapInfo[i].musicfn);
if (MapInfo[i].ext_musicfn != NULL) Bfree(MapInfo[i].ext_musicfn);
G_FreeMapState(i); G_FreeMapState(i);
} }
@ -10696,7 +10645,6 @@ static void G_Cleanup(void)
for (i=MAXSOUNDS-1; i>=0; i--) for (i=MAXSOUNDS-1; i>=0; i--)
{ {
if (g_sounds[i].filename != NULL) Bfree(g_sounds[i].filename); 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 !defined LUNATIC
if (label != NULL && label != (char *)&sprite[0]) Bfree(label); if (label != NULL && label != (char *)&sprite[0]) Bfree(label);

View file

@ -1105,7 +1105,7 @@ int32_t G_StartTrack(int32_t level)
{ {
// Only set g_musicIndex on success. // Only set g_musicIndex on success.
g_musicIndex = musicIndex; g_musicIndex = musicIndex;
S_PlayMusic(MapInfo[musicIndex].musicfn, g_musicIndex); S_PlayMusic(MapInfo[musicIndex].musicfn);
return 0; return 0;
} }

View file

@ -595,7 +595,7 @@ typedef struct {
typedef struct { typedef struct {
int32_t partime, designertime; int32_t partime, designertime;
char *name, *filename, *musicfn, *alt_musicfn; char *name, *filename, *musicfn;
void *savedstate; void *savedstate;
} map_t; } map_t;
]]) ]])

View file

@ -286,9 +286,13 @@ static int32_t G_CacheSound(uint32_t num)
if (num >= MAXSOUNDS || ud.config.SoundToggle == 0) return 0; if (num >= MAXSOUNDS || ud.config.SoundToggle == 0) return 0;
if (ud.config.FXDevice < 0) return 0; if (ud.config.FXDevice < 0) return 0;
if (!g_sounds[num].filename && !g_sounds[num].filename1) return 0; if (EDUKE32_PREDICT_FALSE(!g_sounds[num].filename)) 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 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) if (fp == -1)
{ {
// OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n",g_sounds[num].filename,num); // 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) if (MapInfo[MUS_LOADING].musicfn)
{ {
S_StopMusic(); S_StopMusic();
S_PlayMusic(MapInfo[MUS_LOADING].musicfn, MUS_LOADING); S_PlayMusic(MapInfo[MUS_LOADING].musicfn);
} }
starttime = getticks(); 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 if (ln == 0 && vn == 3 && (!g_netServer && ud.multimode < 2) && ud.lockout == 0
&& (G_GetLogoFlags() & LOGO_NOE4CUTSCENE)==0) && (G_GetLogoFlags() & LOGO_NOE4CUTSCENE)==0)
{ {
S_PlayMusic(MapInfo[MUS_BRIEFING].musicfn, MUS_BRIEFING); S_PlayMusic(MapInfo[MUS_BRIEFING].musicfn);
flushperms(); flushperms();
setview(0,0,xdim-1,ydim-1); 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 // levnamebuf should have at least size BMAX_PATH
void G_SetupFilenameBasedMusic(char *levnamebuf, const char *boardfilename, int32_t level_number) 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) if ((fil = kopen4loadfrommod(levnamebuf, 0)) != -1)
{ {
kclose(fil); kclose(fil);
G_ReallocCopyMusicName(level_number, levnamebuf, i < (ARRAY_SIZE(exts) - 1)); realloc_copy(&MapInfo[level_number].musicfn, levnamebuf);
return; return;
} }
} }
DO_FREE_AND_NULL(MapInfo[level_number].ext_musicfn); realloc_copy(&MapInfo[level_number].musicfn, "dethtoll.mid");
G_ReallocCopyMusicName(level_number, "dethtoll.mid", 0);
} }
static inline int G_HaveUserMap(void) static inline int G_HaveUserMap(void)
@ -1918,7 +1913,7 @@ int32_t G_EnterLevel(int32_t g)
{ {
g_musicIndex = mii; g_musicIndex = mii;
if (MapInfo[g_musicIndex].musicfn != NULL) 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)) if (g & (MODE_GAME|MODE_EOL))

View file

@ -1995,7 +1995,7 @@ static void postloadplayer(int32_t savegamep)
S_StopMusic(); S_StopMusic();
g_musicIndex = musicIdx; g_musicIndex = musicIdx;
S_PlayMusic(MapInfo[g_musicIndex].musicfn, g_musicIndex); S_PlayMusic(MapInfo[g_musicIndex].musicfn);
} }
S_PauseMusic(0); S_PauseMusic(0);

View file

@ -102,7 +102,7 @@ extern void G_RestoreMapState();
typedef struct { typedef struct {
int32_t partime, designertime; int32_t partime, designertime;
char *name, *filename, *musicfn, *ext_musicfn; char *name, *filename, *musicfn;
mapstate_t *savedstate; mapstate_t *savedstate;
} map_t; } map_t;

View file

@ -36,6 +36,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "osd.h" #include "osd.h"
#include "sounds.h" #include "sounds.h"
#include "common_game.h"
#ifdef _WIN32 #ifdef _WIN32
#include "winlayer.h" #include "winlayer.h"
#endif #endif
@ -173,11 +175,11 @@ void S_RestartMusic(void)
if (ud.recstat != 2 && g_player[myconnectindex].ps->gm&MODE_GAME) if (ud.recstat != 2 && g_player[myconnectindex].ps->gm&MODE_GAME)
{ {
if (MapInfo[g_musicIndex].musicfn != NULL) 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) 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)]); 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; int32_t fp, MusicLen;
const char *alt = 0;
if (ud.config.MusicToggle == 0) return 0; if (ud.config.MusicToggle == 0) return 0;
if (ud.config.MusicDevice < 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) if (fn == NULL)
return 0; return 0;
testfn = (char *)Xmalloc(strlen(fn) + 6); #if defined HAVE_FLAC || defined HAVE_VORBIS
strcpy(testfn, fn); if ((fp = S_UpgradeFormat(fn, 0)) < 0)
extension = strrchr(testfn, '.'); #endif
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
fp = kopen4loadfrommod(fn, 0); fp = kopen4loadfrommod(fn, 0);
if (alt && fp < 0)
fp = kopen4loadfrommod(ofn, 0);
}
while (0);
if (EDUKE32_PREDICT_FALSE(fp < 0)) if (EDUKE32_PREDICT_FALSE(fp < 0))
{ {
OSD_Printf(OSD_ERROR "S_PlayMusic(): error: can't open \"%s\" for playback!\n",fn); 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; MusicIsWaveform = 1;
} }
return (alt != 0); return 0;
} }
int32_t S_GetMusicPosition(void) 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 (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); OSD_Printf(OSD_ERROR "Sound (#%d) not defined!\n",num);
return 0; return 0;
} }
if (g_sounds[num].filename1) #if defined HAVE_FLAC || defined HAVE_VORBIS
fp = kopen4loadfrommod(g_sounds[num].filename1,g_loadFromGroupOnly); fp = S_UpgradeFormat(g_sounds[num].filename, g_loadFromGroupOnly);
if (fp == -1) if (fp == -1)
#endif
{ {
fp = kopen4loadfrommod(g_sounds[num].filename,g_loadFromGroupOnly); 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.FXDevice < 0) return -1;
if (ud.config.SoundToggle==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); OSD_Printf("WARNING: invalid sound #%d\n",num);
return -1; return -1;

View file

@ -60,17 +60,17 @@ int32_t EnumAudioDevs(struct audioenumdrv **wave, struct audioenumdev **midi, st
typedef struct typedef struct
{ {
int16_t voice;
int16_t ow;
uint32_t sndist; uint32_t sndist;
uint32_t clock; uint32_t clock;
int16_t voice;
int16_t ow;
} SOUNDOWNER; } SOUNDOWNER;
typedef struct typedef struct
{ {
char *filename, *ptr; // 8b/16b
int32_t length, num, soundsiz; // 12b int32_t length, num, soundsiz; // 12b
char *filename, *ptr, *filename1; // 12b/24b
SOUNDOWNER SoundOwner[MAXSOUNDINSTANCES]; // 64b SOUNDOWNER SoundOwner[MAXSOUNDINSTANCES]; // 64b
int16_t ps,pe,vo; // 6b int16_t ps,pe,vo; // 6b
char pr,m; // 2b char pr,m; // 2b
@ -95,7 +95,7 @@ void S_MusicStartup(void);
void S_MusicVolume(int32_t volume); void S_MusicVolume(int32_t volume);
void S_RestartMusic(void); void S_RestartMusic(void);
void S_PauseMusic(int32_t onf); 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_PlaySound(int32_t num);
int32_t S_PlaySound3D(int32_t num,int32_t i,const vec3_t *pos); int32_t S_PlaySound3D(int32_t num,int32_t i,const vec3_t *pos);
void S_SoundShutdown(void); void S_SoundShutdown(void);

View file

@ -137,8 +137,11 @@ int32_t S_LoadSound(uint32_t num)
return 0; return 0;
} }
if (g_sounds[num].filename1) fp = kopen4loadfrommod(g_sounds[num].filename1,0);//pathsearchmode #if defined HAVE_FLAC || defined HAVE_VORBIS
if (fp == -1) fp = kopen4loadfrommod(g_sounds[num].filename,0); fp = S_UpgradeFormat(g_sounds[num].filename, 0);
if (fp == -1)
#endif
fp = kopen4loadfrommod(g_sounds[num].filename,0);
if (fp == -1) if (fp == -1)
{ {
OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n",g_sounds[num].filename,num); OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n",g_sounds[num].filename,num);

View file

@ -35,8 +35,8 @@ typedef struct {
} SOUNDOWNER; } SOUNDOWNER;
typedef struct { typedef struct {
char *filename, *ptr;
int32_t length, num, soundsiz; int32_t length, num, soundsiz;
char *filename, *ptr, *filename1;
SOUNDOWNER SoundOwner[4]; SOUNDOWNER SoundOwner[4];
int16_t ps,pe,vo; int16_t ps,pe,vo;
char pr,m; char pr,m;