|
@@ -10,24 +10,21 @@
|
10
|
10
|
#include <OpenAL/al.h>
|
11
|
11
|
#else
|
12
|
12
|
#include <AL/al.h>
|
|
13
|
+#include <fcntl.h>
|
|
14
|
+#include <unistd.h>
|
13
|
15
|
#endif
|
14
|
16
|
|
15
|
17
|
#include <AL/alut.h>
|
16
|
18
|
|
17
|
|
-#include <time.h>
|
18
|
|
-#include <stdio.h>
|
19
|
|
-#include <stdlib.h>
|
20
|
|
-#include <sys/time.h>
|
21
|
|
-#include <sys/types.h>
|
22
|
|
-#include <sys/stat.h>
|
23
|
|
-#include <fcntl.h>
|
24
|
|
-#include <unistd.h>
|
|
19
|
+#include <cstdio>
|
|
20
|
+#include <cstdlib>
|
25
|
21
|
#include <assert.h>
|
26
|
22
|
|
27
|
23
|
#include "math/math.h"
|
28
|
24
|
#include "Sound.h"
|
29
|
25
|
|
30
|
26
|
Sound::Sound() {
|
|
27
|
+ mEnabled = false;
|
31
|
28
|
mInit = false;
|
32
|
29
|
mVolume = 1.0f;
|
33
|
30
|
}
|
|
@@ -37,19 +34,18 @@ Sound::~Sound() {
|
37
|
34
|
alutExit();
|
38
|
35
|
}
|
39
|
36
|
|
40
|
|
-int Sound::init() {
|
|
37
|
+int Sound::initialize() {
|
41
|
38
|
assert(mInit == false);
|
42
|
39
|
|
43
|
|
-#ifndef __APPLE__
|
44
|
|
- int fd;
|
45
|
|
-
|
46
|
|
- fd = open("/dev/dsp", O_RDWR);
|
|
40
|
+ if (!mEnabled)
|
|
41
|
+ return 0;
|
47
|
42
|
|
|
43
|
+#ifndef __APPLE__
|
|
44
|
+ int fd = open("/dev/dsp", O_RDWR);
|
48
|
45
|
if (fd < 0) {
|
49
|
|
- perror("Sound::Init> Could not open /dev/dsp : ");
|
|
46
|
+ printf("Could not initialize Sound (/dev/dsp)\n");
|
50
|
47
|
return -1;
|
51
|
48
|
}
|
52
|
|
-
|
53
|
49
|
close(fd);
|
54
|
50
|
#endif
|
55
|
51
|
|
|
@@ -58,27 +54,48 @@ int Sound::init() {
|
58
|
54
|
alcMakeContextCurrent(Context);
|
59
|
55
|
|
60
|
56
|
if (alutInitWithoutContext(NULL, NULL) == AL_FALSE) {
|
61
|
|
- printf("Sound::Init> Could not initialize alut (%s)\n", alutGetErrorString(alutGetError()));
|
|
57
|
+ printf("ALUT-Error: %s\n", alutGetErrorString(alutGetError()));
|
62
|
58
|
return -2;
|
63
|
59
|
}
|
64
|
60
|
|
65
|
61
|
mInit = true;
|
66
|
|
- printf("Created OpenAL Context\n");
|
67
|
62
|
|
68
|
63
|
return 0;
|
69
|
64
|
}
|
70
|
65
|
|
|
66
|
+void Sound::setEnabled(bool on) {
|
|
67
|
+ mEnabled = on;
|
|
68
|
+}
|
|
69
|
+
|
|
70
|
+void Sound::setVolume(float vol) {
|
|
71
|
+ assert(mSource.size() == mBuffer.size());
|
|
72
|
+
|
|
73
|
+ if (mEnabled) {
|
|
74
|
+ if ((mSource.size() > 0) && (!equalEpsilon(mVolume, vol))) {
|
|
75
|
+ // Apply new volume to old sources if needed
|
|
76
|
+ for (size_t i = 0; i < mSource.size(); i++)
|
|
77
|
+ alSourcef(mSource[i], AL_GAIN, vol);
|
|
78
|
+ }
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ mVolume = vol;
|
|
82
|
+}
|
|
83
|
+
|
71
|
84
|
int Sound::registeredSources() {
|
72
|
|
- assert(mInit == true);
|
73
|
85
|
assert(mSource.size() == mBuffer.size());
|
74
|
86
|
|
|
87
|
+ if ((!mEnabled) || (!mInit))
|
|
88
|
+ return 0;
|
|
89
|
+
|
75
|
90
|
return mSource.size();
|
76
|
91
|
}
|
77
|
92
|
|
78
|
93
|
void Sound::clear() {
|
79
|
|
- assert(mInit == true);
|
80
|
94
|
assert(mSource.size() == mBuffer.size());
|
81
|
95
|
|
|
96
|
+ if ((!mEnabled) || (!mInit))
|
|
97
|
+ return;
|
|
98
|
+
|
82
|
99
|
for (size_t i = 0; i < mSource.size(); i++) {
|
83
|
100
|
alDeleteSources(1, &mSource[i]);
|
84
|
101
|
alDeleteBuffers(1, &mBuffer[i]);
|
|
@@ -88,36 +105,27 @@ void Sound::clear() {
|
88
|
105
|
mBuffer.clear();
|
89
|
106
|
}
|
90
|
107
|
|
91
|
|
-void Sound::setVolume(float vol) {
|
92
|
|
- assert(mInit == true);
|
93
|
|
- assert(mSource.size() == mBuffer.size());
|
94
|
|
-
|
95
|
|
- if ((mSource.size() > 0) && (!equalEpsilon(mVolume, vol))) {
|
96
|
|
- // Apply new volume to old sources if needed
|
97
|
|
- for (size_t i = 0; i < mSource.size(); i++)
|
98
|
|
- alSourcef(mSource[i], AL_GAIN, vol);
|
99
|
|
- }
|
100
|
|
-
|
101
|
|
- mVolume = vol;
|
102
|
|
-}
|
103
|
|
-
|
104
|
108
|
void Sound::listenAt(float pos[3], float angle[3]) {
|
105
|
|
- assert(mInit == true);
|
106
|
109
|
assert(mSource.size() == mBuffer.size());
|
107
|
110
|
assert(pos != NULL);
|
108
|
111
|
assert(angle != NULL);
|
109
|
112
|
|
|
113
|
+ if ((!mEnabled) || (!mInit))
|
|
114
|
+ return;
|
|
115
|
+
|
110
|
116
|
alListenerfv(AL_POSITION, pos);
|
111
|
117
|
alListenerfv(AL_ORIENTATION, angle);
|
112
|
118
|
}
|
113
|
119
|
|
114
|
120
|
void Sound::sourceAt(int source, float pos[3]) {
|
115
|
|
- assert(mInit == true);
|
116
|
121
|
assert(mSource.size() == mBuffer.size());
|
117
|
122
|
assert(source >= 0);
|
118
|
123
|
assert(source < (int)mSource.size());
|
119
|
124
|
assert(pos != NULL);
|
120
|
125
|
|
|
126
|
+ if ((!mEnabled) || (!mInit))
|
|
127
|
+ return;
|
|
128
|
+
|
121
|
129
|
alSourcefv(mSource[source], AL_POSITION, pos);
|
122
|
130
|
}
|
123
|
131
|
|
|
@@ -129,34 +137,36 @@ int Sound::addFile(const char *filename, int *source, unsigned int flags) {
|
129
|
137
|
ALvoid *data;
|
130
|
138
|
int id;
|
131
|
139
|
|
132
|
|
- assert(mInit == true);
|
133
|
140
|
assert(mSource.size() == mBuffer.size());
|
134
|
141
|
assert(filename != NULL);
|
135
|
142
|
assert(filename[0] != '\0');
|
136
|
143
|
assert(source != NULL);
|
137
|
144
|
|
|
145
|
+ if ((!mEnabled) || (!mInit)) {
|
|
146
|
+ *source = 0;
|
|
147
|
+ return 0;
|
|
148
|
+ }
|
|
149
|
+
|
138
|
150
|
*source = -1;
|
139
|
151
|
id = mSource.size();
|
140
|
152
|
|
141
|
153
|
alGetError();
|
142
|
154
|
alGenBuffers(1, &mBuffer[id]);
|
143
|
155
|
if (alGetError() != AL_NO_ERROR) {
|
144
|
|
- fprintf(stderr, "Sound::AddFile> alGenBuffers call failed\n");
|
|
156
|
+ printf("Could not load wav file (alGenBuffers)\n");
|
145
|
157
|
return -1;
|
146
|
158
|
}
|
147
|
159
|
|
148
|
160
|
alGetError();
|
149
|
161
|
alGenSources(1, &mSource[id]);
|
150
|
162
|
if (alGetError() != AL_NO_ERROR) {
|
151
|
|
- fprintf(stderr, "Sound::AddFile> alGenSources call failed\n");
|
|
163
|
+ printf("Could not load wav file (alGenSources)\n");
|
152
|
164
|
return -2;
|
153
|
165
|
}
|
154
|
166
|
|
155
|
|
- // err = alutLoadWAV(filename, &data, &format, &size, &bits, &freq);
|
156
|
|
- // is deprecated!
|
157
|
167
|
data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
|
158
|
168
|
if (alutGetError() != ALUT_ERROR_NO_ERROR) {
|
159
|
|
- fprintf(stderr, "Could not load %s\n", filename);
|
|
169
|
+ printf("Could not load %s\n", filename);
|
160
|
170
|
return -3;
|
161
|
171
|
}
|
162
|
172
|
|
|
@@ -182,11 +192,15 @@ int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigne
|
182
|
192
|
int error = 0;
|
183
|
193
|
int id;
|
184
|
194
|
|
185
|
|
- assert(mInit == true);
|
186
|
195
|
assert(mSource.size() == mBuffer.size());
|
187
|
196
|
assert(wav != NULL);
|
188
|
197
|
assert(source != NULL);
|
189
|
198
|
|
|
199
|
+ if ((!mEnabled) || (!mInit)) {
|
|
200
|
+ *source = 0;
|
|
201
|
+ return 0;
|
|
202
|
+ }
|
|
203
|
+
|
190
|
204
|
*source = -1;
|
191
|
205
|
id = mSource.size();
|
192
|
206
|
|
|
@@ -195,26 +209,20 @@ int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigne
|
195
|
209
|
alGetError();
|
196
|
210
|
alGenBuffers(1, &mBuffer[id]);
|
197
|
211
|
if (alGetError() != AL_NO_ERROR) {
|
198
|
|
- fprintf(stderr, "Sound::AddWave> alGenBuffers call failed\n");
|
|
212
|
+ printf("Could not load wav (alGenBuffers)\n");
|
199
|
213
|
return -1;
|
200
|
214
|
}
|
201
|
215
|
|
202
|
216
|
alGetError();
|
203
|
217
|
alGenSources(1, &mSource[id]);
|
204
|
218
|
if (alGetError() != AL_NO_ERROR) {
|
205
|
|
- fprintf(stderr, "Sound::AddWave> alGenSources call failed\n");
|
|
219
|
+ printf("Could not load wav (alGenSources)\n");
|
206
|
220
|
return -2;
|
207
|
221
|
}
|
208
|
222
|
|
209
|
|
- //AL_FORMAT_WAVE_EXT does not exist on Mac!"
|
210
|
|
- // alBufferData(mBuffer[id], AL_FORMAT_WAVE_EXT, data, size, freq);
|
211
|
|
- // Idea: Fill Buffer with
|
212
|
|
- // alutLoadMemoryFromFileImage
|
213
|
|
- // (const ALvoid *data, ALsizei length, ALenum *format, ALsizei *size, ALfloat *frequency)
|
214
|
|
-
|
215
|
223
|
data = alutLoadMemoryFromFileImage(wav, length, &format, &size, &freq);
|
216
|
224
|
if (((error = alutGetError()) != ALUT_ERROR_NO_ERROR) || (data == NULL)) {
|
217
|
|
- fprintf(stderr, "Could not load wav buffer (%s)\n", alutGetErrorString(error));
|
|
225
|
+ printf("Could not load wav buffer (%s)\n", alutGetErrorString(error));
|
218
|
226
|
return -3;
|
219
|
227
|
}
|
220
|
228
|
|
|
@@ -235,20 +243,24 @@ int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigne
|
235
|
243
|
}
|
236
|
244
|
|
237
|
245
|
void Sound::play(int source) {
|
238
|
|
- assert(mInit == true);
|
239
|
246
|
assert(mSource.size() == mBuffer.size());
|
240
|
247
|
assert(source >= 0);
|
241
|
248
|
assert(source < (int)mSource.size());
|
242
|
249
|
|
|
250
|
+ if ((!mEnabled) || (!mInit))
|
|
251
|
+ return;
|
|
252
|
+
|
243
|
253
|
alSourcePlay(mSource[source]);
|
244
|
254
|
}
|
245
|
255
|
|
246
|
256
|
void Sound::stop(int source) {
|
247
|
|
- assert(mInit == true);
|
248
|
257
|
assert(mSource.size() == mBuffer.size());
|
249
|
258
|
assert(source >= 0);
|
250
|
259
|
assert(source < (int)mSource.size());
|
251
|
260
|
|
|
261
|
+ if ((!mEnabled) || (!mInit))
|
|
262
|
+ return;
|
|
263
|
+
|
252
|
264
|
alSourceStop(mSource[source]);
|
253
|
265
|
}
|
254
|
266
|
|