- Fixed: FString::StripRight() stripped the final character of the string if

there were no designated characters to strip at the end of it.
- Added support for Shoutcast/Icecast playlists.
- Added an error message when a playlist could not be opened.
- Added support for PLS format playlists, in addition to M3U.
- Changed FPlayList to use an array of FStrings.
- Fixed: Playlists required every song to be specified by an absolute path.


SVN r951 (trunk)
This commit is contained in:
Randy Heit 2008-04-30 05:36:24 +00:00
parent 8040cdd4ff
commit c86d7e0afd
12 changed files with 292 additions and 126 deletions

View File

@ -1,4 +1,11 @@
April 29, 2008 April 29, 2008
- Fixed: FString::StripRight() stripped the final character of the string if
there were no designated characters to strip at the end of it.
- Added support for Shoutcast/Icecast playlists.
- Added an error message when a playlist could not be opened.
- Added support for PLS format playlists, in addition to M3U.
- Changed FPlayList to use an array of FStrings.
- Fixed: Playlists required every song to be specified by an absolute path.
- Fixed a copy-and-paste error in win32/i_main.cpp for 64-bit mode. - Fixed a copy-and-paste error in win32/i_main.cpp for 64-bit mode.
- Tweaked OPL centering a little. - Tweaked OPL centering a little.

View File

