]> git.mxchange.org Git - simgear.git/blob - simgear/sound/soundmgr.hxx
Various code massaging.
[simgear.git] / simgear / sound / soundmgr.hxx
1 // soundmgr.hxx -- Sound effect management class
2 //
3 // Sound manager initially written by David Findlay
4 // <david_j_findlay@yahoo.com.au> 2001
5 //
6 // C++-ified by Curtis Olson, started March 2001.
7 //
8 // Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #ifndef _SG_SOUNDMGR_HXX
28 #define _SG_SOUNDMGR_HXX 1
29
30 #ifndef __cplusplus
31 # error This library requires C++
32 #endif
33
34 #include <simgear/compiler.h>
35 #include <simgear/timing/timestamp.hxx>
36
37 #include STL_STRING
38 #include <map>
39
40 #include <plib/sl.h>
41 #include <plib/sm.h>
42
43 SG_USING_STD(map);
44 SG_USING_STD(string);
45
46
47 // manages everything we need to know for an individual sound sample
48 class SGSimpleSound {
49
50 private:
51
52     slSample *sample;
53     slEnvelope *pitch_envelope;
54     slEnvelope *volume_envelope;
55     double pitch;
56     double volume;
57
58 public:
59
60     SGSimpleSound( const char *path, const char *file = NULL );
61     SGSimpleSound( unsigned char *buffer, int len );
62     ~SGSimpleSound();
63
64     void play( slScheduler *sched, bool looped );
65     void stop( slScheduler *sched, bool quick = true );
66
67     inline void play_once( slScheduler *sched ) { play( sched, false); }
68     inline void play_looped( slScheduler *sched ) { play( sched, true); }
69     inline bool is_playing( ) {
70         return ( sample->getPlayCount() > 0 );
71     }
72
73     inline double get_pitch() const { return pitch; }
74     inline void set_pitch( double p ) {
75        pitch = p;
76        pitch_envelope->setStep( 0, 0.01, pitch );
77     }
78     inline double get_volume() const { return volume; }
79     inline void set_volume( double v ) {
80        volume = v;
81        volume_envelope->setStep( 0, 0.01, volume );
82     }
83
84     inline slSample *get_sample() { return sample; }
85     inline slEnvelope *get_pitch_envelope() { return pitch_envelope; }
86     inline slEnvelope *get_volume_envelope() { return volume_envelope; }
87 };
88
89
90 typedef struct {
91         int n;
92         slSample *sample;
93 } sample_ref;
94
95 typedef map < string, sample_ref * > sample_map;
96 typedef sample_map::iterator sample_map_iterator;
97 typedef sample_map::const_iterator const_sample_map_iterator;
98
99 typedef map < string, SGSimpleSound * > sound_map;
100 typedef sound_map::iterator sound_map_iterator;
101 typedef sound_map::const_iterator const_sound_map_iterator;
102
103
104 class SGSoundMgr
105 {
106
107     slScheduler *audio_sched;
108     smMixer *audio_mixer;
109
110     sound_map sounds;
111     sample_map samples;
112
113     double safety;
114
115 public:
116
117     SGSoundMgr();
118     ~SGSoundMgr();
119
120
121     /**
122      * (re) initialize the sound manager.
123      */
124     void init();
125
126
127     /**
128      * Bind properties for the sound manager.
129      */
130     void bind ();
131
132
133     /**
134      * Unbind properties for the sound manager.
135      */
136     void unbind ();
137
138
139     /**
140      * Run the audio scheduler.
141      */
142     void update(double dt);
143
144
145     /**
146      * Pause all sounds.
147      */
148     void pause ();
149
150
151     /**
152      * Resume all sounds.
153      */
154     void resume ();
155
156
157     // is audio working?
158     inline bool is_working() const { return !audio_sched->notWorking(); }
159
160     // reinitialize the sound manager
161     inline void reinit() { init(); }
162
163     // add a sound effect, return true if successful
164     bool add( SGSimpleSound *sound, const string& refname);
165
166     // add a sound file, return the sample if successful, else return NULL
167     SGSimpleSound *add( const string& refname,
168                       const char *path, const char *file = NULL );
169
170     // remove a sound effect, return true if successful
171     bool remove( const string& refname );
172
173     // return true of the specified sound exists in the sound manager system
174     bool exists( const string& refname );
175
176     // return a pointer to the SGSimpleSound if the specified sound
177     // exists in the sound manager system, otherwise return NULL
178     SGSimpleSound *find( const string& refname );
179
180     // tell the scheduler to play the indexed sample in a continuous
181     // loop
182     bool play_looped( const string& refname );
183
184     // tell the scheduler to play the indexed sample once
185     bool play_once( const string& refname );
186
187     // return true of the specified sound is currently being played
188     bool is_playing( const string& refname );
189
190     // immediate stop playing the sound
191     bool stop( const string& refname );
192
193     // return the audio scheduler 
194     inline slScheduler *get_scheduler( ) { return audio_sched; };
195 };
196
197
198 #endif // _SG_SOUNDMGR_HXX
199
200