2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
|
|
|
Copyright (C) 2005-2007 Warzone Resurrection Project
|
|
|
|
|
|
|
|
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-02-01 13:56:02 -08:00
|
|
|
/** \file
|
|
|
|
* Sound library-specific functions
|
|
|
|
*/
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
// this has to be first
|
2006-06-02 12:34:58 -07:00
|
|
|
#include "lib/framework/frame.h"
|
2008-03-07 04:42:42 -08:00
|
|
|
#include "lib/framework/math-help.h"
|
2007-04-09 05:16:31 -07:00
|
|
|
#include "lib/framework/frameresource.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
|
|
|
# ifdef WZ_OS_MAC
|
|
|
|
# include <OpenAL/al.h>
|
|
|
|
# include <OpenAL/alc.h>
|
|
|
|
# else
|
|
|
|
# include <AL/al.h>
|
|
|
|
# include <AL/alc.h>
|
|
|
|
# endif
|
2006-08-12 03:45:49 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-06-16 12:10:23 -07:00
|
|
|
#include <physfs.h>
|
2006-11-06 06:40:07 -08:00
|
|
|
#include <string.h>
|
2008-01-27 16:22:02 -08:00
|
|
|
#include <math.h>
|
2006-06-16 12:10:23 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "tracklib.h"
|
|
|
|
#include "audio.h"
|
2007-09-16 07:05:01 -07:00
|
|
|
#include "cdaudio.h"
|
2007-04-30 10:58:27 -07:00
|
|
|
#include "oggvorbis.h"
|
2007-12-13 15:16:03 -08:00
|
|
|
#include "openal_error.h"
|
2008-02-01 13:26:37 -08:00
|
|
|
#include "mixer.h"
|
2007-01-24 11:42:20 -08:00
|
|
|
|
2006-09-20 12:46:33 -07:00
|
|
|
#define ATTENUATION_FACTOR 0.0003f
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2008-07-08 11:24:42 -07:00
|
|
|
ALuint current_queue_sample = -1;
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-06-18 09:29:10 -07:00
|
|
|
static BOOL openal_initialized = false;
|
|
|
|
|
2008-01-27 13:28:35 -08:00
|
|
|
struct __audio_stream
|
2007-12-31 07:16:59 -08:00
|
|
|
{
|
|
|
|
#ifndef WZ_NOSOUND
|
|
|
|
ALuint source; // OpenAL name of the sound source
|
|
|
|
#endif
|
|
|
|
struct OggVorbisDecoderState* decoder;
|
|
|
|
PHYSFS_file* fileHandle;
|
|
|
|
float volume;
|
|
|
|
|
|
|
|
// Callbacks
|
|
|
|
void (*onFinished)(void*);
|
|
|
|
void *user_data;
|
|
|
|
|
2008-01-27 15:32:55 -08:00
|
|
|
size_t bufferSize;
|
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
// Linked list pointer
|
|
|
|
struct __audio_stream *next;
|
2008-01-27 13:28:35 -08:00
|
|
|
};
|
2007-12-31 07:16:59 -08:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
typedef struct SAMPLE_LIST
|
|
|
|
{
|
|
|
|
struct AUDIO_SAMPLE *curr;
|
|
|
|
struct SAMPLE_LIST *next;
|
|
|
|
} SAMPLE_LIST;
|
|
|
|
|
|
|
|
static SAMPLE_LIST *active_samples = NULL;
|
2007-12-31 07:16:59 -08:00
|
|
|
static AUDIO_STREAM* active_streams = NULL;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-28 10:47:08 -07:00
|
|
|
static ALfloat sfx_volume = 1.0;
|
|
|
|
static ALfloat sfx3d_volume = 1.0;
|
|
|
|
|
2008-07-09 12:07:42 -07:00
|
|
|
static ALCdevice* device = NULL;
|
|
|
|
static ALCcontext* context = NULL;
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
/** Removes the given sample from the "active_samples" linked list
|
|
|
|
* \param previous either NULL (if \c to_remove is the first item in the
|
|
|
|
* list) or the item occurring just before \c to_remove in
|
|
|
|
* the list
|
|
|
|
* \param to_remove the item to actually remove from the list
|
|
|
|
*/
|
|
|
|
static void sound_RemoveSample(SAMPLE_LIST* previous, SAMPLE_LIST* to_remove)
|
|
|
|
{
|
|
|
|
if (previous != NULL && previous != to_remove)
|
|
|
|
{
|
|
|
|
// Verify that the given two samples actually follow eachother in the list
|
|
|
|
ASSERT(previous->next == to_remove, "Sound samples don't follow eachother in the list, we're probably removing the wrong item.");
|
|
|
|
|
|
|
|
// Remove the item to remove from the linked list by skipping
|
|
|
|
// it in the pointer sequence.
|
|
|
|
previous->next = to_remove->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Apparently we're removing the first item from the list. So
|
|
|
|
// make the next one the list's head.
|
|
|
|
active_samples = to_remove->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-12-13 15:16:03 -08:00
|
|
|
static void PrintOpenALVersion(code_part part)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-10-21 11:40:05 -07:00
|
|
|
const ALchar* pDeviceNames = NULL;
|
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
debug(part, "OpenAL Vendor: %s", alGetString(AL_VENDOR));
|
|
|
|
debug(part, "OpenAL Version: %s", alGetString(AL_VERSION));
|
|
|
|
debug(part, "OpenAL Renderer: %s", alGetString(AL_RENDERER));
|
|
|
|
debug(part, "OpenAL Extensions: %s", alGetString(AL_EXTENSIONS));
|
2008-10-21 11:40:05 -07:00
|
|
|
|
|
|
|
// We are parsing to see what devices are available
|
|
|
|
pDeviceNames = alcGetString( NULL, ALC_DEVICE_SPECIFIER );
|
|
|
|
while ( strlen(pDeviceNames) )
|
|
|
|
{
|
|
|
|
debug(part, "available openAL device(s) are: %s", pDeviceNames);
|
|
|
|
pDeviceNames += strlen(pDeviceNames)+1;
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
BOOL sound_InitLibrary( void )
|
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-12-13 15:16:03 -08:00
|
|
|
int err;
|
2008-07-09 12:07:42 -07:00
|
|
|
const ALfloat listenerVel[3] = { 0.0, 0.0, 0.0 };
|
|
|
|
const ALfloat listenerOri[6] = { 0.0, 0.0, 1.0, 0.0, 1.0, 0.0 };
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
device = alcOpenDevice(0);
|
2008-07-09 12:07:42 -07:00
|
|
|
if (!device)
|
2007-12-13 15:16:03 -08:00
|
|
|
{
|
|
|
|
PrintOpenALVersion(LOG_ERROR);
|
2007-06-28 10:47:08 -07:00
|
|
|
debug(LOG_ERROR, "Couldn't open audio device.");
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
context = alcCreateContext(device, NULL); //NULL was contextAttributes
|
2008-07-09 12:07:42 -07:00
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
PrintOpenALVersion(LOG_ERROR);
|
|
|
|
debug(LOG_ERROR, "Couldn't open audio context.");
|
|
|
|
return false;
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
alcMakeContextCurrent(context);
|
2006-06-02 12:34:58 -07:00
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
err = sound_GetDeviceError(device);
|
|
|
|
if (err != ALC_NO_ERROR)
|
|
|
|
{
|
|
|
|
PrintOpenALVersion(LOG_ERROR);
|
|
|
|
debug(LOG_ERROR, "Couldn't initialize audio context: %s", alcGetString(device, err));
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
openal_initialized = true;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-28 10:47:08 -07:00
|
|
|
// Clear Error Codes
|
|
|
|
alGetError();
|
|
|
|
alcGetError(device);
|
|
|
|
|
|
|
|
// Check what version of Open AL we are using
|
2007-12-13 15:16:03 -08:00
|
|
|
PrintOpenALVersion(LOG_SOUND);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
alListener3f(AL_POSITION, 0.f, 0.f, 0.f);
|
2007-06-28 10:47:08 -07:00
|
|
|
alListenerfv( AL_VELOCITY, listenerVel );
|
|
|
|
alListenerfv( AL_ORIENTATION, listenerOri );
|
|
|
|
alDistanceModel( AL_NONE );
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-08-28 13:12:47 -07:00
|
|
|
static void sound_UpdateStreams(void);
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
void sound_ShutdownLibrary( void )
|
|
|
|
{
|
2008-08-28 13:12:47 -07:00
|
|
|
AUDIO_STREAM* stream;
|
2006-08-23 05:58:48 -07:00
|
|
|
SAMPLE_LIST * aSample = active_samples, * tmpSample = NULL;
|
|
|
|
|
2008-06-18 09:29:10 -07:00
|
|
|
if ( !openal_initialized )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-07-09 12:07:42 -07:00
|
|
|
debug(LOG_SOUND, "starting shutdown");
|
|
|
|
|
2008-08-28 13:12:47 -07:00
|
|
|
// Stop all streams, sound_UpdateStreams() will deallocate all stopped streams
|
|
|
|
for (stream = active_streams; stream != NULL; stream = stream->next)
|
|
|
|
{
|
|
|
|
sound_StopStream(stream);
|
|
|
|
}
|
|
|
|
sound_UpdateStreams();
|
|
|
|
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2008-07-09 12:07:42 -07:00
|
|
|
/* On Linux since this caused some versions of OpenAL to hang on exit. - Per */
|
|
|
|
debug(LOG_SOUND, "make default context NULL");
|
|
|
|
alcMakeContextCurrent(NULL);
|
|
|
|
|
|
|
|
debug(LOG_SOUND, "destroy previous context");
|
|
|
|
alcDestroyContext(context); // this gives a long delay on some impl.
|
|
|
|
|
|
|
|
debug(LOG_SOUND, "close device");
|
|
|
|
alcCloseDevice(device);
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2006-02-18 10:54:37 -08:00
|
|
|
|
2006-08-23 05:58:48 -07:00
|
|
|
while( aSample )
|
2007-12-31 07:16:59 -08:00
|
|
|
{
|
2006-08-23 05:58:48 -07:00
|
|
|
tmpSample = aSample->next;
|
|
|
|
free( aSample );
|
|
|
|
aSample = tmpSample;
|
|
|
|
}
|
2007-12-13 15:16:03 -08:00
|
|
|
active_samples = NULL;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-12-15 12:49:02 -08:00
|
|
|
/** Deletes the given sample and updates the \c previous and \c current iterators
|
|
|
|
* \param previous iterator to the previous sample in the list
|
|
|
|
* \param sample iterator to the current sample in the list which you want to delete
|
|
|
|
*/
|
|
|
|
static void sound_DestroyIteratedSample(SAMPLE_LIST** previous, SAMPLE_LIST** sample)
|
|
|
|
{
|
|
|
|
#ifndef WZ_NOSOUND
|
|
|
|
// If an OpenAL source is associated with this sample, release it
|
|
|
|
if ((*sample)->curr->iSample != (ALuint)AL_INVALID)
|
|
|
|
{
|
|
|
|
alDeleteSources(1, &(*sample)->curr->iSample);
|
|
|
|
sound_GetError();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Remove the sample from the list
|
|
|
|
sound_RemoveSample(*previous, *sample);
|
|
|
|
// Free it
|
|
|
|
free(*sample);
|
2007-12-31 07:16:59 -08:00
|
|
|
|
2007-12-15 12:49:02 -08:00
|
|
|
// Get a pointer to the next node, the previous pointer doesn't change
|
|
|
|
*sample = (*previous != NULL) ? (*previous)->next : active_samples;
|
|
|
|
}
|
|
|
|
|
2007-12-19 12:17:54 -08:00
|
|
|
/** Counts the number of samples in active_samples
|
|
|
|
* \return the number of actively playing sound samples
|
|
|
|
*/
|
|
|
|
unsigned int sound_GetActiveSamplesCount()
|
|
|
|
{
|
|
|
|
unsigned int num = 0;
|
|
|
|
SAMPLE_LIST* node = active_samples;
|
|
|
|
|
|
|
|
while(node)
|
|
|
|
{
|
|
|
|
num++;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
2007-12-31 07:16:59 -08:00
|
|
|
|
2007-12-14 07:11:59 -08:00
|
|
|
void sound_Update()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-12-13 15:16:03 -08:00
|
|
|
SAMPLE_LIST* node = active_samples;
|
|
|
|
SAMPLE_LIST* previous = NULL;
|
2007-12-14 07:11:59 -08:00
|
|
|
ALCenum err;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-06-18 09:29:10 -07:00
|
|
|
if ( !openal_initialized )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
// Update all streaming audio
|
|
|
|
sound_UpdateStreams();
|
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
while (node != NULL)
|
|
|
|
{
|
2007-12-14 07:11:59 -08:00
|
|
|
ALenum state, err;
|
2007-12-13 15:16:03 -08:00
|
|
|
|
2008-07-13 11:46:53 -07:00
|
|
|
//ASSERT(alIsSource(node->curr->iSample), "Not a valid source!");
|
2007-12-13 15:16:03 -08:00
|
|
|
alGetSourcei(node->curr->iSample, AL_SOURCE_STATE, &state);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
// Check whether an error occurred while retrieving the state.
|
|
|
|
// If one did, the state returned is useless. So instead of
|
|
|
|
// using it continue with the next sample.
|
|
|
|
err = sound_GetError();
|
2007-12-14 04:13:23 -08:00
|
|
|
if (err != AL_NO_ERROR)
|
2007-12-13 15:54:28 -08:00
|
|
|
{
|
2007-12-18 15:25:32 -08:00
|
|
|
// Make sure to invoke the "finished" callback
|
|
|
|
sound_FinishedCallback(node->curr);
|
|
|
|
|
2007-12-15 12:49:02 -08:00
|
|
|
// Destroy this object and move to the next object
|
|
|
|
sound_DestroyIteratedSample(&previous, &node);
|
2007-12-13 15:16:03 -08:00
|
|
|
continue;
|
2007-12-13 15:54:28 -08:00
|
|
|
}
|
2007-12-13 15:16:03 -08:00
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
2007-06-28 10:47:08 -07:00
|
|
|
case AL_PLAYING:
|
2006-02-11 01:00:20 -08:00
|
|
|
case AL_PAUSED:
|
2007-12-13 15:16:03 -08:00
|
|
|
// If we haven't finished playing yet, just
|
|
|
|
// continue with the next item in the list.
|
|
|
|
|
|
|
|
// sound_SetObjectPosition(i->curr->iSample, i->curr->x, i->curr->y, i->curr->z);
|
|
|
|
|
|
|
|
// Move to the next object
|
|
|
|
previous = node;
|
|
|
|
node = node->next;
|
2007-06-28 10:47:08 -07:00
|
|
|
break;
|
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
case AL_STOPPED:
|
|
|
|
sound_FinishedCallback(node->curr);
|
|
|
|
|
|
|
|
default:
|
2007-12-15 12:49:02 -08:00
|
|
|
sound_DestroyIteratedSample(&previous, &node);
|
2007-06-28 10:47:08 -07:00
|
|
|
break;
|
|
|
|
}
|
2007-12-13 15:16:03 -08:00
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-06-04 03:01:52 -07:00
|
|
|
// Reset the current error state
|
|
|
|
alcGetError(device);
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
alcProcessContext(context);
|
2007-06-04 03:01:52 -07:00
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
err = sound_GetDeviceError(device);
|
2007-06-04 03:01:52 -07:00
|
|
|
if (err != ALC_NO_ERROR)
|
|
|
|
{
|
2007-06-28 10:47:08 -07:00
|
|
|
debug(LOG_ERROR, "Error while processing audio context: %s",
|
2007-06-04 03:01:52 -07:00
|
|
|
alGetString(err));
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-12-14 07:11:59 -08:00
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
BOOL sound_QueueSamplePlaying( void )
|
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-12-14 04:13:23 -08:00
|
|
|
ALenum state;
|
|
|
|
|
2008-06-18 09:29:10 -07:00
|
|
|
if ( !openal_initialized )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-08 11:24:42 -07:00
|
|
|
if ( current_queue_sample == (ALuint)AL_INVALID )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-12-31 07:16:59 -08:00
|
|
|
|
2008-07-08 11:24:42 -07:00
|
|
|
alGetSourcei(current_queue_sample, AL_SOURCE_STATE, &state);
|
2007-12-13 15:16:03 -08:00
|
|
|
|
|
|
|
// Check whether an error occurred while retrieving the state.
|
|
|
|
// If one did, the state returned is useless. So instead of
|
|
|
|
// using it return false.
|
|
|
|
if (sound_GetError() != AL_NO_ERROR)
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-12-13 15:16:03 -08:00
|
|
|
|
|
|
|
if (state == AL_PLAYING)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-12-13 15:16:03 -08:00
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-07-08 11:24:42 -07:00
|
|
|
if (current_queue_sample != (ALuint)AL_INVALID)
|
2008-07-08 10:45:33 -07:00
|
|
|
{
|
2008-07-08 11:24:42 -07:00
|
|
|
SAMPLE_LIST* node = active_samples;
|
|
|
|
SAMPLE_LIST* previous = NULL;
|
|
|
|
|
|
|
|
// We need to remove it from the queue of actively played samples
|
|
|
|
while (node != NULL)
|
2008-07-08 10:45:33 -07:00
|
|
|
{
|
2008-07-08 11:24:42 -07:00
|
|
|
if (node->curr->iSample == current_queue_sample)
|
|
|
|
{
|
|
|
|
sound_DestroyIteratedSample(&previous, &node);
|
|
|
|
current_queue_sample = AL_INVALID;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
previous = node;
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
node = node->next;
|
|
|
|
}
|
2008-07-08 09:12:14 -07:00
|
|
|
}
|
2008-07-08 11:24:42 -07:00
|
|
|
debug(LOG_ERROR, "Sample %u not deleted because it wasn't in the active queue!", current_queue_sample);
|
|
|
|
current_queue_sample = AL_INVALID;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-04-30 16:52:51 -07:00
|
|
|
/** Decodes an opened OggVorbis file into an OpenAL buffer
|
|
|
|
* \param psTrack pointer to object which will contain the final buffer
|
|
|
|
* \param PHYSFS_fileHandle file handle given by PhysicsFS to the opened file
|
|
|
|
* \return on success the psTrack pointer, otherwise it will be free'd and a NULL pointer is returned instead
|
|
|
|
*/
|
|
|
|
static inline TRACK* sound_DecodeOggVorbisTrack(TRACK *psTrack, PHYSFS_file* PHYSFS_fileHandle)
|
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-04-30 16:52:51 -07:00
|
|
|
ALenum format;
|
|
|
|
ALuint buffer;
|
2008-06-18 09:29:10 -07:00
|
|
|
struct OggVorbisDecoderState *decoder;
|
|
|
|
soundDataBuffer *soundBuffer;
|
2007-04-30 16:52:51 -07:00
|
|
|
|
2008-06-18 09:29:10 -07:00
|
|
|
if ( !openal_initialized )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
decoder = sound_CreateOggVorbisDecoder(PHYSFS_fileHandle, true);
|
2007-04-30 17:30:37 -07:00
|
|
|
soundBuffer = sound_DecodeOggVorbis(decoder, 0);
|
2007-04-30 16:52:51 -07:00
|
|
|
sound_DestroyOggVorbisDecoder(decoder);
|
|
|
|
|
|
|
|
if (soundBuffer == NULL)
|
|
|
|
{
|
|
|
|
free(psTrack);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-23 09:14:28 -07:00
|
|
|
if (soundBuffer->size == 0)
|
|
|
|
{
|
|
|
|
debug(LOG_WARNING, "sound_DecodeOggVorbisTrack: OggVorbis track is entirely empty after decoding");
|
|
|
|
// NOTE: I'm not entirely sure if a track that's empty after decoding should be
|
|
|
|
// considered an error condition. Therefore I'll only error out on DEBUG
|
|
|
|
// builds. (Returning NULL here __will__ result in a program termination.)
|
|
|
|
#ifdef DEBUG
|
|
|
|
free(soundBuffer);
|
|
|
|
free(psTrack);
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-04-30 16:52:51 -07:00
|
|
|
// Determine PCM data format
|
|
|
|
format = (soundBuffer->channelCount == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
|
|
|
|
|
|
|
|
// Create an OpenAL buffer and fill it with the decoded data
|
|
|
|
alGenBuffers(1, &buffer);
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError();
|
2007-04-30 16:52:51 -07:00
|
|
|
alBufferData(buffer, format, soundBuffer->data, soundBuffer->size, soundBuffer->frequency);
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError();
|
2007-04-30 16:52:51 -07:00
|
|
|
|
2007-04-30 17:30:37 -07:00
|
|
|
free(soundBuffer);
|
|
|
|
|
2007-04-30 16:52:51 -07:00
|
|
|
// save buffer name in track
|
|
|
|
psTrack->iBufferName = buffer;
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-04-30 16:52:51 -07:00
|
|
|
|
|
|
|
return psTrack;
|
|
|
|
}
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2007-04-09 05:16:31 -07:00
|
|
|
TRACK* sound_LoadTrackFromFile(const char *fileName)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-04-09 05:16:31 -07:00
|
|
|
TRACK* pTrack;
|
2007-04-30 09:55:53 -07:00
|
|
|
PHYSFS_file* fileHandle;
|
2007-11-26 07:16:51 -08:00
|
|
|
size_t filename_size;
|
2008-03-28 16:09:32 -07:00
|
|
|
char* track_name;
|
2007-04-03 04:15:41 -07:00
|
|
|
|
2007-02-18 13:30:51 -08:00
|
|
|
// Use PhysicsFS to open the file
|
2007-04-30 09:55:53 -07:00
|
|
|
fileHandle = PHYSFS_openRead(fileName);
|
2008-09-07 13:00:54 -07:00
|
|
|
debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName);
|
2007-04-30 09:55:53 -07:00
|
|
|
if (fileHandle == NULL)
|
2007-04-08 11:51:15 -07:00
|
|
|
{
|
2007-04-30 09:55:53 -07:00
|
|
|
debug(LOG_ERROR, "sound_LoadTrackFromFile: PHYSFS_openRead(\"%s\") failed with error: %s\n", fileName, PHYSFS_getLastError());
|
2007-04-08 11:51:15 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-11-26 07:16:51 -08:00
|
|
|
if (GetLastResourceFilename() == NULL)
|
|
|
|
{
|
|
|
|
// This is a non fatal error. We just can't find filename for some reason.
|
|
|
|
debug(LOG_WARNING, "sound_LoadTrackFromFile: missing resource filename?");
|
|
|
|
filename_size = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filename_size = strlen(GetLastResourceFilename()) + 1;
|
|
|
|
}
|
|
|
|
|
2007-04-09 05:16:31 -07:00
|
|
|
// allocate track, plus the memory required to contain the filename
|
|
|
|
// one malloc call ensures only one free call is required
|
2007-11-26 07:16:51 -08:00
|
|
|
pTrack = (TRACK*)malloc(sizeof(TRACK) + filename_size);
|
2007-04-09 05:16:31 -07:00
|
|
|
if (pTrack == NULL)
|
|
|
|
{
|
|
|
|
debug( LOG_ERROR, "sound_ConstructTrack: couldn't allocate memory\n" );
|
|
|
|
abort();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize everyting (except for the filename) to zero
|
|
|
|
memset(pTrack, 0, sizeof(TRACK));
|
2007-04-18 05:40:50 -07:00
|
|
|
|
2007-11-26 07:16:51 -08:00
|
|
|
// Set filename pointer; if the filename (as returned by
|
|
|
|
// GetLastResourceFilename()) is a NULL pointer, then this will be a
|
|
|
|
// NULL pointer as well.
|
2008-03-28 16:09:32 -07:00
|
|
|
track_name = filename_size ? (char*)(pTrack + 1) : NULL;
|
2007-10-24 14:11:29 -07:00
|
|
|
|
2007-11-26 07:16:51 -08:00
|
|
|
// Copy the filename into the struct, if we don't have a NULL pointer
|
|
|
|
if (filename_size != 0)
|
2007-11-22 10:35:03 -08:00
|
|
|
{
|
2008-03-28 16:09:32 -07:00
|
|
|
strcpy(track_name, GetLastResourceFilename());
|
2007-11-22 10:35:03 -08:00
|
|
|
}
|
2008-03-28 16:09:32 -07:00
|
|
|
pTrack->fileName = track_name;
|
2007-11-22 10:35:03 -08:00
|
|
|
|
2007-02-18 13:30:51 -08:00
|
|
|
// Now use sound_ReadTrackFromBuffer to decode the file's contents
|
2007-04-30 16:52:51 -07:00
|
|
|
pTrack = sound_DecodeOggVorbisTrack(pTrack, fileHandle);
|
2007-02-18 14:16:55 -08:00
|
|
|
|
2007-04-30 09:55:53 -07:00
|
|
|
PHYSFS_close(fileHandle);
|
2007-04-09 05:16:31 -07:00
|
|
|
return pTrack;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void sound_FreeTrack( TRACK *psTrack )
|
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-12-13 15:16:03 -08:00
|
|
|
alDeleteBuffers(1, &psTrack->iBufferName);
|
|
|
|
sound_GetError();
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2006-09-19 11:45:48 -07:00
|
|
|
static void sound_AddActiveSample( AUDIO_SAMPLE *psSample )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
SAMPLE_LIST *tmp = (SAMPLE_LIST *) malloc( sizeof(SAMPLE_LIST) );
|
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
// Prepend the given sample to our list of active samples
|
2007-06-28 10:47:08 -07:00
|
|
|
tmp->curr = psSample;
|
|
|
|
tmp->next = active_samples;
|
|
|
|
active_samples = tmp;
|
|
|
|
}
|
2007-12-14 03:43:15 -08:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-12-13 15:47:46 -08:00
|
|
|
/** Routine gets rid of the psObj's sound sample and reference in active_samples.
|
|
|
|
*/
|
|
|
|
void sound_RemoveActiveSample( AUDIO_SAMPLE *psSample )
|
|
|
|
{
|
|
|
|
SAMPLE_LIST* node = active_samples;
|
|
|
|
SAMPLE_LIST* previous = NULL;
|
|
|
|
|
|
|
|
while (node != NULL)
|
|
|
|
{
|
|
|
|
if (node->curr->psObj == psSample->psObj)
|
|
|
|
{
|
|
|
|
debug(LOG_MEMORY, "Removing object 0x%p from active_samples list 0x%p\n", psSample->psObj, node);
|
|
|
|
|
|
|
|
// Buginator: should we wait for it to finish, or just stop it?
|
|
|
|
sound_StopSample(node->curr);
|
|
|
|
|
|
|
|
sound_FinishedCallback(node->curr); //tell the callback it is finished.
|
|
|
|
|
2007-12-15 12:49:02 -08:00
|
|
|
sound_DestroyIteratedSample(&previous, &node);
|
2007-12-13 15:47:46 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Move to the next sample object
|
|
|
|
previous = node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-14 03:43:15 -08:00
|
|
|
|
|
|
|
#ifndef WZ_NOSOUND
|
2007-12-13 15:16:03 -08:00
|
|
|
static bool sound_SetupChannel( AUDIO_SAMPLE *psSample )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-04-12 10:42:15 -07:00
|
|
|
sound_AddActiveSample( psSample );
|
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
return sound_TrackLooped(psSample->iTrack);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
BOOL sound_Play2DSample( TRACK *psTrack, AUDIO_SAMPLE *psSample, BOOL bQueued )
|
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-28 10:47:08 -07:00
|
|
|
ALfloat zero[3] = { 0.0, 0.0, 0.0 };
|
2007-11-02 11:47:22 -07:00
|
|
|
ALfloat volume;
|
2008-08-24 05:41:10 -07:00
|
|
|
ALint error;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-11-02 11:47:22 -07:00
|
|
|
if (sfx_volume == 0.0)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-11-02 15:37:20 -07:00
|
|
|
volume = ((float)psTrack->iVol / 100.0f); // each object can have OWN volume!
|
|
|
|
psSample->fVol = volume; // save computed volume
|
|
|
|
volume *= sfx_volume; // and now take into account the Users sound Prefs.
|
2008-08-24 05:41:10 -07:00
|
|
|
|
|
|
|
// Clear error codes
|
|
|
|
alGetError();
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
alGenSources( 1, &(psSample->iSample) );
|
2008-08-24 05:41:10 -07:00
|
|
|
|
2008-08-23 12:44:00 -07:00
|
|
|
error = sound_GetError();
|
|
|
|
if (error != AL_NO_ERROR)
|
2008-08-24 05:41:10 -07:00
|
|
|
{
|
|
|
|
/* FIXME: We run out of OpenAL sources very quickly, so we
|
|
|
|
* should handle the case where we've ran out of them.
|
|
|
|
* Currently we don't do this, causing some unpleasant side
|
|
|
|
* effects, e.g. crashing...
|
|
|
|
*/
|
2008-08-23 12:44:00 -07:00
|
|
|
}
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
alSourcef( psSample->iSample, AL_PITCH, 1.0f );
|
2007-11-02 11:47:22 -07:00
|
|
|
alSourcef( psSample->iSample, AL_GAIN,volume );
|
2007-06-28 10:47:08 -07:00
|
|
|
alSourcefv( psSample->iSample, AL_POSITION, zero );
|
|
|
|
alSourcefv( psSample->iSample, AL_VELOCITY, zero );
|
2006-09-24 11:53:08 -07:00
|
|
|
alSourcei( psSample->iSample, AL_BUFFER, psTrack->iBufferName );
|
2007-06-28 10:47:08 -07:00
|
|
|
alSourcei( psSample->iSample, AL_SOURCE_RELATIVE, AL_TRUE );
|
2007-04-12 10:42:15 -07:00
|
|
|
alSourcei( psSample->iSample, AL_LOOPING, (sound_SetupChannel(psSample)) ? AL_TRUE : AL_FALSE );
|
2008-08-24 05:41:10 -07:00
|
|
|
|
|
|
|
// Clear error codes
|
|
|
|
alGetError();
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
alSourcePlay( psSample->iSample );
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError();
|
2008-08-24 05:41:10 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
if ( bQueued )
|
|
|
|
{
|
2008-07-08 11:24:42 -07:00
|
|
|
current_queue_sample = psSample->iSample;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2008-07-08 11:24:42 -07:00
|
|
|
else if ( current_queue_sample == psSample->iSample )
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-07-08 11:24:42 -07:00
|
|
|
current_queue_sample = -1;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-06-06 10:09:23 -07: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
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
BOOL sound_Play3DSample( TRACK *psTrack, AUDIO_SAMPLE *psSample )
|
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-28 10:47:08 -07:00
|
|
|
ALfloat zero[3] = { 0.0, 0.0, 0.0 };
|
2007-11-02 11:47:22 -07:00
|
|
|
ALfloat volume;
|
2008-08-24 05:41:10 -07:00
|
|
|
ALint error;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-11-02 11:47:22 -07:00
|
|
|
if (sfx3d_volume == 0.0)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-11-02 15:37:20 -07:00
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
volume = ((float)psTrack->iVol / 100.f); // max range is 0-100
|
2007-11-02 15:37:20 -07:00
|
|
|
psSample->fVol = volume; // store results for later
|
2008-08-24 05:41:10 -07:00
|
|
|
|
|
|
|
// Clear error codes
|
|
|
|
alGetError();
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
alGenSources( 1, &(psSample->iSample) );
|
2008-08-24 05:41:10 -07:00
|
|
|
|
2008-08-23 12:44:00 -07:00
|
|
|
error = sound_GetError();
|
|
|
|
if (error != AL_NO_ERROR)
|
2008-08-24 05:41:10 -07:00
|
|
|
{
|
|
|
|
/* FIXME: We run out of OpenAL sources very quickly, so we
|
|
|
|
* should handle the case where we've ran out of them.
|
|
|
|
* Currently we don't do this, causing some unpleasant side
|
|
|
|
* effects, e.g. crashing...
|
|
|
|
*/
|
2008-08-23 12:44:00 -07:00
|
|
|
}
|
|
|
|
|
2007-07-14 11:42:39 -07:00
|
|
|
// HACK: this is a workaround for a bug in the 64bit implementation of OpenAL on GNU/Linux
|
|
|
|
// The AL_PITCH value really should be 1.0.
|
|
|
|
alSourcef( psSample->iSample, AL_PITCH, 1.001 );
|
2007-12-31 07:16:59 -08:00
|
|
|
|
2007-11-02 11:47:22 -07:00
|
|
|
sound_SetObjectPosition( psSample );
|
2007-06-28 10:47:08 -07:00
|
|
|
alSourcefv( psSample->iSample, AL_VELOCITY, zero );
|
2006-09-24 11:53:08 -07:00
|
|
|
alSourcei( psSample->iSample, AL_BUFFER, psTrack->iBufferName );
|
2007-04-12 10:42:15 -07:00
|
|
|
alSourcei( psSample->iSample, AL_LOOPING, (sound_SetupChannel(psSample)) ? AL_TRUE : AL_FALSE );
|
2008-08-24 05:41:10 -07:00
|
|
|
|
|
|
|
// Clear error codes
|
|
|
|
alGetError();
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
alSourcePlay( psSample->iSample );
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError();
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
/** Plays the audio data from the given file
|
|
|
|
* \param fileHandle PhysicsFS file handle to stream the audio from
|
|
|
|
* \param volume the volume to play the audio at (in a range of 0.0 to 1.0)
|
|
|
|
* \param onFinished callback to invoke when we're finished playing
|
|
|
|
* \param user_data user-data pointer to pass to the \c onFinished callback
|
2008-01-27 13:28:35 -08:00
|
|
|
* \return a pointer to the currently playing stream when playing started
|
|
|
|
* succesfully, NULL otherwise.
|
|
|
|
* \post When a non-NULL pointer is returned the audio stream system will
|
|
|
|
* close the PhysicsFS file handle. Otherwise (when false is returned)
|
|
|
|
* this is left to the user.
|
|
|
|
* \note The returned pointer will become invalid/dangling immediately after
|
|
|
|
* the \c onFinished callback is invoked.
|
|
|
|
* \note You must _never_ manually free() the memory used by the returned
|
|
|
|
* pointer.
|
2007-12-31 07:16:59 -08:00
|
|
|
*/
|
2008-01-27 13:28:35 -08:00
|
|
|
AUDIO_STREAM* sound_PlayStream(PHYSFS_file* fileHandle, float volume, void (*onFinished)(void*), void* user_data)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-01-27 15:32:55 -08:00
|
|
|
// Default buffer size
|
|
|
|
static const size_t streamBufferSize = 16 * 1024;
|
|
|
|
// Default buffer count
|
|
|
|
static const unsigned int buffer_count = 2;
|
|
|
|
|
|
|
|
return sound_PlayStreamWithBuf(fileHandle, volume, onFinished, user_data, streamBufferSize, buffer_count);
|
|
|
|
}
|
2007-12-31 07:16:59 -08:00
|
|
|
|
2008-01-27 15:32:55 -08:00
|
|
|
/** Plays the audio data from the given file
|
2008-01-29 05:05:38 -08:00
|
|
|
* \param fileHandle,volume,onFinished,user_data see sound_PlayStream()
|
2008-01-27 15:32:55 -08:00
|
|
|
* \param streamBufferSize the size to use for the decoded audio buffers
|
|
|
|
* \param buffer_count the amount of audio buffers to use
|
|
|
|
* \see sound_PlayStream() for details about the rest of the function
|
|
|
|
* parameters and other details.
|
|
|
|
*/
|
|
|
|
AUDIO_STREAM* sound_PlayStreamWithBuf(PHYSFS_file* fileHandle, float volume, void (*onFinished)(void*), void* user_data, size_t streamBufferSize, unsigned int buffer_count)
|
|
|
|
{
|
2007-12-31 07:16:59 -08:00
|
|
|
AUDIO_STREAM* stream;
|
2008-01-27 15:32:55 -08:00
|
|
|
ALuint* buffers = alloca(sizeof(ALuint) * buffer_count);
|
2008-08-24 05:41:10 -07:00
|
|
|
ALint error;
|
2007-12-31 07:16:59 -08:00
|
|
|
unsigned int i;
|
|
|
|
|
2008-06-18 09:29:10 -07:00
|
|
|
if ( !openal_initialized )
|
|
|
|
{
|
2008-07-10 08:47:27 -07:00
|
|
|
debug(LOG_WARNING, "OpenAL isn't initialized, not creating an audio stream");
|
2008-06-18 09:29:10 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
stream = malloc(sizeof(AUDIO_STREAM));
|
|
|
|
if (stream == NULL)
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "sound_PlayStream: Out of memory");
|
|
|
|
abort();
|
2008-01-27 13:28:35 -08:00
|
|
|
return NULL;
|
2007-12-31 07:16:59 -08:00
|
|
|
}
|
|
|
|
|
2008-08-24 05:41:10 -07:00
|
|
|
// Clear error codes
|
|
|
|
alGetError();
|
|
|
|
|
|
|
|
// Retrieve an OpenAL sound source
|
2008-08-23 12:44:00 -07:00
|
|
|
alGenSources(1, &(stream->source));
|
2008-08-24 05:41:10 -07:00
|
|
|
|
2008-08-23 12:44:00 -07:00
|
|
|
error = sound_GetError();
|
|
|
|
if (error != AL_NO_ERROR)
|
|
|
|
{
|
2008-08-24 05:41:10 -07:00
|
|
|
// Failed to create OpenAL sound source, so bail out...
|
|
|
|
debug(LOG_SOUND, "alGenSources failed, most likely out of sound sources");
|
|
|
|
free(stream);
|
2008-08-23 12:44:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
stream->fileHandle = fileHandle;
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
stream->decoder = sound_CreateOggVorbisDecoder(stream->fileHandle, false);
|
2007-12-31 07:16:59 -08:00
|
|
|
if (stream->decoder == NULL)
|
|
|
|
{
|
|
|
|
debug(LOG_ERROR, "sound_PlayStream: Failed to open audio file for decoding");
|
|
|
|
free(stream);
|
2008-01-27 13:28:35 -08:00
|
|
|
return NULL;
|
2007-12-31 07:16:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->volume = volume;
|
2008-01-27 15:32:55 -08:00
|
|
|
stream->bufferSize = streamBufferSize;
|
2007-12-31 07:16:59 -08:00
|
|
|
|
2008-05-13 06:50:08 -07:00
|
|
|
alSourcef(stream->source, AL_GAIN, stream->volume);
|
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
// HACK: this is a workaround for a bug in the 64bit implementation of OpenAL on GNU/Linux
|
|
|
|
// The AL_PITCH value really should be 1.0.
|
|
|
|
alSourcef(stream->source, AL_PITCH, 1.001);
|
|
|
|
|
|
|
|
// Create some OpenAL buffers to store the decoded data in
|
|
|
|
alGenBuffers(buffer_count, buffers);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
// Fill some buffers with audio data
|
|
|
|
for (i = 0; i < buffer_count; ++i)
|
|
|
|
{
|
|
|
|
// Decode some audio data
|
2008-01-27 15:32:55 -08:00
|
|
|
soundDataBuffer* soundBuffer = sound_DecodeOggVorbis(stream->decoder, stream->bufferSize);
|
2007-12-31 07:16:59 -08:00
|
|
|
|
|
|
|
// If we actually decoded some data
|
|
|
|
if (soundBuffer && soundBuffer->size > 0)
|
|
|
|
{
|
|
|
|
// Determine PCM data format
|
|
|
|
ALenum format = (soundBuffer->channelCount == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
|
|
|
|
|
|
|
|
// Copy the audio data into one of OpenAL's own buffers
|
|
|
|
alBufferData(buffers[i], format, soundBuffer->data, soundBuffer->size, soundBuffer->frequency);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
// Clean up our memory
|
|
|
|
free(soundBuffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If no data has been decoded we're probably at the end of our
|
|
|
|
// stream. So cleanup the excess stuff here.
|
|
|
|
|
|
|
|
// First remove the data buffer itself
|
|
|
|
free(soundBuffer);
|
|
|
|
|
|
|
|
// Then remove OpenAL's buffers
|
|
|
|
alDeleteBuffers(buffer_count - i, &buffers[i]);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail out if we didn't fill any buffers
|
|
|
|
if (i == 0)
|
|
|
|
{
|
2008-07-10 08:47:27 -07:00
|
|
|
debug(LOG_ERROR, "Failed to fill buffers with decoded audio data!");
|
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
// Destroy the decoder
|
|
|
|
sound_DestroyOggVorbisDecoder(stream->decoder);
|
|
|
|
|
|
|
|
// Destroy the OpenAL source
|
|
|
|
alDeleteSources(1, &stream->source);
|
|
|
|
|
|
|
|
// Free allocated memory
|
|
|
|
free(stream);
|
|
|
|
|
2008-01-27 13:28:35 -08:00
|
|
|
return NULL;
|
2007-12-31 07:16:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attach the OpenAL buffers to our OpenAL source
|
|
|
|
// (i = the amount of buffers we worked on in the above for-loop)
|
|
|
|
alSourceQueueBuffers(stream->source, i, buffers);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
// Start playing the source
|
|
|
|
alSourcePlay(stream->source);
|
|
|
|
|
2008-01-27 13:28:35 -08:00
|
|
|
sound_GetError();
|
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
// Set callback info
|
|
|
|
stream->onFinished = onFinished;
|
|
|
|
stream->user_data = user_data;
|
|
|
|
|
|
|
|
// Prepend this stream to the linked list
|
|
|
|
stream->next = active_streams;
|
|
|
|
active_streams = stream;
|
|
|
|
|
2008-01-27 13:28:35 -08:00
|
|
|
return stream;
|
2007-12-31 07:16:59 -08:00
|
|
|
}
|
|
|
|
|
2008-01-27 13:33:10 -08:00
|
|
|
/** Stops the current stream from playing.
|
|
|
|
* \param stream the stream to stop
|
|
|
|
* \post The stopped stream will be destroyed on the next invocation of
|
|
|
|
* sound_UpdateStreams(). So calling this function will result in the
|
|
|
|
* \c onFinished callback being called and the \c stream pointer becoming
|
|
|
|
* invalid.
|
|
|
|
*/
|
|
|
|
void sound_StopStream(AUDIO_STREAM* stream)
|
|
|
|
{
|
|
|
|
assert(stream != NULL);
|
|
|
|
|
|
|
|
// Tell OpenAL to stop playing on the given source
|
|
|
|
alSourceStop(stream->source);
|
|
|
|
sound_GetError();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Pauses playing of this stream until playing is resumed with
|
|
|
|
* sound_ResumeStream() or completely stopped with sound_StopStream().
|
|
|
|
* \param stream the stream to pause playing for
|
|
|
|
*/
|
|
|
|
void sound_PauseStream(AUDIO_STREAM* stream)
|
|
|
|
{
|
|
|
|
ALint state;
|
|
|
|
|
|
|
|
// To be sure we won't go mutilating this OpenAL source, check wether
|
|
|
|
// it's playing first.
|
|
|
|
alGetSourcei(stream->source, AL_SOURCE_STATE, &state);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
if (state != AL_PLAYING)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pause playing of this OpenAL source
|
|
|
|
alSourcePause(stream->source);
|
|
|
|
sound_GetError();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Resumes playing of a stream that's paused by means of sound_PauseStream().
|
|
|
|
* \param stream the stream to resume playing for
|
|
|
|
*/
|
|
|
|
void sound_ResumeStream(AUDIO_STREAM* stream)
|
|
|
|
{
|
|
|
|
ALint state;
|
|
|
|
|
|
|
|
// To be sure we won't go mutilating this OpenAL source, check wether
|
|
|
|
// it's paused first.
|
|
|
|
alGetSourcei(stream->source, AL_SOURCE_STATE, &state);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
if (state != AL_PAUSED)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resume playing of this OpenAL source
|
|
|
|
alSourcePlay(stream->source);
|
|
|
|
sound_GetError();
|
|
|
|
}
|
|
|
|
|
2008-05-13 07:01:25 -07:00
|
|
|
/** Retrieve the playing volume of the given stream.
|
|
|
|
*
|
|
|
|
* @param stream the stream to retrieve the volume for.
|
|
|
|
*
|
|
|
|
* @return a floating point value between 0.f and 1.f, representing this
|
|
|
|
* stream's volume.
|
|
|
|
*/
|
|
|
|
float sound_GetStreamVolume(const AUDIO_STREAM* stream)
|
|
|
|
{
|
|
|
|
ALfloat volume;
|
|
|
|
alGetSourcef(stream->source, AL_GAIN, &volume);
|
|
|
|
|
|
|
|
return volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the playing volume of the given stream.
|
|
|
|
*
|
|
|
|
* @param stream the stream to change the volume for.
|
|
|
|
* @param volume a floating point value between 0.f and 1.f, to use as this
|
|
|
|
* @c stream's volume.
|
|
|
|
*/
|
|
|
|
void sound_SetStreamVolume(AUDIO_STREAM* stream, float volume)
|
|
|
|
{
|
|
|
|
stream->volume = volume;
|
|
|
|
alSourcef(stream->source, AL_GAIN, stream->volume);
|
|
|
|
}
|
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
/** Update the given stream by making sure its buffers remain full
|
|
|
|
* \param stream the stream to update
|
|
|
|
* \return true when the stream is still playing, false when it has stopped
|
|
|
|
*/
|
|
|
|
static bool sound_UpdateStream(AUDIO_STREAM* stream)
|
|
|
|
{
|
|
|
|
ALint state, buffer_count;
|
|
|
|
|
|
|
|
alGetSourcei(stream->source, AL_SOURCE_STATE, &state);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
if (state != AL_PLAYING && state != AL_PAUSED)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the amount of buffers which were processed and need refilling
|
|
|
|
alGetSourcei(stream->source, AL_BUFFERS_PROCESSED, &buffer_count);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
// Refill and reattach all buffers
|
|
|
|
for (; buffer_count != 0; --buffer_count)
|
|
|
|
{
|
|
|
|
soundDataBuffer* soundBuffer;
|
|
|
|
ALuint buffer;
|
|
|
|
|
|
|
|
// Retrieve the buffer to work on
|
|
|
|
alSourceUnqueueBuffers(stream->source, 1, &buffer);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
// Decode some data to stuff in our buffer
|
2008-01-27 15:32:55 -08:00
|
|
|
soundBuffer = sound_DecodeOggVorbis(stream->decoder, stream->bufferSize);
|
2007-12-31 07:16:59 -08:00
|
|
|
|
|
|
|
// If we actually decoded some data
|
|
|
|
if (soundBuffer && soundBuffer->size > 0)
|
|
|
|
{
|
|
|
|
// Determine PCM data format
|
|
|
|
ALenum format = (soundBuffer->channelCount == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
|
|
|
|
|
|
|
|
// Insert the data into the buffer
|
|
|
|
alBufferData(buffer, format, soundBuffer->data, soundBuffer->size, soundBuffer->frequency);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
// Reattach the buffer to the source
|
|
|
|
alSourceQueueBuffers(stream->source, 1, &buffer);
|
|
|
|
sound_GetError();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If no data has been decoded we're probably at the end of our
|
|
|
|
// stream. So cleanup this buffer.
|
|
|
|
|
|
|
|
// Then remove OpenAL's buffer
|
|
|
|
alDeleteBuffers(1, &buffer);
|
|
|
|
sound_GetError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now remove the data buffer itself
|
|
|
|
free(soundBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Destroy the given stream and release its associated resources. This function
|
|
|
|
* also handles calling of the \c onFinished callback function and closing of
|
|
|
|
* the PhysicsFS file handle.
|
|
|
|
* \param stream the stream to destroy
|
|
|
|
*/
|
|
|
|
static void sound_DestroyStream(AUDIO_STREAM* stream)
|
|
|
|
{
|
2008-08-24 05:41:10 -07:00
|
|
|
ALint buffer_count;
|
|
|
|
ALuint* buffers;
|
|
|
|
ALint error;
|
2007-12-31 07:16:59 -08:00
|
|
|
|
|
|
|
// Stop the OpenAL source from playing
|
|
|
|
alSourceStop(stream->source);
|
2008-08-23 12:44:00 -07:00
|
|
|
error = sound_GetError();
|
|
|
|
|
2008-08-24 05:41:10 -07:00
|
|
|
if (error != AL_NO_ERROR)
|
2008-08-23 12:44:00 -07:00
|
|
|
{
|
2008-08-24 05:41:10 -07:00
|
|
|
// FIXME: We should really handle these errors.
|
2008-08-23 12:44:00 -07:00
|
|
|
}
|
2007-12-31 07:16:59 -08:00
|
|
|
|
|
|
|
// Retrieve the amount of buffers which were processed
|
|
|
|
alGetSourcei(stream->source, AL_BUFFERS_PROCESSED, &buffer_count);
|
2008-08-23 12:44:00 -07:00
|
|
|
error = sound_GetError();
|
2008-08-24 05:41:10 -07:00
|
|
|
if (error != AL_NO_ERROR)
|
2008-08-23 12:44:00 -07:00
|
|
|
{
|
2008-08-24 05:41:10 -07:00
|
|
|
/* FIXME: We're leaking memory and resources here when bailing
|
|
|
|
* out. But not doing so could cause stack overflows as a
|
|
|
|
* result of the below alloca() call (due to buffer_count not
|
|
|
|
* being properly initialised.
|
|
|
|
*/
|
|
|
|
debug(LOG_SOUND, "alGetSourcei(AL_BUFFERS_PROCESSED) failed; bailing out...");
|
|
|
|
return;
|
2008-08-23 12:44:00 -07:00
|
|
|
}
|
2007-12-31 07:16:59 -08:00
|
|
|
|
|
|
|
// Detach all buffers and retrieve their ID numbers
|
|
|
|
buffers = alloca(buffer_count * sizeof(ALuint));
|
|
|
|
alSourceUnqueueBuffers(stream->source, buffer_count, buffers);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
// Destroy all of these buffers
|
|
|
|
alDeleteBuffers(buffer_count, buffers);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
// Destroy the OpenAL source
|
|
|
|
alDeleteSources(1, &stream->source);
|
|
|
|
sound_GetError();
|
|
|
|
|
|
|
|
// Destroy the sound decoder
|
|
|
|
sound_DestroyOggVorbisDecoder(stream->decoder);
|
|
|
|
|
|
|
|
// Now close the file
|
|
|
|
PHYSFS_close(stream->fileHandle);
|
|
|
|
|
|
|
|
// Now call the finished callback
|
|
|
|
if (stream->onFinished)
|
|
|
|
{
|
|
|
|
stream->onFinished(stream->user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free the memory used by this stream
|
|
|
|
free(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Update all currently running streams and destroy them when they're finished.
|
|
|
|
*/
|
|
|
|
static void sound_UpdateStreams()
|
|
|
|
{
|
|
|
|
AUDIO_STREAM *stream = active_streams, *previous = NULL;
|
|
|
|
|
|
|
|
while (stream != NULL)
|
|
|
|
{
|
|
|
|
// Attempt to update the current stream, if we find that impossible,
|
|
|
|
// destroy it instead.
|
|
|
|
if (!sound_UpdateStream(stream))
|
|
|
|
{
|
|
|
|
// First remove our current stream from the linked list
|
|
|
|
if (previous)
|
|
|
|
{
|
|
|
|
// Make the previous item skip over the current to the next item
|
|
|
|
previous->next = stream->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Apparently this is the first item in the list, so make the
|
|
|
|
// next item the list-head.
|
|
|
|
active_streams = stream->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now actually destroy the current stream
|
|
|
|
sound_DestroyStream(stream);
|
|
|
|
|
|
|
|
// Make sure the current stream pointer is intact again
|
|
|
|
stream = (previous != NULL) ? previous->next : active_streams;
|
|
|
|
|
|
|
|
// Skip regular style iterator incrementing
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment our iterator pair
|
|
|
|
previous = stream;
|
|
|
|
stream = stream->next;
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2007-12-13 15:16:03 -08:00
|
|
|
void sound_StopSample(AUDIO_SAMPLE* psSample)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-12-13 15:16:03 -08:00
|
|
|
if (psSample->iSample == (ALuint)SAMPLE_NOT_ALLOCATED)
|
|
|
|
{
|
|
|
|
debug(LOG_SOUND, "sound_StopSample: sample number (%u) out of range, we probably have run out of available OpenAL sources", psSample->iSample);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell OpenAL to stop playing the given sample
|
|
|
|
alSourceStop(psSample->iSample);
|
|
|
|
sound_GetError();
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-08-03 10:51:43 -07:00
|
|
|
void sound_SetPlayerPos(Vector3f pos)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2008-08-03 10:51:43 -07:00
|
|
|
alListener3f(AL_POSITION, pos.x, pos.y, pos.z);
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError();
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2008-08-03 10:51:43 -07:00
|
|
|
/**
|
|
|
|
* Sets the player's orientation to use for sound
|
|
|
|
* \param forward forward pointing vector
|
|
|
|
* \param up upward pointing vector
|
2007-01-07 08:28:54 -08:00
|
|
|
*/
|
2008-08-03 10:51:43 -07:00
|
|
|
void sound_SetPlayerOrientation(Vector3f forward, Vector3f up)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2008-08-03 10:51:43 -07:00
|
|
|
const ALfloat ori[6] =
|
|
|
|
{
|
|
|
|
forward.x, forward.y, forward.z,
|
|
|
|
up.x, up.y, up.z,
|
|
|
|
};
|
2008-03-07 04:42:42 -08:00
|
|
|
|
2008-08-03 10:51:43 -07:00
|
|
|
alListenerfv(AL_ORIENTATION, ori);
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError();
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
2007-11-02 11:47:22 -07:00
|
|
|
void sound_SetObjectPosition(AUDIO_SAMPLE *psSample)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-28 10:47:08 -07:00
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2007-01-07 08:28:54 -08:00
|
|
|
// coordinates
|
|
|
|
float listenerX, listenerY, listenerZ, dX, dY, dZ;
|
|
|
|
|
|
|
|
// calculation results
|
|
|
|
float distance, gain;
|
2007-06-28 10:47:08 -07:00
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2007-01-07 08:28:54 -08:00
|
|
|
// compute distance
|
|
|
|
alGetListener3f( AL_POSITION, &listenerX, &listenerY, &listenerZ );
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError();
|
2007-11-02 11:47:22 -07:00
|
|
|
dX = psSample->x - listenerX; // distances on all axis
|
|
|
|
dY = psSample->y - listenerY;
|
|
|
|
dZ = psSample->z - listenerZ;
|
2007-05-20 07:47:32 -07:00
|
|
|
distance = sqrtf(dX * dX + dY * dY + dZ * dZ); // Pythagorean theorem
|
2007-01-07 08:28:54 -08:00
|
|
|
|
2007-12-31 07:16:59 -08:00
|
|
|
// compute gain
|
2007-11-02 11:47:22 -07:00
|
|
|
gain = (1.0 - (distance * ATTENUATION_FACTOR)) * psSample->fVol * sfx3d_volume;
|
|
|
|
if (gain > 1.0)
|
|
|
|
{
|
|
|
|
gain = 1.0;
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
if ( gain < 0.0 )
|
|
|
|
{
|
|
|
|
gain = 0.0;
|
|
|
|
}
|
2007-11-02 11:47:22 -07:00
|
|
|
alSourcef( psSample->iSample, AL_GAIN, gain );
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-01-08 14:35:43 -08:00
|
|
|
// the alSource3i variant would be better, if it wouldn't provide linker errors however
|
2007-11-02 11:47:22 -07:00
|
|
|
alSource3f( psSample->iSample, AL_POSITION, (float)psSample->x,(float)psSample->x,(float)psSample->x );
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError();
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
void sound_PauseSample( AUDIO_SAMPLE *psSample )
|
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-28 10:47:08 -07:00
|
|
|
alSourcePause( psSample->iSample );
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError();
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
void sound_ResumeSample( AUDIO_SAMPLE *psSample )
|
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-28 10:47:08 -07:00
|
|
|
alSourcePlay( psSample->iSample );
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError();
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
void sound_PauseAll( void )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
void sound_ResumeAll( void )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
void sound_StopAll( void )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
BOOL sound_SampleIsFinished( AUDIO_SAMPLE *psSample )
|
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-28 10:47:08 -07:00
|
|
|
//~~~~~~~~~~
|
|
|
|
ALenum state;
|
|
|
|
//~~~~~~~~~~
|
|
|
|
|
|
|
|
alGetSourcei( psSample->iSample, AL_SOURCE_STATE, &state );
|
2007-12-13 15:16:03 -08:00
|
|
|
sound_GetError(); // check for an error and clear the error state for later on in this function
|
|
|
|
if (state == AL_PLAYING || state == AL_PAUSED)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-12-31 07:16:59 -08:00
|
|
|
|
2007-12-13 15:16:03 -08:00
|
|
|
if (psSample->iSample != (ALuint)AL_INVALID)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-12-13 15:16:03 -08:00
|
|
|
alDeleteSources(1, &(psSample->iSample));
|
|
|
|
sound_GetError();
|
|
|
|
psSample->iSample = AL_INVALID;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
// =======================================================================================================================
|
|
|
|
// =======================================================================================================================
|
|
|
|
//
|
|
|
|
|
2007-06-25 11:09:38 -07:00
|
|
|
float sound_GetUIVolume()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-25 11:09:38 -07:00
|
|
|
return sfx_volume;
|
2007-06-06 10:09:23 -07:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-06-25 11:09:38 -07:00
|
|
|
void sound_SetUIVolume(float volume)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-25 11:09:38 -07:00
|
|
|
sfx_volume = volume;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-05-13 06:50:08 -07:00
|
|
|
// Keep volume in the range of 0.0 - 1.0
|
2007-06-25 11:09:38 -07:00
|
|
|
if (sfx_volume < 0.0)
|
|
|
|
{
|
2007-06-28 10:47:08 -07:00
|
|
|
sfx_volume = 0.0;
|
2007-06-25 11:09:38 -07:00
|
|
|
}
|
|
|
|
else if (sfx_volume > 1.0)
|
|
|
|
{
|
2007-06-28 10:47:08 -07:00
|
|
|
sfx_volume = 1.0;
|
|
|
|
}
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-06-25 11:09:38 -07:00
|
|
|
float sound_GetEffectsVolume()
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-25 11:09:38 -07:00
|
|
|
return sfx3d_volume;
|
2007-06-06 10:09:23 -07:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
2007-06-25 11:09:38 -07:00
|
|
|
void sound_SetEffectsVolume(float volume)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-06-06 10:09:23 -07:00
|
|
|
#ifndef WZ_NOSOUND
|
2007-06-25 11:09:38 -07:00
|
|
|
sfx3d_volume = volume;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-05-13 06:50:08 -07:00
|
|
|
// Keep volume in the range of 0.0 - 1.0
|
2007-06-25 11:09:38 -07:00
|
|
|
if (sfx3d_volume < 0.0)
|
|
|
|
{
|
2007-06-28 10:47:08 -07:00
|
|
|
sfx3d_volume = 0.0;
|
2007-06-25 11:09:38 -07:00
|
|
|
}
|
|
|
|
else if (sfx3d_volume > 1.0)
|
|
|
|
{
|
2007-06-28 10:47:08 -07:00
|
|
|
sfx3d_volume = 1.0;
|
|
|
|
}
|
2007-06-06 10:09:23 -07:00
|
|
|
#endif
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2008-07-09 12:07:42 -07:00
|
|
|
|
|
|
|
void soundTest()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 25; i++)
|
|
|
|
{
|
|
|
|
assert(sound_InitLibrary());
|
|
|
|
sound_ShutdownLibrary();
|
|
|
|
}
|
|
|
|
fprintf(stdout, "\tSound self-test: PASSED\n");
|
|
|
|
}
|