ソースを参照

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
     int registeredSources();
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
      * \brief Move listener and repositions them
59
      * \brief Move listener and repositions them
49
      * \param pos New position for listener
60
      * \param pos New position for listener
50
      * \param angle New orientation for listener
61
      * \param angle New orientation for listener
78
     int addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags);
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
      * \brief Play sound source
92
      * \brief Play sound source
87
      * \param source sound source to play
93
      * \param source sound source to play
88
      */
94
      */
97
 private:
103
 private:
98
 
104
 
99
     bool mInit;                        //!< Guard to ensure ausio system is active
105
     bool mInit;                        //!< Guard to ensure ausio system is active
106
+    float mVolume;                     //!< Listener gain
100
     std::vector<unsigned int> mBuffer; //!< Audio buffer id list
107
     std::vector<unsigned int> mBuffer; //!< Audio buffer id list
101
     std::vector<unsigned int> mSource; //!< Audio source id list
108
     std::vector<unsigned int> mSource; //!< Audio source id list
102
 };
109
 };

+ 33
- 13
src/Sound.cpp ファイルの表示

28
 
28
 
29
 Sound::Sound() {
29
 Sound::Sound() {
30
     mInit = false;
30
     mInit = false;
31
+    mVolume = 1.0f;
31
 }
32
 }
32
 
33
 
33
 Sound::~Sound() {
34
 Sound::~Sound() {
36
 }
37
 }
37
 
38
 
38
 int Sound::init() {
39
 int Sound::init() {
40
+    assert(mInit == false);
41
+
39
 #ifndef __APPLE__
42
 #ifndef __APPLE__
40
     int fd;
43
     int fd;
41
 
44
 
71
     return mSource.size();
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
 void Sound::listenAt(float pos[3], float angle[3]) {
103
 void Sound::listenAt(float pos[3], float angle[3]) {
75
     assert(mInit == true);
104
     assert(mInit == true);
76
     assert(mSource.size() == mBuffer.size());
105
     assert(mSource.size() == mBuffer.size());
138
         alSourcei(mSource[id], AL_LOOPING, 1);
167
         alSourcei(mSource[id], AL_LOOPING, 1);
139
     }
168
     }
140
 
169
 
170
+    alSourcef(mSource[id], AL_GAIN, mVolume);
171
+
141
     *source = id;
172
     *source = id;
142
 
173
 
143
     return 0;
174
     return 0;
194
         alSourcei(mSource[id], AL_LOOPING, 1);
225
         alSourcei(mSource[id], AL_LOOPING, 1);
195
     }
226
     }
196
 
227
 
228
+    alSourcef(mSource[id], AL_GAIN, mVolume);
229
+
197
     *source = id;
230
     *source = id;
198
 
231
 
199
     //! \fixme Should free alut buffer?
232
     //! \fixme Should free alut buffer?
201
     return 0;
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
 void Sound::play(int source) {
237
 void Sound::play(int source) {
218
     assert(mInit == true);
238
     assert(mInit == true);
219
     assert(mSource.size() == mBuffer.size());
239
     assert(mSource.size() == mBuffer.size());

読み込み中…
キャンセル
保存