File: SoundPlayer.h

package info (click to toggle)
transcend 0.3.dfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 5,796 kB
  • ctags: 3,074
  • sloc: cpp: 26,886; ansic: 693; sh: 210; makefile: 99; perl: 67
file content (240 lines) | stat: -rw-r--r-- 6,406 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
/*
 * 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.
 */



#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.
         */
        SoundPlayer( int inSampleRate,
                     int inMaxSimultaneousRealtimeSounds,
                     void *inMusicPlayer = NULL,
                     double inMusicLoudness = 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 );


        
        /**
         * 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;

        unsigned long mSampleRate;
        
        char mAudioInitialized;

        int mMaxSimultaneousRealtimeSounds;

        // Typed as (void*) to avoid an include loop.
        void *mMusicPlayer;
        double mMusicLoudness;
        
        
        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