From 77d1fb3feb867c9b773b0e0af67b727b67ae4e93 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 27 Feb 2005 05:55:18 +0000 Subject: [PATCH] introduction of AudioCore --- include/audio/AudioCore.hpp | 52 +++++++++++++++ src/audio/AudioCore.cpp | 124 ++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 include/audio/AudioCore.hpp create mode 100644 src/audio/AudioCore.cpp diff --git a/include/audio/AudioCore.hpp b/include/audio/AudioCore.hpp new file mode 100644 index 0000000..fe3acd2 --- /dev/null +++ b/include/audio/AudioCore.hpp @@ -0,0 +1,52 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: AudioCore.hpp,v 1.1 2005/02/27 05:55:18 cozman Exp $ + +#ifndef PHOTON_AUDIO_AUDIOCORE_HPP +#define PHOTON_AUDIO_AUDIOCORE_HPP + +#include "al.h" +#include "alc.h" + +#include "util/Singleton.hpp" +#include "util/VersionInfo.hpp" + +namespace photon +{ +namespace audio +{ + +class AudioCore : public util::Singleton +{ +public: + std::string getAudioDeviceName() const; + +private: + util::VersionInfo initOpenAL(); + static std::string checkOpenALError(); + +public: + static void setDesiredDevice(const std::string& name); + +private: + static std::string deviceName_; + +// Singleton-required code +private: + AudioCore(); + ~AudioCore(); + + friend class util::Singleton; + friend class std::auto_ptr; +}; + + +} +} + +#endif //PHOTON_AUDIO_AUDIOCORE_HPP diff --git a/src/audio/AudioCore.cpp b/src/audio/AudioCore.cpp new file mode 100644 index 0000000..9427b76 --- /dev/null +++ b/src/audio/AudioCore.cpp @@ -0,0 +1,124 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: AudioCore.cpp,v 1.1 2005/02/27 05:55:18 cozman Exp $ + +#include "audio/AudioCore.hpp" + +#include +#include "exceptions.hpp" + +namespace photon +{ +namespace audio +{ + +std::string AudioCore::getAudioDeviceName() const +{ + ALCdevice* device (alcGetContextsDevice( alcGetCurrentContext() )); + std::string name ( reinterpret_cast( + alcGetString(device, ALC_DEVICE_SPECIFIER)) ); + return name; +} + +util::VersionInfo AudioCore::initOpenAL() +{ + ALCdevice* device; + ALCcontext* context; + std::stringstream ss; + std::string junks; + char junkc; + uint major,minor; + + // obtain default device if no deviceName is set, otherwise use deviceName + device = alcOpenDevice(deviceName_.empty() ? 0 : + reinterpret_cast(deviceName_.c_str()) ); + + if(device == 0) + { + throw APIError("Failed to obtain OpenAL device " + deviceName_ + ": " + + checkOpenALError()); + } + + // create context + context = alcCreateContext(device,0); + + if(context == 0) + { + throw APIError("Failed to obtain an OpenAL context: " + + checkOpenALError()); + } + + alcMakeContextCurrent(context); + + ss << alGetString(AL_VERSION); + ss >> junks >> major >> junkc >> minor; + return util::VersionInfo(major,minor,0); +} + +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: + err = "Unknown OpenAL error."; + break; + } + + return err; +} + +void AudioCore::setDesiredDevice(const std::string& name) +{ + deviceName_ = name; +} + +std::string AudioCore::deviceName_; + +AudioCore::AudioCore() +{ + util::VersionInfo oalReq(1,0,0); // requires OpenAL 1.0 + + util::ensureVersion("OpenAL", initOpenAL(), oalReq); +} + +AudioCore::~AudioCore() +{ + ALCcontext* context( alcGetCurrentContext() ); + ALCdevice* device( alcGetContextsDevice(context) ); + + // set current context to null + alcMakeContextCurrent(0); + + // destroy context & device + alcDestroyContext(context); + alcCloseDevice(device); +} + +} +}