@ -33,127 +33,143 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <errno.h>
#include "cmdlib.h" #include "cmdlib.h"
#include "s_playlist.h" #include "s_playlist.h"
#include "templates.h" #include "templates.h"
#include "v_text.h"
FPlayList::FPlayList (const char *path) FPlayList::FPlayList (const char *path)
{ {
Songs = NULL;
SongList = NULL;
ChangeList (path); ChangeList (path);
} }
FPlayList::~FPlayList () FPlayList::~FPlayList ()
{ {
if (Songs) delete[] Songs;
if (SongList) delete[] SongList;
} }
bool FPlayList::ChangeList (const char *path) bool FPlayList::ChangeList (const char *path)
{ {
char linebuff[256]; FString playlistdir;
size_t songlengths; FString song;
int songcount;
FILE *file; FILE *file;
bool first;
bool pls;
int i;
if (Songs) Songs.Clear();
{
delete[] Songs;
Songs = NULL;
}
if (SongList)
{
delete[] SongList;
SongList = NULL;
}
Position = 0; Position = 0;
NumSongs = 0;
if ( (file = fopen (path, "r")) == NULL) if ( (file = fopen (path, "rb")) == NULL)
return false;
songlengths = 0;
songcount = 0;
while (NextLine (file, linebuff, sizeof(linebuff)))
{ {
songcount++; Printf ("Could not open "TEXTCOLOR_BOLD"%s"TEXTCOLOR_NORMAL": %s\n", path, strerror(errno));
songlengths += strlen (linebuff) + 1; return false;
} }
rewind (file); first = true;
pls = false;
if (songcount > 0) playlistdir = ExtractFilePath(path);
while ((song = NextLine(file)).IsNotEmpty())
{ {
Songs = new char *[songcount]; if (first)
SongList = new char[songlengths];
NumSongs = songcount;
songlengths = 0;
for (songcount = 0; songcount < NumSongs &&
NextLine (file, linebuff, sizeof(linebuff)); songcount++)
{ {
size_t len = strlen (linebuff) + 1; first = false;
// Check for ID tags.
memcpy (SongList + songlengths, linebuff, len); if (song.Compare("[playlist]") == 0)
Songs[songcount] = SongList + songlengths; {
songlengths += len; pls = true;
continue;
}
} }
NumSongs = songcount; // For a .PLS file, skip anything that doesn't start with File[0-9]+=
} if (pls)
{
if (strncmp(song, "File", 4) != 0)
{
continue;
}
for (i = 4; song[i] >= '0' && song[i] <= '9'; ++i)
{
}
if (song[i] != '=')
{
continue;
}
song = song.Mid(i + 1);
}
// Check for relative paths.
long slashpos = song.IndexOf('/');
if (slashpos == 0)
{
// First character is a slash, so it's absolute.
}
#ifdef _WIN32
else if (slashpos == 2 && song[1] == ':')
{
// Name is something like X:/, so it's absolute.
}
#endif
else if (song.IndexOf("://") == slashpos - 1)
{
// Name is a URL, so it's absolute.
}
else
{
// Path is relative; append it to the playlist directory.
song = playlistdir + song;
}
Songs.Push(song);
}
fclose (file); fclose (file);
return NumSongs > 0; return Songs.Size() != 0;
} }
bool FPlayList::NextLine (FILE *file, char *buffer, int n) FString FPlayList::NextLine (FILE *file)
{ {
char buffer[512];
char *skipper; char *skipper;
do do
{ {
if (NULL == fgets (buffer, n, file)) if (NULL == fgets (buffer, countof(buffer), file))
return false; return "";
for (skipper = buffer; *skipper != 0 && *skipper <= ' '; skipper++) for (skipper = buffer; *skipper != 0 && *skipper <= ' '; skipper++)
; ;
} while (*skipper == '#' || *skipper == 0); } while (*skipper == '#' || *skipper == 0);
if (skipper > buffer) FString str(skipper);
memmove (buffer, skipper, strlen (skipper)+1); str.StripRight("\r\n");
FixPathSeperator(str);
if (buffer[strlen (buffer)-1] == '\n') return str;
buffer[strlen (buffer)-1] = 0;
FixPathSeperator (buffer);
return true;
} }
// Shuffles the playlist and resets the position to the start // Shuffles the playlist and resets the position to the start
void FPlayList::Shuffle () void FPlayList::Shuffle ()
{ {
unsigned int numsongs = Songs.Size();
int i; int i;
for (i = 0; i < NumSongs; ++i) for (i = 0; i < numsongs; ++i)
{ {
swap (Songs[i], Songs[(rand() % (NumSongs - i)) + i]); swap (Songs[i], Songs[(rand() % (numsongs - i)) + i]);
} }
Position = 0; Position = 0;
} }
int FPlayList::GetNumSongs () const int FPlayList::GetNumSongs () const
{ {
return NumSongs; return (int)Songs.Size();
} }
int FPlayList::SetPosition (int position) int FPlayList::SetPosition (int position)
{ {
if ((unsigned)position >= (unsigned)NumSongs) if ((unsigned)position >= Songs.Size())
{ {
Position = 0; Position = 0;
} }
@ -172,7 +188,7 @@ int FPlayList::GetPosition () const
int FPlayList::Advance () int FPlayList::Advance ()
{ {
if (++Position >= NumSongs) if (++Position >= Songs.Size())
{ {
Position = 0; Position = 0;
} }
@ -184,7 +200,7 @@ int FPlayList::Backup ()
{ {
if (--Position < 0) if (--Position < 0)
{ {
Position = NumSongs - 1; Position = Songs.Size() - 1;
} }
DPrintf ("Playlist backed up to song %d\n", Position); DPrintf ("Playlist backed up to song %d\n", Position);
return Position; return Position;
@ -192,7 +208,7 @@ int FPlayList::Backup ()
const char *FPlayList::GetSong (int position) const const char *FPlayList::GetSong (int position) const
{ {
if ((unsigned)position >= (unsigned)NumSongs) if ((unsigned)position >= Songs.Size())
return NULL; return NULL;
return Songs[position]; return Songs[position];

View File

@ -51,12 +51,10 @@ public:
const char *GetSong (int position) const; const char *GetSong (int position) const;
private: private:
static bool NextLine (FILE *file, char *buffer, int n); static FString NextLine (FILE *file);
int Position; unsigned int Position;
int NumSongs; TArray<FString> Songs;
char **Songs; // Pointers into SongList
char *SongList;
}; };
#endif //__S_PLAYLIST_H__ #endif //__S_PLAYLIST_H__

View File

@ -1181,10 +1181,12 @@ void S_UpdateSounds (void *listener_p)
if (GSnd == NULL) if (GSnd == NULL)
return; return;
// [RH] Update playlist // [RH] Update music and/or playlist. I_QrySongPlaying() must be called
if (PlayList && // to attempt to reconnect to broken net streams and to advance the
mus_playing.handle && // playlist when the current song finishes.
!I_QrySongPlaying(mus_playing.handle)) if (mus_playing.handle != NULL &&
!I_QrySongPlaying(mus_playing.handle) &&
PlayList)
{ {
PlayList->Advance(); PlayList->Advance();
S_ActivatePlayList(false); S_ActivatePlayList(false);
@ -1335,42 +1337,64 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force)
int lumpnum = -1; int lumpnum = -1;
int offset, length; int offset, length;
int device = MDEV_DEFAULT; int device = MDEV_DEFAULT;
void *handle = NULL;
int *devp = MidiDevices.CheckKey(FName(musicname)); int *devp = MidiDevices.CheckKey(FName(musicname));
if (devp != NULL) device = *devp; if (devp != NULL) device = *devp;
// Strip off any leading file:// component.
if (strncmp(musicname, "file://", 7) == 0)
{
musicname += 7;
}
if (!FileExists (musicname)) if (!FileExists (musicname))
{ {
if ((lumpnum = Wads.CheckNumForFullName (musicname, true, ns_music)) == -1) if ((lumpnum = Wads.CheckNumForFullName (musicname, true, ns_music)) == -1)
{ {
Printf ("Music \"%s\" not found\n", musicname); if (strstr(musicname, "://") > musicname)
return false;
}
if (!Wads.IsUncompressedFile(lumpnum))
{
// We must cache the music data and use it from memory.
// shut down old music before reallocating and overwriting the cache!
S_StopMusic (true);
offset = -1; // this tells the low level code that the music
// is being used from memory
length = Wads.LumpLength (lumpnum);
if (length == 0)
{ {
// Looks like a URL; try it as such.
handle = I_RegisterURLSong(musicname);
if (handle == NULL)
{
Printf ("Could not open \"%s\"\n", musicname);
return false;
}
}
else
{
Printf ("Music \"%s\" not found\n", musicname);
return false; return false;
} }
musiccache.Resize(length);
Wads.ReadLump(lumpnum, &musiccache[0]);
} }
else if (handle == NULL)
{ {
offset = Wads.GetLumpOffset (lumpnum); if (!Wads.IsUncompressedFile(lumpnum))
length = Wads.LumpLength (lumpnum);
if (length == 0)
{ {
return false; // We must cache the music data and use it from memory.
// shut down old music before reallocating and overwriting the cache!
S_StopMusic (true);
offset = -1; // this tells the low level code that the music
// is being used from memory
length = Wads.LumpLength (lumpnum);
if (length == 0)
{
return false;
}
musiccache.Resize(length);
Wads.ReadLump(lumpnum, &musiccache[0]);
}
else
{
offset = Wads.GetLumpOffset (lumpnum);
length = Wads.LumpLength (lumpnum);
if (length == 0)
{
return false;
}
} }
} }
} }
@ -1394,7 +1418,11 @@ bool S_ChangeMusic (const char *musicname, int order, bool looping, bool force)
} }
// load & register it // load & register it
if (offset != -1) if (handle != NULL)
{
mus_playing.handle = handle;
}
else if (offset != -1)
{ {
mus_playing.handle = I_RegisterSong (lumpnum != -1 ? mus_playing.handle = I_RegisterSong (lumpnum != -1 ?
Wads.GetWadFullName (Wads.GetLumpFile (lumpnum)) : Wads.GetWadFullName (Wads.GetLumpFile (lumpnum)) :

View File

@ -208,7 +208,8 @@ static const char *OpenStateNames[] =
"Error", "Error",
"Connecting", "Connecting",
"Buffering", "Buffering",
"Seeking" "Seeking",
"Streaming"
}; };
// CODE -------------------------------------------------------------------- // CODE --------------------------------------------------------------------
@ -266,9 +267,9 @@ static const char *Enum_NameForNum(const FEnumList *list, int num)
class FMODStreamCapsule : public SoundStream class FMODStreamCapsule : public SoundStream
{ {
public: public:
FMODStreamCapsule(FMOD::Sound *stream, FMODSoundRenderer *owner) FMODStreamCapsule(FMOD::Sound *stream, FMODSoundRenderer *owner, const char *url)
: Owner(owner), Stream(NULL), Channel(NULL), : Owner(owner), Stream(NULL), Channel(NULL),
UserData(NULL), Callback(NULL), Ended(false) UserData(NULL), Callback(NULL), Ended(false), URL(url)
{ {
SetStream(stream); SetStream(stream);
} }
@ -280,6 +281,10 @@ public:
~FMODStreamCapsule() ~FMODStreamCapsule()
{ {
if (Channel != NULL)
{
Channel->stop();
}
if (Stream != NULL) if (Stream != NULL)
{ {
Stream->release(); Stream->release();
@ -303,7 +308,11 @@ public:
{ {
FMOD_RESULT result; FMOD_RESULT result;
Stream->setMode(looping ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF); if (URL.IsNotEmpty())
{ // Net streams cannot be looped, because they cannot be seeked.
looping = false;
}
Stream->setMode((looping ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF) | FMOD_SOFTWARE | FMOD_2D);
result = Owner->Sys->playSound(FMOD_CHANNEL_FREE, Stream, true, &Channel); result = Owner->Sys->playSound(FMOD_CHANNEL_FREE, Stream, true, &Channel);
if (result != FMOD_OK) if (result != FMOD_OK)
{ {
@ -320,6 +329,11 @@ public:
Channel->setReverbProperties(&reverb); Channel->setReverbProperties(&reverb);
} }
Channel->setPaused(false); Channel->setPaused(false);
Ended = false;
JustStarted = true;
Starved = false;
Loop = looping;
Volume = volume;
return true; return true;
} }
@ -352,12 +366,82 @@ public:
return 0; return 0;
} }
bool IsEnded()
{
bool is;
FMOD_OPENSTATE openstate = FMOD_OPENSTATE_MAX;
bool starving;
if (Stream == NULL)
{
return true;
}
if (FMOD_OK != Stream->getOpenState(&openstate, NULL, &starving))
{
openstate = FMOD_OPENSTATE_ERROR;
}
if (openstate == FMOD_OPENSTATE_ERROR)
{
if (Channel != NULL)
{
Channel->stop();
Channel = NULL;
}
return true;
}
if (Channel != NULL && (FMOD_OK != Channel->isPlaying(&is) || is == false))
{
return true;
}
if (Ended)
{
Channel->stop();
Channel = NULL;
return true;
}
if (URL.IsNotEmpty() && !JustStarted && openstate == FMOD_OPENSTATE_READY)
{
// Reconnect the stream, since it seems to have stalled.
// The only way to do this appears to be to completely recreate it.
FMOD_RESULT result;
Channel->stop();
Stream->release();
Channel = NULL;
Stream = NULL;
Owner->Sys->setStreamBufferSize(64*1024, FMOD_TIMEUNIT_RAWBYTES);
// Open the stream asynchronously, so we don't hang the game while trying to reconnect.
// (It would be nice to do the initial open asynchronously as well, but I'd need to rethink
// the music system design to pull that off.)
result = Owner->Sys->createSound(URL, (Loop ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF) | FMOD_SOFTWARE | FMOD_2D |
FMOD_CREATESTREAM | FMOD_NONBLOCKING, NULL, &Stream);
JustStarted = true;
Owner->Sys->setStreamBufferSize(16*1024, FMOD_TIMEUNIT_RAWBYTES);
return result != FMOD_OK;
}
if (JustStarted && openstate == FMOD_OPENSTATE_STREAMING)
{
JustStarted = false;
}
if (JustStarted && Channel == NULL && openstate == FMOD_OPENSTATE_READY)
{
return !Play(Loop, Volume);
}
if (starving != Starved)
{ // Mute the sound if it's starving.
Channel->setVolume(starving ? 0 : Volume);
Starved = starving;
}
return false;
}
void SetVolume(float volume) void SetVolume(float volume)
{ {
if (Channel != NULL) if (Channel != NULL && !Starved)
{ {
Channel->setVolume(volume); Channel->setVolume(volume);
} }
Volume = volume;
} }
// Sets the current order number for a MOD-type song, or the position in ms // Sets the current order number for a MOD-type song, or the position in ms
@ -387,12 +471,17 @@ public:
if (FMOD_OK == Stream->getOpenState(&openstate, &percentbuffered, &starving)) if (FMOD_OK == Stream->getOpenState(&openstate, &percentbuffered, &starving))
{ {
stats = (openstate <= FMOD_OPENSTATE_SEEKING ? OpenStateNames[openstate] : "Unknown state"); stats = (openstate <= FMOD_OPENSTATE_STREAMING ? OpenStateNames[openstate] : "Unknown state");
stats.AppendFormat(",%3d%% buffered, %s", percentbuffered, starving ? "Starving" : "Well-fed"); stats.AppendFormat(",%3d%% buffered, %s", percentbuffered, starving ? "Starving" : "Well-fed");
} }
if (Channel != NULL && FMOD_OK == Channel->getPosition(&position, FMOD_TIMEUNIT_MS)) if (Channel != NULL && FMOD_OK == Channel->getPosition(&position, FMOD_TIMEUNIT_MS))
{ {
stats.AppendFormat(", %d ms", position); stats.AppendFormat(", %d", position);
if (FMOD_OK == Stream->getLength(&position, FMOD_TIMEUNIT_MS))
{
stats.AppendFormat("/%d", position);
}
stats += " ms";
} }
return stats; return stats;
} }
@ -428,7 +517,12 @@ private:
FMOD::Channel *Channel; FMOD::Channel *Channel;
void *UserData; void *UserData;
SoundStreamCallback Callback; SoundStreamCallback Callback;
FString URL;
bool Ended; bool Ended;
bool JustStarted;
bool Starved;
bool Loop;
float Volume;
}; };
//========================================================================== //==========================================================================
@ -1086,6 +1180,7 @@ SoundStream *FMODSoundRenderer::OpenStream(const char *filename_or_data, int fla
FMOD_CREATESOUNDEXINFO exinfo = { sizeof(exinfo), }; FMOD_CREATESOUNDEXINFO exinfo = { sizeof(exinfo), };
FMOD::Sound *stream; FMOD::Sound *stream;
FMOD_RESULT result; FMOD_RESULT result;
bool url;
mode = FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM; mode = FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM;
if (flags & SoundStream::Loop) if (flags & SoundStream::Loop)
@ -1104,7 +1199,19 @@ SoundStream *FMODSoundRenderer::OpenStream(const char *filename_or_data, int fla
exinfo.dlsname = snd_midipatchset; exinfo.dlsname = snd_midipatchset;
} }
url = (offset == 0 && length == 0 && strstr(filename_or_data, "://") > filename_or_data);
if (url)
{
// Use a larger buffer for URLs so that it's less likely to be effected
// by hiccups in the data rate from the remote server.
Sys->setStreamBufferSize(64*1024, FMOD_TIMEUNIT_RAWBYTES);
}
result = Sys->createSound(filename_or_data, mode, &exinfo, &stream); result = Sys->createSound(filename_or_data, mode, &exinfo, &stream);
if (url)
{
// Restore standard buffer size.
Sys->setStreamBufferSize(16*1024, FMOD_TIMEUNIT_RAWBYTES);
}
if (result == FMOD_ERR_FORMAT && exinfo.dlsname != NULL) if (result == FMOD_ERR_FORMAT && exinfo.dlsname != NULL)
{ {
// FMOD_ERR_FORMAT could refer to either the main sound file or // FMOD_ERR_FORMAT could refer to either the main sound file or
@ -1119,7 +1226,7 @@ SoundStream *FMODSoundRenderer::OpenStream(const char *filename_or_data, int fla
} }
if (result == FMOD_OK) if (result == FMOD_OK)
{ {
return new FMODStreamCapsule(stream, this); return new FMODStreamCapsule(stream, this, url ? filename_or_data : NULL);
} }
return NULL; return NULL;
} }

View File

@ -259,6 +259,23 @@ void I_UnRegisterSong (void *handle)
} }
} }
void *I_RegisterURLSong (const char *url)
{
StreamSong *song;
if (GSnd == NULL)
{
return NULL;
}
song = new StreamSong(url, 0, 0);
if (song->IsValid())
{
return song;
}
delete song;
return NULL;
}
void *I_RegisterSong (const char *filename, char *musiccache, int offset, int len, int device) void *I_RegisterSong (const char *filename, char *musiccache, int offset, int len, int device)
{ {
FILE *file; FILE *file;

View File

@ -56,6 +56,7 @@ void I_ResumeSong (void *handle);
// Registers a song handle to song data. // Registers a song handle to song data.
void *I_RegisterSong (const char *file, char * musiccache, int offset, int length, int device); void *I_RegisterSong (const char *file, char * musiccache, int offset, int length, int device);
void *I_RegisterCDSong (int track, int cdid = 0); void *I_RegisterCDSong (int track, int cdid = 0);
void *I_RegisterURLSong (const char *url);
// Called by anything that wishes to start music. // Called by anything that wishes to start music.
// Plays a song, and when the song is done, // Plays a song, and when the song is done,

View File

@ -452,10 +452,9 @@ public:
FString GetStats(); FString GetStats();
protected: protected:
StreamSong () : m_Stream(NULL), m_LastPos(0) {} StreamSong () : m_Stream(NULL) {}
SoundStream *m_Stream; SoundStream *m_Stream;
int m_LastPos;
}; };
// MIDI file played with Timidity and possibly streamed through FMOD -------- // MIDI file played with Timidity and possibly streamed through FMOD --------

View File

@ -53,12 +53,13 @@ public:
Loop = 16 Loop = 16
}; };
virtual bool Play (bool looping, float volume) = 0; virtual bool Play(bool looping, float volume) = 0;
virtual void Stop () = 0; virtual void Stop() = 0;
virtual void SetVolume (float volume) = 0; virtual void SetVolume(float volume) = 0;
virtual bool SetPaused (bool paused) = 0; virtual bool SetPaused(bool paused) = 0;
virtual unsigned int GetPosition () = 0; virtual unsigned int GetPosition() = 0;
virtual bool SetPosition (int pos); virtual bool IsEnded() = 0;
virtual bool SetPosition(int pos);
virtual FString GetStats(); virtual FString GetStats();
}; };

