Audio update

OpenAL is now loaded dynamically
Added (back) Audio::[Get|Set]ListenerRotation


Former-commit-id: d99ba9f15ec250f3963cd50d6a4a892209235c4a
This commit is contained in:
Lynix
2014-01-08 11:06:40 +01:00
parent 9ac4df3c6c
commit 26a6158f8f
7 changed files with 692 additions and 109 deletions

View File

@@ -3,23 +3,15 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Audio/Audio.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/Enums.hpp>
#include <Nazara/Audio/OpenAL.hpp>
#include <Nazara/Audio/Loaders/sndfile.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/Enums.hpp>
#include <Nazara/Audio/Loaders/sndfile.hpp>
#include <AL/al.h>
#include <AL/alc.h>
#include <Nazara/Audio/Debug.hpp>
namespace
{
ALenum formats[nzAudioFormat_Max+1] = {0};
ALCdevice* device = nullptr;
ALCcontext* context = nullptr;
}
nzAudioFormat NzAudio::GetAudioFormat(unsigned int channelCount)
{
switch (channelCount)
@@ -66,26 +58,17 @@ NzVector3f NzAudio::GetListenerPosition()
return position;
}
/*
NzQuaternionf NzAudio::GetListenerRotation()
{
// http://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another
float orientation[6];
alGetListenerfv(AL_ORIENTATION, orientation);
NzVector3f forward(orientation[0], orientation[1], orientation[2]);
NzVector3f up(orientation[3], orientation[4], orientation[5]);
NzQuaternionf rotation;
NzVector3f a = NzVector3f::CrossProduct(forward, up);
rotation.x = a.x;
rotation.y = a.y;
rotation.z = a.z;
rotation.w = std::sqrt(forward.SquaredLength() * up.SquaredLength()) + NzVector3f::DotProduct(forward, up);
return rotation;
return NzQuaternionf::RotationBetween(NzVector3f::Forward(), forward);
}
*/
NzVector3f NzAudio::GetListenerVelocity()
{
NzVector3f velocity;
@@ -113,54 +96,17 @@ bool NzAudio::Initialize()
return false;
}
// Initialisation du module
device = alcOpenDevice(nullptr); // On choisit le device par défaut
if (!device)
// Initialisation d'OpenGL
if (!NzOpenAL::Initialize())
{
NazaraError("Failed to open default device");
NazaraError("Failed to initialize OpenAL");
Uninitialize();
return false;
}
// Un seul contexte nous suffira
context = alcCreateContext(device, nullptr);
if (!context)
{
NazaraError("Failed to create context");
Uninitialize();
return false;
}
if (!alcMakeContextCurrent(context))
{
NazaraError("Failed to activate context");
Uninitialize();
return false;
}
// Définition de l'orientation
/*{
NzVector3f forward = NzVector3f::Forward();
NzVector3f up = NzVector3f::Up();
ALfloat orientation[6] =
{
forward.x, forward.y, forward.z,
up.x, up.y, up.z
};
alListenerfv(AL_ORIENTATION, orientation);
}*/
formats[nzAudioFormat_Mono] = AL_FORMAT_MONO16;
formats[nzAudioFormat_Stereo] = AL_FORMAT_STEREO16;
formats[nzAudioFormat_Quad] = alGetEnumValue("AL_FORMAT_QUAD16");
formats[nzAudioFormat_5_1] = alGetEnumValue("AL_FORMAT_51CHN16");
formats[nzAudioFormat_6_1] = alGetEnumValue("AL_FORMAT_61CHN16");
formats[nzAudioFormat_7_1] = alGetEnumValue("AL_FORMAT_71CHN16");
// Définition de l'orientation par défaut
SetListenerDirection(NzVector3f::Forward());
// Loaders
NzLoaders_sndfile_Register();
@@ -175,7 +121,7 @@ bool NzAudio::IsFormatSupported(nzAudioFormat format)
if (format == nzAudioFormat_Unknown)
return false;
return formats[format] != 0;
return NzOpenAL::AudioFormat[format] != 0;
}
bool NzAudio::IsInitialized()
@@ -195,10 +141,12 @@ void NzAudio::SetGlobalVolume(float volume)
void NzAudio::SetListenerDirection(const NzVector3f& direction)
{
NzVector3f up = NzVector3f::Up();
ALfloat orientation[6] =
{
direction.x, direction.y, direction.z,
0.f, 1.f, 0.f
up.x, up.y, up.z
};
alListenerfv(AL_ORIENTATION, orientation);
@@ -206,10 +154,12 @@ void NzAudio::SetListenerDirection(const NzVector3f& direction)
void NzAudio::SetListenerDirection(float dirX, float dirY, float dirZ)
{
NzVector3f up = NzVector3f::Up();
ALfloat orientation[6] =
{
dirX, dirY, dirZ,
0.f, 1.f, 0.f
up.x, up.y, up.z
};
alListenerfv(AL_ORIENTATION, orientation);
@@ -224,7 +174,7 @@ void NzAudio::SetListenerPosition(float x, float y, float z)
{
alListener3f(AL_POSITION, x, y, z);
}
/*
void NzAudio::SetListenerRotation(const NzQuaternionf& rotation)
{
NzVector3f forward = rotation * NzVector3f::Forward();
@@ -238,7 +188,7 @@ void NzAudio::SetListenerRotation(const NzQuaternionf& rotation)
alListenerfv(AL_ORIENTATION, orientation);
}
*/
void NzAudio::SetListenerVelocity(const NzVector3f& velocity)
{
alListenerfv(AL_VELOCITY, velocity);
@@ -271,19 +221,7 @@ void NzAudio::Uninitialize()
// Loaders
NzLoaders_sndfile_Unregister();
// Libération d'OpenAL
if (device)
{
if (context)
{
alcMakeContextCurrent(nullptr);
alcDestroyContext(context);
}
if (!alcCloseDevice(device))
// Nous n'avons pas pu fermer le device, ce qui signifie qu'il est en cours d'utilisation
NazaraWarning("Failed to close device");
}
NzOpenAL::Uninitialize();
NazaraNotice("Uninitialized: Audio module");
@@ -291,17 +229,4 @@ void NzAudio::Uninitialize()
NzCore::Uninitialize();
}
unsigned int NzAudio::GetOpenALFormat(nzAudioFormat format)
{
#ifdef NAZARA_DEBUG
if (format == nzAudioFormat_Unknown)
{
NazaraInternalError("Invalid audio format");
return 0;
}
#endif
return formats[format];
}
unsigned int NzAudio::s_moduleReferenceCounter = 0;