abstracted AudioCore, created OALAudioCore

This commit is contained in:
James Turk 2005-10-28 22:13:03 +00:00
parent 3ba1927472
commit 8b16a21ec3
4 changed files with 60 additions and 46 deletions

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: AudioCore.hpp,v 1.14 2005/10/15 04:57:19 cozman Exp $
// $Id: AudioCore.hpp,v 1.15 2005/10/28 22:13:33 cozman Exp $
#ifdef PHOTON_USE_OPENAL
@ -37,25 +37,34 @@ class AudioCore
public:
// Function: AudioCore
// Initialize underlying APIs and setup <Task> internals.
//AudioCore();
AudioCore(const std::string& deviceName);
AudioCore(const std::string& deviceName) {};
// Function: ~AudioCore
// Shutdown underlying APIs.
~AudioCore();
virtual ~AudioCore() { };
// Group: Accessors
public:
// Function: getAudioDeviceName
// Get name of active audio device.
//
// Returns:
// Name of audio device currently in use.
std::string getAudioDeviceName() const;
// Group: Error Checking
// Returns name of audio device currently in use, if available from
// underlying audio API..
virtual std::string getAudioDeviceName() const=0;
};
class OALAudioCore : public AudioCore
{
public:
// Function: checkOpenALError
OALAudioCore(const std::string& deviceName);
~OALAudioCore();
public:
std::string getAudioDeviceName() const;
public:
// No-Doc Function: checkOpenALError
// Checks for OpenAL internal errors, returning a descriptive string if
// the OpenAL error state is currently set. Will return an empty string
// if there is no error set.
@ -64,7 +73,7 @@ public:
// String describing OpenAL error, empty string if no error exists.
static std::string checkOpenALError();
// Function: throwOpenALError
// No-Doc Function: throwOpenALError
// Checks for OpenAL internal errors, throwing an <APIError> if the OpenAL
// error state is set and doing nothing if not. Optionally makes the
// thrown exception more descriptive by adding in a function string
@ -81,14 +90,13 @@ public:
private:
util::VersionInfo initOpenAL(const std::string& deviceName);
// data members
// data members specific to implementation
private:
ALfloat listenerPos_[3];
ALfloat listenerVel_[3];
ALfloat listenerOri_[6];
};
}
}

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Source.hpp,v 1.4 2005/08/08 21:39:41 cozman Exp $
// $Id: Source.hpp,v 1.5 2005/10/28 22:13:33 cozman Exp $
#ifdef PHOTON_USE_OPENAL
@ -23,17 +23,20 @@ namespace audio
{
// Class: Source
// Simple OO wrapper around an OpenAL source, defines the interface used for
// Sample and Music.
// Simple wrapper object around an OpenAL source, defines the interface used
// for Sample and Music.
//
// Source is a template class and can not be used directly, use either Sample
// or Music. Sample is for playing small files such as sound effects. Music
// is for streaming files such as background music.
//
// Source is a resource managed class, and therefore all resources should
// be registered using <Source::addResource> and then loaded by their assigned
// name via <Source::open> or the appropriate constructor.
//
// Operators:
// - Source = Source
// - bool : True if source has loaded buffer, false if not.
// - ostream& << Source
template <class ResMgrT>
class Source : public ResourceManaged<ResMgrT>
{
@ -69,7 +72,7 @@ public:
public:
// Function: open
// Opens an audio file, supported formats are WAV and Ogg.
// Opens an audio file, supported formats are WAV and Ogg Vorbis.
//
// Parameters:
// name - Name of the Source <Resource> to open.
@ -168,7 +171,7 @@ template<class ResMgrT>
Source<ResMgrT>::Source()
{
alGenSources(1, &sourceID_);
AudioCore::throwOpenALError("Source::Source()");
OALAudioCore::throwOpenALError("Source::Source()");
}
template<class ResMgrT>
@ -176,14 +179,14 @@ Source<ResMgrT>::Source(const Source &rhs) :
ResourceManaged<ResMgrT>(rhs)
{
alGenSources(1, &sourceID_);
AudioCore::throwOpenALError("Source::Source(const Source&)");
OALAudioCore::throwOpenALError("Source::Source(const Source&)");
}
template<class ResMgrT>
Source<ResMgrT>::Source(const std::string& name)
{
alGenSources(1, &sourceID_);
AudioCore::throwOpenALError("Source::Source(const std::string&)");
OALAudioCore::throwOpenALError("Source::Source(const std::string&)");
open(name);
}
@ -205,14 +208,14 @@ void Source<ResMgrT>::open(const std::string& name)
ResourceManaged<ResMgrT>::resMgr_.getAudioData(
ResourceManaged<ResMgrT>::getName(), bufferID);
// attach buffer to source
// attach buffer to source and set default settings
alSourcei(sourceID_, AL_BUFFER, bufferID);
alSourcef(sourceID_, AL_PITCH, 1.0);
alSourcef(sourceID_, AL_GAIN, 1.0);
alSourcefv(sourceID_, AL_POSITION, ORIGIN);
alSourcefv(sourceID_, AL_VELOCITY, ORIGIN);
AudioCore::throwOpenALError("Source::open");
OALAudioCore::throwOpenALError("Source::open");
}
template<class ResMgrT>
@ -233,7 +236,7 @@ Source<ResMgrT>& Source<ResMgrT>::operator=(const Source<ResMgrT>& rhs)
alSourcefv(sourceID_, AL_POSITION, ORIGIN);
alSourcefv(sourceID_, AL_VELOCITY, ORIGIN);
AudioCore::throwOpenALError("Source::operator=");
OALAudioCore::throwOpenALError("Source::operator=");
}
return *this;
}
@ -241,7 +244,7 @@ Source<ResMgrT>& Source<ResMgrT>::operator=(const Source<ResMgrT>& rhs)
template<class ResMgrT>
Source<ResMgrT>::operator bool() const
{
return isValid();
return isValid(); // do the work in isValid to avoid split implementation
}
template<class ResMgrT>
@ -252,7 +255,7 @@ void Source<ResMgrT>::play()
throw PreconditionException("Invalid Source::play call.");
}
alSourcePlay(sourceID_);
alSourcePlay(sourceID_); // play it
}
template<class ResMgrT>
@ -263,7 +266,7 @@ void Source<ResMgrT>::stop()
throw PreconditionException("Invalid Source::stop call.");
}
alSourceStop(sourceID_);
alSourceStop(sourceID_); // stop it
}
template<class ResMgrT>
@ -274,7 +277,7 @@ void Source<ResMgrT>::pause()
throw PreconditionException("Invalid Source::pause call.");
}
alSourcePause(sourceID_);
alSourcePause(sourceID_); // pause it
}
template<class ResMgrT>
@ -285,7 +288,7 @@ void Source<ResMgrT>::rewind()
throw PreconditionException("Invalid Source::rewind call.");
}
alSourceRewind(sourceID_);
alSourceRewind(sourceID_); // rewind it (doesn't stop)
}
template<class ResMgrT>
@ -296,13 +299,13 @@ void Source<ResMgrT>::setLooping(bool loop)
throw PreconditionException("Invalid Source::setLooping call.");
}
alSourcei(sourceID_, AL_LOOPING, loop);
alSourcei(sourceID_, AL_LOOPING, loop); // toggle looping
}
template<class ResMgrT>
bool Source<ResMgrT>::isValid() const
{
return alIsSource(sourceID_) == AL_TRUE;
return alIsSource(sourceID_) == AL_TRUE; // true if valid audio loaded
}
template<class ResMgrT>
@ -313,7 +316,7 @@ bool Source<ResMgrT>::isPlaying() const
throw PreconditionException("Invalid Source::isPlaying call.");
}
// check state
// check state using OpenAL query function
int state;
alGetSourcei(sourceID_, AL_SOURCE_STATE, &state);
return state == AL_PLAYING;
@ -327,7 +330,7 @@ bool Source<ResMgrT>::isLooping() const
throw PreconditionException("Invalid Source::isLooping call.");
}
// check looping status
// query looping status
int loop;
alGetSourcei(sourceID_, AL_LOOPING, &loop);
return loop == AL_TRUE;
@ -337,6 +340,7 @@ template<class ResMgrT>
void Source<ResMgrT>::addResource(const std::string& name,
const std::string& path)
{
// adds an aliased resource
ResourceManaged<ResMgrT>::resMgr_.newResource(name,
ResourceDescriptor(path));
}
@ -344,6 +348,7 @@ void Source<ResMgrT>::addResource(const std::string& name,
template<class ResMgrT>
void Source<ResMgrT>::addResource(const std::string& path)
{
// add non-aliased resource
ResourceManaged<ResMgrT>::resMgr_.newResource(path,
ResourceDescriptor(path));
}

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: AudioCore.cpp,v 1.14 2005/10/15 04:57:19 cozman Exp $
// $Id: AudioCore.cpp,v 1.15 2005/10/28 22:13:03 cozman Exp $
#ifdef PHOTON_USE_OPENAL
@ -19,8 +19,9 @@ namespace photon
namespace audio
{
//AudioCore::AudioCore()
AudioCore::AudioCore(const std::string& deviceName)
OALAudioCore::OALAudioCore(const std::string& deviceName) :
AudioCore(deviceName)
{
//util::VersionInfo oalReq(0,0,7); // requires OpenAL 1.0 (TODO: check?)
//util::ensureVersion("OpenAL", initOpenAL(), oalReq);
@ -28,7 +29,7 @@ AudioCore::AudioCore(const std::string& deviceName)
initOpenAL(deviceName); // don't check version for now
}
AudioCore::~AudioCore()
OALAudioCore::~OALAudioCore()
{
// retrieve both the context and device
ALCcontext* context( alcGetCurrentContext() );
@ -42,7 +43,7 @@ AudioCore::~AudioCore()
alcMakeContextCurrent(0);
}
std::string AudioCore::getAudioDeviceName() const
std::string OALAudioCore::getAudioDeviceName() const
{
ALCdevice* device (alcGetContextsDevice( alcGetCurrentContext() ));
std::string name ( reinterpret_cast<const char*>(
@ -51,7 +52,7 @@ std::string AudioCore::getAudioDeviceName() const
return name;
}
std::string AudioCore::checkOpenALError()
std::string OALAudioCore::checkOpenALError()
{
ALenum errCode = alGetError(); // fetch error code
std::string err;
@ -85,7 +86,7 @@ std::string AudioCore::checkOpenALError()
return err;
}
void AudioCore::throwOpenALError(const std::string& func)
void OALAudioCore::throwOpenALError(const std::string& func)
{
std::string err( checkOpenALError() );
if(err.length()) // throw exception if non-empty string
@ -94,7 +95,7 @@ void AudioCore::throwOpenALError(const std::string& func)
}
}
util::VersionInfo AudioCore::initOpenAL(const std::string& deviceName)
util::VersionInfo OALAudioCore::initOpenAL(const std::string& deviceName)
{
ALCdevice* device(0);
ALCcontext* context(0);
@ -135,7 +136,7 @@ util::VersionInfo AudioCore::initOpenAL(const std::string& deviceName)
ss << alGetString(AL_VERSION);
ss >> major >> junkc >> minor >> extra;
throwOpenALError("AudioCore::initOpenAL");
throwOpenALError("OALAudioCore::initOpenAL");
return util::VersionInfo(major,minor,0,extra);
}

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: SampleResourceManager.cpp,v 1.4 2005/08/10 05:36:30 cozman Exp $
// $Id: SampleResourceManager.cpp,v 1.5 2005/10/28 22:13:03 cozman Exp $
#ifdef PHOTON_USE_OPENAL
@ -46,14 +46,14 @@ void SampleResourceManager::loadResourceData(SampleResource &res,
alGenBuffers(1, &res.bufferID); // create OpenAL buffer
AudioCore::throwOpenALError("alGenBuffers");
OALAudioCore::throwOpenALError("alGenBuffers");
// load WAV and pass it into OpenAL buffer
loadWAVMemory(&filedata[0], format, data, size, freq);
alBufferData(res.bufferID, format, (ALvoid*)data, size, freq);
freeWAVMemory(data);
AudioCore::throwOpenALError("alBufferData");
OALAudioCore::throwOpenALError("alBufferData");
}
void SampleResourceManager::freeResourceData(SampleResource& res)