]> git.mxchange.org Git - flightgear.git/blob - src/Main/soundmgr.cxx
Added a first pass at a C++ sound manager class.
[flightgear.git] / src / Main / soundmgr.cxx
1 // soundmgr.cxx -- Sound effect management class
2 //
3 // Written by Curtis Olson, started March 2001.
4 //
5 // Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #include <simgear/misc/fgpath.hxx>
25
26 #include "globals.hxx"
27 #include "soundmgr.hxx"
28
29
30 // constructor
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 );
39 }
40
41 // destructor
42 FGSimpleSound::~FGSimpleSound() {
43     delete pitch_envelope;
44     delete volume_envelope;
45     delete sample;
46 }
47
48
49 // constructor
50 FGSoundMgr::FGSoundMgr() {
51     audio_sched = new slScheduler( 8000 );
52     audio_mixer = new smMixer;
53 }
54
55 // destructor
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();
62         delete s;
63     }
64
65     delete audio_sched;
66     delete audio_mixer;
67 }
68
69
70 // initialize the sound manager
71 bool FGSoundMgr::init() {
72     audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
73     audio_sched -> setSafetyMargin ( 1.0 ) ;
74
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     sounds.clear();
83
84     if ( audio_sched->not_working() ) {
85         return false;
86     } else {
87         return true;
88     }
89 }
90
91
92 // run the audio scheduler
93 bool FGSoundMgr::update() {
94     if ( !audio_sched->not_working() ) {
95         audio_sched -> update();
96         return true;
97     } else {
98         return false;
99     }
100 }
101
102
103 // add a sound effect
104 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname  ) {
105     sounds[refname] = sound;
106
107     return true;
108 }
109
110
111 // tell the scheduler to play the indexed sample in a continuous
112 // loop
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(),
120                                         SL_PITCH_ENVELOPE );
121         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
122                                         sample->get_volume_envelope(),
123                                         SL_VOLUME_ENVELOPE );
124         
125         return true;
126     } else {
127         return false;
128     }
129 }
130
131
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(),
140                                         SL_PITCH_ENVELOPE );
141         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
142                                         sample->get_volume_envelope(),
143                                         SL_VOLUME_ENVELOPE );
144         
145         return true;
146     } else {
147         return false;
148     }
149 }