]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundmgr.cxx
Working on vor audio ident (morse).
[flightgear.git] / src / Sound / soundmgr.cxx
1 // soundmgr.cxx -- Sound effect management class
2 //
3 // Sound manager initially written by David Findlay
4 // <david_j_findlay@yahoo.com.au> 2001
5 //
6 // C++-ified by Curtis Olson, started March 2001.
7 //
8 // Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
9 //
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25
26
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/fgpath.hxx>
29
30 #include <Main/globals.hxx>
31
32 #include "soundmgr.hxx"
33
34
35 // constructor
36 FGSimpleSound::FGSimpleSound( string file ) {
37     FGPath slfile( globals->get_fg_root() );
38     slfile.append( file );
39     sample = new slSample ( (char *)slfile.c_str() );
40     pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
41     volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
42     pitch_envelope->setStep ( 0, 0.01, 1.0 );
43     volume_envelope->setStep ( 0, 0.01, 1.0 );
44 }
45
46 FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len ) {
47     sample = new slSample ( buffer, len );
48     pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
49     volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
50     pitch_envelope->setStep ( 0, 0.01, 1.0 );
51     volume_envelope->setStep ( 0, 0.01, 1.0 );
52 }
53
54 // destructor
55 FGSimpleSound::~FGSimpleSound() {
56     delete pitch_envelope;
57     delete volume_envelope;
58     delete sample;
59 }
60
61
62 // constructor
63 FGSoundMgr::FGSoundMgr() {
64     audio_sched = new slScheduler( 8000 );
65     audio_mixer = new smMixer;
66
67     FG_LOG( FG_GENERAL, FG_INFO,
68             "Rate = " << audio_sched->getRate()
69             << "  Bps = " << audio_sched->getBps()
70             << "  Stereo = " << audio_sched->getStereo() );
71 }
72
73 // destructor
74 FGSoundMgr::~FGSoundMgr() {
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();
80         delete s;
81     }
82
83     delete audio_sched;
84     delete audio_mixer;
85 }
86
87
88 // initialize the sound manager
89 bool FGSoundMgr::init() {
90     audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
91     audio_sched -> setSafetyMargin ( 1.0 ) ;
92
93     sound_map_iterator current = sounds.begin();
94     sound_map_iterator end = sounds.end();
95     for ( ; current != end; ++current ) {
96         FGSimpleSound *s = current->second;
97         delete s->get_sample();
98         delete s;
99     }
100     sounds.clear();
101
102     if ( audio_sched->not_working() ) {
103         return false;
104     } else {
105         return true;
106     }
107 }
108
109
110 // run the audio scheduler
111 bool FGSoundMgr::update() {
112     if ( !audio_sched->not_working() ) {
113         audio_sched -> update();
114         return true;
115     } else {
116         return false;
117     }
118 }
119
120
121 // add a sound effect, return true if successful
122 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname  ) {
123     sounds[refname] = sound;
124
125     return true;
126 }
127
128
129 // remove a sound effect, return true if successful
130 bool FGSoundMgr::remove( const string& refname ) {
131     sound_map_iterator it = sounds.find( refname );
132     if ( it != sounds.end() ) {
133         // first stop the sound from playing (so we don't bomb the
134         // audio scheduler)
135         FGSimpleSound *sample = it->second;
136
137         cout << "Playing "
138              << sample->get_sample()->getPlayCount() << " instances!" << endl;
139
140         audio_sched->stopSample( sample->get_sample() );
141         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
142                                         NULL,
143                                         SL_PITCH_ENVELOPE );
144         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
145                                         NULL,
146                                         SL_VOLUME_ENVELOPE );
147
148         // must call audio_sched->update() after stopping the sound
149         // but before deleting it.
150         audio_sched -> update();
151         cout << "Still playing "
152              << sample->get_sample()->getPlayCount() << " instances!" << endl;
153
154         delete sample;
155         sounds.erase( it );
156
157         return true;
158    } else {
159         return false;
160     }
161 }
162
163
164 // return true of the specified sound exists in the sound manager system
165 bool FGSoundMgr::exists( const string& refname ) {
166     sound_map_iterator it = sounds.find( refname );
167     if ( it != sounds.end() ) {
168         return true;
169    } else {
170         return false;
171     }
172 }
173
174
175 // tell the scheduler to play the indexed sample in a continuous
176 // loop
177 bool FGSoundMgr::play_looped( const string& refname ) {
178     sound_map_iterator it = sounds.find( refname );
179     if ( it != sounds.end() ) {
180         FGSimpleSound *sample = it->second;
181         audio_sched->loopSample( sample->get_sample() );
182         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
183                                         sample->get_pitch_envelope(),
184                                         SL_PITCH_ENVELOPE );
185         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
186                                         sample->get_volume_envelope(),
187                                         SL_VOLUME_ENVELOPE );
188         
189         return true;
190     } else {
191         return false;
192     }
193 }
194
195
196 // tell the scheduler to play the indexed sample once
197 bool FGSoundMgr::FGSoundMgr::play_once( const string& refname ) {
198     sound_map_iterator it = sounds.find( refname );
199     if ( it != sounds.end() ) {
200         FGSimpleSound *sample = it->second;
201         audio_sched->stopSample( sample->get_sample() );
202         audio_sched->playSample( sample->get_sample() );
203         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
204                                         sample->get_pitch_envelope(),
205                                         SL_PITCH_ENVELOPE );
206         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
207                                         sample->get_volume_envelope(),
208                                         SL_VOLUME_ENVELOPE );
209         
210         return true;
211     } else {
212         return false;
213     }
214 }
215
216
217 // return true of the specified sound is currently being played
218 bool FGSoundMgr::is_playing( const string& refname ) {
219     sound_map_iterator it = sounds.find( refname );
220     if ( it != sounds.end() ) {
221         FGSimpleSound *sample = it->second;
222         return (sample->get_sample()->getPlayCount() > 0 );
223         return true;
224     } else {
225         return false;
226     }
227 }
228
229
230 // immediate stop playing the sound
231 bool FGSoundMgr::stop( const string& refname ) {
232     sound_map_iterator it = sounds.find( refname );
233     if ( it != sounds.end() ) {
234         FGSimpleSound *sample = it->second;
235         audio_sched->stopSample( sample->get_sample() );
236         return true;
237     } else {
238         return false;
239     }
240 }