]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.hxx
1344df4493f4a5961ba11e6b348e7284731dec48
[simgear.git] / simgear / sound / sample_group.hxx
1 // soundmgr.hxx -- Sound effect management class
2 //
3 // Sampel Group handler initially written by Erik Hofman
4 //
5 // Copyright (C) 2009  Erik Hofman - <erik@ehofman.com>
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 /**
24  * \file sample_group.hxx
25  * sample groups contain all sounds related to one specific object and
26  * have to be added to the sound manager, otherwise they won't get processed.
27  */
28
29 #ifndef _SG_SAMPLE_GROUP_OPENAL_HXX
30 #define _SG_SAMPLE_GROUP_OPENAL_HXX 1
31
32 #ifndef __cplusplus
33 # error This library requires C++
34 #endif
35
36 #if defined(__APPLE__)
37 # include <OpenAL/al.h>
38 #elif defined(_WIN32)
39 # include <al.h>
40 #else
41 # include <AL/al.h>
42 #endif
43
44 #include <string>
45 #include <map>
46
47 #include <simgear/compiler.h>
48 #include <simgear/math/SGMath.hxx>
49 #include <simgear/structure/SGReferenced.hxx>
50 #include <simgear/structure/SGSharedPtr.hxx>
51 #include <simgear/structure/exception.hxx>
52
53 #include "sample_openal.hxx"
54
55 using std::map;
56 using std::string;
57
58 typedef map < string, SGSharedPtr<SGSoundSample> > sample_map;
59 typedef sample_map::iterator sample_map_iterator;
60 typedef sample_map::const_iterator const_sample_map_iterator;
61
62 class SGSoundMgr;
63
64 class SGSampleGroup : public SGReferenced
65 {
66 public:
67     SGSampleGroup ();
68     SGSampleGroup ( SGSoundMgr *smgr, const string &refname );
69     ~SGSampleGroup ();
70
71     virtual void update (double dt);
72
73     /**
74      * add a sound effect, return true if successful
75      */
76     bool add( SGSoundSample *sound, const string& refname );
77
78     /**
79      * remove a sound effect, return true if successful
80      */
81     bool remove( const string& refname );
82
83     /**
84      * return true of the specified sound exists in the sound manager system
85      */
86     bool exists( const string& refname );
87
88     /**
89      * return a pointer to the SGSoundSample if the specified sound
90      * exists in the sound manager system, otherwise return NULL
91      */
92     SGSoundSample *find( const string& refname );
93
94     /**
95      * request to stop playing all associated samples until further notice
96      */
97     void suspend();
98
99     /**
100      * request to resume playing all associated samples
101      */
102     void resume();
103
104
105     /**
106      * request to start playing the associated samples
107      */
108     bool play( const string& refname, bool looping );
109     
110     /**
111      * tell the scheduler to play the indexed sample in a continuous
112      * loop
113      */
114     inline bool play_looped( const string& refname ) {
115         return play( refname, true );
116     }
117
118     /**
119      * tell the scheduler to play the indexed sample once
120      */
121     inline bool play_once( const string& refname ) {
122         return play( refname, false );
123     }
124
125     /**
126      * return true of the specified sound is currently being played
127      */
128     bool is_playing( const string& refname );
129
130     /**
131      * request to stop playing the associated samples
132      */
133     bool stop( const string& refname );
134
135     /**
136      * set overall volume for the application.
137      * @param must be between 0.0 and 1.0
138      */
139     void set_volume( float vol );
140
141     /**
142      * set the positions of all managed sound sources
143      */
144     void set_position( SGVec3d pos );
145
146     /**
147      * set the velocities of all managed sound sources
148      */
149     void set_velocity( SGVec3f vel );
150
151     /**
152      * set the orientation of all managed sound sources
153      */
154     void set_orientation( SGVec3f ori );
155
156     inline void tie_to_listener() { _tied_to_listener = true; }
157
158
159 protected:
160     SGSoundMgr *_smgr;
161     string _refname;
162     bool _active;
163
164 private:
165     float _volume;
166     bool _tied_to_listener;
167
168     SGVec3d _position;
169     SGVec3f _velocity;
170     SGVec3f _orientation;
171
172     sample_map _samples;
173
174     bool testForALError(string s);
175     bool testForError(void *p, string s);
176
177     void update_sample_config( SGSoundSample *sound );
178 };
179
180 #endif // _SG_SAMPLE_GROUP_OPENAL_HXX
181