implemented music playlist system with 10 playlists, automatic track
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 29 Jan 2010 19:03:08 +0000 (19:03 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 29 Jan 2010 19:03:08 +0000 (19:03 +0000)
resume when switching between playlists (setting sampleposition to 0
will cause it to save and resume the position in the track, -1 restarts)
and random play options for each playlist
playlist system overrides normal cd console commands if index is not -1

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@9883 d7cf8633-e32d-0410-b094-e92efae38249

cd_shared.c
cdaudio.h
snd_main.c
snd_null.c
sound.h

index 33a5dcc..06589a0 100644 (file)
@@ -43,6 +43,64 @@ extern void CDAudio_SysShutdown (void);
 cvar_t cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0","indicates if CD Audio system is active"};
 cvar_t cdaudio = {CVAR_SAVE,"cdaudio","1","CD playing mode (0 = never access CD drive, 1 = play CD tracks if no replacement available, 2 = play fake tracks if no CD track available, 3 = play only real CD tracks, 4 = play real CD tracks even instead of named fake tracks)"};
 
+#define MAX_PLAYLISTS 10
+int music_playlist_active = -1;
+int music_playlist_playing = 0; // 0 = not playing, 1 = playing, -1 = tried and failed
+
+cvar_t music_playlist_index = {0, "music_playlist_index", "-1", "selects which of the music_playlist_ variables is the active one, -1 disables playlists"};
+cvar_t music_playlist_list[MAX_PLAYLISTS] =
+{
+       {0, "music_playlist_list0", "", "list of tracks to play"},
+       {0, "music_playlist_list1", "", "list of tracks to play"},
+       {0, "music_playlist_list2", "", "list of tracks to play"},
+       {0, "music_playlist_list3", "", "list of tracks to play"},
+       {0, "music_playlist_list4", "", "list of tracks to play"},
+       {0, "music_playlist_list5", "", "list of tracks to play"},
+       {0, "music_playlist_list6", "", "list of tracks to play"},
+       {0, "music_playlist_list7", "", "list of tracks to play"},
+       {0, "music_playlist_list8", "", "list of tracks to play"},
+       {0, "music_playlist_list9", "", "list of tracks to play"}
+};
+cvar_t music_playlist_current[MAX_PLAYLISTS] =
+{
+       {0, "music_playlist_current0", "0", "current track index to play in list"},
+       {0, "music_playlist_current1", "0", "current track index to play in list"},
+       {0, "music_playlist_current2", "0", "current track index to play in list"},
+       {0, "music_playlist_current3", "0", "current track index to play in list"},
+       {0, "music_playlist_current4", "0", "current track index to play in list"},
+       {0, "music_playlist_current5", "0", "current track index to play in list"},
+       {0, "music_playlist_current6", "0", "current track index to play in list"},
+       {0, "music_playlist_current7", "0", "current track index to play in list"},
+       {0, "music_playlist_current8", "0", "current track index to play in list"},
+       {0, "music_playlist_current9", "0", "current track index to play in list"},
+};
+cvar_t music_playlist_random[MAX_PLAYLISTS] =
+{
+       {0, "music_playlist_random0", "0", "enables random play order if 1, 0 is sequential play"},
+       {0, "music_playlist_random1", "0", "enables random play order if 1, 0 is sequential play"},
+       {0, "music_playlist_random2", "0", "enables random play order if 1, 0 is sequential play"},
+       {0, "music_playlist_random3", "0", "enables random play order if 1, 0 is sequential play"},
+       {0, "music_playlist_random4", "0", "enables random play order if 1, 0 is sequential play"},
+       {0, "music_playlist_random5", "0", "enables random play order if 1, 0 is sequential play"},
+       {0, "music_playlist_random6", "0", "enables random play order if 1, 0 is sequential play"},
+       {0, "music_playlist_random7", "0", "enables random play order if 1, 0 is sequential play"},
+       {0, "music_playlist_random8", "0", "enables random play order if 1, 0 is sequential play"},
+       {0, "music_playlist_random9", "0", "enables random play order if 1, 0 is sequential play"},
+};
+cvar_t music_playlist_sampleposition[MAX_PLAYLISTS] =
+{
+       {0, "music_playlist_sampleposition0", "-1", "resume position for track, -1 restarts every time"},
+       {0, "music_playlist_sampleposition1", "-1", "resume position for track, -1 restarts every time"},
+       {0, "music_playlist_sampleposition2", "-1", "resume position for track, -1 restarts every time"},
+       {0, "music_playlist_sampleposition3", "-1", "resume position for track, -1 restarts every time"},
+       {0, "music_playlist_sampleposition4", "-1", "resume position for track, -1 restarts every time"},
+       {0, "music_playlist_sampleposition5", "-1", "resume position for track, -1 restarts every time"},
+       {0, "music_playlist_sampleposition6", "-1", "resume position for track, -1 restarts every time"},
+       {0, "music_playlist_sampleposition7", "-1", "resume position for track, -1 restarts every time"},
+       {0, "music_playlist_sampleposition8", "-1", "resume position for track, -1 restarts every time"},
+       {0, "music_playlist_sampleposition9", "-1", "resume position for track, -1 restarts every time"},
+};
+
 static qboolean wasPlaying = false;
 static qboolean initialized = false;
 static qboolean enabled = false;
@@ -142,7 +200,7 @@ qboolean CDAudio_Play_real (int track, qboolean looping, qboolean complain)
        return true;
 }
 
