]> git.mxchange.org Git - flightgear.git/blob - src/Main/soundmgr.cxx
Continued work on morse code generator.
[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 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 );
50 }
51
52 // destructor
53 FGSimpleSound::~FGSimpleSound() {
54     delete pitch_envelope;
55     delete volume_envelope;
56     delete sample;
57 }
58
59
60 // constructor
61 FGSoundMgr::FGSoundMgr() {
62     audio_sched = new slScheduler( 8000 );
63     audio_mixer = new smMixer;
64 }
65
66 // destructor
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();
73         delete s;
74     }
75
76     delete audio_sched;
77     delete audio_mixer;
78 }
79
80
81 // initialize the sound manager
82 bool FGSoundMgr::init() {
83     audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
84     audio_sched -> setSafetyMargin ( 1.0 ) ;
85
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();
91         delete s;
92     }
93     sounds.clear();
94
95     if ( audio_sched->not_working() ) {
96         return false;
97     } else {
98         return true;
99     }
100 }
101
102
103 // run the audio scheduler
104 bool FGSoundMgr::update() {
105     if ( !audio_sched->not_working() ) {
106         audio_sched -> update();
107         return true;
108     } else {
109         return false;
110     }
111 }
112
113
114 // add a sound effect
115 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname  ) {
116     sounds[refname] = sound;
117
118     return true;
119 }
120
121
122 // tell the scheduler to play the indexed sample in a continuous
123 // loop
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(),
131                                         SL_PITCH_ENVELOPE );
132         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
133                                         sample->get_volume_envelope(),
134                                         SL_VOLUME_ENVELOPE );
135         
136         return true;
137     } else {
138         return false;
139     }
140 }
141
142
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(),
151                                         SL_PITCH_ENVELOPE );
152         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
153                                         sample->get_volume_envelope(),
154                                         SL_VOLUME_ENVELOPE );
155         
156         return true;
157     } else {
158         return false;
159     }
160 }