1 // soundmgr.cxx -- Sound effect management class
3 // Written by Curtis Olson, started March 2001.
5 // Copyright (C) 2001 Curtis L. Olson - curt@flightgear.org
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.
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.
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
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/misc/fgpath.hxx>
26 #include "globals.hxx"
27 #include "soundmgr.hxx"
31 FGSimpleSound::FGSimpleSound( string file ) {
32 FGPath slfile( globals->get_fg_root() );
33 slfile.append( file );
34 sample = new slSample ( (char *)slfile.c_str() );
35 pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
36 volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
37 pitch_envelope->setStep ( 0, 0.01, 1.0 );
38 volume_envelope->setStep ( 0, 0.01, 1.0 );
42 FGSimpleSound::~FGSimpleSound() {
43 delete pitch_envelope;
44 delete volume_envelope;
50 FGSoundMgr::FGSoundMgr() {
51 audio_sched = new slScheduler( 8000 );
52 audio_mixer = new smMixer;
56 FGSoundMgr::~FGSoundMgr() {
57 sound_map_iterator current = sounds.begin();
58 sound_map_iterator end = sounds.end();
59 for ( ; current != end; ++current ) {
60 FGSimpleSound *s = current->second;
61 delete s->get_sample();
70 // initialize the sound manager
71 bool FGSoundMgr::init() {
72 audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
73 audio_sched -> setSafetyMargin ( 1.0 ) ;
75 sound_map_iterator current = sounds.begin();
76 sound_map_iterator end = sounds.end();
77 for ( ; current != end; ++current ) {
78 FGSimpleSound *s = current->second;
79 delete s->get_sample();
84 if ( audio_sched->not_working() ) {
92 // run the audio scheduler
93 bool FGSoundMgr::update() {
94 if ( !audio_sched->not_working() ) {
95 audio_sched -> update();
103 // add a sound effect
104 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
105 sounds[refname] = sound;
111 // tell the scheduler to play the indexed sample in a continuous
113 bool FGSoundMgr::play_looped( const string& refname ) {
114 sound_map_iterator it = sounds.find( refname );
115 if ( it != sounds.end() ) {
116 FGSimpleSound *sample = it->second;
117 audio_sched->loopSample( sample->get_sample() );
118 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
119 sample->get_pitch_envelope(),
121 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
122 sample->get_volume_envelope(),
123 SL_VOLUME_ENVELOPE );
132 // tell the scheduler to play the indexed sample once
133 bool FGSoundMgr::FGSoundMgr::play_once( const string& refname ) {
134 sound_map_iterator it = sounds.find( refname );
135 if ( it != sounds.end() ) {
136 FGSimpleSound *sample = it->second;
137 audio_sched->playSample( sample->get_sample() );
138 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
139 sample->get_pitch_envelope(),
141 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
142 sample->get_volume_envelope(),
143 SL_VOLUME_ENVELOPE );