2006-03-20 17:12:09 +00:00
/*
2014-06-01 17:55:23 +00:00
* Portions of this file are copyright Rebirth contributors and licensed as
* described in COPYING . txt .
* Portions of this file are copyright Parallax Software and licensed
* according to the Parallax license below .
* See COPYING . txt for license details .
2006-03-20 17:12:09 +00:00
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ( " PARALLAX " ) . PARALLAX , IN DISTRIBUTING THE CODE TO
END - USERS , AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN , GRANTS A
ROYALTY - FREE , PERPETUAL LICENSE TO SUCH END - USERS FOR USE BY SUCH END - USERS
IN USING , DISPLAYING , AND CREATING DERIVATIVE WORKS THEREOF , SO LONG AS
SUCH USE , DISPLAY OR CREATION IS FOR NON - COMMERCIAL , ROYALTY OR REVENUE
FREE PURPOSES . IN NO EVENT SHALL THE END - USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE - BEARING PURPOSES . THE END - USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE .
COPYRIGHT 1993 - 1999 PARALLAX SOFTWARE CORPORATION . ALL RIGHTS RESERVED .
*/
/*
*
* Routines to manage the songs in Descent .
*
*/
# include <stdio.h>
2009-03-03 12:55:27 +00:00
# include <stdlib.h>
2006-03-20 17:12:09 +00:00
# include <string.h>
2012-07-07 18:35:06 +00:00
# include "dxxerror.h"
2006-03-20 17:12:09 +00:00
# include "pstypes.h"
# include "songs.h"
2013-12-26 04:18:28 +00:00
# include "strutil.h"
2006-03-20 17:12:09 +00:00
# include "digi.h"
# include "rbaudio.h"
2016-09-11 18:49:15 +00:00
# if DXX_USE_SDLMIXER
2010-06-14 08:13:16 +00:00
# include "digi_mixer_music.h"
# endif
2018-07-30 00:49:59 +00:00
# include "jukebox.h"
2009-03-03 12:55:27 +00:00
# include "config.h"
2006-03-20 17:12:09 +00:00
# include "timer.h"
2013-12-26 04:18:28 +00:00
# include "u_mem.h"
2013-03-03 19:41:09 +00:00
# include "args.h"
2014-12-11 02:35:03 +00:00
# include "physfsx.h"
2015-07-25 23:10:45 +00:00
# include "game.h"
2019-08-25 16:43:17 +00:00
# include "compiler-make_unique.h"
2019-10-24 17:39:08 +00:00
# include "console.h"
2019-08-25 16:43:17 +00:00
namespace dcx {
namespace {
class user_configured_level_songs : std : : unique_ptr < bim_song_info [ ] >
{
using base_type = std : : unique_ptr < bim_song_info [ ] > ;
unsigned count ;
public :
using base_type : : operator [ ] ;
using base_type : : operator bool ;
user_configured_level_songs ( ) = default ;
user_configured_level_songs ( const std : : size_t length ) :
base_type ( make_unique < bim_song_info [ ] > ( length ) ) , count ( length )
{
}
unsigned size ( ) const
{
return count ;
}
void reset ( )
{
count = 0 ;
base_type : : reset ( ) ;
}
void resize ( const std : : size_t length )
{
base_type : : operator = ( make_unique < bim_song_info [ ] > ( length ) ) ;
count = length ;
}
user_configured_level_songs & operator = ( user_configured_level_songs & & ) = default ;
void operator = ( std : : vector < bim_song_info > & & v ) ;
} ;
void user_configured_level_songs : : operator = ( std : : vector < bim_song_info > & & v )
{
2019-11-02 04:36:15 +00:00
if ( v . empty ( ) )
{
/* If the vector is empty, use `reset` so that this object
* stores ` nullptr ` instead of a pointer to a zero - length array .
* Storing ` nullptr ` causes ` operator bool ( ) ` to return false .
* Storing a zero - length array causes ` operator bool ( ) ` to
* return true .
*
* A true return causes a divide - by - zero later when the object
* is considered true , but then reports a length of zero .
*/
reset ( ) ;
return ;
}
2019-08-25 16:43:17 +00:00
resize ( v . size ( ) ) ;
std : : move ( v . begin ( ) , v . end ( ) , & this - > operator [ ] ( 0u ) ) ;
}
2006-03-20 17:12:09 +00:00
int Songs_initialized = 0 ;
2011-04-18 12:32:36 +00:00
static int Song_playing = - 1 ; // -1 if no song playing, else the Descent song number
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2010-08-01 17:42:38 +00:00
static int Redbook_playing = 0 ; // Redbook track num differs from Song_playing. We need this for Redbook repeat hooks.
2018-07-14 17:23:15 +00:00
# endif
2010-04-05 11:54:23 +00:00
2019-08-25 16:43:17 +00:00
user_configured_level_songs BIMSongs ;
user_configured_level_songs BIMSecretSongs ;
2006-03-20 17:12:09 +00:00
2019-08-25 16:43:17 +00:00
}
2019-08-15 21:20:52 +00:00
2009-03-03 12:55:27 +00:00
# define EXTMUSIC_VOLUME_SCALE (255)
2006-03-20 17:12:09 +00:00
//takes volume in range 0..8
2011-07-16 09:36:40 +00:00
//NOTE that we do not check what is playing right now (except Redbook) This is because here we don't (want) know WHAT we're playing - let the subfunctions do it (i.e. digi_win32_set_music_volume() knows if a MIDI plays or not)
2010-06-14 08:13:16 +00:00
void songs_set_volume ( int volume )
2006-03-20 17:12:09 +00:00
{
2010-06-14 08:13:16 +00:00
# ifdef _WIN32
2010-11-28 15:49:40 +00:00
digi_win32_set_midi_volume ( volume ) ;
2010-06-14 08:13:16 +00:00
# endif
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2010-06-14 08:13:16 +00:00
if ( GameCfg . MusicType = = MUSIC_TYPE_REDBOOK )
{
RBASetVolume ( 0 ) ;
2010-08-19 15:54:19 +00:00
RBASetVolume ( volume ) ;
2010-06-14 08:13:16 +00:00
}
2018-07-14 17:23:15 +00:00
# else
( void ) volume ;
# endif
2016-09-11 18:49:15 +00:00
# if DXX_USE_SDLMIXER
2010-08-19 15:54:19 +00:00
mix_set_music_volume ( volume ) ;
2010-06-14 08:13:16 +00:00
# endif
2006-03-20 17:12:09 +00:00
}
2019-08-15 21:20:52 +00:00
static int is_valid_song_extension ( const char * dot )
{
return ( ! d_stricmp ( dot , SONG_EXT_HMP )
# if DXX_USE_SDLMIXER
| |
! d_stricmp ( dot , SONG_EXT_MID ) | |
! d_stricmp ( dot , SONG_EXT_OGG ) | |
! d_stricmp ( dot , SONG_EXT_FLAC ) | |
! d_stricmp ( dot , SONG_EXT_MP3 )
# endif
) ;
}
2019-08-25 16:43:17 +00:00
template < std : : size_t N >
static inline void assign_builtin_song ( bim_song_info & song , const char ( & str ) [ N ] )
{
strncpy ( song . filename , str , sizeof ( song . filename ) ) ;
}
static void add_song ( std : : vector < bim_song_info > & songs , const char * const input )
{
songs . emplace_back ( ) ;
auto & filename = songs . back ( ) . filename ;
sscanf ( input , " %15s " , filename ) ;
if ( const char * dot = strrchr ( filename , ' . ' ) )
{
+ + dot ;
if ( is_valid_song_extension ( dot ) )
return ;
}
songs . pop_back ( ) ;
}
2019-08-18 17:18:34 +00:00
}
namespace dsx {
2011-07-16 09:36:40 +00:00
// Set up everything for our music
// NOTE: you might think this is done once per runtime but it's not! It's done for EACH song so that each mission can have it's own descent.sng structure. We COULD optimize that by only doing this once per mission.
2013-10-27 22:00:14 +00:00
static void songs_init ( )
2006-03-20 17:12:09 +00:00
{
2010-08-01 17:42:38 +00:00
Songs_initialized = 0 ;
2019-08-25 16:43:17 +00:00
BIMSongs . reset ( ) ;
BIMSecretSongs . reset ( ) ;
2010-02-19 15:30:57 +00:00
2019-08-15 21:20:52 +00:00
int canUseExtensions = 0 ;
2015-01-17 18:31:42 +00:00
// try dxx-r.sng - a songfile specifically for dxx which level authors CAN use (dxx does not care if descent.sng contains MP3/OGG/etc. as well) besides the normal descent.sng containing files other versions of the game cannot play. this way a mission can contain a DOS-Descent compatible OST (hmp files) as well as a OST using MP3, OGG, etc.
auto fp = PHYSFSX_openReadBuffered ( " dxx-r.sng " ) ;
2011-03-06 20:02:50 +00:00
2015-01-17 18:31:42 +00:00
if ( ! fp ) // try to open regular descent.sng
2011-06-01 07:59:55 +00:00
fp = PHYSFSX_openReadBuffered ( " descent.sng " ) ;
2019-08-15 21:20:52 +00:00
else
canUseExtensions = 1 ; // can use extensions ONLY if dxx-r.sng
2019-08-25 16:43:17 +00:00
unsigned i = 0 ;
2015-01-17 18:31:42 +00:00
if ( ! fp ) // No descent.sng available. Define a default song-set
2010-04-05 11:54:23 +00:00
{
2019-08-25 16:43:17 +00:00
constexpr std : : size_t predef = 30 ; // define 30 songs - period
2010-08-01 17:42:38 +00:00
2019-08-25 16:43:17 +00:00
user_configured_level_songs builtin_songs ( predef ) ;
2010-08-01 17:42:38 +00:00
2019-08-25 16:43:17 +00:00
assign_builtin_song ( builtin_songs [ SONG_TITLE ] , " descent.hmp " ) ;
assign_builtin_song ( builtin_songs [ SONG_BRIEFING ] , " briefing.hmp " ) ;
assign_builtin_song ( builtin_songs [ SONG_CREDITS ] , " credits.hmp " ) ;
assign_builtin_song ( builtin_songs [ SONG_ENDLEVEL ] , " endlevel.hmp " ) ; // can't find it? give a warning
assign_builtin_song ( builtin_songs [ SONG_ENDGAME ] , " endgame.hmp " ) ; // ditto
2010-08-01 17:42:38 +00:00
for ( i = SONG_FIRST_LEVEL_SONG ; i < predef ; i + + ) {
2019-08-25 16:43:17 +00:00
auto & s = builtin_songs [ i ] ;
snprintf ( s . filename , sizeof ( s . filename ) , " game%02d.hmp " , i - SONG_FIRST_LEVEL_SONG + 1 ) ;
if ( ! PHYSFSX_exists ( s . filename , 1 ) & &
! PHYSFSX_exists ( ( snprintf ( s . filename , sizeof ( s . filename ) , " game%d.hmp " , i - SONG_FIRST_LEVEL_SONG ) , s . filename ) , 1 ) )
2010-04-05 11:54:23 +00:00
{
2019-08-25 16:43:17 +00:00
s = { } ; // music not available
2010-08-01 17:42:38 +00:00
break ;
2010-04-05 11:54:23 +00:00
}
2006-03-20 17:12:09 +00:00
}
2019-08-25 16:43:17 +00:00
BIMSongs = std : : move ( builtin_songs ) ;
2010-04-05 11:54:23 +00:00
}
else
{
2014-09-07 19:48:10 +00:00
PHYSFSX_gets_line_t < 81 > inputline ;
2019-08-25 16:43:17 +00:00
std : : vector < bim_song_info > main_songs , secret_songs ;
2015-10-09 02:46:11 +00:00
while ( PHYSFSX_fgets ( inputline , fp ) )
2006-03-20 17:12:09 +00:00
{
2019-08-25 16:43:17 +00:00
if ( ! inputline [ 0 ] )
continue ;
2006-03-20 17:12:09 +00:00
{
2019-08-15 21:20:52 +00:00
if ( canUseExtensions )
{
2019-08-18 17:18:34 +00:00
auto & secret_label = " !Rebirth.secret " ;
constexpr auto secret_label_len = sizeof ( secret_label ) - 1 ;
2019-08-15 21:20:52 +00:00
// extension stuffs
2019-08-18 17:18:34 +00:00
if ( ! strncmp ( inputline , secret_label , secret_label_len ) ) {
2019-08-25 16:43:17 +00:00
add_song ( secret_songs , & inputline [ secret_label_len ] ) ;
2019-08-15 21:20:52 +00:00
continue ;
}
}
2019-08-25 16:43:17 +00:00
add_song ( main_songs , inputline ) ;
2006-03-20 17:12:09 +00:00
}
}
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
// HACK: If Descent.hog is patched from 1.0 to 1.5, descent.sng is turncated. So let's patch it up here
2019-08-25 16:43:17 +00:00
constexpr std : : size_t truncated_song_count = 12 ;
if ( ! canUseExtensions & & main_songs . size ( ) = = truncated_song_count & & PHYSFSX_fsize ( " descent.sng " ) = = 422 )
2013-03-03 01:03:33 +00:00
{
2019-08-25 16:43:17 +00:00
main_songs . resize ( truncated_song_count + 15 ) ;
2013-03-03 01:03:33 +00:00
for ( i = 12 ; i < = 26 ; i + + )
2019-08-25 16:43:17 +00:00
{
auto & s = main_songs [ i ] ;
snprintf ( s . filename , sizeof ( s . filename ) , " game%02d.hmp " , i - 4 ) ;
}
2013-03-03 01:03:33 +00:00
}
# endif
2019-08-25 16:43:17 +00:00
BIMSongs = std : : move ( main_songs ) ;
BIMSecretSongs = std : : move ( secret_songs ) ;
2006-03-20 17:12:09 +00:00
}
Songs_initialized = 1 ;
2015-01-17 18:31:42 +00:00
fp . reset ( ) ;
2006-03-20 17:12:09 +00:00
2015-12-24 04:01:26 +00:00
if ( CGameArg . SndNoMusic )
2010-06-14 17:55:25 +00:00
GameCfg . MusicType = MUSIC_TYPE_NONE ;
2010-06-14 08:13:16 +00:00
// If SDL_Mixer is not supported (or deactivated), switch to no-music type if SDL_mixer-related music type was selected
2015-12-24 04:01:26 +00:00
else if ( CGameArg . SndDisableSdlMixer )
2010-06-14 08:13:16 +00:00
{
# ifndef _WIN32
if ( GameCfg . MusicType = = MUSIC_TYPE_BUILTIN )
GameCfg . MusicType = MUSIC_TYPE_NONE ;
# endif
if ( GameCfg . MusicType = = MUSIC_TYPE_CUSTOM )
GameCfg . MusicType = MUSIC_TYPE_NONE ;
}
2018-07-14 17:23:15 +00:00
switch ( GameCfg . MusicType )
{
# if DXX_USE_SDL_REDBOOK_AUDIO
case MUSIC_TYPE_REDBOOK :
RBAInit ( ) ;
break ;
2010-06-14 08:13:16 +00:00
# endif
2018-07-14 17:23:15 +00:00
case MUSIC_TYPE_CUSTOM :
jukebox_load ( ) ;
break ;
}
2010-06-14 08:13:16 +00:00
songs_set_volume ( GameCfg . MusicVolume ) ;
}
2016-08-25 04:05:32 +00:00
}
2006-03-20 17:12:09 +00:00
2010-06-14 08:13:16 +00:00
void songs_uninit ( )
2006-03-20 17:12:09 +00:00
{
2011-07-16 09:36:40 +00:00
songs_stop_all ( ) ;
2016-09-11 18:49:15 +00:00
# if DXX_USE_SDLMIXER
2010-06-14 08:13:16 +00:00
jukebox_unload ( ) ;
# endif
2019-08-25 16:43:17 +00:00
BIMSecretSongs . reset ( ) ;
BIMSongs . reset ( ) ;
2010-06-14 08:13:16 +00:00
Songs_initialized = 0 ;
2006-03-20 17:12:09 +00:00
}
2010-06-14 08:13:16 +00:00
//stop any songs - builtin, redbook or jukebox - that are currently playing
2006-03-20 17:12:09 +00:00
void songs_stop_all ( void )
{
2010-06-14 08:13:16 +00:00
# ifdef _WIN32
2011-07-15 08:43:03 +00:00
digi_win32_stop_midi_song ( ) ; // Stop midi song, if playing
2010-06-14 08:13:16 +00:00
# endif
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2010-06-14 08:13:16 +00:00
RBAStop ( ) ;
2018-07-14 17:23:15 +00:00
# endif
2016-09-11 18:49:15 +00:00
# if DXX_USE_SDLMIXER
2010-06-14 08:13:16 +00:00
mix_stop_music ( ) ;
# endif
2011-04-18 12:32:36 +00:00
Song_playing = - 1 ;
2006-03-20 17:12:09 +00:00
}
2010-06-14 08:13:16 +00:00
void songs_pause ( void )
2006-03-20 17:12:09 +00:00
{
2010-11-28 15:49:40 +00:00
# ifdef _WIN32
digi_win32_pause_midi_song ( ) ;
# endif
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2010-06-14 08:13:16 +00:00
if ( GameCfg . MusicType = = MUSIC_TYPE_REDBOOK )
RBAPause ( ) ;
2018-07-14 17:23:15 +00:00
# endif
2016-09-11 18:49:15 +00:00
# if DXX_USE_SDLMIXER
2010-06-14 08:13:16 +00:00
mix_pause_music ( ) ;
# endif
2006-03-20 17:12:09 +00:00
}
2010-06-14 08:13:16 +00:00
void songs_resume ( void )
{
2010-11-28 15:49:40 +00:00
# ifdef _WIN32
digi_win32_resume_midi_song ( ) ;
# endif
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2010-06-14 08:13:16 +00:00
if ( GameCfg . MusicType = = MUSIC_TYPE_REDBOOK )
RBAResume ( ) ;
2018-07-14 17:23:15 +00:00
# endif
2016-09-11 18:49:15 +00:00
# if DXX_USE_SDLMIXER
2010-06-14 08:13:16 +00:00
mix_resume_music ( ) ;
# endif
}
2006-03-20 17:12:09 +00:00
2010-06-14 08:13:16 +00:00
void songs_pause_resume ( void )
2006-03-20 17:12:09 +00:00
{
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2010-06-14 08:13:16 +00:00
if ( GameCfg . MusicType = = MUSIC_TYPE_REDBOOK )
RBAPauseResume ( ) ;
2018-07-14 17:23:15 +00:00
# endif
2016-09-11 18:49:15 +00:00
# if DXX_USE_SDLMIXER
2010-06-14 08:13:16 +00:00
mix_pause_resume_music ( ) ;
# endif
2006-03-20 17:12:09 +00:00
}
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
/*
* This list may not be exhaustive ! !
*/
# define D1_MAC_OEM_DISCID 0xde0feb0e // Descent CD that came with the Mac Performa 6400, hope mine isn't scratched [too much]
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2013-03-03 01:03:33 +00:00
# define REDBOOK_ENDLEVEL_TRACK 4
# define REDBOOK_ENDGAME_TRACK (RBAGetNumberOfTracks())
# define REDBOOK_FIRST_LEVEL_TRACK (songs_have_cd() ? 6 : 1)
2018-07-14 17:23:15 +00:00
# endif
2013-03-03 01:03:33 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
/*
* Some of these have different Track listings !
* Which one is the " correct " order ?
*/
# define D2_1_DISCID 0x7d0ff809 // Descent II
# define D2_2_DISCID 0xe010a30e // Descent II
# define D2_3_DISCID 0xd410070d // Descent II
# define D2_4_DISCID 0xc610080d // Descent II
# define D2_DEF_DISCID 0x87102209 // Definitive collection Disc 2
# define D2_OEM_DISCID 0xac0bc30d // Destination: Quartzon
# define D2_OEM2_DISCID 0xc40c0a0d // Destination: Quartzon
# define D2_VERTIGO_DISCID 0x53078208 // Vertigo
# define D2_VERTIGO2_DISCID 0x64071408 // Vertigo + DMB
# define D2_MAC_DISCID 0xb70ee40e // Macintosh
# define D2_IPLAY_DISCID 0x22115710 // iPlay for Macintosh
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2010-06-14 08:13:16 +00:00
# define REDBOOK_TITLE_TRACK 2
# define REDBOOK_CREDITS_TRACK 3
2013-03-03 01:03:33 +00:00
# define REDBOOK_FIRST_LEVEL_TRACK (songs_have_cd() ? 4 : 1)
# endif
2018-07-14 17:23:15 +00:00
# endif
2006-03-20 17:12:09 +00:00
// 0 otherwise
2016-08-25 04:05:32 +00:00
namespace dsx {
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2013-03-03 01:03:33 +00:00
static int songs_have_cd ( )
2006-03-20 17:12:09 +00:00
{
2014-01-07 21:04:23 +00:00
unsigned long discid ;
2010-04-05 11:54:23 +00:00
2009-05-06 12:19:28 +00:00
if ( GameCfg . OrigTrackOrder )
return 1 ;
2010-06-14 08:13:16 +00:00
if ( ! ( GameCfg . MusicType = = MUSIC_TYPE_REDBOOK ) )
2006-03-20 17:12:09 +00:00
return 0 ;
discid = RBAGetDiscID ( ) ;
2019-10-24 17:39:08 +00:00
con_printf ( CON_DEBUG , " CD-ROM disc ID is 0x%08lx " , discid ) ;
2006-03-20 17:12:09 +00:00
switch ( discid ) {
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
case D1_MAC_OEM_DISCID : // Doesn't work with your Mac Descent CD? Please tell!
return 1 ;
# elif defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
case D2_1_DISCID :
case D2_2_DISCID :
case D2_3_DISCID :
case D2_4_DISCID :
case D2_DEF_DISCID :
case D2_OEM_DISCID :
case D2_OEM2_DISCID :
case D2_VERTIGO_DISCID :
case D2_VERTIGO2_DISCID :
case D2_MAC_DISCID :
case D2_IPLAY_DISCID :
return 1 ;
2013-03-03 01:03:33 +00:00
# endif
default :
return 0 ;
2006-03-20 17:12:09 +00:00
}
}
2018-07-14 17:23:15 +00:00
# endif
2016-08-25 04:05:32 +00:00
}
2006-03-20 17:12:09 +00:00
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2013-03-03 01:03:33 +00:00
static void redbook_repeat_func ( )
{
2015-10-09 02:46:09 +00:00
pause_game_world_time p ;
2013-03-03 01:03:33 +00:00
RBAPlayTracks ( Redbook_playing , 0 , redbook_repeat_func ) ;
}
2018-07-14 17:23:15 +00:00
# endif
2013-03-03 01:03:33 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO || DXX_USE_SDLMIXER
2013-03-03 01:03:33 +00:00
static void play_credits_track ( )
2009-03-03 12:55:27 +00:00
{
2015-10-09 02:46:09 +00:00
pause_game_world_time p ;
2010-08-23 11:29:45 +00:00
songs_play_song ( SONG_CREDITS , 1 ) ;
2009-03-03 12:55:27 +00:00
}
2013-03-03 01:03:33 +00:00
# endif
2018-07-14 17:23:15 +00:00
# endif
2009-03-03 12:55:27 +00:00
2012-06-10 12:02:43 +00:00
// play a filename as music, depending on filename extension.
2013-06-08 22:24:17 +00:00
int songs_play_file ( const char * filename , int repeat , void ( * hook_finished_track ) ( ) )
2012-06-10 12:02:43 +00:00
{
songs_stop_all ( ) ;
2017-11-25 01:56:51 +00:00
# if defined(_WIN32) || DXX_USE_SDLMIXER
2013-10-06 03:04:43 +00:00
const char * fptr = strrchr ( filename , ' . ' ) ;
2012-06-10 12:02:43 +00:00
if ( fptr = = NULL )
return 0 ;
2013-12-18 23:35:06 +00:00
+ + fptr ;
2012-06-10 12:02:43 +00:00
if ( ! d_stricmp ( fptr , SONG_EXT_HMP ) )
{
# if defined(_WIN32)
return digi_win32_play_midi_song ( filename , repeat ) ;
2016-09-11 18:49:15 +00:00
# elif DXX_USE_SDLMIXER
2012-06-10 12:02:43 +00:00
return mix_play_file ( filename , repeat , hook_finished_track ) ;
# else
return 0 ;
# endif
}
2016-09-11 18:49:15 +00:00
# if DXX_USE_SDLMIXER
2012-06-10 12:02:43 +00:00
else if ( ! d_stricmp ( fptr , SONG_EXT_MID ) | |
! d_stricmp ( fptr , SONG_EXT_OGG ) | |
! d_stricmp ( fptr , SONG_EXT_FLAC ) | |
! d_stricmp ( fptr , SONG_EXT_MP3 ) )
{
return mix_play_file ( filename , repeat , hook_finished_track ) ;
}
2017-11-25 01:56:51 +00:00
# endif
2012-06-10 12:02:43 +00:00
# endif
return 0 ;
}
2016-08-25 04:05:32 +00:00
namespace dsx {
2010-04-05 11:54:23 +00:00
int songs_play_song ( int songnum , int repeat )
2006-03-20 17:12:09 +00:00
{
2008-11-19 15:52:07 +00:00
songs_init ( ) ;
2010-08-01 17:42:38 +00:00
if ( ! Songs_initialized )
return 0 ;
2006-03-20 17:12:09 +00:00
2010-06-14 08:13:16 +00:00
switch ( GameCfg . MusicType )
2010-04-05 11:54:23 +00:00
{
2010-06-14 08:13:16 +00:00
case MUSIC_TYPE_BUILTIN :
{
2011-07-16 09:36:40 +00:00
// EXCEPTION: If SONG_ENDLEVEL is not available, continue playing level song.
2019-08-25 16:43:17 +00:00
auto & s = BIMSongs [ songnum ] ;
if ( Song_playing > = SONG_FIRST_LEVEL_SONG & & songnum = = SONG_ENDLEVEL & & ! PHYSFSX_exists ( s . filename , 1 ) )
2011-07-16 09:36:40 +00:00
return Song_playing ;
2011-04-18 12:32:36 +00:00
Song_playing = - 1 ;
2019-08-25 16:43:17 +00:00
if ( songs_play_file ( s . filename , repeat , NULL ) )
2012-06-10 12:02:43 +00:00
Song_playing = songnum ;
2010-06-14 08:13:16 +00:00
break ;
}
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2010-06-14 08:13:16 +00:00
case MUSIC_TYPE_REDBOOK :
{
int num_tracks = RBAGetNumberOfTracks ( ) ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
Song_playing = - 1 ;
if ( ( songnum < SONG_ENDGAME ) & & ( songnum + 2 < = num_tracks ) )
{
if ( RBAPlayTracks ( songnum + 2 , songnum + 2 , repeat ? redbook_repeat_func : NULL ) )
{
Redbook_playing = songnum + 2 ;
Song_playing = songnum ;
}
}
else if ( ( songnum = = SONG_ENDGAME ) & & ( REDBOOK_ENDGAME_TRACK < = num_tracks ) ) // The endgame track is the last track
{
if ( RBAPlayTracks ( REDBOOK_ENDGAME_TRACK , REDBOOK_ENDGAME_TRACK , repeat ? redbook_repeat_func : NULL ) )
{
Redbook_playing = REDBOOK_ENDGAME_TRACK ;
Song_playing = songnum ;
}
}
else if ( ( songnum > SONG_ENDGAME ) & & ( songnum + 1 < = num_tracks ) )
{
if ( RBAPlayTracks ( songnum + 1 , songnum + 1 , repeat ? redbook_repeat_func : NULL ) )
{
Redbook_playing = songnum + 1 ;
Song_playing = songnum ;
}
}
# elif defined(DXX_BUILD_DESCENT_II)
2011-04-18 12:32:36 +00:00
//Song_playing = -1; // keep playing current music if chosen song is unavailable (e.g. SONG_ENDLEVEL)
2010-06-14 08:13:16 +00:00
if ( ( songnum = = SONG_TITLE ) & & ( REDBOOK_TITLE_TRACK < = num_tracks ) )
{
2011-01-10 19:40:55 +00:00
if ( RBAPlayTracks ( REDBOOK_TITLE_TRACK , REDBOOK_TITLE_TRACK , repeat ? play_credits_track : NULL ) )
2010-06-14 08:13:16 +00:00
{
Redbook_playing = REDBOOK_TITLE_TRACK ;
Song_playing = songnum ;
}
}
else if ( ( songnum = = SONG_CREDITS ) & & ( REDBOOK_CREDITS_TRACK < = num_tracks ) )
{
2011-01-10 19:40:55 +00:00
if ( RBAPlayTracks ( REDBOOK_CREDITS_TRACK , REDBOOK_CREDITS_TRACK , repeat ? play_credits_track : NULL ) )
2010-06-14 08:13:16 +00:00
{
Redbook_playing = REDBOOK_CREDITS_TRACK ;
Song_playing = songnum ;
}
}
2013-03-03 01:03:33 +00:00
# endif
2010-06-14 08:13:16 +00:00
break ;
}
2018-07-14 17:23:15 +00:00
# endif
2016-09-11 18:49:15 +00:00
# if DXX_USE_SDLMIXER
2010-06-14 08:13:16 +00:00
case MUSIC_TYPE_CUSTOM :
{
2010-08-01 17:42:38 +00:00
// EXCEPTION: If SONG_ENDLEVEL is undefined, continue playing level song.
2016-08-06 19:55:25 +00:00
if ( Song_playing > = SONG_FIRST_LEVEL_SONG & & songnum = = SONG_ENDLEVEL & & ! CGameCfg . CMMiscMusic [ songnum ] [ 0 ] )
2010-08-01 17:42:38 +00:00
return Song_playing ;
2011-04-18 12:32:36 +00:00
Song_playing = - 1 ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2016-08-06 19:55:25 +00:00
int play = songs_play_file ( CGameCfg . CMMiscMusic [ songnum ] . data ( ) , repeat , NULL ) ;
2013-03-03 01:03:33 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
int use_credits_track = ( songnum = = SONG_TITLE & & GameCfg . OrigTrackOrder ) ;
2016-08-06 19:55:25 +00:00
int play = songs_play_file ( CGameCfg . CMMiscMusic [ songnum ] . data ( ) ,
2010-08-25 00:40:55 +00:00
// Play the credits track after the title track and loop the credits track if original CD track order was chosen
2013-03-03 01:03:33 +00:00
use_credits_track ? 0 : repeat ,
use_credits_track ? play_credits_track : NULL ) ;
# endif
if ( play )
2010-06-14 08:13:16 +00:00
Song_playing = songnum ;
break ;
}
# endif
default :
2011-04-18 12:32:36 +00:00
Song_playing = - 1 ;
2010-06-14 08:13:16 +00:00
break ;
2010-04-05 11:54:23 +00:00
}
2010-06-14 08:13:16 +00:00
2010-04-05 11:54:23 +00:00
return Song_playing ;
2006-03-20 17:12:09 +00:00
}
2016-08-25 04:05:32 +00:00
}
2006-03-20 17:12:09 +00:00
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2013-10-27 22:00:14 +00:00
static void redbook_first_song_func ( )
2009-03-03 12:55:27 +00:00
{
2015-10-09 02:46:09 +00:00
pause_game_world_time p ;
2012-03-29 12:49:45 +00:00
Song_playing = - 1 ; // Playing Redbook tracks will not modify Song_playing. To repeat we must reset this so songs_play_level_song does not think we want to re-play the same song again.
2010-06-14 08:13:16 +00:00
songs_play_level_song ( 1 , 0 ) ;
2009-03-03 12:55:27 +00:00
}
2018-07-14 17:23:15 +00:00
# endif
2009-03-03 12:55:27 +00:00
2010-06-14 08:13:16 +00:00
// play track given by levelnum (depending on the music type and it's playing behaviour) or increment/decrement current track number via offset value
2016-08-25 04:05:32 +00:00
namespace dsx {
2010-06-14 08:13:16 +00:00
int songs_play_level_song ( int levelnum , int offset )
2006-03-20 17:12:09 +00:00
{
2019-08-25 16:43:17 +00:00
int songnum ;
2006-03-20 17:12:09 +00:00
Assert ( levelnum ! = 0 ) ;
2008-11-19 15:52:07 +00:00
songs_init ( ) ;
2010-08-01 17:42:38 +00:00
if ( ! Songs_initialized )
return 0 ;
2011-04-18 12:32:36 +00:00
2006-03-20 17:12:09 +00:00
songnum = ( levelnum > 0 ) ? ( levelnum - 1 ) : ( - levelnum ) ;
2010-06-14 08:13:16 +00:00
switch ( GameCfg . MusicType )
{
case MUSIC_TYPE_BUILTIN :
{
2010-08-01 17:42:38 +00:00
if ( offset )
return Song_playing ;
2011-04-18 12:32:36 +00:00
Song_playing = - 1 ;
2019-08-25 16:43:17 +00:00
/* count_level_songs excludes songs assigned to non-levels,
* such as SONG_TITLE , SONG_BRIEFING , etc .
*/
int count_level_songs ;
if ( levelnum < 0 & & BIMSecretSongs )
2019-08-15 21:20:52 +00:00
{
2019-08-25 16:43:17 +00:00
/* Secret songs are processed separately and do not need
* to exclude non - levels .
*/
const int secretsongnum = ( - levelnum - 1 ) % BIMSecretSongs . size ( ) ;
2019-08-15 21:20:52 +00:00
if ( songs_play_file ( BIMSecretSongs [ secretsongnum ] . filename , 1 , NULL ) )
Song_playing = secretsongnum ;
}
2019-08-25 16:43:17 +00:00
else if ( ( count_level_songs = BIMSongs . size ( ) - SONG_FIRST_LEVEL_SONG ) > 0 )
2010-06-14 08:13:16 +00:00
{
2019-08-25 16:43:17 +00:00
songnum = SONG_FIRST_LEVEL_SONG + ( songnum % count_level_songs ) ;
2012-06-10 12:02:43 +00:00
if ( songs_play_file ( BIMSongs [ songnum ] . filename , 1 , NULL ) )
Song_playing = songnum ;
2010-06-14 08:13:16 +00:00
}
break ;
}
2018-07-14 17:23:15 +00:00
# if DXX_USE_SDL_REDBOOK_AUDIO
2010-06-14 08:13:16 +00:00
case MUSIC_TYPE_REDBOOK :
{
int n_tracks = RBAGetNumberOfTracks ( ) ;
int tracknum ;
2009-03-03 12:55:27 +00:00
2010-06-14 08:13:16 +00:00
if ( ! offset )
2010-08-01 17:42:38 +00:00
{
2011-07-16 09:36:40 +00:00
// we have just been told to play the same as we do already -> ignore
if ( Song_playing > = SONG_FIRST_LEVEL_SONG & & songnum + SONG_FIRST_LEVEL_SONG = = Song_playing )
2010-08-01 17:42:38 +00:00
return Song_playing ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
int bn_tracks = n_tracks ;
# elif defined(DXX_BUILD_DESCENT_II)
int bn_tracks = n_tracks + 1 ;
# endif
tracknum = REDBOOK_FIRST_LEVEL_TRACK + ( ( bn_tracks < = REDBOOK_FIRST_LEVEL_TRACK ) ? 0 : ( songnum % ( bn_tracks - REDBOOK_FIRST_LEVEL_TRACK ) ) ) ;
2010-08-01 17:42:38 +00:00
}
2010-06-14 08:13:16 +00:00
else
{
tracknum = Redbook_playing + offset ;
if ( tracknum < REDBOOK_FIRST_LEVEL_TRACK )
tracknum = n_tracks - ( REDBOOK_FIRST_LEVEL_TRACK - tracknum ) + 1 ;
else if ( tracknum > n_tracks )
tracknum = REDBOOK_FIRST_LEVEL_TRACK + ( tracknum - n_tracks ) - 1 ;
}
2006-03-20 17:12:09 +00:00
2011-04-18 12:32:36 +00:00
Song_playing = - 1 ;
2010-06-14 08:13:16 +00:00
if ( RBAEnabled ( ) & & ( tracknum < = n_tracks ) )
{
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
int have_cd = songs_have_cd ( ) ;
int play = RBAPlayTracks ( tracknum , ! have_cd ? n_tracks : tracknum , have_cd ? redbook_repeat_func : redbook_first_song_func ) ;
# elif defined(DXX_BUILD_DESCENT_II)
int play = RBAPlayTracks ( tracknum , n_tracks , redbook_first_song_func ) ;
# endif
if ( play )
2010-06-14 08:13:16 +00:00
{
2010-08-01 17:42:38 +00:00
Song_playing = songnum + SONG_FIRST_LEVEL_SONG ;
2010-06-14 08:13:16 +00:00
Redbook_playing = tracknum ;
}
}
break ;
}
2018-07-14 17:23:15 +00:00
# endif
2016-09-11 18:49:15 +00:00
# if DXX_USE_SDLMIXER
2010-06-14 08:13:16 +00:00
case MUSIC_TYPE_CUSTOM :
{
2011-01-20 11:17:30 +00:00
if ( GameCfg . CMLevelMusicPlayOrder = = MUSIC_CM_PLAYORDER_RAND )
2016-08-06 19:55:25 +00:00
CGameCfg . CMLevelMusicTrack [ 0 ] = d_rand ( ) % CGameCfg . CMLevelMusicTrack [ 1 ] ; // simply a random selection - no check if this song has already been played. But that's how I roll!
2011-01-20 11:17:30 +00:00
else if ( ! offset )
2010-06-14 08:13:16 +00:00
{
if ( GameCfg . CMLevelMusicPlayOrder = = MUSIC_CM_PLAYORDER_CONT )
{
static int last_songnum = - 1 ;
2011-04-18 12:32:36 +00:00
2010-08-01 17:42:38 +00:00
if ( Song_playing > = SONG_FIRST_LEVEL_SONG )
return Song_playing ;
2006-03-20 17:12:09 +00:00
2010-06-14 08:13:16 +00:00
// As soon as we start a new level, go to next track
if ( last_songnum ! = - 1 & & songnum ! = last_songnum )
2016-08-06 19:55:25 +00:00
( ( CGameCfg . CMLevelMusicTrack [ 0 ] + 1 > = CGameCfg . CMLevelMusicTrack [ 1 ] )
? CGameCfg . CMLevelMusicTrack [ 0 ] = 0
: CGameCfg . CMLevelMusicTrack [ 0 ] + + ) ;
2010-06-14 08:13:16 +00:00
last_songnum = songnum ;
}
2010-08-17 14:59:57 +00:00
else if ( GameCfg . CMLevelMusicPlayOrder = = MUSIC_CM_PLAYORDER_LEVEL )
2016-08-06 19:55:25 +00:00
CGameCfg . CMLevelMusicTrack [ 0 ] = ( songnum % CGameCfg . CMLevelMusicTrack [ 1 ] ) ;
2010-06-14 08:13:16 +00:00
}
else
{
2016-08-06 19:55:25 +00:00
CGameCfg . CMLevelMusicTrack [ 0 ] + = offset ;
if ( CGameCfg . CMLevelMusicTrack [ 0 ] < 0 )
CGameCfg . CMLevelMusicTrack [ 0 ] = CGameCfg . CMLevelMusicTrack [ 1 ] + CGameCfg . CMLevelMusicTrack [ 0 ] ;
if ( CGameCfg . CMLevelMusicTrack [ 0 ] + 1 > CGameCfg . CMLevelMusicTrack [ 1 ] )
CGameCfg . CMLevelMusicTrack [ 0 ] = CGameCfg . CMLevelMusicTrack [ 0 ] - CGameCfg . CMLevelMusicTrack [ 1 ] ;
2010-06-14 08:13:16 +00:00
}
2006-03-20 17:12:09 +00:00
2011-04-18 12:32:36 +00:00
Song_playing = - 1 ;
2010-06-14 08:13:16 +00:00
if ( jukebox_play ( ) )
2010-08-01 17:42:38 +00:00
Song_playing = songnum + SONG_FIRST_LEVEL_SONG ;
2006-03-20 17:12:09 +00:00
2010-06-14 08:13:16 +00:00
break ;
}
# endif
default :
2011-04-18 12:32:36 +00:00
Song_playing = - 1 ;
2010-06-14 08:13:16 +00:00
break ;
}
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
// If we couldn't play the song, most likely because it wasn't specified, play no music.
if ( Song_playing = = - 1 )
songs_stop_all ( ) ;
# endif
2010-06-14 08:13:16 +00:00
return Song_playing ;
2006-03-20 17:12:09 +00:00
}
2016-08-25 04:05:32 +00:00
}
2006-03-20 17:12:09 +00:00
2011-04-18 12:32:36 +00:00
// check which song is playing, or -1 if not playing anything
2010-04-05 11:54:23 +00:00
int songs_is_playing ( )
{
return Song_playing ;
}