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 );
44 FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len ) {
45 sample = new slSample ( buffer, len );
46 pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
47 volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
48 pitch_envelope->setStep ( 0, 0.01, 1.0 );
49 volume_envelope->setStep ( 0, 0.01, 1.0 );
53 FGSimpleSound::~FGSimpleSound() {
54 delete pitch_envelope;
55 delete volume_envelope;
61 FGSoundMgr::FGSoundMgr() {
62 audio_sched = new slScheduler( 8000 );
63 audio_mixer = new smMixer;
67 FGSoundMgr::~FGSoundMgr() {
68 sound_map_iterator current = sounds.begin();
69 sound_map_iterator end = sounds.end();
70 for ( ; current != end; ++current ) {
71 FGSimpleSound *s = current->second;
72 delete s->get_sample();
81 // initialize the sound manager
82 bool FGSoundMgr::init() {
83 audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
84 audio_sched -> setSafetyMargin ( 1.0 ) ;
86 sound_map_iterator current = sounds.begin();
87 sound_map_iterator end = sounds.end();
88 for ( ; current != end; ++current ) {
89 FGSimpleSound *s = current->second;
90 delete s->get_sample();
95 if ( audio_sched->not_working() ) {
103 // run the audio scheduler
104 bool FGSoundMgr::update() {
105 if ( !audio_sched->not_working() ) {
106 audio_sched -> update();
114 // add a sound effect
115 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
116 sounds[refname] = sound;
122 // tell the scheduler to play the indexed sample in a continuous
124 bool FGSoundMgr::play_looped( const string& refname ) {
125 sound_map_iterator it = sounds.find( refname );
126 if ( it != sounds.end() ) {
127 FGSimpleSound *sample = it->second;
128 audio_sched->loopSample( sample->get_sample() );
129 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
130 sample->get_pitch_envelope(),
132 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
133 sample->get_volume_envelope(),
134 SL_VOLUME_ENVELOPE );
143 // tell the scheduler to play the indexed sample once
144 bool FGSoundMgr::FGSoundMgr::play_once( const string& refname ) {
145 sound_map_iterator it = sounds.find( refname );
146 if ( it != sounds.end() ) {
147 FGSimpleSound *sample = it->second;
148 audio_sched->playSample( sample->get_sample() );
149 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
150 sample->get_pitch_envelope(),
152 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
153 sample->get_volume_envelope(),
154 SL_VOLUME_ENVELOPE );