View File

@ -8,7 +8,6 @@ void StreamSong::Play (bool looping)
if (m_Stream->Play (m_Looping, 1)) if (m_Stream->Play (m_Looping, 1))
{ {
m_Status = STATE_Playing; m_Status = STATE_Playing;
m_LastPos = 0;
} }
} }
@ -65,18 +64,11 @@ bool StreamSong::IsPlaying ()
{ {
if (m_Status != STATE_Stopped) if (m_Status != STATE_Stopped)
{ {
if (m_Looping) if (m_Stream->IsEnded())
return true;
int pos = m_Stream->GetPosition ();
if (pos < m_LastPos)
{ {
Stop (); Stop();
return false; return false;
} }
m_LastPos = pos;
return true; return true;
} }
return false; return false;

View File

@ -385,7 +385,7 @@ FString FString::Mid (size_t pos, size_t numChars) const
{ {
return FString(); return FString();
} }
if (pos + numChars > len) if (pos + numChars > len || pos + numChars < pos)
{ {
numChars = len - pos; numChars = len - pos;
} }
@ -631,7 +631,7 @@ void FString::StripLeft (const char *charset)
void FString::StripRight () void FString::StripRight ()
{ {
size_t max = Len(), i; size_t max = Len(), i;
for (i = max - 1; i-- > 0; ) for (i = max; i-- > 0; )
{ {
if (!isspace(Chars[i])) if (!isspace(Chars[i]))
break; break;
@ -658,7 +658,7 @@ void FString::StripRight (const FString &charset)
void FString::StripRight (const char *charset) void FString::StripRight (const char *charset)
{ {
size_t max = Len(), i; size_t max = Len(), i;
for (i = max - 1; i-- > 0; ) for (i = max; i-- > 0; )
{ {
if (!strchr (charset, Chars[i])) if (!strchr (charset, Chars[i]))
break; break;

View File

@ -164,7 +164,7 @@ public:
FString Left (size_t numChars) const; FString Left (size_t numChars) const;
FString Right (size_t numChars) const; FString Right (size_t numChars) const;
FString Mid (size_t pos, size_t numChars) const; FString Mid (size_t pos, size_t numChars = ~(size_t)0) const;
long IndexOf (const FString &substr, long startIndex=0) const; long IndexOf (const FString &substr, long startIndex=0) const;
long IndexOf (const char *substr, long startIndex=0) const; long IndexOf (const char *substr, long startIndex=0) const;