]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundmgr.hxx
Latest JSBSim changes.
[flightgear.git] / src / 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 _SOUNDMGR_HXX
28 #define _SOUNDMGR_HXX
29
30 #ifdef HAVE_CONFIG_H
31 #  include <config.h>
32 #endif
33
34 #include <simgear/compiler.h>
35
36 #include STL_STRING
37 #include <map>
38
39 #include <plib/sl.h>
40 #include <plib/sm.h>
41
42 #include <simgear/timing/timestamp.hxx>
43
44 #include <Main/fgfs.hxx>
45
46 SG_USING_STD(map);
47 SG_USING_STD(string);
48
49
50 // manages everything we need to know for an individual sound sample
51 class FGSimpleSound {
52
53     slSample *sample;
54     slEnvelope *pitch_envelope;
55     slEnvelope *volume_envelope;
56     double pitch;
57     double volume;
58
59 public:
60
61     FGSimpleSound( string file );
62     FGSimpleSound( unsigned char *buffer, int len );
63     ~FGSimpleSound();
64
65     inline double get_pitch() const { return pitch; }
66     inline void set_pitch( double p ) {
67         pitch = p;
68         pitch_envelope->setStep( 0, 0.01, pitch );
69     }
70     inline double get_volume() const { return volume; }
71     inline void set_volume( double v ) {
72         volume = v;
73         volume_envelope->setStep( 0, 0.01, volume );
74     }
75
76     inline slSample *get_sample() { return sample; }
77     inline slEnvelope *get_pitch_envelope() { return pitch_envelope; }
78     inline slEnvelope *get_volume_envelope() { return volume_envelope; }
79 };
80
81
82 typedef map < string, FGSimpleSound * > sound_map;
83 typedef sound_map::iterator sound_map_iterator;
84 typedef sound_map::const_iterator const_sound_map_iterator;
85
86
87 class FGSoundMgr : public FGSubsystem
88 {
89
90     slScheduler *audio_sched;
91     smMixer *audio_mixer;
92     sound_map sounds;
93
94     SGTimeStamp last;
95     double safety;
96
97 public:
98
99     FGSoundMgr();
100     ~FGSoundMgr();
101
102
103     /**
104      * Initialize the sound manager.
105      */
106     void init();
107
108
109     /**
110      * Bind properties for the sound manager.
111      */
112     void bind ();
113
114
115     /**
116      * Unbind properties for the sound manager.
117      */
118     void unbind ();
119
120
121     /**
122      * Run the audio scheduler.
123      */
124     void update();
125
126
127     // is audio working?
128     inline bool is_working() const { return !audio_sched->not_working(); }
129
130     // add a sound effect, return true if successful
131     bool add( FGSimpleSound *sound, const string& refname );
132
133     // remove a sound effect, return true if successful
134     bool remove( const string& refname );
135
136     // return true of the specified sound exists in the sound manager system
137     bool exists( const string& refname );
138
139     // return a pointer to the FGSimpleSound if the specified sound
140     // exists in the sound manager system, otherwise return NULL
141     FGSimpleSound *find( const string& refname );
142
143     // tell the scheduler to play the indexed sample in a continuous
144     // loop
145     bool play_looped( const string& refname );
146
147     // tell the scheduler to play the indexed sample once
148     bool play_once( const string& refname );
149
150     // return true of the specified sound is currently being played
151     bool is_playing( const string& refname );
152
153     // immediate stop playing the sound
154     bool stop( const string& refname );
155 };
156
157
158 #endif // _SOUNDMGR_HXX
159
160