]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.hxx
b3170696389d808c14f155825e4f0a70a1e968df
[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 #else
39 # include <AL/al.h>
40 #endif
41
42 #include <string>
43 #include <map>
44
45 #include <simgear/compiler.h>
46 #include <simgear/math/SGMath.hxx>
47 #include <simgear/structure/SGReferenced.hxx>
48 #include <simgear/structure/SGSharedPtr.hxx>
49 #include <simgear/structure/exception.hxx>
50
51 #include "sample_openal.hxx"
52
53 using std::map;
54 using std::string;
55
56 typedef map < string, SGSharedPtr<SGSoundSample> > sample_map;
57 typedef sample_map::iterator sample_map_iterator;
58 typedef sample_map::const_iterator const_sample_map_iterator;
59
60 class SGSoundMgr;
61
62 class SGSampleGroup : public SGReferenced
63 {
64 public:
65     SGSampleGroup ();
66     SGSampleGroup ( SGSoundMgr *smgr, const string &refname );
67     ~SGSampleGroup ();
68
69     virtual void update (double dt);
70
71     /**
72      * add a sound effect, return true if successful
73      */
74     bool add( SGSoundSample *sound, const string& refname );
75
76     /**
77      * remove a sound effect, return true if successful
78      */
79     bool remove( const string& refname );
80
81     /**
82      * return true of the specified sound exists in the sound manager system
83      */
84     bool exists( const string& refname );
85
86     /**
87      * return a pointer to the SGSoundSample if the specified sound
88      * exists in the sound manager system, otherwise return NULL
89      */
90     SGSoundSample *find( const string& refname );
91
92     /**
93      * request to stop playing all associated samples until further notice
94      */
95     void suspend();
96
97     /**
98      * request to resume playing all associated samples
99      */
100     void resume();
101
102
103     /**
104      * request to start playing the associated samples
105      */
106     bool play( const string& refname, bool looping );
107     
108     /**
109      * tell the scheduler to play the indexed sample in a continuous
110      * loop
111      */
112     inline bool play_looped( const string& refname ) {
113         return play( refname, true );
114     }
115
116     /**
117      * tell the scheduler to play the indexed sample once
118      */
119     inline bool play_once( const string& refname ) {
120         return play( refname, false );
121     }
122
123     /**
124      * return true of the specified sound is currently being played
125      */
126     bool is_playing( const string& refname );
127
128     /**
129      * request to stop playing the associated samples
130      */
131     bool stop( const string& refname );
132
133     /**
134      * set overall volume for the application.
135      * @param must be between 0.0 and 1.0
136      */
137     void set_volume( float vol );
138
139     /**
140      * set the positions of all managed sound sources
141      */
142     void set_position( SGVec3d pos );
143
144     /**
145      * set the velocities of all managed sound sources
146      */
147     void set_velocity( SGVec3f vel );
148
149     /**
150      * set the orientation of all managed sound sources
151      */
152     void set_orientation( SGVec3f ori );
153
154     inline void tie_to_listener() { _tied_to_listener = true; }
155
156
157 protected:
158     SGSoundMgr *_smgr;
159     bool _active;
160
161 private:
162     float _volume;
163     SGVec3d _position;
164     SGVec3f _orientation;
165     bool _tied_to_listener;
166
167     sample_map _samples;
168
169     bool testForALError(string s);
170     bool testForError(void *p, string s);
171
172     string _refname;
173
174     void update_sample_config( SGSoundSample *sound );
175 };
176
177 #endif // _SG_SAMPLE_GROUP_OPENAL_HXX
178