doom3-bfg/doomclassic/doom/i_sound_openal.cpp

1047 lines
22 KiB
C++
Raw Normal View History

2013-11-13 17:20:39 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
Copyright (C) 2013 Felix Rueegg
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code 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 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code 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 Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "Precompiled.h"
#include "globaldata.h"
//
// DESCRIPTION:
// System interface for sound.
//
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include <sys/types.h>
#include <fcntl.h>
// Timer stuff. Experimental.
#include <time.h>
#include <signal.h>
#include "z_zone.h"
#include "i_system.h"
#include "i_sound.h"
#include "m_argv.h"
#include "m_misc.h"
#include "w_wad.h"
#include "d_main.h"
#include "doomdef.h"
#include "libs/timidity/timidity.h"
#include "libs/timidity/controls.h"
2013-11-13 17:20:39 +00:00
#include "sound/snd_local.h"
#pragma warning ( disable : 4244 )
#define SFX_RATE 11050
#define SFX_SAMPLETYPE AL_FORMAT_MONO8
#define MIDI_CHANNELS 2
#define MIDI_RATE 22050
#define MIDI_SAMPLETYPE AL_FORMAT_STEREO8
#define MIDI_FORMAT AUDIO_U8
#define MIDI_FORMAT_BYTES 1
ALuint alMusicSourceVoice;
ALuint alMusicBuffer;
MidiSong* doomMusic;
byte* musicBuffer;
int totalBufferSize;
bool waitingForMusic;
bool musicReady;
2022-09-05 20:25:33 +00:00
typedef struct
{
2013-11-13 17:20:39 +00:00
float x;
float y;
float z;
} vec3_t;
2022-09-05 20:25:33 +00:00
typedef struct
{
2013-11-13 17:20:39 +00:00
vec3_t OrientTop;
vec3_t OrientFront;
vec3_t Position;
} doomListener_t;
2022-09-05 20:25:33 +00:00
typedef struct tagActiveSound_t
{
2013-11-13 17:20:39 +00:00
ALuint alSourceVoice;
int id;
int valid;
int start;
int player;
bool localSound;
2022-09-05 20:25:33 +00:00
mobj_t* originator;
2013-11-13 17:20:39 +00:00
} activeSound_t;
// cheap little struct to hold a sound
2022-09-05 20:25:33 +00:00
typedef struct
{
2013-11-13 17:20:39 +00:00
int vol;
int player;
int pitch;
int priority;
2022-09-05 20:25:33 +00:00
mobj_t* originator;
mobj_t* listener;
2013-11-13 17:20:39 +00:00
} soundEvent_t;
// array of all the possible sounds
// in split screen we only process the loudest sound of each type per frame
soundEvent_t soundEvents[128];
extern int PLAYERCOUNT;
// Source voice settings for all sound effects
const ALfloat SFX_MAX_DISTANCE = 1200.f;
const ALfloat SFX_REFERENCE_DISTANCE = 100.f;
const ALfloat SFX_ROLLOFF_FACTOR = 0.2f;
2013-11-13 17:20:39 +00:00
// Real volumes
const float GLOBAL_VOLUME_MULTIPLIER = 0.5f;
float x_SoundVolume = GLOBAL_VOLUME_MULTIPLIER;
float x_MusicVolume = GLOBAL_VOLUME_MULTIPLIER;
// The actual lengths of all sound effects.
static int lengths[NUMSFX];
ALuint alBuffers[NUMSFX];
activeSound_t activeSounds[NUM_SOUNDBUFFERS] = {0};
int S_initialized = 0;
bool Music_initialized = false;
static bool soundHardwareInitialized = false;
static int numOutputChannels = 0;
doomListener_t doom_Listener;
void I_InitSoundChannel( int channel, int numOutputChannels_ );
/*
======================
getsfx
======================
*/
// This function loads the sound data from the WAD lump,
// for single sound.
//
2022-09-05 20:25:33 +00:00
void* getsfx( const char* sfxname, int* len )
2013-11-13 17:20:39 +00:00
{
unsigned char* sfx;
unsigned char* sfxmem;
int size;
char name[20];
int sfxlump;
//float scale = 1.0f;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Get the sound data from the WAD
idStr::snPrintf( name, sizeof( name ), "ds%s", sfxname );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Scale down the plasma gun, it clips
//if ( strcmp( sfxname, "plasma" ) == 0 ) {
// scale = 0.75f;
//}
//if ( strcmp( sfxname, "itemup" ) == 0 ) {
// scale = 1.333f;
//}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// If sound requested is not found in current WAD, use pistol as default
2022-09-05 20:25:33 +00:00
if( W_CheckNumForName( name ) == -1 )
{
2013-11-13 17:20:39 +00:00
sfxlump = W_GetNumForName( "dspistol" );
2022-09-05 20:25:33 +00:00
}
2013-11-13 17:20:39 +00:00
else
2022-09-05 20:25:33 +00:00
{
2013-11-13 17:20:39 +00:00
sfxlump = W_GetNumForName( name );
2022-09-05 20:25:33 +00:00
}
2013-11-13 17:20:39 +00:00
// Sound lump headers are 8 bytes.
const int SOUND_LUMP_HEADER_SIZE_IN_BYTES = 8;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
size = W_LumpLength( sfxlump ) - SOUND_LUMP_HEADER_SIZE_IN_BYTES;
2022-09-05 20:25:33 +00:00
sfx = ( unsigned char* )W_CacheLumpNum( sfxlump, PU_CACHE_SHARED );
const unsigned char* sfxSampleStart = sfx + SOUND_LUMP_HEADER_SIZE_IN_BYTES;
2013-11-13 17:20:39 +00:00
// Allocate from zone memory.
//sfxmem = (float*)DoomLib::Z_Malloc( size*(sizeof(float)), PU_SOUND_SHARED, 0 );
2022-09-05 20:25:33 +00:00
sfxmem = ( unsigned char* )malloc( size * sizeof( unsigned char ) );
2013-11-13 17:20:39 +00:00
// Now copy, and convert to Xbox360 native float samples, do initial volume ramp, and scale
2022-09-05 20:25:33 +00:00
for( int i = 0; i < size; i++ )
{
2013-11-13 17:20:39 +00:00
sfxmem[i] = sfxSampleStart[i];// * scale;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Remove the cached lump.
Z_Free( sfx );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Set length.
*len = size;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Return allocated padded data.
2022-09-05 20:25:33 +00:00
return ( void* )( sfxmem );
2013-11-13 17:20:39 +00:00
}
/*
======================
I_SetChannels
======================
*/
void I_SetChannels()
{
// Original Doom set up lookup tables here
}
/*
======================
I_SetSfxVolume
======================
*/
void I_SetSfxVolume( int volume )
{
2022-09-05 20:25:33 +00:00
x_SoundVolume = ( ( float )volume / 15.f ) * GLOBAL_VOLUME_MULTIPLIER;
2013-11-13 17:20:39 +00:00
}
/*
======================
I_GetSfxLumpNum
======================
*/
//
// Retrieve the raw data lump index
// for a given SFX name.
//
int I_GetSfxLumpNum( sfxinfo_t* sfx )
{
char namebuf[9];
idStr::snPrintf( namebuf, sizeof( namebuf ), "ds%s", sfx->name );
2013-11-13 17:20:39 +00:00
return W_GetNumForName( namebuf );
}
/*
======================
I_StartSound2
======================
*/
// Starting a sound means adding it
// to the current list of active sounds
// in the internal channels.
// As the SFX info struct contains
// e.g. a pointer to the raw data,
// it is ignored.
// As our sound handling does not handle
// priority, it is ignored.
// Pitching (that is, increased speed of playback) is set
//
2022-09-05 20:25:33 +00:00
int I_StartSound2( int id, int player, mobj_t* origin, mobj_t* listener_origin, int pitch, int priority )
2013-11-13 17:20:39 +00:00
{
2022-09-05 20:25:33 +00:00
if( !soundHardwareInitialized || id == 0 )
{
2013-11-13 17:20:39 +00:00
return id;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
int i;
activeSound_t* sound = 0;
int oldest = 0, oldestnum = -1;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// these id's should not overlap
2022-09-05 20:25:33 +00:00
if( id == sfx_sawup || id == sfx_sawidl || id == sfx_sawful || id == sfx_sawhit || id == sfx_stnmov )
{
2013-11-13 17:20:39 +00:00
// Loop all channels, check.
2022-09-05 20:25:33 +00:00
for( i = 0; i < NUM_SOUNDBUFFERS; i++ )
2013-11-13 17:20:39 +00:00
{
sound = &activeSounds[i];
2022-09-05 20:25:33 +00:00
if( sound->valid && ( sound->id == id && sound->player == player ) )
{
2013-11-13 17:20:39 +00:00
I_StopSound( sound->id, player );
break;
}
}
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// find a valid channel, or one that has finished playing
2022-09-05 20:25:33 +00:00
for( i = 0; i < NUM_SOUNDBUFFERS; i++ )
{
2013-11-13 17:20:39 +00:00
sound = &activeSounds[i];
2022-09-05 20:25:33 +00:00
if( !sound->valid )
{
2013-11-13 17:20:39 +00:00
break;
2022-09-05 20:25:33 +00:00
}
if( !oldest || oldest > sound->start )
{
2013-11-13 17:20:39 +00:00
oldestnum = i;
oldest = sound->start;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
ALint sourceState;
alGetSourcei( sound->alSourceVoice, AL_SOURCE_STATE, &sourceState );
2022-09-05 20:25:33 +00:00
if( sourceState == AL_STOPPED )
{
2013-11-13 17:20:39 +00:00
break;
}
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// none found, so use the oldest one
2022-09-05 20:25:33 +00:00
if( i == NUM_SOUNDBUFFERS )
{
2013-11-13 17:20:39 +00:00
i = oldestnum;
sound = &activeSounds[i];
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
alSourceStop( sound->alSourceVoice );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Attach the source voice to the correct buffer
2022-09-05 20:25:33 +00:00
if( sound->id != id )
{
2013-11-13 17:20:39 +00:00
alSourcei( sound->alSourceVoice, AL_BUFFER, 0 );
alSourcei( sound->alSourceVoice, AL_BUFFER, alBuffers[id] );
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Set the source voice volume
alSourcef( sound->alSourceVoice, AL_GAIN, x_SoundVolume );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Set the source voice pitch
2022-09-05 20:25:33 +00:00
alSourcef( sound->alSourceVoice, AL_PITCH, 1 + ( ( float )pitch - 128.f ) / 95.f );
2013-11-13 17:20:39 +00:00
// Set the source voice position
ALfloat x = 0.f;
ALfloat y = 0.f;
ALfloat z = 0.f;
2022-09-05 20:25:33 +00:00
if( origin )
{
if( origin == listener_origin )
{
2013-11-13 17:20:39 +00:00
sound->localSound = true;
2022-09-05 20:25:33 +00:00
}
else
{
2013-11-13 17:20:39 +00:00
sound->localSound = false;
2022-09-05 20:25:33 +00:00
x = ( ALfloat )( origin->x >> FRACBITS );
z = ( ALfloat )( origin->y >> FRACBITS );
2013-11-13 17:20:39 +00:00
}
2022-09-05 20:25:33 +00:00
}
else
{
2013-11-13 17:20:39 +00:00
sound->localSound = true;
}
2022-09-05 20:25:33 +00:00
if( sound->localSound )
{
2013-11-13 17:20:39 +00:00
x = doom_Listener.Position.x;
z = doom_Listener.Position.z;
}
alSource3f( sound->alSourceVoice, AL_POSITION, x, y, z );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
alSourcePlay( sound->alSourceVoice );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Set id, and start time
sound->id = id;
sound->start = ::g->gametic;
sound->valid = 1;
sound->player = player;
sound->originator = origin;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
return id;
}
/*
======================
I_ProcessSoundEvents
======================
*/
void I_ProcessSoundEvents( void )
{
2022-09-05 20:25:33 +00:00
for( int i = 0; i < 128; i++ )
{
if( soundEvents[i].pitch )
{
2013-11-13 17:20:39 +00:00
I_StartSound2( i, soundEvents[i].player, soundEvents[i].originator, soundEvents[i].listener,
2022-09-05 20:25:33 +00:00
soundEvents[i].pitch, soundEvents[i].priority );
2013-11-13 17:20:39 +00:00
}
}
memset( soundEvents, 0, sizeof( soundEvents ) );
}
/*
======================
I_StartSound
======================
*/
2022-09-05 20:25:33 +00:00
int I_StartSound( int id, mobj_t* origin, mobj_t* listener_origin, int vol, int pitch, int priority )
2013-11-13 17:20:39 +00:00
{
// only allow player 0s sounds in intermission and finale screens
2022-09-05 20:25:33 +00:00
if( ::g->gamestate != GS_LEVEL && DoomLib::GetPlayer() != 0 )
{
2013-11-13 17:20:39 +00:00
return 0;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// if we're only one player or we're trying to play the chainsaw sound, do it normal
// otherwise only allow one sound of each type per frame
2022-09-05 20:25:33 +00:00
if( PLAYERCOUNT == 1 || id == sfx_sawup || id == sfx_sawidl || id == sfx_sawful || id == sfx_sawhit )
{
2013-11-13 17:20:39 +00:00
return I_StartSound2( id, ::g->consoleplayer, origin, listener_origin, pitch, priority );
2022-09-05 20:25:33 +00:00
}
else
{
if( soundEvents[ id ].vol < vol )
{
2013-11-13 17:20:39 +00:00
soundEvents[ id ].player = DoomLib::GetPlayer();
soundEvents[ id ].pitch = pitch;
soundEvents[ id ].priority = priority;
soundEvents[ id ].vol = vol;
soundEvents[ id ].originator = origin;
soundEvents[ id ].listener = listener_origin;
}
return id;
}
}
/*
======================
I_StopSound
======================
*/
2022-09-05 20:25:33 +00:00
void I_StopSound( int handle, int player )
2013-11-13 17:20:39 +00:00
{
// You need the handle returned by StartSound.
// Would be looping all channels,
// tracking down the handle,
// and setting the channel to zero.
int i;
activeSound_t* sound = 0;
2022-09-05 20:25:33 +00:00
for( i = 0; i < NUM_SOUNDBUFFERS; ++i )
{
2013-11-13 17:20:39 +00:00
sound = &activeSounds[i];
2022-09-05 20:25:33 +00:00
if( !sound->valid || sound->id != handle || ( player >= 0 && sound->player != player ) )
{
2013-11-13 17:20:39 +00:00
continue;
2022-09-05 20:25:33 +00:00
}
2013-11-13 17:20:39 +00:00
break;
}
2022-09-05 20:25:33 +00:00
if( i == NUM_SOUNDBUFFERS )
{
2013-11-13 17:20:39 +00:00
return;
2022-09-05 20:25:33 +00:00
}
2013-11-13 17:20:39 +00:00
// Stop the sound
alSourceStop( sound->alSourceVoice );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
sound->valid = 0;
sound->player = -1;
}
/*
======================
I_SoundIsPlaying
======================
*/
int I_SoundIsPlaying( int handle )
{
2022-09-05 20:25:33 +00:00
if( !soundHardwareInitialized )
{
2013-11-13 17:20:39 +00:00
return 0;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
int i;
activeSound_t* sound;
2022-09-05 20:25:33 +00:00
for( i = 0; i < NUM_SOUNDBUFFERS; ++i )
{
2013-11-13 17:20:39 +00:00
sound = &activeSounds[i];
2022-09-05 20:25:33 +00:00
if( !sound->valid || sound->id != handle )
{
2013-11-13 17:20:39 +00:00
continue;
2022-09-05 20:25:33 +00:00
}
2013-11-13 17:20:39 +00:00
ALint sourceState;
alGetSourcei( sound->alSourceVoice, AL_SOURCE_STATE, &sourceState );
2022-09-05 20:25:33 +00:00
if( sourceState == AL_PLAYING )
{
2013-11-13 17:20:39 +00:00
return 1;
}
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
return 0;
}
/*
======================
I_UpdateSound
======================
*/
// Update listener position and go through all the
// channels and update sound positions.
void I_UpdateSound( void )
{
2022-09-05 20:25:33 +00:00
if( !soundHardwareInitialized )
{
2013-11-13 17:20:39 +00:00
return;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Update listener orientation and position
2022-09-05 20:25:33 +00:00
mobj_t* playerObj = ::g->players[0].mo;
if( playerObj )
{
2013-11-13 17:20:39 +00:00
angle_t pAngle = playerObj->angle;
fixed_t fx, fz;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
pAngle >>= ANGLETOFINESHIFT;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
fx = finecosine[pAngle];
fz = finesine[pAngle];
2022-09-05 20:25:33 +00:00
doom_Listener.OrientFront.x = ( float )( fx ) / 65535.f;
2013-11-13 17:20:39 +00:00
doom_Listener.OrientFront.y = 0.f;
2022-09-05 20:25:33 +00:00
doom_Listener.OrientFront.z = ( float )( fz ) / 65535.f;
doom_Listener.Position.x = ( float )( playerObj->x >> FRACBITS );
2013-11-13 17:20:39 +00:00
doom_Listener.Position.y = 0.f;
2022-09-05 20:25:33 +00:00
doom_Listener.Position.z = ( float )( playerObj->y >> FRACBITS );
}
else
{
2013-11-13 17:20:39 +00:00
doom_Listener.OrientFront.x = 0.f;
doom_Listener.OrientFront.y = 0.f;
doom_Listener.OrientFront.z = 1.f;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
doom_Listener.Position.x = 0.f;
doom_Listener.Position.y = 0.f;
doom_Listener.Position.z = 0.f;
}
2022-09-05 20:25:33 +00:00
ALfloat listenerOrientation[] = { doom_Listener.OrientFront.x, doom_Listener.OrientFront.y,
2022-09-05 20:25:33 +00:00
doom_Listener.OrientFront.z, doom_Listener.OrientTop.x, doom_Listener.OrientTop.y,
doom_Listener.OrientTop.z
};
2013-11-13 17:20:39 +00:00
alListenerfv( AL_ORIENTATION, listenerOrientation );
alListener3f( AL_POSITION, doom_Listener.Position.x, doom_Listener.Position.y, doom_Listener.Position.z );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Update playing source voice positions
int i;
activeSound_t* sound;
2022-09-05 20:25:33 +00:00
for( i = 0; i < NUM_SOUNDBUFFERS; i++ )
{
2013-11-13 17:20:39 +00:00
sound = &activeSounds[i];
2022-09-05 20:25:33 +00:00
if( !sound->valid )
{
2013-11-13 17:20:39 +00:00
continue;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
ALint sourceState;
alGetSourcei( sound->alSourceVoice, AL_SOURCE_STATE, &sourceState );
2022-09-05 20:25:33 +00:00
if( sourceState == AL_PLAYING )
{
if( sound->localSound )
{
2013-11-13 17:20:39 +00:00
alSource3f( sound->alSourceVoice, AL_POSITION, doom_Listener.Position.x,
2022-09-05 20:25:33 +00:00
doom_Listener.Position.y, doom_Listener.Position.z );
}
else
{
ALfloat x = ( ALfloat )( sound->originator->x >> FRACBITS );
2013-11-13 17:20:39 +00:00
ALfloat y = 0.f;
2022-09-05 20:25:33 +00:00
ALfloat z = ( ALfloat )( sound->originator->y >> FRACBITS );
2013-11-13 17:20:39 +00:00
alSource3f( sound->alSourceVoice, AL_POSITION, x, y, z );
}
}
}
}
/*
======================
I_UpdateSoundParams
======================
*/
void I_UpdateSoundParams( int handle, int vol, int sep, int pitch )
{
}
/*
======================
I_ShutdownSound
======================
*/
void I_ShutdownSound( void )
{
int done = 0;
int i;
2022-09-05 20:25:33 +00:00
if( S_initialized )
{
2013-11-13 17:20:39 +00:00
// Stop all sounds
2022-09-05 20:25:33 +00:00
for( i = 0; i < NUM_SOUNDBUFFERS; i++ )
{
activeSound_t* sound = &activeSounds[i];
if( !sound )
{
2013-11-13 17:20:39 +00:00
continue;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
I_StopSound( sound->id, 0 );
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Free allocated sound memory
2022-09-05 20:25:33 +00:00
for( i = 1; i < NUMSFX; i++ )
{
if( S_sfx[i].data && !( S_sfx[i].link ) )
{
2013-11-13 17:20:39 +00:00
free( S_sfx[i].data );
}
}
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
I_StopSong( 0 );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
S_initialized = 0;
}
/*
======================
I_InitSoundHardware
Called from the tech4x initialization code. Sets up Doom classic's
sound channels.
======================
*/
void I_InitSoundHardware( int numOutputChannels_, int channelMask )
{
::numOutputChannels = numOutputChannels_;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Initialize source voices
2022-09-05 20:25:33 +00:00
for( int i = 0; i < NUM_SOUNDBUFFERS; i++ )
{
2013-11-13 17:20:39 +00:00
I_InitSoundChannel( i, numOutputChannels );
}
// Create OpenAL buffers for all sounds
2022-09-05 20:25:33 +00:00
for( int i = 1; i < NUMSFX; i++ )
{
alGenBuffers( ( ALuint )1, &alBuffers[i] );
2013-11-13 17:20:39 +00:00
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
I_InitMusic();
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
soundHardwareInitialized = true;
}
/*
======================
I_ShutdownSoundHardware
Called from the tech4x shutdown code. Tears down Doom classic's
sound channels.
======================
*/
void I_ShutdownSoundHardware()
{
soundHardwareInitialized = false;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
I_ShutdownMusic();
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Delete all source voices
2022-09-05 20:25:33 +00:00
for( int i = 0; i < NUM_SOUNDBUFFERS; ++i )
{
activeSound_t* sound = &activeSounds[i];
if( !sound )
{
2013-11-13 17:20:39 +00:00
continue;
}
2022-09-05 20:25:33 +00:00
if( sound->alSourceVoice )
{
2013-11-13 17:20:39 +00:00
alSourceStop( sound->alSourceVoice );
alSourcei( sound->alSourceVoice, AL_BUFFER, 0 );
alDeleteSources( 1, &sound->alSourceVoice );
}
}
// Delete OpenAL buffers for all sounds
2022-09-05 20:25:33 +00:00
for( int i = 0; i < NUMSFX; i++ )
{
2013-11-13 17:20:39 +00:00
alDeleteBuffers( 1, &alBuffers[i] );
}
}
/*
======================
I_InitSoundChannel
======================
*/
void I_InitSoundChannel( int channel, int numOutputChannels_ )
{
2022-09-05 20:25:33 +00:00
activeSound_t* soundchannel = &activeSounds[ channel ];
alGenSources( ( ALuint )1, &soundchannel->alSourceVoice );
2013-11-13 17:20:39 +00:00
alSource3f( soundchannel->alSourceVoice, AL_VELOCITY, 0.f, 0.f, 0.f );
alSourcef( soundchannel->alSourceVoice, AL_LOOPING, AL_FALSE );
alSourcef( soundchannel->alSourceVoice, AL_MAX_DISTANCE, SFX_MAX_DISTANCE );
alSourcef( soundchannel->alSourceVoice, AL_REFERENCE_DISTANCE, SFX_REFERENCE_DISTANCE );
alSourcef( soundchannel->alSourceVoice, AL_ROLLOFF_FACTOR, SFX_ROLLOFF_FACTOR );
2013-11-13 17:20:39 +00:00
}
/*
======================
I_InitSound
======================
*/
void I_InitSound()
{
2022-09-05 20:25:33 +00:00
if( S_initialized == 0 )
{
2013-11-13 17:20:39 +00:00
// Set up listener parameters
doom_Listener.OrientFront.x = 0.f;
doom_Listener.OrientFront.y = 0.f;
doom_Listener.OrientFront.z = 1.f;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
doom_Listener.OrientTop.x = 0.f;
doom_Listener.OrientTop.y = -1.f;
2013-11-13 17:20:39 +00:00
doom_Listener.OrientTop.z = 0.f;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
doom_Listener.Position.x = 0.f;
doom_Listener.Position.y = 0.f;
doom_Listener.Position.z = 0.f;
2022-09-05 20:25:33 +00:00
for( int i = 1; i < NUMSFX; i++ )
{
2013-11-13 17:20:39 +00:00
// Alias? Example is the chaingun sound linked to pistol.
2022-09-05 20:25:33 +00:00
if( !S_sfx[i].link )
{
2013-11-13 17:20:39 +00:00
// Load data from WAD file.
S_sfx[i].data = getsfx( S_sfx[i].name, &lengths[i] );
2022-09-05 20:25:33 +00:00
}
else
{
2013-11-13 17:20:39 +00:00
// Previously loaded already?
S_sfx[i].data = S_sfx[i].link->data;
2022-09-05 20:25:33 +00:00
lengths[i] = lengths[( S_sfx[i].link - S_sfx ) / sizeof( sfxinfo_t ) ];
2013-11-13 17:20:39 +00:00
}
2022-09-05 20:25:33 +00:00
if( S_sfx[i].data )
{
alBufferData( alBuffers[i], SFX_SAMPLETYPE, ( byte* )S_sfx[i].data, lengths[i], SFX_RATE );
2013-11-13 17:20:39 +00:00
}
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
S_initialized = 1;
}
}
/*
======================
I_SubmitSound
======================
*/
void I_SubmitSound( void )
{
// Only do this for player 0, it will still handle positioning
// for other players, but it can't be outside the game
// frame like the soundEvents are.
2022-09-05 20:25:33 +00:00
if( DoomLib::GetPlayer() == 0 )
{
2013-11-13 17:20:39 +00:00
// Do 3D positioning of sounds
I_UpdateSound();
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Change music if required
I_UpdateMusic();
}
}
// =========================================================
// =========================================================
// Background Music
// =========================================================
// =========================================================
/*
======================
I_SetMusicVolume
======================
*/
void I_SetMusicVolume( int volume )
{
2022-09-05 20:25:33 +00:00
x_MusicVolume = ( float )volume / 15.f;
2013-11-13 17:20:39 +00:00
}
/*
======================
I_InitMusic
======================
*/
void I_InitMusic( void )
{
2022-09-05 20:25:33 +00:00
if( !Music_initialized )
{
2013-11-13 17:20:39 +00:00
// Initialize Timidity
Timidity_Init( MIDI_RATE, MIDI_FORMAT, MIDI_CHANNELS, MIDI_RATE, "classicmusic/gravis.cfg" );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
musicBuffer = NULL;
totalBufferSize = 0;
waitingForMusic = false;
musicReady = false;
2022-09-05 20:25:33 +00:00
alGenSources( ( ALuint )1, &alMusicSourceVoice );
2013-11-13 17:20:39 +00:00
alSourcef( alMusicSourceVoice, AL_PITCH, 1.f );
alSourcef( alMusicSourceVoice, AL_LOOPING, AL_TRUE );
2022-09-05 20:25:33 +00:00
alGenBuffers( ( ALuint )1, &alMusicBuffer );
2013-11-13 17:20:39 +00:00
Music_initialized = true;
}
}
/*
======================
I_ShutdownMusic
======================
*/
void I_ShutdownMusic( void )
{
2022-09-05 20:25:33 +00:00
if( Music_initialized )
{
if( alMusicSourceVoice )
{
2013-11-13 17:20:39 +00:00
I_StopSong( 0 );
alSourcei( alMusicSourceVoice, AL_BUFFER, 0 );
alDeleteSources( 1, &alMusicSourceVoice );
}
2022-09-05 20:25:33 +00:00
if( alMusicBuffer )
{
2013-11-13 17:20:39 +00:00
alDeleteBuffers( 1, &alMusicBuffer );
}
2022-09-05 20:25:33 +00:00
if( musicBuffer )
{
2013-11-13 17:20:39 +00:00
free( musicBuffer );
musicBuffer = NULL;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
Timidity_Shutdown();
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
totalBufferSize = 0;
waitingForMusic = false;
musicReady = false;
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
Music_initialized = false;
}
2022-09-05 20:25:33 +00:00
int Mus2Midi( unsigned char* bytes, unsigned char* out, int* len );
2013-11-13 17:20:39 +00:00
2022-09-05 20:25:33 +00:00
namespace
{
const int MaxMidiConversionSize = 1024 * 1024;
unsigned char midiConversionBuffer[MaxMidiConversionSize];
2013-11-13 17:20:39 +00:00
}
/*
======================
I_LoadSong
======================
*/
2022-09-05 20:25:33 +00:00
void I_LoadSong( const char* songname )
2013-11-13 17:20:39 +00:00
{
idStr lumpName = "d_";
2022-09-05 20:25:33 +00:00
lumpName += static_cast< const char* >( songname );
unsigned char* musFile = static_cast< unsigned char* >( W_CacheLumpName( lumpName.c_str(), PU_STATIC_SHARED ) );
2013-11-13 17:20:39 +00:00
int length = 0;
Mus2Midi( musFile, midiConversionBuffer, &length );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
doomMusic = Timidity_LoadSongMem( midiConversionBuffer, length );
2022-09-05 20:25:33 +00:00
if( doomMusic )
{
musicBuffer = ( byte* )malloc( MIDI_CHANNELS * MIDI_FORMAT_BYTES * doomMusic->samples );
2013-11-13 17:20:39 +00:00
totalBufferSize = doomMusic->samples * MIDI_CHANNELS * MIDI_FORMAT_BYTES;
Timidity_Start( doomMusic );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
int rc = RC_NO_RETURN_VALUE;
int num_bytes = 0;
int offset = 0;
2022-09-05 20:25:33 +00:00
do
{
2013-11-13 17:20:39 +00:00
rc = Timidity_PlaySome( musicBuffer + offset, MIDI_RATE, &num_bytes );
offset += num_bytes;
2022-09-05 20:25:33 +00:00
}
while( rc != RC_TUNE_END );
2013-11-13 17:20:39 +00:00
Timidity_Stop();
Timidity_FreeSong( doomMusic );
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
musicReady = true;
}
/*
======================
I_PlaySong
======================
*/
2022-09-05 20:25:33 +00:00
void I_PlaySong( const char* songname, int looping )
2013-11-13 17:20:39 +00:00
{
2022-09-05 20:25:33 +00:00
if( !Music_initialized )
{
2013-11-13 17:20:39 +00:00
return;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
I_StopSong( 0 );
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
// Clear old state
2022-09-05 20:25:33 +00:00
if( musicBuffer )
{
2013-11-13 17:20:39 +00:00
free( musicBuffer );
musicBuffer = 0;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
musicReady = false;
I_LoadSong( songname );
waitingForMusic = true;
2022-09-05 20:25:33 +00:00
if( DoomLib::GetPlayer() >= 0 )
{
2013-11-13 17:20:39 +00:00
::g->mus_looping = looping;
}
}
/*
======================
I_UpdateMusic
======================
*/
void I_UpdateMusic( void )
{
2022-09-05 20:25:33 +00:00
if( !Music_initialized )
{
2013-11-13 17:20:39 +00:00
return;
}
2022-09-05 20:25:33 +00:00
if( alMusicSourceVoice )
{
2013-11-13 17:20:39 +00:00
// Set the volume
alSourcef( alMusicSourceVoice, AL_GAIN, x_MusicVolume * GLOBAL_VOLUME_MULTIPLIER );
}
2022-09-05 20:25:33 +00:00
if( waitingForMusic )
{
if( musicReady && alMusicSourceVoice )
{
if( musicBuffer )
{
2013-11-13 17:20:39 +00:00
alSourcei( alMusicSourceVoice, AL_BUFFER, 0 );
alBufferData( alMusicBuffer, MIDI_SAMPLETYPE, musicBuffer, totalBufferSize, MIDI_RATE );
alSourcei( alMusicSourceVoice, AL_BUFFER, alMusicBuffer );
alSourcePlay( alMusicSourceVoice );
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
waitingForMusic = false;
}
}
}
/*
======================
I_PauseSong
======================
*/
2022-09-05 20:25:33 +00:00
void I_PauseSong( int handle )
2013-11-13 17:20:39 +00:00
{
2022-09-05 20:25:33 +00:00
if( !Music_initialized || !alMusicSourceVoice )
{
2013-11-13 17:20:39 +00:00
return;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
alSourcePause( alMusicSourceVoice );
}
/*
======================
I_ResumeSong
======================
*/
2022-09-05 20:25:33 +00:00
void I_ResumeSong( int handle )
2013-11-13 17:20:39 +00:00
{
2022-09-05 20:25:33 +00:00
if( !Music_initialized || !alMusicSourceVoice )
{
2013-11-13 17:20:39 +00:00
return;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
alSourcePlay( alMusicSourceVoice );
}
/*
======================
I_StopSong
======================
*/
void I_StopSong( int handle )
{
2022-09-05 20:25:33 +00:00
if( !Music_initialized || !alMusicSourceVoice )
{
2013-11-13 17:20:39 +00:00
return;
}
2022-09-05 20:25:33 +00:00
2013-11-13 17:20:39 +00:00
alSourceStop( alMusicSourceVoice );
}
/*
======================
I_UnRegisterSong
======================
*/
void I_UnRegisterSong( int handle )
{
// does nothing
}
/*
======================
I_RegisterSong
======================
*/
int I_RegisterSong( void* data, int length )
{
// does nothing
return 0;
}