abstracted AudioCore, created OALAudioCore
This commit is contained in:
parent
3ba1927472
commit
8b16a21ec3
@ -5,7 +5,7 @@
|
|||||||
// James Turk (jpt2433@rit.edu)
|
// James Turk (jpt2433@rit.edu)
|
||||||
//
|
//
|
||||||
// Version:
|
// 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
|
#ifdef PHOTON_USE_OPENAL
|
||||||
|
|
||||||
@ -37,25 +37,34 @@ class AudioCore
|
|||||||
public:
|
public:
|
||||||
// Function: AudioCore
|
// Function: AudioCore
|
||||||
// Initialize underlying APIs and setup <Task> internals.
|
// Initialize underlying APIs and setup <Task> internals.
|
||||||
//AudioCore();
|
AudioCore(const std::string& deviceName) {};
|
||||||
AudioCore(const std::string& deviceName);
|
|
||||||
|
|
||||||
// Function: ~AudioCore
|
// Function: ~AudioCore
|
||||||
// Shutdown underlying APIs.
|
// Shutdown underlying APIs.
|
||||||
~AudioCore();
|
virtual ~AudioCore() { };
|
||||||
|
|
||||||
// Group: Accessors
|
// Group: Accessors
|
||||||
public:
|
public:
|
||||||
// Function: getAudioDeviceName
|
// Function: getAudioDeviceName
|
||||||
// Get name of active audio device.
|
|
||||||
//
|
//
|
||||||
// Returns:
|
// Returns name of audio device currently in use, if available from
|
||||||
// Name of audio device currently in use.
|
// underlying audio API..
|
||||||
std::string getAudioDeviceName() const;
|
virtual std::string getAudioDeviceName() const=0;
|
||||||
|
};
|
||||||
// Group: Error Checking
|
|
||||||
|
class OALAudioCore : public AudioCore
|
||||||
|
{
|
||||||
|
|
||||||
public:
|
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
|
// Checks for OpenAL internal errors, returning a descriptive string if
|
||||||
// the OpenAL error state is currently set. Will return an empty string
|
// the OpenAL error state is currently set. Will return an empty string
|
||||||
// if there is no error set.
|
// if there is no error set.
|
||||||
@ -64,7 +73,7 @@ public:
|
|||||||
// String describing OpenAL error, empty string if no error exists.
|
// String describing OpenAL error, empty string if no error exists.
|
||||||
static std::string checkOpenALError();
|
static std::string checkOpenALError();
|
||||||
|
|
||||||
// Function: throwOpenALError
|
// No-Doc Function: throwOpenALError
|
||||||
// Checks for OpenAL internal errors, throwing an <APIError> if the OpenAL
|
// Checks for OpenAL internal errors, throwing an <APIError> if the OpenAL
|
||||||
// error state is set and doing nothing if not. Optionally makes the
|
// error state is set and doing nothing if not. Optionally makes the
|
||||||
// thrown exception more descriptive by adding in a function string
|
// thrown exception more descriptive by adding in a function string
|
||||||
@ -81,14 +90,13 @@ public:
|
|||||||
private:
|
private:
|
||||||
util::VersionInfo initOpenAL(const std::string& deviceName);
|
util::VersionInfo initOpenAL(const std::string& deviceName);
|
||||||
|
|
||||||
// data members
|
// data members specific to implementation
|
||||||
private:
|
private:
|
||||||
ALfloat listenerPos_[3];
|
ALfloat listenerPos_[3];
|
||||||
ALfloat listenerVel_[3];
|
ALfloat listenerVel_[3];
|
||||||
ALfloat listenerOri_[6];
|
ALfloat listenerOri_[6];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
// James Turk (jpt2433@rit.edu)
|
// James Turk (jpt2433@rit.edu)
|
||||||
//
|
//
|
||||||
// Version:
|
// 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
|
#ifdef PHOTON_USE_OPENAL
|
||||||
|
|
||||||
@ -23,17 +23,20 @@ namespace audio
|
|||||||
{
|
{
|
||||||
|
|
||||||
// Class: Source
|
// Class: Source
|
||||||
// Simple OO wrapper around an OpenAL source, defines the interface used for
|
// Simple wrapper object around an OpenAL source, defines the interface used
|
||||||
// Sample and Music.
|
// for Sample and Music.
|
||||||
//
|
//
|
||||||
// Source is a template class and can not be used directly, use either Sample
|
// 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
|
// or Music. Sample is for playing small files such as sound effects. Music
|
||||||
// is for streaming files such as background 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:
|
// Operators:
|
||||||
// - Source = Source
|
// - Source = Source
|
||||||
// - bool : True if source has loaded buffer, false if not.
|
// - bool : True if source has loaded buffer, false if not.
|
||||||
// - ostream& << Source
|
|
||||||
template <class ResMgrT>
|
template <class ResMgrT>
|
||||||
class Source : public ResourceManaged<ResMgrT>
|
class Source : public ResourceManaged<ResMgrT>
|
||||||
{
|
{
|
||||||
@ -69,7 +72,7 @@ public:
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
// Function: open
|
// Function: open
|
||||||
// Opens an audio file, supported formats are WAV and Ogg.
|
// Opens an audio file, supported formats are WAV and Ogg Vorbis.
|
||||||
//
|
//
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// name - Name of the Source <Resource> to open.
|
// name - Name of the Source <Resource> to open.
|
||||||
@ -168,7 +171,7 @@ template<class ResMgrT>
|
|||||||
Source<ResMgrT>::Source()
|
Source<ResMgrT>::Source()
|
||||||
{
|
{
|
||||||
alGenSources(1, &sourceID_);
|
alGenSources(1, &sourceID_);
|
||||||
AudioCore::throwOpenALError("Source::Source()");
|
OALAudioCore::throwOpenALError("Source::Source()");
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
@ -176,14 +179,14 @@ Source<ResMgrT>::Source(const Source &rhs) :
|
|||||||
ResourceManaged<ResMgrT>(rhs)
|
ResourceManaged<ResMgrT>(rhs)
|
||||||
{
|
{
|
||||||
alGenSources(1, &sourceID_);
|
alGenSources(1, &sourceID_);
|
||||||
AudioCore::throwOpenALError("Source::Source(const Source&)");
|
OALAudioCore::throwOpenALError("Source::Source(const Source&)");
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
Source<ResMgrT>::Source(const std::string& name)
|
Source<ResMgrT>::Source(const std::string& name)
|
||||||
{
|
{
|
||||||
alGenSources(1, &sourceID_);
|
alGenSources(1, &sourceID_);
|
||||||
AudioCore::throwOpenALError("Source::Source(const std::string&)");
|
OALAudioCore::throwOpenALError("Source::Source(const std::string&)");
|
||||||
open(name);
|
open(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,14 +208,14 @@ void Source<ResMgrT>::open(const std::string& name)
|
|||||||
ResourceManaged<ResMgrT>::resMgr_.getAudioData(
|
ResourceManaged<ResMgrT>::resMgr_.getAudioData(
|
||||||
ResourceManaged<ResMgrT>::getName(), bufferID);
|
ResourceManaged<ResMgrT>::getName(), bufferID);
|
||||||
|
|
||||||
// attach buffer to source
|
// attach buffer to source and set default settings
|
||||||
alSourcei(sourceID_, AL_BUFFER, bufferID);
|
alSourcei(sourceID_, AL_BUFFER, bufferID);
|
||||||
alSourcef(sourceID_, AL_PITCH, 1.0);
|
alSourcef(sourceID_, AL_PITCH, 1.0);
|
||||||
alSourcef(sourceID_, AL_GAIN, 1.0);
|
alSourcef(sourceID_, AL_GAIN, 1.0);
|
||||||
alSourcefv(sourceID_, AL_POSITION, ORIGIN);
|
alSourcefv(sourceID_, AL_POSITION, ORIGIN);
|
||||||
alSourcefv(sourceID_, AL_VELOCITY, ORIGIN);
|
alSourcefv(sourceID_, AL_VELOCITY, ORIGIN);
|
||||||
|
|
||||||
AudioCore::throwOpenALError("Source::open");
|
OALAudioCore::throwOpenALError("Source::open");
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
@ -233,7 +236,7 @@ Source<ResMgrT>& Source<ResMgrT>::operator=(const Source<ResMgrT>& rhs)
|
|||||||
alSourcefv(sourceID_, AL_POSITION, ORIGIN);
|
alSourcefv(sourceID_, AL_POSITION, ORIGIN);
|
||||||
alSourcefv(sourceID_, AL_VELOCITY, ORIGIN);
|
alSourcefv(sourceID_, AL_VELOCITY, ORIGIN);
|
||||||
|
|
||||||
AudioCore::throwOpenALError("Source::operator=");
|
OALAudioCore::throwOpenALError("Source::operator=");
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -241,7 +244,7 @@ Source<ResMgrT>& Source<ResMgrT>::operator=(const Source<ResMgrT>& rhs)
|
|||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
Source<ResMgrT>::operator bool() const
|
Source<ResMgrT>::operator bool() const
|
||||||
{
|
{
|
||||||
return isValid();
|
return isValid(); // do the work in isValid to avoid split implementation
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
@ -252,7 +255,7 @@ void Source<ResMgrT>::play()
|
|||||||
throw PreconditionException("Invalid Source::play call.");
|
throw PreconditionException("Invalid Source::play call.");
|
||||||
}
|
}
|
||||||
|
|
||||||
alSourcePlay(sourceID_);
|
alSourcePlay(sourceID_); // play it
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
@ -263,7 +266,7 @@ void Source<ResMgrT>::stop()
|
|||||||
throw PreconditionException("Invalid Source::stop call.");
|
throw PreconditionException("Invalid Source::stop call.");
|
||||||
}
|
}
|
||||||
|
|
||||||
alSourceStop(sourceID_);
|
alSourceStop(sourceID_); // stop it
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
@ -274,7 +277,7 @@ void Source<ResMgrT>::pause()
|
|||||||
throw PreconditionException("Invalid Source::pause call.");
|
throw PreconditionException("Invalid Source::pause call.");
|
||||||
}
|
}
|
||||||
|
|
||||||
alSourcePause(sourceID_);
|
alSourcePause(sourceID_); // pause it
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
@ -285,7 +288,7 @@ void Source<ResMgrT>::rewind()
|
|||||||
throw PreconditionException("Invalid Source::rewind call.");
|
throw PreconditionException("Invalid Source::rewind call.");
|
||||||
}
|
}
|
||||||
|
|
||||||
alSourceRewind(sourceID_);
|
alSourceRewind(sourceID_); // rewind it (doesn't stop)
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
@ -296,13 +299,13 @@ void Source<ResMgrT>::setLooping(bool loop)
|
|||||||
throw PreconditionException("Invalid Source::setLooping call.");
|
throw PreconditionException("Invalid Source::setLooping call.");
|
||||||
}
|
}
|
||||||
|
|
||||||
alSourcei(sourceID_, AL_LOOPING, loop);
|
alSourcei(sourceID_, AL_LOOPING, loop); // toggle looping
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
bool Source<ResMgrT>::isValid() const
|
bool Source<ResMgrT>::isValid() const
|
||||||
{
|
{
|
||||||
return alIsSource(sourceID_) == AL_TRUE;
|
return alIsSource(sourceID_) == AL_TRUE; // true if valid audio loaded
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
@ -313,7 +316,7 @@ bool Source<ResMgrT>::isPlaying() const
|
|||||||
throw PreconditionException("Invalid Source::isPlaying call.");
|
throw PreconditionException("Invalid Source::isPlaying call.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// check state
|
// check state using OpenAL query function
|
||||||
int state;
|
int state;
|
||||||
alGetSourcei(sourceID_, AL_SOURCE_STATE, &state);
|
alGetSourcei(sourceID_, AL_SOURCE_STATE, &state);
|
||||||
return state == AL_PLAYING;
|
return state == AL_PLAYING;
|
||||||
@ -327,7 +330,7 @@ bool Source<ResMgrT>::isLooping() const
|
|||||||
throw PreconditionException("Invalid Source::isLooping call.");
|
throw PreconditionException("Invalid Source::isLooping call.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// check looping status
|
// query looping status
|
||||||
int loop;
|
int loop;
|
||||||
alGetSourcei(sourceID_, AL_LOOPING, &loop);
|
alGetSourcei(sourceID_, AL_LOOPING, &loop);
|
||||||
return loop == AL_TRUE;
|
return loop == AL_TRUE;
|
||||||
@ -337,6 +340,7 @@ template<class ResMgrT>
|
|||||||
void Source<ResMgrT>::addResource(const std::string& name,
|
void Source<ResMgrT>::addResource(const std::string& name,
|
||||||
const std::string& path)
|
const std::string& path)
|
||||||
{
|
{
|
||||||
|
// adds an aliased resource
|
||||||
ResourceManaged<ResMgrT>::resMgr_.newResource(name,
|
ResourceManaged<ResMgrT>::resMgr_.newResource(name,
|
||||||
ResourceDescriptor(path));
|
ResourceDescriptor(path));
|
||||||
}
|
}
|
||||||
@ -344,6 +348,7 @@ void Source<ResMgrT>::addResource(const std::string& name,
|
|||||||
template<class ResMgrT>
|
template<class ResMgrT>
|
||||||
void Source<ResMgrT>::addResource(const std::string& path)
|
void Source<ResMgrT>::addResource(const std::string& path)
|
||||||
{
|
{
|
||||||
|
// add non-aliased resource
|
||||||
ResourceManaged<ResMgrT>::resMgr_.newResource(path,
|
ResourceManaged<ResMgrT>::resMgr_.newResource(path,
|
||||||
ResourceDescriptor(path));
|
ResourceDescriptor(path));
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
// James Turk (jpt2433@rit.edu)
|
// James Turk (jpt2433@rit.edu)
|
||||||
//
|
//
|
||||||
// Version:
|
// 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
|
#ifdef PHOTON_USE_OPENAL
|
||||||
|
|
||||||
@ -19,8 +19,9 @@ namespace photon
|
|||||||
namespace audio
|
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::VersionInfo oalReq(0,0,7); // requires OpenAL 1.0 (TODO: check?)
|
||||||
//util::ensureVersion("OpenAL", initOpenAL(), oalReq);
|
//util::ensureVersion("OpenAL", initOpenAL(), oalReq);
|
||||||
@ -28,7 +29,7 @@ AudioCore::AudioCore(const std::string& deviceName)
|
|||||||
initOpenAL(deviceName); // don't check version for now
|
initOpenAL(deviceName); // don't check version for now
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioCore::~AudioCore()
|
OALAudioCore::~OALAudioCore()
|
||||||
{
|
{
|
||||||
// retrieve both the context and device
|
// retrieve both the context and device
|
||||||
ALCcontext* context( alcGetCurrentContext() );
|
ALCcontext* context( alcGetCurrentContext() );
|
||||||
@ -42,7 +43,7 @@ AudioCore::~AudioCore()
|
|||||||
alcMakeContextCurrent(0);
|
alcMakeContextCurrent(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string AudioCore::getAudioDeviceName() const
|
std::string OALAudioCore::getAudioDeviceName() const
|
||||||
{
|
{
|
||||||
ALCdevice* device (alcGetContextsDevice( alcGetCurrentContext() ));
|
ALCdevice* device (alcGetContextsDevice( alcGetCurrentContext() ));
|
||||||
std::string name ( reinterpret_cast<const char*>(
|
std::string name ( reinterpret_cast<const char*>(
|
||||||
@ -51,7 +52,7 @@ std::string AudioCore::getAudioDeviceName() const
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string AudioCore::checkOpenALError()
|
std::string OALAudioCore::checkOpenALError()
|
||||||
{
|
{
|
||||||
ALenum errCode = alGetError(); // fetch error code
|
ALenum errCode = alGetError(); // fetch error code
|
||||||
std::string err;
|
std::string err;
|
||||||
@ -85,7 +86,7 @@ std::string AudioCore::checkOpenALError()
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioCore::throwOpenALError(const std::string& func)
|
void OALAudioCore::throwOpenALError(const std::string& func)
|
||||||
{
|
{
|
||||||
std::string err( checkOpenALError() );
|
std::string err( checkOpenALError() );
|
||||||
if(err.length()) // throw exception if non-empty string
|
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);
|
ALCdevice* device(0);
|
||||||
ALCcontext* context(0);
|
ALCcontext* context(0);
|
||||||
@ -135,7 +136,7 @@ util::VersionInfo AudioCore::initOpenAL(const std::string& deviceName)
|
|||||||
ss << alGetString(AL_VERSION);
|
ss << alGetString(AL_VERSION);
|
||||||
ss >> major >> junkc >> minor >> extra;
|
ss >> major >> junkc >> minor >> extra;
|
||||||
|
|
||||||
throwOpenALError("AudioCore::initOpenAL");
|
throwOpenALError("OALAudioCore::initOpenAL");
|
||||||
|
|
||||||
return util::VersionInfo(major,minor,0,extra);
|
return util::VersionInfo(major,minor,0,extra);
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
// James Turk (jpt2433@rit.edu)
|
// James Turk (jpt2433@rit.edu)
|
||||||
//
|
//
|
||||||
// Version:
|
// 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
|
#ifdef PHOTON_USE_OPENAL
|
||||||
|
|
||||||
@ -46,14 +46,14 @@ void SampleResourceManager::loadResourceData(SampleResource &res,
|
|||||||
|
|
||||||
alGenBuffers(1, &res.bufferID); // create OpenAL buffer
|
alGenBuffers(1, &res.bufferID); // create OpenAL buffer
|
||||||
|
|
||||||
AudioCore::throwOpenALError("alGenBuffers");
|
OALAudioCore::throwOpenALError("alGenBuffers");
|
||||||
|
|
||||||
// load WAV and pass it into OpenAL buffer
|
// load WAV and pass it into OpenAL buffer
|
||||||
loadWAVMemory(&filedata[0], format, data, size, freq);
|
loadWAVMemory(&filedata[0], format, data, size, freq);
|
||||||
alBufferData(res.bufferID, format, (ALvoid*)data, size, freq);
|
alBufferData(res.bufferID, format, (ALvoid*)data, size, freq);
|
||||||
freeWAVMemory(data);
|
freeWAVMemory(data);
|
||||||
|
|
||||||
AudioCore::throwOpenALError("alBufferData");
|
OALAudioCore::throwOpenALError("alBufferData");
|
||||||
}
|
}
|
||||||
|
|
||||||
void SampleResourceManager::freeResourceData(SampleResource& res)
|
void SampleResourceManager::freeResourceData(SampleResource& res)
|
||||||
|
Loading…
Reference in New Issue
Block a user