]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundmgr.hxx
Dynamically adjust audio safety margin (how much audio we stuff in the
[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 SG_USING_STD(map);
45 SG_USING_STD(string);
46
47
48 // manages everything we need to know for an individual sound sample
49 class FGSimpleSound {
50
51     slSample *sample;
52     slEnvelope *pitch_envelope;
53     slEnvelope *volume_envelope;
54     double pitch;
55     double volume;
56
57 public:
58
59     FGSimpleSound( string file );
60     FGSimpleSound( unsigned char *buffer, int len );
61     ~FGSimpleSound();
62
63     inline double get_pitch() const { return pitch; }
64     inline void set_pitch( double p ) {
65         pitch = p;
66         pitch_envelope->setStep( 0, 0.01, pitch );
67     }
68     inline double get_volume() const { return volume; }
69     inline void set_volume( double v ) {
70         volume = v;
71         volume_envelope->setStep( 0, 0.01, volume );
72     }
73
74     inline slSample *get_sample() { return sample; }
75     inline slEnvelope *get_pitch_envelope() { return pitch_envelope; }
76     inline slEnvelope *get_volume_envelope() { return volume_envelope; }
77 };
78
79
80 typedef map < string, FGSimpleSound * > sound_map;
81 typedef sound_map::iterator sound_map_iterator;
82 typedef sound_map::const_iterator const_sound_map_iterator;
83
84
85 class FGSoundMgr {
86
87     slScheduler *audio_sched;
88     smMixer *audio_mixer;
89     sound_map sounds;
90
91     SGTimeStamp last;
92     double safety;
93
94 public:
95
96     FGSoundMgr();
97     ~FGSoundMgr();
98
99     // initialize the sound manager
100     bool init();
101
102     // run the audio scheduler
103     bool update();
104
105     // is audio working?
106     inline bool is_working() const { return !audio_sched->not_working(); }
107
108     // add a sound effect, return true if successful
109     bool add( FGSimpleSound *sound, const string& refname );
110
111     // remove a sound effect, return true if successful
112     bool remove( const string& refname );
113
114     // return true of the specified sound exists in the sound manager system
115     bool exists( const string& refname );
116
117     // return a pointer to the FGSimpleSound if the specified sound
118     // exists in the sound manager system, otherwise return NULL
119     FGSimpleSound *find( const string& refname );
120
121     // tell the scheduler to play the indexed sample in a continuous
122     // loop
123     bool play_looped( const string& refname );
124
125     // tell the scheduler to play the indexed sample once
126     bool play_once( const string& refname );
127
128     // return true of the specified sound is currently being played
129     bool is_playing( const string& refname );
130
131     // immediate stop playing the sound
132     bool stop( const string& refname );
133 };
134
135
136 #endif // _SOUNDMGR_HXX
137
138