-
Notifications
You must be signed in to change notification settings - Fork 0
/
Audio.hpp
131 lines (110 loc) · 3.28 KB
/
Audio.hpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "Parameters.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ao/ao.h>
#include <sndfile.h>
#include <mutex>
#define BUFFER_SIZE 4096
struct Sound_State {
int gameState;
bool reload;
std::mutex mutex;
bool stop;
};
class Audio {
protected:
ao_device *devices[3];
ao_sample_format formats[3];
SF_INFO sfinfo[3];
SNDFILE *infile[3];
bool dones[3];
int default_driver;
Sound_State *sound_state;
public:
Audio(Sound_State *sound_state) {
ao_initialize();
default_driver = ao_default_driver_id();
memset(&sfinfo, 0, sizeof(SF_INFO));
infile[0] = sf_open("sounds/start.wav", SFM_READ, &sfinfo[0]);
infile[1] = sf_open("sounds/going.wav", SFM_READ, &sfinfo[1]);
infile[2] = sf_open("sounds/crash.wav", SFM_READ, &sfinfo[2]);
dones[0] = false;
dones[1] = false;
dones[2] = false;
for(int i = 0; i < 3; i++){
formats[i].bits = 16;
formats[i].channels = sfinfo[0].channels;
formats[i].rate = sfinfo[0].samplerate;
formats[i].byte_format = AO_FMT_NATIVE;
formats[i].matrix = nullptr;
devices[i] = ao_open_live(default_driver, &formats[i], nullptr);
}
this->sound_state = sound_state;
}
~Audio() {
sf_close(infile[0]);
sf_close(infile[1]);
sf_close(infile[2]);
for(ao_device *device : devices){
ao_close(device);
}
ao_shutdown();
}
void play(int sound) {
int16_t buffer[BUFFER_SIZE];
if (dones[sound]) {
if (sound == 1) {
sf_seek(infile[1], 0, SEEK_SET);
dones[1] = false;
} else {
return;
}
}
sf_count_t items_read = sf_read_short(infile[sound], buffer, BUFFER_SIZE);
if (items_read > 0) {
ao_play(devices[sound], (char *) buffer, items_read * sizeof(int16_t));
} else {
dones[sound] = true;
}
}
void start() {
int gameState;
bool reload;
bool stop;
sound_state->mutex.lock();
stop = sound_state->stop;
gameState = sound_state->gameState;
reload = sound_state->reload;
sound_state->mutex.unlock();
while (!stop) {
if (reload) {
sf_seek(infile[0], 0, SEEK_SET);
sf_seek(infile[1], 0, SEEK_SET);
sf_seek(infile[2], 0, SEEK_SET);
for(bool &done : dones){
done = false;
}
sound_state->mutex.lock();
sound_state->reload = false;
sound_state->mutex.unlock();
}
switch (gameState) {
case INITIAL_SCREEN:
play(0);
break;
case GAME_SCREEN:
play(1);
break;
case GAME_OVER_ANIMATION:
play(2);
break;
}
sound_state->mutex.lock();
stop = sound_state->stop;
gameState = sound_state->gameState;
reload = sound_state->reload;
sound_state->mutex.unlock();
}
}
};