201 lines
7.2 KiB
C++
201 lines
7.2 KiB
C++
//
|
|
// FileManager.cpp
|
|
// KREngine
|
|
//
|
|
// Copyright 2012 Kearwood Gilbert. All rights reserved.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
// permitted provided that the following conditions are met:
|
|
//
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
// conditions and the following disclaimer.
|
|
//
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
// of conditions and the following disclaimer in the documentation and/or other materials
|
|
// provided with the distribution.
|
|
//
|
|
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
|
|
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
//
|
|
// The views and conclusions contained in the software and documentation are those of the
|
|
// authors and should not be interpreted as representing official policies, either expressed
|
|
// or implied, of Kearwood Gilbert.
|
|
//
|
|
|
|
#include "KRAudioManager.h"
|
|
#include "KREngine-common.h"
|
|
#include "KRDataBlock.h"
|
|
|
|
OSStatus alcASASetListenerProc(const ALuint property, ALvoid *data, ALuint dataSize);
|
|
ALvoid alcMacOSXRenderingQualityProc(const ALint value);
|
|
|
|
KRAudioManager::KRAudioManager(KRContext &context) : KRContextObject(context)
|
|
{
|
|
// ----- Initialize OpenAL -----
|
|
ALDEBUG(m_alDevice = alcOpenDevice(NULL));
|
|
ALDEBUG(m_alContext=alcCreateContext(m_alDevice,NULL));
|
|
ALDEBUG(alcMakeContextCurrent(m_alContext));
|
|
|
|
// ----- Configure listener -----
|
|
ALDEBUG(alSpeedOfSound(1116.43701f)); // 1116.43701 feet per second
|
|
ALDEBUG(alDistanceModel(AL_EXPONENT_DISTANCE));
|
|
|
|
#if TARGET_OS_IPHONE
|
|
ALDEBUG(alcMacOSXRenderingQualityProc(ALC_IPHONE_SPATIAL_RENDERING_QUALITY_HEADPHONES));
|
|
#else
|
|
ALDEBUG(alcMacOSXRenderingQualityProc(ALC_MAC_OSX_SPATIAL_RENDERING_QUALITY_HIGH));
|
|
#endif
|
|
UInt32 setting = 1;
|
|
ALDEBUG(alcASASetListenerProc(ALC_ASA_REVERB_ON, &setting, sizeof(setting)));
|
|
ALfloat global_reverb_level = -5.0f;
|
|
ALDEBUG(alcASASetListenerProc(ALC_ASA_REVERB_GLOBAL_LEVEL, &global_reverb_level, sizeof(global_reverb_level)));
|
|
|
|
setting = ALC_ASA_REVERB_ROOM_TYPE_SmallRoom; // ALC_ASA_REVERB_ROOM_TYPE_MediumHall2;
|
|
ALDEBUG(alcASASetListenerProc(ALC_ASA_REVERB_ROOM_TYPE, &setting, sizeof(setting)));
|
|
|
|
|
|
ALfloat global_reverb_eq_gain = 0.0f;
|
|
ALDEBUG(alcASASetListenerProc(ALC_ASA_REVERB_EQ_GAIN, &global_reverb_eq_gain, sizeof(global_reverb_eq_gain)));
|
|
|
|
ALfloat global_reverb_eq_bandwidth = 0.0f;
|
|
ALDEBUG(alcASASetListenerProc(ALC_ASA_REVERB_EQ_BANDWITH, &global_reverb_eq_bandwidth, sizeof(global_reverb_eq_bandwidth)));
|
|
|
|
ALfloat global_reverb_eq_freq = 0.0f;
|
|
ALDEBUG(alcASASetListenerProc(ALC_ASA_REVERB_EQ_FREQ, &global_reverb_eq_freq, sizeof(global_reverb_eq_freq)));
|
|
}
|
|
|
|
KRAudioManager::~KRAudioManager()
|
|
{
|
|
for(map<std::string, KRAudioSample *>::iterator name_itr=m_sounds.begin(); name_itr != m_sounds.end(); name_itr++) {
|
|
delete (*name_itr).second;
|
|
}
|
|
|
|
if(m_alContext) {
|
|
ALDEBUG(alcDestroyContext(m_alContext));
|
|
m_alContext = 0;
|
|
}
|
|
if(m_alDevice) {
|
|
ALDEBUG(alcCloseDevice(m_alDevice));
|
|
m_alDevice = 0;
|
|
}
|
|
|
|
for(std::vector<KRDataBlock *>::iterator itr = m_bufferPoolIdle.begin(); itr != m_bufferPoolIdle.end(); itr++) {
|
|
delete *itr;
|
|
}
|
|
}
|
|
|
|
void KRAudioManager::makeCurrentContext()
|
|
{
|
|
ALDEBUG(alcMakeContextCurrent(m_alContext));
|
|
}
|
|
|
|
void KRAudioManager::setViewMatrix(const KRMat4 &viewMatrix)
|
|
{
|
|
KRMat4 invView = viewMatrix;
|
|
invView.invert();
|
|
|
|
KRVector3 player_position = KRMat4::Dot(invView, KRVector3(0.0, 0.0, 0.0));
|
|
KRVector3 vectorForward = KRMat4::Dot(invView, KRVector3(0.0, 0.0, -1.0)) - player_position;
|
|
KRVector3 vectorUp = KRMat4::Dot(invView, KRVector3(0.0, 1.0, 0.0)) - player_position;
|
|
|
|
vectorUp.normalize();
|
|
vectorForward.normalize();
|
|
|
|
makeCurrentContext();
|
|
player_position = KRVector3(1.0, 0.0, 0.0); // FINDME - HACK - TEST CODE
|
|
ALDEBUG(alListener3f(AL_POSITION, player_position.x, player_position.y, player_position.z));
|
|
ALfloat orientation[] = {vectorForward.x, vectorForward.y, vectorForward.z, vectorUp.x, vectorUp.y, vectorUp.z};
|
|
ALDEBUG(alListenerfv(AL_ORIENTATION, orientation));
|
|
}
|
|
|
|
void KRAudioManager::add(KRAudioSample *sound)
|
|
{
|
|
std::string lower_name = sound->getName();
|
|
std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), ::tolower);
|
|
|
|
map<std::string, KRAudioSample *>::iterator name_itr = m_sounds.find(lower_name);
|
|
if(name_itr != m_sounds.end()) {
|
|
delete (*name_itr).second;
|
|
(*name_itr).second = sound;
|
|
} else {
|
|
m_sounds[lower_name] = sound;
|
|
}
|
|
}
|
|
|
|
KRAudioSample *KRAudioManager::load(const std::string &name, const std::string &extension, KRDataBlock *data)
|
|
{
|
|
KRAudioSample *Sound = new KRAudioSample(getContext(), name, extension, data);
|
|
if(Sound) add(Sound);
|
|
return Sound;
|
|
}
|
|
|
|
KRAudioSample *KRAudioManager::get(const std::string &name)
|
|
{
|
|
std::string lower_name = name;
|
|
std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), ::tolower);
|
|
return m_sounds[lower_name];
|
|
}
|
|
|
|
KRDataBlock *KRAudioManager::getBufferData(int size)
|
|
{
|
|
KRDataBlock *data;
|
|
// Note: We only store and recycle buffers with a size of CIRCA_AUDIO_MAX_BUFFER_SIZE
|
|
if(size == KRENGINE_AUDIO_MAX_BUFFER_SIZE && m_bufferPoolIdle.size() > 0) {
|
|
// Recycle a buffer from the pool
|
|
data = m_bufferPoolIdle.back();
|
|
m_bufferPoolIdle.pop_back();
|
|
} else {
|
|
data = new KRDataBlock();
|
|
data->expand(size);
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
void KRAudioManager::recycleBufferData(KRDataBlock *data)
|
|
{
|
|
if(data != NULL) {
|
|
if(data->getSize() == KRENGINE_AUDIO_MAX_BUFFER_SIZE && m_bufferPoolIdle.size() < KRENGINE_AUDIO_MAX_POOL_SIZE) {
|
|
m_bufferPoolIdle.push_back(data);
|
|
} else {
|
|
delete data;
|
|
}
|
|
}
|
|
}
|
|
|
|
OSStatus alcASASetListenerProc(const ALuint property, ALvoid *data, ALuint dataSize)
|
|
{
|
|
OSStatus err = noErr;
|
|
static alcASASetListenerProcPtr proc = NULL;
|
|
|
|
if (proc == NULL) {
|
|
proc = (alcASASetListenerProcPtr) alcGetProcAddress(NULL, "alcASASetListener");
|
|
}
|
|
|
|
if (proc)
|
|
err = proc(property, data, dataSize);
|
|
return (err);
|
|
}
|
|
|
|
ALvoid alcMacOSXRenderingQualityProc(const ALint value)
|
|
{
|
|
static alcMacOSXRenderingQualityProcPtr proc = NULL;
|
|
|
|
if (proc == NULL) {
|
|
proc = (alcMacOSXRenderingQualityProcPtr) alcGetProcAddress(NULL, (const ALCchar*) "alcMacOSXRenderingQuality");
|
|
}
|
|
|
|
if (proc)
|
|
proc(value);
|
|
|
|
return;
|
|
}
|