]> git.mxchange.org Git - flightgear.git/blob - src/Main/soundmgr.cxx
Fixes to initial position for JSBSim.
[flightgear.git] / src / Main / 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/misc/fgpath.hxx>
28
29 #include "globals.hxx"
30 #include "soundmgr.hxx"
31
32
33 // constructor
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 );
42 }
43
44 // destructor
45 FGSimpleSound::~FGSimpleSound() {
46     delete pitch_envelope;
47     delete volume_envelope;
48     delete sample;
49 }
50
51
52 // constructor
53 FGSoundMgr::FGSoundMgr() {
54     audio_sched = new slScheduler( 8000 );
55     audio_mixer = new smMixer;
56 }
57
58 // destructor
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();
65         delete s;
66     }
67
68     delete audio_sched;
69     delete audio_mixer;
70 }
71
72
73 // initialize the sound manager
74 bool FGSoundMgr::init() {
75     audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
76     audio_sched -> setSafetyMargin ( 1.0 ) ;
77
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();
83         delete s;
84     }
85     sounds.clear();
86
87     if ( audio_sched->not_working() ) {
88         return false;
89     } else {
90         return true;
91     }
92 }
93
94
95 // run the audio scheduler
96 bool FGSoundMgr::update() {
97     if ( !audio_sched->not_working() ) {
98         audio_sched -> update();
99         return true;
100     } else {
101         return false;
102     }
103 }
104
105
106 // add a sound effect
107 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname  ) {
108     sounds[refname] = sound;
109
110     return true;
111 }
112
113
114 // tell the scheduler to play the indexed sample in a continuous
115 // loop
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(),
123                                         SL_PITCH_ENVELOPE );
124         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
125                                         sample->get_volume_envelope(),
126                                         SL_VOLUME_ENVELOPE );
127         
128         return true;
129     } else {
130         return false;
131     }
132 }
133
134
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(),
143                                         SL_PITCH_ENVELOPE );
144         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
145                                         sample->get_volume_envelope(),
146                                         SL_VOLUME_ENVELOPE );
147         
148         return true;
149     } else {
150         return false;
151     }
152 }