Doppler effect

This commit is contained in:
Adam
2014-03-13 20:00:41 +01:00
parent 192589f9e5
commit 5afb3932e9
4 changed files with 35 additions and 9 deletions
+9
View File
@@ -19,6 +19,7 @@ void GameWorld::Initialize()
std::shared_ptr<Components::Camera> camera;
std::shared_ptr<Components::Bounds> bounds;
std::shared_ptr<Components::Collision> collision;
std::shared_ptr<Components::SoundEmitter> soundEmitter;
EntityID ent;
// Camera
@@ -103,6 +104,14 @@ void GameWorld::Initialize()
bounds = AddComponent<Components::Bounds>(m_Player, "Bounds");
bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3);
bounds->VolumeVector = glm::vec3(4.f,0.7f,4);
soundEmitter = AddComponent<Components::SoundEmitter>(m_Player, "SoundEmitter");
soundEmitter->Loop = true;
soundEmitter->MaxDistance = FLT_MAX;
soundEmitter->ReferenceDistance = 10;
soundEmitter->Gain = 1;
soundEmitter->Pitch = 1;
soundEmitter->Path = "Sounds/hallelujah.wav";
GetSystem<Systems::SoundSystem>("SoundSystem")->PlaySound(soundEmitter);
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
@@ -12,7 +12,6 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID
transform->Position[1] += parentTransform->Position[1];
transform->Position[2] += parentTransform->Position[2];
}
LOG_INFO("Updating entity %i with parent %i", entity, parent);
auto entities = m_World->GetEntities();
+3 -1
View File
@@ -20,7 +20,9 @@ public:
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) 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 path); // Use if you want to play a temporary .wav file not from component
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter); // Use if you want to play .wav file from component // imon no hate plx T.T
void StopSound(std::shared_ptr<Components::SoundEmitter> emitter);
private:
ALuint LoadFile(std::string fileName);
+23 -7
View File
@@ -72,14 +72,27 @@ void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID par
}
}
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 path)
{
ALuint buffer = LoadFile(fileName);
ALuint buffer = LoadFile(path);
ALuint source = m_Source[emitter.get()];
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(m_Source[emitter.get()]);
}
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter)
{
ALuint buffer = LoadFile(emitter->Path);
ALuint source = m_Source[emitter.get()];
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(m_Source[emitter.get()]);
}
void Systems::SoundSystem::StopSound(std::shared_ptr<Components::SoundEmitter> emitter)
{
alSourceStop(m_Source[emitter.get()]);
}
void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{
if(type == "SoundEmitter") {
@@ -88,13 +101,13 @@ void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<
}
}
ALuint Systems::SoundSystem::LoadFile(std::string fileName)
ALuint Systems::SoundSystem::LoadFile(std::string path)
{
if (m_BufferCache.find(fileName) != m_BufferCache.end())
return m_BufferCache[fileName];
if (m_BufferCache.find(path) != m_BufferCache.end())
return m_BufferCache[path];
FILE *fp = NULL;
fp = fopen(fileName.c_str(), "rb");
fp = fopen(path.c_str(), "rb");
//CHECK FOR VALID WAVE-FILE
fread(type, sizeof(char), 4, fp);
@@ -160,7 +173,7 @@ ALuint Systems::SoundSystem::LoadFile(std::string fileName)
alBufferData(buffer, format, buf, dataSize, sampleRate);
delete[] buf;
m_BufferCache[fileName] = buffer;
m_BufferCache[path] = buffer;
return buffer;
}
@@ -169,5 +182,8 @@ ALuint Systems::SoundSystem::CreateSource()
ALuint source;
alGenSources((ALuint)1, &source);
alDopplerFactor(2.f); // Numbers greater than 1 will increase Doppler effect, numbers lower than 1 will decrease the Doppler effect
alDopplerVelocity(350.f); // Defines the velocity of the sound
return source;
}