-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudio.cpp
More file actions
70 lines (58 loc) · 1.66 KB
/
Audio.cpp
File metadata and controls
70 lines (58 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include "Audio.h"
#define VOLUME 0.5f
Audio::Audio()
{
engine = irrklang::createIrrKlangDevice();
if (!engine) {
std::cerr << "Sound Engine error\n";
}
}
void Audio::UpdateListenerPosition(glm::vec3 position, glm::vec3 front, glm::vec3 world_up)
{
// position of the listener
irrklang::vec3df _position(position.x, position.y, position.z);
// the direction the listener looks into
irrklang::vec3df _look_direction(-front.x, front.y, -front.z);
// only relevant for doppler effects
irrklang::vec3df _vel_per_second(0, 0, 0);
// where 'up' is in your 3D scene
irrklang::vec3df _up_vector(world_up.x, world_up.y, world_up.z);
engine->setListenerPosition(_position, _look_direction, _vel_per_second, _up_vector);
}
void Audio::PlayMusic3D(const std::string& soundFile, float volume, bool repeat)
{
std::string fullPath = "assets/sounds/" + soundFile;
music = engine->play3D(fullPath.c_str(), irrklang::vec3df(0, 0, 0), repeat, true);
if (music) {
music->setMinDistance(1.0f);
music->setIsPaused(false);
music->setVolume(volume);
}
}
void Audio::UpdateMusicPosition(float posOnCircle, float radius)
{
irrklang::vec3df pos3d(radius * cosf(posOnCircle), 0, radius * sinf(posOnCircle * 0.5f));
engine->setListenerPosition(irrklang::vec3df(0, 0, 0), irrklang::vec3df(0, 0, 1));
if (music)
music->setPosition(pos3d);
}
void Audio::UpdateMusicVolume(float amount)
{
music->setVolume(amount * VOLUME);
}
void Audio::Walk()
{
engine->play2D(walk_sound ? "./assets/sounds/step1.wav" : "./assets/sounds/step2.wav");
walk_sound = !walk_sound;
}
Audio::~Audio()
{
if (music) {
music->stop();
music->drop();
}
if (engine) {
engine->drop();
}
}