1 // soundmgr.cxx -- Sound effect management class
3 // Sound manager initially written by David Findlay
4 // <david_j_findlay@yahoo.com.au> 2001
6 // C++-ified by Curtis Olson, started March 2001.
8 // Copyright (C) 2001 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
28 #if defined(__APPLE__)
29 # include <OpenAL/al.h>
30 # include <OpenAL/alut.h>
31 # include <OpenAL/alc.h>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sg_path.hxx>
41 #include "soundmgr_openal.hxx"
49 SGSoundMgr::SGSoundMgr() {
51 SG_LOG( SG_GENERAL, SG_ALERT, "Initializing OpenAL sound manager" );
56 if ( alGetError() == AL_NO_ERROR) {
60 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
63 listener_pos[0] = 0.0;
64 listener_pos[1] = 0.0;
65 listener_pos[2] = 0.0;
67 listener_vel[0] = 0.0;
68 listener_vel[1] = 0.0;
69 listener_vel[2] = 0.0;
71 listener_ori[0] = 0.0;
72 listener_ori[1] = 0.0;
73 listener_ori[2] = -1.0;
74 listener_ori[3] = 0.0;
75 listener_ori[4] = 1.0;
76 listener_ori[5] = 0.0;
78 alListenerfv( AL_POSITION, listener_pos );
79 alListenerfv( AL_VELOCITY, listener_vel );
80 alListenerfv( AL_ORIENTATION, listener_ori );
82 if ( alGetError() != AL_NO_ERROR) {
83 SG_LOG( SG_GENERAL, SG_ALERT,
84 "Oops AL error after audio initialization!" );
87 // exaggerate the ear candy?
89 alDopplerVelocity(340.0); // speed of sound in meters per second.
94 SGSoundMgr::~SGSoundMgr() {
97 // Remove the samples from the sample manager.
99 sample_map_iterator sample_current = samples.begin();
100 sample_map_iterator sample_end = samples.end();
101 for ( ; sample_current != sample_end; ++sample_current ) {
102 SGSoundSample *sample = sample_current->second;
110 // initialize the sound manager
111 void SGSoundMgr::init() {
113 // Remove the samples from the sample manager.
115 sample_map_iterator sample_current = samples.begin();
116 sample_map_iterator sample_end = samples.end();
117 for ( ; sample_current != sample_end; ++sample_current ) {
118 SGSoundSample *sample = sample_current->second;
125 void SGSoundMgr::bind ()
131 void SGSoundMgr::unbind ()
137 // run the audio scheduler
138 void SGSoundMgr::update( double dt ) {
145 ALCcontext *pCurContext = alcGetCurrentContext();
146 alcSuspendContext( pCurContext );
147 if ( alGetError() != AL_NO_ERROR) {
148 SG_LOG( SG_GENERAL, SG_ALERT,
149 "Oops AL error after soundmgr pause()!" );
155 SGSoundMgr::resume ()
157 ALCcontext *pCurContext = alcGetCurrentContext();
158 alcProcessContext( pCurContext );
159 if ( alGetError() != AL_NO_ERROR) {
160 SG_LOG( SG_GENERAL, SG_ALERT,
161 "Oops AL error after soundmgr resume()!" );
166 // add a sound effect, return true if successful
167 bool SGSoundMgr::add( SGSoundSample *sound, const string& refname ) {
169 sample_map_iterator sample_it = samples.find( refname );
170 if ( sample_it != samples.end() ) {
171 // sound already exists
175 samples[refname] = sound;
181 // remove a sound effect, return true if successful
182 bool SGSoundMgr::remove( const string &refname ) {
184 sample_map_iterator sample_it = samples.find( refname );
185 if ( sample_it != samples.end() ) {
186 // first stop the sound from playing (so we don't bomb the
188 SGSoundSample *sample = sample_it->second;
190 samples.erase( sample_it );
192 // cout << "sndmgr: removed -> " << refname << endl;
195 // cout << "sndmgr: failed remove -> " << refname << endl;
201 // return true of the specified sound exists in the sound manager system
202 bool SGSoundMgr::exists( const string &refname ) {
203 sample_map_iterator sample_it = samples.find( refname );
204 if ( sample_it != samples.end() ) {
212 // return a pointer to the SGSoundSample if the specified sound exists
213 // in the sound manager system, otherwise return NULL
214 SGSoundSample *SGSoundMgr::find( const string &refname ) {
215 sample_map_iterator sample_it = samples.find( refname );
216 if ( sample_it != samples.end() ) {
217 return sample_it->second;
224 // tell the scheduler to play the indexed sample in a continuous
226 bool SGSoundMgr::play_looped( const string &refname ) {
227 SGSoundSample *sample;
229 if ( (sample = find( refname )) == NULL ) {
232 sample->play( true );
238 // tell the scheduler to play the indexed sample once
239 bool SGSoundMgr::play_once( const string& refname ) {
240 SGSoundSample *sample;
242 if ( (sample = find( refname )) == NULL ) {
245 sample->play( false );
251 // return true of the specified sound is currently being played
252 bool SGSoundMgr::is_playing( const string& refname ) {
253 SGSoundSample *sample;
255 if ( (sample = find( refname )) == NULL ) {
258 return ( sample->is_playing() );
263 // immediate stop playing the sound
264 bool SGSoundMgr::stop( const string& refname ) {
265 SGSoundSample *sample;
267 if ( (sample = find( refname )) == NULL ) {
276 // set source position of all managed sounds
277 void SGSoundMgr::set_source_pos_all( ALfloat *pos ) {
278 sample_map_iterator sample_current = samples.begin();
279 sample_map_iterator sample_end = samples.end();
280 for ( ; sample_current != sample_end; ++sample_current ) {
281 SGSoundSample *sample = sample_current->second;
282 sample->set_source_pos( pos );
287 // set source velocity of all managed sounds
288 void SGSoundMgr::set_source_vel_all( ALfloat *vel ) {
289 sample_map_iterator sample_current = samples.begin();
290 sample_map_iterator sample_end = samples.end();
291 for ( ; sample_current != sample_end; ++sample_current ) {
292 SGSoundSample *sample = sample_current->second;
293 sample->set_source_vel( vel );