cpp_photon/src/audio/AudioCore.cpp

142 lines
3.5 KiB
C++
Raw Normal View History

2005-02-27 05:55:18 +00:00
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
2005-07-04 03:06:48 +00:00
// $Id: AudioCore.cpp,v 1.6 2005/07/04 03:06:48 cozman Exp $
#ifdef PHOTON_USE_OPENAL
2005-02-27 05:55:18 +00:00
#include "audio/AudioCore.hpp"
#include <string>
#include "exceptions.hpp"
namespace photon
{
namespace audio
{
2005-03-14 05:34:08 +00:00
2005-03-15 18:41:27 +00:00
AudioCore::AudioCore()
2005-03-14 05:34:08 +00:00
{
2005-05-15 02:50:52 +00:00
util::VersionInfo oalReq(0,0,7); // requires OpenAL 1.0 (TODO: check?)
2005-03-14 05:34:08 +00:00
util::ensureVersion("OpenAL", initOpenAL(), oalReq);
}
AudioCore::~AudioCore()
{
// retrieve both the context and device
ALCcontext* context( alcGetCurrentContext() );
ALCdevice* device( alcGetContextsDevice(context) );
// set current context to null
alcMakeContextCurrent(0);
// destroy context & device
alcDestroyContext(context);
alcCloseDevice(device);
}
2005-02-27 05:55:18 +00:00
std::string AudioCore::getAudioDeviceName() const
{
ALCdevice* device (alcGetContextsDevice( alcGetCurrentContext() ));
std::string name ( reinterpret_cast<const char*>(
alcGetString(device, ALC_DEVICE_SPECIFIER)) );
2005-05-15 02:50:52 +00:00
2005-02-27 05:55:18 +00:00
return name;
}
util::VersionInfo AudioCore::initOpenAL()
{
2005-03-03 09:25:19 +00:00
ALCdevice* device(0);
ALCcontext* context(0);
2005-03-15 18:41:27 +00:00
std::stringstream ss; // stream for parsing version
std::string junks; // junk string for parsing
char junkc; // junk character for parsing
2005-05-15 02:50:52 +00:00
uint major,minor,patch; // version numbers
2005-02-27 05:55:18 +00:00
// obtain default device if no deviceName is set, otherwise use deviceName
2005-03-14 05:34:08 +00:00
device = alcOpenDevice(deviceName_.empty() ? 0 :
2005-02-27 05:55:18 +00:00
reinterpret_cast<const ALubyte*>(deviceName_.c_str()) );
if(device == 0)
{
2005-03-14 05:34:08 +00:00
throw APIError("Failed to obtain OpenAL device " + deviceName_ + ": " +
2005-02-27 05:55:18 +00:00
checkOpenALError());
}
// create context
context = alcCreateContext(device,0);
if(context == 0)
{
2005-03-14 05:34:08 +00:00
throw APIError("Failed to obtain an OpenAL context: " +
2005-02-27 05:55:18 +00:00
checkOpenALError());
}
2005-03-14 05:34:08 +00:00
alcMakeContextCurrent(context); // context must be current to get version
2005-02-27 05:55:18 +00:00
2005-05-15 02:50:52 +00:00
2005-02-27 05:55:18 +00:00
ss << alGetString(AL_VERSION);
2005-05-15 02:50:52 +00:00
#if defined(WINVER)
ss >> junks >> major >> junkc >> minor; // format is "OpenAL 1.0"
#elif defined(linux)
ss >> major >> junkc >> minor >> junkc >> patch;
#else
#warning OpenAL only built on Windows/Linux, find out version on OSX
#endif
//std::cerr << util::VersionInfo(major,minor,patch);
return util::VersionInfo(major,minor,patch);
2005-02-27 05:55:18 +00:00
}
std::string AudioCore::checkOpenALError()
{
ALenum errCode = alGetError();
std::string err;
switch(errCode)
{
case AL_NO_ERROR:
// do nothing
break;
case AL_INVALID_NAME:
err = "Invalid name/ID parameter in OpenAL call";
break;
case AL_INVALID_ENUM:
err = "Invalid enum parameter in OpenAL call";
break;
case AL_INVALID_VALUE:
err = "Invalid value parameter in OpenAL call";
break;
case AL_INVALID_OPERATION:
err = "Invalid OpenAL operation";
break;
case AL_OUT_OF_MEMORY:
err = "OpenAL out of memory";
break;
default:
2005-03-03 09:25:19 +00:00
err = "Unknown OpenAL error";
2005-02-27 05:55:18 +00:00
break;
}
return err;
}
void AudioCore::setDesiredDevice(const std::string& name)
{
2005-03-03 09:25:19 +00:00
// deviceName_ is used inside initOpenAL, must be set prior to construction
2005-03-15 18:41:27 +00:00
// or not set at all (in which case default device will be used)
2005-02-27 05:55:18 +00:00
deviceName_ = name;
}
2005-03-14 05:34:08 +00:00
std::string AudioCore::deviceName_;
2005-02-27 05:55:18 +00:00
}
}
2005-07-04 03:06:48 +00:00
#endif //PHOTON_USE_OPENAL