Soundsystem ALMOST DONE

This commit is contained in:
Adam
2014-03-10 01:58:29 +01:00
parent ba6cc660ca
commit 720d9d8776
3 changed files with 63 additions and 29 deletions
+5 -2
View File
@@ -10,8 +10,11 @@ namespace Components
struct SoundEmitter : Component struct SoundEmitter : Component
{ {
float Volume; float Gain;
float MaxRange; float MaxDistance;
float ReferenceDistance;
float Pitch;
bool Loop;
}; };
} }
+9 -4
View File
@@ -5,11 +5,14 @@
#include "AL/alc.h" #include "AL/alc.h"
#include "System.h" #include "System.h"
#include "Components/Transform.h"
#include "Components/SoundEmitter.h" #include "Components/SoundEmitter.h"
#include <AL/al.h> #include <AL/al.h>
#include <AL/alc.h> #include <AL/alc.h>
#include <windows.h> #include <windows.h>
#include <vector> #include <vector>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
namespace Systems namespace Systems
{ {
@@ -24,20 +27,22 @@ public:
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override; void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName); void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName);
void LoadFile(const char* fileName); void LoadFile(const char* fileName);
void CreateSource(int ID, float pos[3], ALboolean looping); ALuint CreateSource();
private: private:
ALCcontext* context; ALCcontext* context;
//File-info
char type[4]; char type[4];
DWORD size, chunkSize; DWORD size, chunkSize;
short formatType, channels; short formatType, channels;
DWORD sampleRate, avgBytesPerSec; DWORD sampleRate, avgBytesPerSec;
short bytesPerSample, bitsPerSample; short bytesPerSample, bitsPerSample;
DWORD dataSize; DWORD dataSize;
unsigned char* buf; unsigned char* buf;
std::vector<ALuint> source; std::map<Component*, ALuint> source;
std::map<std::string, ALuint> buffer; // string = fileName
}; };
} }
+48 -22
View File
@@ -25,16 +25,59 @@ void Systems::SoundSystem::Update(double dt)
void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{ {
auto soundEmitter = m_World->GetComponent<Components::SoundEmitter>(entity, "SoundEmitter"); auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
if(soundEmitter == nullptr) if (transformComponent == nullptr)
return; return;
auto entityName = m_World->GetProperty<std::string>(entity, "Name");
if (entityName == "Player")
{
glm::vec3 playerPos = transformComponent->Position;
ALfloat listenerPos[3] = { playerPos.x, playerPos.y, playerPos.z };
glm::vec3 playerVel = transformComponent->Velocity;
ALfloat listenerVel[3] = { playerVel.x, playerVel.y, playerVel.z };
glm::fquat playerQuatOri = transformComponent->Orientation;
glm::vec3 playerOriFW = glm::rotate(playerQuatOri, glm::vec3(0, 0, -1));
glm::vec3 playerOriUP = glm::rotate(playerQuatOri, glm::vec3(0, 1, 0));
ALfloat listenerOri[6] = { playerOriFW.x, playerOriFW.y, playerOriFW.z, playerOriUP.x, playerOriUP.y, playerOriUP.z };
//Listener
alListenerfv(AL_POSITION, listenerPos);
alListenerfv(AL_VELOCITY, listenerVel);
alListenerfv(AL_ORIENTATION, listenerOri);
}
auto soundEmitter = m_World->GetComponent<Components::SoundEmitter>(entity, "SoundEmitter");
if(soundEmitter != nullptr)
{
alSourcef(source[soundEmitter.get()], AL_GAIN, soundEmitter->Gain); //Volume
alSourcei(source[soundEmitter.get()], AL_MAX_DISTANCE, soundEmitter->MaxDistance);
alSourcei(source[soundEmitter.get()], AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance);
alSourcef(source[soundEmitter.get()], AL_PITCH, soundEmitter->Pitch);
alSourcei(source[soundEmitter.get()], AL_LOOPING, soundEmitter->Loop);
glm::vec3 emitterPos = transformComponent->Position;
ALfloat sourcePos[3] = { emitterPos.x, emitterPos.y, emitterPos.z };
glm::vec3 emitterVel= transformComponent->Position;
ALfloat sourceVel[3] = { emitterVel.x, emitterVel.y, emitterVel.z };
alSourcefv(source[soundEmitter.get()], AL_POSITION, sourcePos);
alSourcefv(source[soundEmitter.get()], AL_VELOCITY, sourceVel);
}
} }
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName) void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName)
{ {
//alSourcePlay(source); alSourcePlay(source[emitter.get()]);
}
void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{
if(type == "SoundEmitter")
source[component.get()] = CreateSource();
} }
void Systems::SoundSystem::LoadFile(const char* fileName) void Systems::SoundSystem::LoadFile(const char* fileName)
@@ -77,7 +120,7 @@ void Systems::SoundSystem::LoadFile(const char* fileName)
fclose(fp); fclose(fp);
} }
void Systems::SoundSystem::CreateSource(int ID, float pos[3], ALboolean looping) ALuint Systems::SoundSystem::CreateSource()
{ {
ALuint source; ALuint source;
ALuint buffer; ALuint buffer;
@@ -105,22 +148,5 @@ void Systems::SoundSystem::CreateSource(int ID, float pos[3], ALboolean looping)
alBufferData(buffer, format, buf, dataSize, frequency); alBufferData(buffer, format, buf, dataSize, frequency);
ALfloat SourcePos[] = { pos[0], pos[1], pos[2] }; return source;
ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; // Playerpos? Camerapos?
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
ALfloat ListenerOri[] = { 0.0, 0.0, -1.0 , 0.0, 1.0, 0.0 }; //two vectors, one defining where the listener points, and one "up" vector
//Listener
alListenerfv(AL_POSITION, ListenerPos);
alListenerfv(AL_VELOCITY, ListenerVel);
alListenerfv(AL_ORIENTATION, ListenerOri);
//Source
alSourcei(source, AL_BUFFER, buffer);
alSourcef(source, AL_PITCH, 1.0f);
alSourcef(source, AL_GAIN, 1.0f); //Volume
alSourcefv(source, AL_POSITION, SourcePos);
alSourcefv(source, AL_VELOCITY, SourceVel);
alSourcei(source, AL_LOOPING, looping);
} }