소스 검색

Added Sound initialization

Thomas Buck 11 년 전
부모
커밋
c5d2af8adb
4개의 변경된 파일91개의 추가작업 그리고 61개의 파일을 삭제
  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 파일 보기

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

+ 10
- 7
include/Sound.h 파일 보기

36
      * \brief Initialize sound system
36
      * \brief Initialize sound system
37
      * \returns 0 on success or < 0 error flags
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
      * \brief Get number of registered sources
50
      * \brief Get number of registered sources
50
     void clear();
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
      * \brief Move listener and repositions them
61
      * \brief Move listener and repositions them
60
      * \param pos New position for listener
62
      * \param pos New position for listener
61
      * \param angle New orientation for listener
63
      * \param angle New orientation for listener
102
 
104
 
103
 private:
105
 private:
104
 
106
 
107
+    bool mEnabled;
105
     bool mInit;                        //!< Guard to ensure ausio system is active
108
     bool mInit;                        //!< Guard to ensure ausio system is active
106
     float mVolume;                     //!< Listener gain
109
     float mVolume;                     //!< Listener gain
107
     std::vector<unsigned int> mBuffer; //!< Audio buffer id list
110
     std::vector<unsigned int> mBuffer; //!< Audio buffer id list

+ 14
- 2
src/OpenRaider.cpp 파일 보기

22
     mInit = false;
22
     mInit = false;
23
     mRunning = false;
23
     mRunning = false;
24
 
24
 
25
+    mSound = new Sound();
25
     mWindow = new WindowSDL();
26
     mWindow = new WindowSDL();
26
 }
27
 }
27
 
28
 
28
 OpenRaider::~OpenRaider() {
29
 OpenRaider::~OpenRaider() {
30
+    if (mSound)
31
+        delete mSound;
32
+
29
     if (mWindow)
33
     if (mWindow)
30
         delete mWindow;
34
         delete mWindow;
31
 }
35
 }
142
             printf("set-audio-Error: Invalid value (%s)\n", value);
146
             printf("set-audio-Error: Invalid value (%s)\n", value);
143
             return -4;
147
             return -4;
144
         }
148
         }
145
-        // TODO enable audio
149
+        mSound->setEnabled(audio);
146
     } else if (strcmp(var, "volume") == 0) {
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
     } else if (strcmp(var, "mouse_x") == 0) {
156
     } else if (strcmp(var, "mouse_x") == 0) {
149
         // TODO set
157
         // TODO set
150
     } else if (strcmp(var, "mouse_y") == 0) {
158
     } else if (strcmp(var, "mouse_y") == 0) {
207
     if (mWindow->initializeFont() != 0)
215
     if (mWindow->initializeFont() != 0)
208
         return -3;
216
         return -3;
209
 
217
 
218
+    // Initialize sound
219
+    if (mSound->initialize() != 0)
220
+        return -4;
221
+
210
     mInit = true;
222
     mInit = true;
211
 
223
 
212
     return 0;
224
     return 0;

+ 64
- 52
src/Sound.cpp 파일 보기

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

Loading…
취소
저장