File: SoundPlayer.h

package info (click to toggle)
cultivation 9+dfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 4,452 kB
  • sloc: cpp: 35,783; ansic: 693; sh: 163; makefile: 122; perl: 67
file content (275 lines) | stat: -rw-r--r-- 7,426 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Modification History
 *
 * 2004-July-17   Jason Rohrer
 * Created.
 *
 * 2004-July-21   Jason Rohrer
 * Changed to use a callback to reduce latency.
 *
 * 2004-August-9   Jason Rohrer
 * Added a limit on the number of simultaneous sounds.
 *
 * 2004-August-12   Jason Rohrer
 * Parameterized the sample rate.
 *
 * 2004-August-13   Jason Rohrer
 * Added Mutex to protect members that are accessed by multiple threads.
 *
 * 2004-August-14   Jason Rohrer
 * Changed to fade sounds out before dropping them.
 *
 * 2004-August-15   Jason Rohrer
 * Added a function for getting the sample rate.
 * Added volume modifier parameter to playSoundNow function.
 *
 * 2004-August-20   Jason Rohrer
 * Added priority flags.
 *
 * 2004-August-23   Jason Rohrer
 * Added music.
 *
 * 2004-August-25   Jason Rohrer
 * Added function for setting music loudness.
 *
 * 2004-August-31   Jason Rohrer
 * Added function for removing filters.
 *
 * 2006-September-19   Jason Rohrer
 * Added global loudness and fade-in.
 *
 * 2006-October-2   Jason Rohrer
 * Added a separate lock for music player to deal with deadlock.
 */



#ifndef SOUND_PLAYER_INCLUDED
#define SOUND_PLAYER_INCLUDED


#include "SoundSamples.h"
#include "SoundFilter.h"
#include "PlayableSound.h"

#include <portaudio.h>


#include "minorGems/util/SimpleVector.h"

#include "minorGems/system/MutexLock.h"



/**
 * Class that plays both running background music and realtime sounds.
 *
 * @author Jason Rohrer
 */
class SoundPlayer {


        
    public:


        
        /**
         * Constructs a sound player.
         *
         * @param inSampleRate the number of samples per second.
         *   Should be a "standard" rate, like 44100, 22050, etc.
         * @param inMaxSimultaneousRealtimeSounds the number of simultaneous
         *   realtime sounds to allow.  When this limit is reached,
         *   older sounds are silenced prematurely to make way for
         *   newer sounds.
         * @param inMusicPlayer the player to get music from, or NULL
         *   to disable music.  Defaults to NULL.
         *   Typed as (void*) to avoid an include loop.
         *   Must be destroyed by caller after this class is destroyed.
         * @param inMusicLoudness an adjustment for music loudness in the
         *   range [0,1].  Defaults to 1.
         * @param inGlobalLoudness the global adjustment for loudness.
         *   In the range [0,1].  Defaults to 1.
         */
        SoundPlayer( int inSampleRate,
                     int inMaxSimultaneousRealtimeSounds,
                     void *inMusicPlayer = NULL,
                     double inMusicLoudness = 1,
                     double inGlobalLoudness = 1 );

        ~SoundPlayer();



        /**
         * Sets the music player.
         *
         * @param inMusicPlayer the player to get music from, or NULL
         *   to disable music.  Typed as (void*) to avoid an include loop.
         *   Must be destroyed by caller after this class is destroyed.
         */
        void setMusicPlayer( void *inMusicPlayer );

        

        /**
         * Sets the loudess of the music.
         *
         * @param inMusicLoudness the loudness in the range [0,1].
         */
        void setMusicLoudness( double inMusicLoudness );


        
        /**
         * Sets the global loudness.
         *
         * @param inLoudness the loudness in the range [0,1].
         */
        void setGlobalLoudness( double inLoudness );



        /**
         * Triggers global loudness to fade up to 1 over time.
         *
         * @param inFadeTimeInSeconds the time until a loudness of 1
         *   is reached.
         */
        void fadeIn( double inFadeTimeInSeconds );

        
        
        /**
         * Mixes a sound to the speakers as quickly as possible.
         *
         * This call does not adjust the volume level of the samples
         * before mixing them with other realtime sounds or the background
         * music.
         *
         * @param inSamples the samples to play.
         *   Must be destroyed by caller.
         * @param inPriorityFlag true if this sound should have
         *   high priority, or false for low priority.  Defaults to false.
         * @param inLoudnessModifier the value to adjust the sound's
         *   loudness by, in [0,1], when playing.  Defaults to 1.
         */
        void playSoundNow( SoundSamples *inSamples,
                           char inPriorityFlag = false,
                           double inLoudnessModifier = 1.0 );


        
        /**
         * Same as earlier playSoundNow, except that it takes a PlayableSound
         * that must be destroyed by the caller.
         */
        void playSoundNow( PlayableSound *inSound,
                           char inPriorityFlag = false,
                           double inLoudnessModifier = 1.0 );
        
               
        
        /**
         * Add the next section of music to be played.
         *
         * This call drives the sound player to send audio to the speakers.
         *
         * @param inSamples the samples to play.
         *   Must be destroyed by caller.
         */
        void addMoreMusic( SoundSamples *inSamples );



        /**
         * Called by the internal portaudio callback function.
         */
        void getSamples( void *outputBuffer, unsigned long inFramesInBuffer );


        
        /**
         * Adds a filter to the end of the chain that will process
         * samples before sending them to the speakers.
         *
         * @param inFilter the filter to add.
         *   Will be destroyed when this class is destroyed.
         */
        void addFilter( SoundFilter *inFilter );



        /**
         * Removes and destroys all filters.
         */
        void removeAllFilters();
        


        /**
         * Gets the current sample rate.
         *
         * @return the sample rate, in samples per second.
         */
        unsigned long getSampleRate();

        
        
    protected:

        MutexLock *mLock;

        // separate lock for setting music player
        // deals with a deadlock issue
        MutexLock *mMusicPlayerLock;
        
        unsigned long mSampleRate;
        
        char mAudioInitialized;

        int mMaxSimultaneousRealtimeSounds;

        // Typed as (void*) to avoid an include loop.
        void *mMusicPlayer;
        double mMusicLoudness;

        double mGlobalLoudness;
        char mFadingIn;
        int mNumFadeFramesRemaining;
        
        PaStream *mAudioStream;

        // realtime sounds that should be mixed into the next to-speaker call
        SimpleVector<PlayableSound *> *mRealtimeSounds;

        SimpleVector<char> *mPriorityFlags;
        
        // one modifier for each realtime sound
        SimpleVector<double> *mSoundLoudnessModifiers;
        
        // one flag for each realtime sound, indicating whether it should
        // be dropped (faded out) during the next frame 
        SimpleVector<char> *mSoundDroppedFlags;

        
        SimpleVector<SoundFilter *> *mFilterChain;


        
        /**
         * Checks the sound set to ensure that our max simultaneous limit
         * is being met.
         *
         * Not thread-safe:  should be called with mLock already locked.
         */
        void checkForExcessSounds();
        
        
        
    };



#endif