2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
2009-02-10 10:01:48 -08:00
|
|
|
Copyright (C) 2005-2009 Warzone Resurrection Project
|
2007-01-15 12:09:25 -08:00
|
|
|
|
|
|
|
Warzone 2100 is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Warzone 2100 is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-01-27 14:31:16 -08:00
|
|
|
|
2006-11-06 06:40:07 -08:00
|
|
|
#include <string.h>
|
2008-01-27 15:34:25 -08:00
|
|
|
#include <physfs.h>
|
2006-06-16 12:10:23 -07:00
|
|
|
|
2006-06-02 12:34:58 -07:00
|
|
|
#include "lib/framework/frame.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
#include "audio.h"
|
2008-01-27 14:31:16 -08:00
|
|
|
#include "track.h"
|
2008-06-21 08:40:56 -07:00
|
|
|
#include "tracklib.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "cdaudio.h"
|
2007-01-10 15:00:04 -08:00
|
|
|
#include "mixer.h"
|
2006-09-13 14:13:19 -07:00
|
|
|
#include "playlist.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2009-01-11 06:34:38 -08:00
|
|
|
static float music_volume = 0.5;
|
|
|
|
|
|
|
|
#if !defined(WZ_NOSOUND)
|
2008-01-27 15:34:25 -08:00
|
|
|
static const size_t bufferSize = 16 * 1024;
|
|
|
|
static const unsigned int buffer_count = 32;
|
2008-06-28 08:32:06 -07:00
|
|
|
static bool music_initialized = false;
|
2008-06-21 08:40:56 -07:00
|
|
|
static bool stopping = true;
|
2007-06-06 10:09:23 -07:00
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
static AUDIO_STREAM* cdStream = NULL;
|
2009-01-11 06:34:38 -08:00
|
|
|
#endif
|
2007-04-30 12:24:46 -07:00
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
BOOL cdAudio_Open(const char* user_musicdir)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
PlayList_Init();
|
2006-02-18 10:54:37 -08:00
|
|
|
|
2008-04-12 14:31:29 -07:00
|
|
|
if (user_musicdir == NULL
|
|
|
|
|| !PlayList_Read(user_musicdir))
|
2007-11-04 07:24:37 -08:00
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2006-02-18 10:54:37 -08:00
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
debug(LOG_SOUND, "called(%s)", user_musicdir);
|
2009-01-11 06:34:38 -08:00
|
|
|
|
|
|
|
#if !defined(WZ_NOSOUND)
|
2008-01-27 14:31:16 -08:00
|
|
|
music_initialized = true;
|
2008-06-21 08:40:56 -07:00
|
|
|
stopping = true;
|
2009-01-11 06:34:38 -08:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
void cdAudio_Close(void)
|
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
debug(LOG_SOUND, "called");
|
|
|
|
cdAudio_Stop();
|
2006-02-18 10:54:37 -08:00
|
|
|
PlayList_Quit();
|
2009-01-11 06:34:38 -08:00
|
|
|
|
|
|
|
#if !defined(WZ_NOSOUND)
|
2008-01-27 14:31:16 -08:00
|
|
|
music_initialized = false;
|
2008-06-21 08:40:56 -07:00
|
|
|
stopping = true;
|
2009-01-11 06:34:38 -08:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2009-01-11 06:34:38 -08:00
|
|
|
#if !defined(WZ_NOSOUND)
|
2008-01-27 14:31:16 -08:00
|
|
|
static void cdAudio_TrackFinished(void*);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
static bool cdAudio_OpenTrack(const char* filename)
|
|
|
|
{
|
|
|
|
if (!music_initialized)
|
|
|
|
{
|
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
debug(LOG_SOUND, "called(%s)", filename);
|
|
|
|
cdAudio_Stop();
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-09-23 07:56:18 -07:00
|
|
|
if (strncasecmp(filename+strlen(filename)-4, ".ogg", 4) == 0)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-01-27 15:34:25 -08:00
|
|
|
PHYSFS_file* music_file = PHYSFS_openRead(filename);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-09-07 13:00:54 -07:00
|
|
|
debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(filename), filename);
|
2008-01-27 15:34:25 -08:00
|
|
|
if (music_file == NULL)
|
|
|
|
{
|
2008-09-07 13:00:54 -07:00
|
|
|
debug(LOG_ERROR, "Failed opening file [directory: %s] %s, with error %s", PHYSFS_getRealDir(filename), filename, PHYSFS_getLastError());
|
2008-01-27 15:34:25 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-02-04 08:35:34 -08:00
|
|
|
cdStream = sound_PlayStreamWithBuf(music_file, music_volume, cdAudio_TrackFinished, (char*)filename, bufferSize, buffer_count);
|
2008-01-27 14:31:16 -08:00
|
|
|
if (cdStream == NULL)
|
2007-04-30 18:08:48 -07:00
|
|
|
{
|
2008-01-27 15:34:25 -08:00
|
|
|
PHYSFS_close(music_file);
|
2008-01-27 14:31:16 -08:00
|
|
|
debug(LOG_ERROR, "Failed creating audio stream for %s", filename);
|
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2006-02-18 10:54:37 -08:00
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
debug(LOG_SOUND, "successful(%s)", filename);
|
|
|
|
stopping = false;
|
2008-01-27 14:31:16 -08:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
return false; // unhandled
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
static void cdAudio_TrackFinished(void* user_data)
|
2007-04-30 18:08:48 -07:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
const char *filename = PlayList_NextSong();
|
2008-02-04 08:35:34 -08:00
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
// This pointer is now officially invalidated; so set it to NULL
|
|
|
|
cdStream = NULL;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
if (filename == NULL)
|
2007-04-30 18:08:48 -07:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
debug(LOG_ERROR, "Out of playlist?! was playing %s", (char *)user_data);
|
2008-01-27 14:31:16 -08:00
|
|
|
return;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
if (!stopping && cdAudio_OpenTrack(filename))
|
2007-04-30 18:08:48 -07:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
debug(LOG_SOUND, "Now playing %s (was playing %s)", filename, (char *)user_data);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
2009-01-11 06:34:38 -08:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
BOOL cdAudio_PlayTrack(SONG_CONTEXT context)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
debug(LOG_SOUND, "called(%d)", (int)context);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-06-25 06:56:17 -07:00
|
|
|
switch (context)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-01-11 06:34:38 -08:00
|
|
|
#if !defined(WZ_NOSOUND)
|
2008-06-25 06:56:17 -07:00
|
|
|
case SONG_FRONTEND:
|
|
|
|
return cdAudio_OpenTrack("music/menu.ogg");
|
|
|
|
|
|
|
|
case SONG_INGAME:
|
|
|
|
{
|
|
|
|
const char *filename = PlayList_CurrentSong();
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-06-25 07:00:20 -07:00
|
|
|
if (filename == NULL)
|
|
|
|
return false;
|
|
|
|
|
2008-06-25 06:56:17 -07:00
|
|
|
return cdAudio_OpenTrack(filename);
|
|
|
|
}
|
2009-01-11 06:34:38 -08:00
|
|
|
#else
|
|
|
|
case SONG_FRONTEND:
|
|
|
|
case SONG_INGAME:
|
|
|
|
return false;
|
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2008-06-25 06:56:17 -07:00
|
|
|
|
|
|
|
ASSERT(!"Invalid songcontext", "Invalid song context specified for playing: %u", (unsigned int)context);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-06-21 08:40:56 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
void cdAudio_Stop()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2009-01-11 06:34:38 -08:00
|
|
|
#if !defined(WZ_NOSOUND)
|
2008-06-21 08:40:56 -07:00
|
|
|
stopping = true;
|
|
|
|
debug(LOG_SOUND, "called, cdStream=%p", cdStream);
|
2009-01-11 06:34:38 -08:00
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
if (cdStream)
|
|
|
|
{
|
|
|
|
sound_StopStream(cdStream);
|
|
|
|
cdStream = NULL;
|
2008-06-21 08:40:56 -07:00
|
|
|
sound_Update();
|
2008-01-27 14:31:16 -08:00
|
|
|
}
|
2009-01-11 06:34:38 -08:00
|
|
|
#else
|
|
|
|
debug(LOG_SOUND, "called");
|
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
void cdAudio_Pause()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
debug(LOG_SOUND, "called");
|
2009-01-11 06:34:38 -08:00
|
|
|
#if !defined(WZ_NOSOUND)
|
2008-01-27 14:31:16 -08:00
|
|
|
if (cdStream)
|
|
|
|
{
|
|
|
|
sound_PauseStream(cdStream);
|
|
|
|
}
|
2009-01-11 06:34:38 -08:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-01-27 14:31:16 -08:00
|
|
|
void cdAudio_Resume()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-06-21 08:40:56 -07:00
|
|
|
debug(LOG_SOUND, "called");
|
2009-01-11 06:34:38 -08:00
|
|
|
#if !defined(WZ_NOSOUND)
|
2008-01-27 14:31:16 -08:00
|
|
|
if (cdStream)
|
2007-04-30 12:24:46 -07:00
|
|
|
{
|
2008-01-27 14:31:16 -08:00
|
|
|
sound_ResumeStream(cdStream);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2009-01-11 06:34:38 -08:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-06-25 11:09:38 -07:00
|
|
|
float sound_GetMusicVolume()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-06-25 11:09:38 -07:00
|
|
|
return music_volume;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-06-25 11:09:38 -07:00
|
|
|
void sound_SetMusicVolume(float volume)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-01-27 14:31:16 -08:00
|
|
|
// Keep volume in the range of 0.0 - 1.0
|
2008-07-15 06:01:22 -07:00
|
|
|
music_volume = clipf(volume, 0.0f, 1.0f);
|
2007-06-25 11:09:38 -07:00
|
|
|
|
2009-01-11 06:34:38 -08:00
|
|
|
#if !defined(WZ_NOSOUND)
|
2008-05-13 07:01:25 -07:00
|
|
|
// Change the volume of the current stream as well (if any)
|
|
|
|
if (cdStream)
|
|
|
|
{
|
|
|
|
sound_SetStreamVolume(cdStream, music_volume);
|
|
|
|
}
|
2009-01-11 06:34:38 -08:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|