Browse Source

Added Sound initialization

Thomas Buck 11 years ago
parent
commit
c5d2af8adb
4 changed files with 91 additions and 61 deletions
  1. 3
    0
      include/OpenRaider.h
  2. 10
    7
      include/Sound.h
  3. 14
    2
      src/OpenRaider.cpp
  4. 64
    52
      src/Sound.cpp

+ 3
- 0
include/OpenRaider.h View File

@@ -10,6 +10,7 @@
10 10
 
11 11
 #include <vector>
12 12
 
13
+#include "Sound.h"
13 14
 #include "Window.h"
14 15
 
15 16
 /*!
@@ -50,6 +51,8 @@ private:
50 51
     bool mInit;
51 52
     bool mRunning;
52 53
     Window *mWindow;
54
+
55
+    Sound *mSound;
53 56
 };
54 57
 
55 58
 #endif

+ 10
- 7
include/Sound.h View File

@@ -36,7 +36,15 @@ public:
36 36
      * \brief Initialize sound system
37 37
      * \returns 0 on success or < 0 error flags
38 38
      */
39
-    int init();
39
+    int initialize();
40
+
41
+    void setEnabled(bool on);
42
+
43
+    /*!
44
+     * \brief Set the volume
45
+     * \param vol new source gain
46
+     */
47
+    void setVolume(float vol);
40 48
 
41 49
     /*!
42 50
      * \brief Get number of registered sources
@@ -50,12 +58,6 @@ public:
50 58
     void clear();
51 59
 
52 60
     /*!
53
-     * \brief Set the volume
54
-     * \param vol new source gain
55
-     */
56
-    void setVolume(float vol);
57
-
58
-    /*!
59 61
      * \brief Move listener and repositions them
60 62
      * \param pos New position for listener
61 63
      * \param angle New orientation for listener
@@ -102,6 +104,7 @@ public:
102 104
 
103 105
 private:
104 106
 
107
+    bool mEnabled;
105 108
     bool mInit;                        //!< Guard to ensure ausio system is active
106 109
     float mVolume;                     //!< Listener gain
107 110
     std::vector<unsigned int> mBuffer; //!< Audio buffer id list

+ 14
- 2
src/OpenRaider.cpp View File

@@ -22,10 +22,14 @@ OpenRaider::OpenRaider() {
22 22
     mInit = false;
23 23
     mRunning = false;
24 24
 
25
+    mSound = new Sound();
25 26
     mWindow = new WindowSDL();
26 27
 }
27 28
 
28 29
 OpenRaider::~OpenRaider() {
30
+    if (mSound)
31
+        delete mSound;
32
+
29 33
     if (mWindow)
30 34
         delete mWindow;
31 35
 }
@@ -142,9 +146,13 @@ int OpenRaider::set(const char *var, const char *value) {
142 146
             printf("set-audio-Error: Invalid value (%s)\n", value);
143 147
             return -4;
144 148
         }
145
-        // TODO enable audio
149
+        mSound->setEnabled(audio);
146 150
     } else if (strcmp(var, "volume") == 0) {
147
-        // TODO set volume
151
+        float vol = 1.0f;
152
+        if (sscanf(value, "%f", &vol) != 1) {
153
+            printf("set-volume-Error: Invalid value (%s)\n", value);
154
+        }
155
+        mSound->setVolume(vol);
148 156
     } else if (strcmp(var, "mouse_x") == 0) {
149 157
         // TODO set
150 158
     } else if (strcmp(var, "mouse_y") == 0) {
@@ -207,6 +215,10 @@ int OpenRaider::initialize() {
207 215
     if (mWindow->initializeFont() != 0)
208 216
         return -3;
209 217
 
218
+    // Initialize sound
219
+    if (mSound->initialize() != 0)
220
+        return -4;
221
+
210 222
     mInit = true;
211 223
 
212 224
     return 0;

+ 64
- 52
src/Sound.cpp View File

@@ -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
 

Loading…
Cancel
Save