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!" );
90 SGSoundMgr::~SGSoundMgr() {
93 // Remove the samples from the sample manager.
95 sample_map_iterator sample_current = samples.begin();
96 sample_map_iterator sample_end = samples.end();
97 for ( ; sample_current != sample_end; ++sample_current ) {
98 SGSoundSample *sample = sample_current->second;
106 // initialize the sound manager
107 void SGSoundMgr::init() {
109 // Remove the samples from the sample manager.
111 sample_map_iterator sample_current = samples.begin();
112 sample_map_iterator sample_end = samples.end();
113 for ( ; sample_current != sample_end; ++sample_current ) {
114 SGSoundSample *sample = sample_current->second;
121 void SGSoundMgr::bind ()
127 void SGSoundMgr::unbind ()
133 // run the audio scheduler
134 void SGSoundMgr::update( double dt ) {
141 ALCcontext *pCurContext = alcGetCurrentContext();
142 alcSuspendContext( pCurContext );
143 if ( alGetError() != AL_NO_ERROR) {
144 SG_LOG( SG_GENERAL, SG_ALERT,
145 "Oops AL error after soundmgr pause()!" );
151 SGSoundMgr::resume ()
153 ALCcontext *pCurContext = alcGetCurrentContext();
154 alcProcessContext( pCurContext );
155 if ( alGetError() != AL_NO_ERROR) {
156 SG_LOG( SG_GENERAL, SG_ALERT,
157 "Oops AL error after soundmgr resume()!" );
162 // add a sound effect, return true if successful
163 bool SGSoundMgr::add( SGSoundSample *sound, const string& refname ) {
165 sample_map_iterator sample_it = samples.find( refname );
166 if ( sample_it != samples.end() ) {
167 // sound already exists
171 samples[refname] = sound;
177 // remove a sound effect, return true if successful
178 bool SGSoundMgr::remove( const string &refname ) {
180 sample_map_iterator sample_it = samples.find( refname );
181 if ( sample_it != samples.end() ) {
182 // first stop the sound from playing (so we don't bomb the
184 SGSoundSample *sample = sample_it->second;
186 samples.erase( sample_it );
188 // cout << "sndmgr: removed -> " << refname << endl;
191 // cout << "sndmgr: failed remove -> " << refname << endl;
197 // return true of the specified sound exists in the sound manager system
198 bool SGSoundMgr::exists( const string &refname ) {
199 sample_map_iterator sample_it = samples.find( refname );
200 if ( sample_it != samples.end() ) {
208 // return a pointer to the SGSoundSample if the specified sound exists
209 // in the sound manager system, otherwise return NULL
210 SGSoundSample *SGSoundMgr::find( const string &refname ) {
211 sample_map_iterator sample_it = samples.find( refname );
212 if ( sample_it != samples.end() ) {
213 return sample_it->second;
220 // tell the scheduler to play the indexed sample in a continuous
222 bool SGSoundMgr::play_looped( const string &refname ) {
223 SGSoundSample *sample;
225 if ( (sample = find( refname )) == NULL ) {
228 sample->play( true );
234 // tell the scheduler to play the indexed sample once
235 bool SGSoundMgr::play_once( const string& refname ) {
236 SGSoundSample *sample;
238 if ( (sample = find( refname )) == NULL ) {
241 sample->play( false );
247 // return true of the specified sound is currently being played
248 bool SGSoundMgr::is_playing( const string& refname ) {
249 SGSoundSample *sample;
251 if ( (sample = find( refname )) == NULL ) {
254 return ( sample->is_playing() );
259 // immediate stop playing the sound
260 bool SGSoundMgr::stop( const string& refname ) {
261 SGSoundSample *sample;
263 if ( (sample = find( refname )) == NULL ) {