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.
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/misc/sg_path.hxx>
35 #include "soundmgr_openal.hxx"
43 SGSoundMgr::SGSoundMgr() {
45 SG_LOG( SG_GENERAL, SG_ALERT, "Initializing OpenAL sound manager" );
50 if ( alGetError() == AL_NO_ERROR) {
54 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
57 listener_pos[0] = 0.0;
58 listener_pos[1] = 0.0;
59 listener_pos[2] = 0.0;
61 listener_vel[0] = 0.0;
62 listener_vel[1] = 0.0;
63 listener_vel[2] = 0.0;
65 listener_ori[0] = 0.0;
66 listener_ori[1] = 0.0;
67 listener_ori[2] = -1.0;
68 listener_ori[3] = 0.0;
69 listener_ori[4] = 1.0;
70 listener_ori[5] = 0.0;
72 alListenerfv( AL_POSITION, listener_pos );
73 alListenerfv( AL_VELOCITY, listener_vel );
74 alListenerfv( AL_ORIENTATION, listener_ori );
76 if ( alGetError() != AL_NO_ERROR) {
77 SG_LOG( SG_GENERAL, SG_ALERT,
78 "Oops AL error after audio initialization!" );
84 SGSoundMgr::~SGSoundMgr() {
87 // Remove the samples from the sample manager.
89 sample_map_iterator sample_current = samples.begin();
90 sample_map_iterator sample_end = samples.end();
91 for ( ; sample_current != sample_end; ++sample_current ) {
92 SGSoundSample *sample = sample_current->second;
100 // initialize the sound manager
101 void SGSoundMgr::init() {
103 // Remove the samples from the sample manager.
105 sample_map_iterator sample_current = samples.begin();
106 sample_map_iterator sample_end = samples.end();
107 for ( ; sample_current != sample_end; ++sample_current ) {
108 SGSoundSample *sample = sample_current->second;
115 void SGSoundMgr::bind ()
121 void SGSoundMgr::unbind ()
127 // run the audio scheduler
128 void SGSoundMgr::update( double dt ) {
135 ALCcontext *pCurContext = alcGetCurrentContext();
136 alcSuspendContext( pCurContext );
137 if ( alGetError() != AL_NO_ERROR) {
138 SG_LOG( SG_GENERAL, SG_ALERT,
139 "Oops AL error after soundmgr pause()!" );
145 SGSoundMgr::resume ()
147 ALCcontext *pCurContext = alcGetCurrentContext();
148 alcProcessContext( pCurContext );
149 if ( alGetError() != AL_NO_ERROR) {
150 SG_LOG( SG_GENERAL, SG_ALERT,
151 "Oops AL error after soundmgr resume()!" );
156 // add a sound effect, return true if successful
157 bool SGSoundMgr::add( SGSoundSample *sound, const string& refname ) {
159 sample_map_iterator sample_it = samples.find( refname );
160 if ( sample_it != samples.end() ) {
161 // sound already exists
165 samples[refname] = sound;
171 // remove a sound effect, return true if successful
172 bool SGSoundMgr::remove( const string &refname ) {
174 sample_map_iterator sample_it = samples.find( refname );
175 if ( sample_it != samples.end() ) {
176 // first stop the sound from playing (so we don't bomb the
178 SGSoundSample *sample = sample_it->second;
180 samples.erase( sample_it );
182 // cout << "sndmgr: removed -> " << refname << endl;
185 // cout << "sndmgr: failed remove -> " << refname << endl;
191 // return true of the specified sound exists in the sound manager system
192 bool SGSoundMgr::exists( const string &refname ) {
193 sample_map_iterator sample_it = samples.find( refname );
194 if ( sample_it != samples.end() ) {
202 // return a pointer to the SGSoundSample if the specified sound exists
203 // in the sound manager system, otherwise return NULL
204 SGSoundSample *SGSoundMgr::find( const string &refname ) {
205 sample_map_iterator sample_it = samples.find( refname );
206 if ( sample_it != samples.end() ) {
207 return sample_it->second;
214 // tell the scheduler to play the indexed sample in a continuous
216 bool SGSoundMgr::play_looped( const string &refname ) {
217 SGSoundSample *sample;
219 if ( (sample = find( refname )) == NULL ) {
222 sample->play( true );
228 // tell the scheduler to play the indexed sample once
229 bool SGSoundMgr::play_once( const string& refname ) {
230 SGSoundSample *sample;
232 if ( (sample = find( refname )) == NULL ) {
235 sample->play( false );
241 // return true of the specified sound is currently being played
242 bool SGSoundMgr::is_playing( const string& refname ) {
243 SGSoundSample *sample;
245 if ( (sample = find( refname )) == NULL ) {
248 return ( sample->is_playing() );
253 // immediate stop playing the sound
254 bool SGSoundMgr::stop( const string& refname ) {
255 SGSoundSample *sample;
257 if ( (sample = find( refname )) == NULL ) {