]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.hxx
9cfd8dfbdf59c119b7c7bedc39faf4610407a89f
[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 velocities of all managed sound sources
143      */
144     void set_velocity( SGVec3d& vel );
145
146     /**
147      * set the position of all managed sound sources
148      */
149     void set_position( SGGeod pos );
150
151     /**
152      * set the orientation of all managed sound sources
153      */
154     void set_orientation( SGQuatd ori );
155
156     void tie_to_listener() { _tied_to_listener = true; }
157
158     void activate() { _active = true; }
159
160 protected:
161     SGSoundMgr *_smgr;
162     string _refname;
163     bool _active;
164
165 private:
166     float _volume;
167     bool _tied_to_listener;
168
169     SGVec3d _velocity;
170     SGGeod _position;
171     SGQuatd _orientation;
172
173     sample_map _samples;
174
175     bool testForALError(string s);
176     bool testForError(void *p, string s);
177
178     void update_sample_config( SGSoundSample *sound );
179 };
180
181 #endif // _SG_SAMPLE_GROUP_OPENAL_HXX
182