-void CDAudio_Play_byName (const char *trackname, qboolean looping)
+void CDAudio_Play_byName (const char *trackname, qboolean looping, qboolean tryreal, float startposition)
 {
        unsigned int track;
        sfx_t* sfx;
@@ -153,7 +211,7 @@ void CDAudio_Play_byName (const char *trackname, qboolean looping)
        if (!enabled)
                return;
 
-       if(strspn(trackname, "0123456789") == strlen(trackname))
+       if(tryreal && strspn(trackname, "0123456789") == strlen(trackname))
        {
                track = (unsigned char) atoi(trackname);
 #ifdef MAXTRACKS
@@ -187,7 +245,7 @@ void CDAudio_Play_byName (const char *trackname, qboolean looping)
 #endif
        }
 
-       if(strspn(trackname, "0123456789") == strlen(trackname))
+       if(tryreal && strspn(trackname, "0123456789") == strlen(trackname))
        {
                track = (unsigned char) atoi(trackname);
                if (track < 1)
@@ -260,7 +318,7 @@ void CDAudio_Play_byName (const char *trackname, qboolean looping)
        }
        if (FS_FileExists(filename) && (sfx = S_PrecacheSound (filename, false, true)))
        {
-               faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
+               faketrack = S_StartSound_StartPosition (-1, 0, sfx, vec3_origin, cdvolume, 0, startposition);
                if (faketrack != -1)
                {
                        if (looping)
@@ -304,8 +362,10 @@ success:
 void CDAudio_Play (int track, qboolean looping)
 {
        char buf[20];
+       if (music_playlist_index.integer >= 0)
+               return;
        dpsnprintf(buf, sizeof(buf), "%d", (int) track);
-       CDAudio_Play_byName(buf, looping);
+       CDAudio_Play_byName(buf, looping, true, 0);
 }
 
 float CDAudio_GetPosition (void)
@@ -315,11 +375,16 @@ float CDAudio_GetPosition (void)
        return -1;
 }
 
+static void CDAudio_StopPlaylistTrack(void);
+
 void CDAudio_Stop (void)
 {
        if (!enabled)
                return;
 
+       // save the playlist position
+       CDAudio_StopPlaylistTrack();
+
        if (faketrack != -1)
        {
                S_StopChannel (faketrack, true);
@@ -435,30 +500,40 @@ static void CD_f (void)
 
        if (strcasecmp(command, "play") == 0)
        {
-               CDAudio_Play_byName(Cmd_Argv (2), false);
+               if (music_playlist_index.integer >= 0)
+                       return;
+               CDAudio_Play_byName(Cmd_Argv (2), false, true, 0);
                return;
        }
 
        if (strcasecmp(command, "loop") == 0)
        {
-               CDAudio_Play_byName(Cmd_Argv (2), true);
+               if (music_playlist_index.integer >= 0)
+                       return;
+               CDAudio_Play_byName(Cmd_Argv (2), true, true, 0);
                return;
        }
 
        if (strcasecmp(command, "stop") == 0)
        {
+               if (music_playlist_index.integer >= 0)
+                       return;
                CDAudio_Stop();
                return;
        }
 
        if (strcasecmp(command, "pause") == 0)
        {
+               if (music_playlist_index.integer >= 0)
+                       return;
                CDAudio_Pause();
                return;
        }
 
        if (strcasecmp(command, "resume") == 0)
        {
+               if (music_playlist_index.integer >= 0)
+                       return;
                CDAudio_Resume();
                return;
        }
@@ -530,22 +605,121 @@ void CDAudio_SetVolume (float newvol)
        cdvolume = newvol;
 }
 
+static void CDAudio_StopPlaylistTrack(void)
+{
+       if (music_playlist_active >= 0 && music_playlist_active < MAX_PLAYLISTS && music_playlist_sampleposition[music_playlist_active].value >= 0)
+       {
+               // save position for resume
+               float position = CDAudio_GetPosition();
+               Cvar_SetValueQuick(&music_playlist_sampleposition[music_playlist_active], position >= 0 ? position : 0);
+       }
+       music_playlist_active = -1;
+       music_playlist_playing = 0; // not playing
+}
+
+void CDAudio_StartPlaylist(qboolean resume)
+{
+       const char *list;
+       const char *t;
+       int index;
+       int current;
+       int randomplay;
+       int count;
+       int listindex;
+       float position;
+       char trackname[MAX_QPATH];
+       CDAudio_Stop();
+       index = music_playlist_index.integer;
+       if (index >= 0 && index < MAX_PLAYLISTS && bgmvolume.value > 0)
+       {
+               list = music_playlist_list[index].string;
+               current = music_playlist_current[index].integer;
+               randomplay = music_playlist_random[index].integer;
+               position = music_playlist_sampleposition[index].value;
+               count = 0;
+               trackname[0] = 0;
+               if (list && list[0])
+               {
+                       for (t = list;;count++)
+                       {
+                               if (!COM_ParseToken_Console(&t))
+                                       break;
+                               // if we don't find the desired track, use the first one
+                               if (count == 0)
+                                       strlcpy(trackname, com_token, sizeof(trackname));
+                       }
+               }
+               if (count > 0)
+               {
+                       // position < 0 means never resume track
+                       if (position < 0)
+                               position = 0;
+                       // advance to next track in playlist if the last one ended
+                       if (!resume)
+                       {
+                               position = 0;
+                               current++;
+                               if (randomplay)
+                                       current = (int)lhrandom(0, count);
+                       }
+                       // wrap playlist position if needed
+                       if (current >= count)
+                               current = 0;
+                       // set current
+                       Cvar_SetValueQuick(&music_playlist_current[index], current);
+                       // get the Nth trackname
+                       if (current >= 0 && current < count)
+                       {
+                               for (listindex = 0, t = list;;listindex++)
+                               {
+                                       if (!COM_ParseToken_Console(&t))
+                                               break;
+                                       if (listindex == current)
+                                       {
+                                               strlcpy(trackname, com_token, sizeof(trackname));
+                                               break;
+                                       }
+                               }
+                       }
+                       if (trackname[0])
+                       {
+                               CDAudio_Play_byName(trackname, false, false, position);
+                               if (faketrack != -1)
+                                       music_playlist_active = index;
+                       }
+               }
+       }
+       music_playlist_playing = music_playlist_active >= 0 ? 1 : -1;
+}
+
 void CDAudio_Update (void)
 {
+       static int lastplaylist = -1;
        if (!enabled)
                return;
 
        CDAudio_SetVolume (bgmvolume.value);
-       
+       if (music_playlist_playing > 0 && CDAudio_GetPosition() < 0)
+       {
+               // this track ended, start a new track from the beginning
+               CDAudio_StartPlaylist(false);
+               lastplaylist = music_playlist_index.integer;
+       }
+       else if (lastplaylist != music_playlist_index.integer
+       || (bgmvolume.value > 0 && !music_playlist_playing))
+       {
+               // active playlist changed, save position and switch track
+               CDAudio_StartPlaylist(true);
+               lastplaylist = music_playlist_index.integer;
+       }
+
        if (faketrack == -1 && cdaudio.integer != 0 && bgmvolume.value != 0)
                CDAudio_SysUpdate();
 }
 
 int CDAudio_Init (void)
 {
-#ifdef MAXTRACKS
        int i;
-#endif
 
        if (cls.state == ca_dedicated)
                return -1;
@@ -566,6 +740,15 @@ int CDAudio_Init (void)
        Cvar_SetValueQuick(&cdaudioinitialized, true);
        enabled = true;
 
+       Cvar_RegisterVariable(&music_playlist_index);
+       for (i = 0;i < MAX_PLAYLISTS;i++)
+       {
+               Cvar_RegisterVariable(&music_playlist_list[i]);
+               Cvar_RegisterVariable(&music_playlist_current[i]);
+               Cvar_RegisterVariable(&music_playlist_random[i]);
+               Cvar_RegisterVariable(&music_playlist_sampleposition[i]);
+       }
+
        Cmd_AddCommand("cd", CD_f, "execute a CD drive command (cd on/off/reset/remap/close/play/loop/stop/pause/resume/eject/info) - use cd by itself for usage");
 
        return 0;
index ff96a3d..3fed67f 100644 (file)
--- a/cdaudio.h
+++ b/cdaudio.h
@@ -40,7 +40,7 @@ int CDAudio_Init(void);
 void CDAudio_Open(void);
 void CDAudio_Close(void);
 void CDAudio_Play(int track, qboolean looping);
-void CDAudio_Play_byName (const char *trackname, qboolean looping);
+void CDAudio_Play_byName (const char *trackname, qboolean looping, qboolean tryreal, float startposition);
 void CDAudio_Stop(void);
 void CDAudio_Pause(void);
 void CDAudio_Resume(void);
@@ -48,3 +48,4 @@ int CDAudio_Startup(void);
 void CDAudio_Shutdown(void);
 void CDAudio_Update(void);
 float CDAudio_GetPosition(void);
+void CDAudio_StartPlaylist(qboolean resume);
index 6bd0b36..a73a518 100644 (file)
@@ -1456,7 +1456,7 @@ void S_PlaySfxOnChannel (sfx_t *sfx, channel_t *target_chan, unsigned int flags,
 }
 
 
-int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
+int S_StartSound_StartPosition (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation, float startposition)
 {
        channel_t *target_chan, *check, *ch;
        int             ch_idx, startpos;
@@ -1493,16 +1493,19 @@ int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
        // if an identical sound has also been started this frame, offset the pos
        // a bit to keep it from just making the first one louder
        check = &channels[NUM_AMBIENTS];
-       startpos = 0;
-       for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
+       startpos = (int)(startposition * S_GetSoundRate());
+       if (startpos == 0)
        {
-               if (check == target_chan)
-                       continue;
-               if (check->sfx == sfx && check->pos == 0)
+               for (ch_idx=NUM_AMBIENTS ; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS ; ch_idx++, check++)
                {
-                       // use negative pos offset to delay this sound effect
-                       startpos = (int)lhrandom(0, -0.1 * snd_renderbuffer->format.speed);
-                       break;
+                       if (check == target_chan)
+                               continue;
+                       if (check->sfx == sfx && check->pos == 0)
+                       {
+                               // use negative pos offset to delay this sound effect
+                               startpos = (int)lhrandom(0, -0.1 * snd_renderbuffer->format.speed);
+                               break;
+                       }
                }
        }
 
@@ -1511,6 +1514,11 @@ int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
        return (target_chan - channels);
 }
 
+int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
+{
+       return S_StartSound_StartPosition(entnum, entchannel, sfx, origin, fvol, attenuation, 0);
+}
+
 void S_StopChannel (unsigned int channel_ind, qboolean lockmutex)
 {
        channel_t *ch;
@@ -1652,6 +1660,8 @@ float S_GetChannelPosition (unsigned int ch_ind)
        int s;
        channel_t *ch = &channels[ch_ind];
        sfx_t *sfx = ch->sfx;
+       if (!sfx)
+               return -1;
 
        s = ch->pos;
        /*
index 0d7276e..d98cf4b 100755 (executable)
@@ -67,6 +67,11 @@ int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
        return -1;
 }
 
+int S_StartSound_StartPosition (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation, float startposition)
+{
+       return -1;
+}
+
 void S_StopChannel (unsigned int channel_ind, qboolean lockmutex)
 {
 }
diff --git a/sound.h b/sound.h
index 183b71e..2161ee8 100644 (file)
--- a/sound.h
+++ b/sound.h
@@ -73,6 +73,7 @@ qboolean S_IsSoundPrecached (const sfx_t *sfx);
 
 // S_StartSound returns the channel index, or -1 if an error occurred
 int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation);
+int S_StartSound_StartPosition (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation, float startposition);
 qboolean S_LocalSound (const char *s);
 
 void S_StaticSound (sfx_t *sfx, vec3_t origin, float fvol, float attenuation);