Преглед на файлове

Added volume setting to Sound

Thomas Buck преди 11 години
родител
ревизия
a31ca8c3a6
променени са 2 файла, в които са добавени 45 реда и са изтрити 18 реда
  1. 12
    5
      include/Sound.h
  2. 33
    13
      src/Sound.cpp

+ 12
- 5
include/Sound.h Целия файл

@@ -45,6 +45,17 @@ public:
45 45
     int registeredSources();
46 46
 
47 47
     /*!
48
+     * \brief Remove all loaded sounds
49
+     */
50
+    void clear();
51
+
52
+    /*!
53
+     * \brief Set the volume
54
+     * \param vol new source gain
55
+     */
56
+    void setVolume(float vol);
57
+
58
+    /*!
48 59
      * \brief Move listener and repositions them
49 60
      * \param pos New position for listener
50 61
      * \param angle New orientation for listener
@@ -78,11 +89,6 @@ public:
78 89
     int addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags);
79 90
 
80 91
     /*!
81
-     * \brief Remove all loaded sounds
82
-     */
83
-    void clear();
84
-
85
-    /*!
86 92
      * \brief Play sound source
87 93
      * \param source sound source to play
88 94
      */
@@ -97,6 +103,7 @@ public:
97 103
 private:
98 104
 
99 105
     bool mInit;                        //!< Guard to ensure ausio system is active
106
+    float mVolume;                     //!< Listener gain
100 107
     std::vector<unsigned int> mBuffer; //!< Audio buffer id list
101 108
     std::vector<unsigned int> mSource; //!< Audio source id list
102 109
 };

+ 33
- 13
src/Sound.cpp Целия файл

@@ -28,6 +28,7 @@
28 28
 
29 29
 Sound::Sound() {
30 30
     mInit = false;
31
+    mVolume = 1.0f;
31 32
 }
32 33
 
33 34
 Sound::~Sound() {
@@ -36,6 +37,8 @@ Sound::~Sound() {
36 37
 }
37 38
 
38 39
 int Sound::init() {
40
+    assert(mInit == false);
41
+
39 42
 #ifndef __APPLE__
40 43
     int fd;
41 44
 
@@ -71,6 +74,32 @@ int Sound::registeredSources() {
71 74
     return mSource.size();
72 75
 }
73 76
 
77
+void Sound::clear() {
78
+    assert(mInit == true);
79
+    assert(mSource.size() == mBuffer.size());
80
+
81
+    for (size_t i = 0; i < mSource.size(); i++) {
82
+        alDeleteSources(1, &mSource[i]);
83
+        alDeleteBuffers(1, &mBuffer[i]);
84
+    }
85
+
86
+    mSource.clear();
87
+    mBuffer.clear();
88
+}
89
+
90
+void Sound::setVolume(float vol) {
91
+    assert(mInit == true);
92
+    assert(mSource.size() == mBuffer.size());
93
+
94
+    if ((mSource.size() > 0) && (mVolume != vol)) {
95
+        // Apply new volume to old sources if needed
96
+        for (size_t i = 0; i < mSource.size(); i++)
97
+            alSourcef(mSource[i], AL_GAIN, vol);
98
+    }
99
+
100
+    mVolume = vol;
101
+}
102
+
74 103
 void Sound::listenAt(float pos[3], float angle[3]) {
75 104
     assert(mInit == true);
76 105
     assert(mSource.size() == mBuffer.size());
@@ -138,6 +167,8 @@ int Sound::addFile(const char *filename, int *source, unsigned int flags)
138 167
         alSourcei(mSource[id], AL_LOOPING, 1);
139 168
     }
140 169
 
170
+    alSourcef(mSource[id], AL_GAIN, mVolume);
171
+
141 172
     *source = id;
142 173
 
143 174
     return 0;
@@ -194,6 +225,8 @@ int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigne
194 225
         alSourcei(mSource[id], AL_LOOPING, 1);
195 226
     }
196 227
 
228
+    alSourcef(mSource[id], AL_GAIN, mVolume);
229
+
197 230
     *source = id;
198 231
 
199 232
     //! \fixme Should free alut buffer?
@@ -201,19 +234,6 @@ int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigne
201 234
     return 0;
202 235
 }
203 236
 
204
-void Sound::clear() {
205
-    assert(mInit == true);
206
-    assert(mSource.size() == mBuffer.size());
207
-
208
-    for (size_t i = 0; i < mSource.size(); i++) {
209
-        alDeleteSources(1, &mSource[i]);
210
-        alDeleteBuffers(1, &mBuffer[i]);
211
-    }
212
-
213
-    mSource.clear();
214
-    mBuffer.clear();
215
-}
216
-
217 237
 void Sound::play(int source) {
218 238
     assert(mInit == true);
219 239
     assert(mSource.size() == mBuffer.size());

Loading…
Отказ
Запис