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.
27 #include <simgear/misc/fgpath.hxx>
29 #include "globals.hxx"
30 #include "soundmgr.hxx"
34 FGSimpleSound::FGSimpleSound( string file ) {
35 FGPath slfile( globals->get_fg_root() );
36 slfile.append( file );
37 sample = new slSample ( (char *)slfile.c_str() );
38 pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
39 volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
40 pitch_envelope->setStep ( 0, 0.01, 1.0 );
41 volume_envelope->setStep ( 0, 0.01, 1.0 );
45 FGSimpleSound::~FGSimpleSound() {
46 delete pitch_envelope;
47 delete volume_envelope;
53 FGSoundMgr::FGSoundMgr() {
54 audio_sched = new slScheduler( 8000 );
55 audio_mixer = new smMixer;
59 FGSoundMgr::~FGSoundMgr() {
60 sound_map_iterator current = sounds.begin();
61 sound_map_iterator end = sounds.end();
62 for ( ; current != end; ++current ) {
63 FGSimpleSound *s = current->second;
64 delete s->get_sample();
73 // initialize the sound manager
74 bool FGSoundMgr::init() {
75 audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
76 audio_sched -> setSafetyMargin ( 1.0 ) ;
78 sound_map_iterator current = sounds.begin();
79 sound_map_iterator end = sounds.end();
80 for ( ; current != end; ++current ) {
81 FGSimpleSound *s = current->second;
82 delete s->get_sample();
87 if ( audio_sched->not_working() ) {
95 // run the audio scheduler
96 bool FGSoundMgr::update() {
97 if ( !audio_sched->not_working() ) {
98 audio_sched -> update();
106 // add a sound effect
107 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
108 sounds[refname] = sound;
114 // tell the scheduler to play the indexed sample in a continuous
116 bool FGSoundMgr::play_looped( const string& refname ) {
117 sound_map_iterator it = sounds.find( refname );
118 if ( it != sounds.end() ) {
119 FGSimpleSound *sample = it->second;
120 audio_sched->loopSample( sample->get_sample() );
121 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
122 sample->get_pitch_envelope(),
124 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
125 sample->get_volume_envelope(),
126 SL_VOLUME_ENVELOPE );
135 // tell the scheduler to play the indexed sample once
136 bool FGSoundMgr::FGSoundMgr::play_once( const string& refname ) {
137 sound_map_iterator it = sounds.find( refname );
138 if ( it != sounds.end() ) {
139 FGSimpleSound *sample = it->second;
140 audio_sched->playSample( sample->get_sample() );
141 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
142 sample->get_pitch_envelope(),
144 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
145 sample->get_volume_envelope(),
146 SL_VOLUME_ENVELOPE );