1 // voiceplayer.cxx -- voice/sound sample player
3 // Written by Jean-Yves Lefort, started September 2005.
5 // Copyright (C) 2005, 2006 Jean-Yves Lefort - jylefort@FreeBSD.org
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.
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.
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.
21 ///////////////////////////////////////////////////////////////////////////////
24 # pragma warning( disable: 4355 )
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/sound/soundmgr_openal.hxx>
41 #include <simgear/structure/exception.hxx>
45 #include "voiceplayer.hxx"
47 ///////////////////////////////////////////////////////////////////////////////
48 // constants //////////////////////////////////////////////////////////////////
49 ///////////////////////////////////////////////////////////////////////////////
52 ///////////////////////////////////////////////////////////////////////////////
53 // helpers ////////////////////////////////////////////////////////////////////
54 ///////////////////////////////////////////////////////////////////////////////
55 #define ADD_VOICE(Var,Sample,Twice) \
56 { make_voice(&Var);append(Var,Sample);\
57 if (Twice) append(Var,Sample); }
59 #define test_bits(_bits, _test) (((_bits) & (_test)) != 0)
61 ///////////////////////////////////////////////////////////////////////////////
62 // FGVoicePlayer //////////////////////////////////////////////////////////////
63 ///////////////////////////////////////////////////////////////////////////////
66 FGVoicePlayer::Speaker::bind (SGPropertyNode *node)
68 // uses xmlsound property names
69 tie(node, "volume", &volume);
70 tie(node, "pitch", &pitch);
74 FGVoicePlayer::Speaker::update_configuration ()
76 map< string, SGSharedPtr<SGSoundSample> >::iterator iter;
77 for (iter = player->samples.begin(); iter != player->samples.end(); iter++)
79 SGSoundSample *sample = (*iter).second;
81 sample->set_pitch(pitch);
85 player->voice->volume_changed();
88 FGVoicePlayer::Voice::~Voice ()
90 for (iter = elements.begin(); iter != elements.end(); iter++)
91 delete *iter; // we owned the element
96 FGVoicePlayer::Voice::play ()
98 iter = elements.begin();
101 element->play(get_volume());
105 FGVoicePlayer::Voice::stop (bool now)
109 if (now || element->silence)
115 iter = elements.end() - 1; // stop after the current element finishes
120 FGVoicePlayer::Voice::set_volume (float _volume)
127 FGVoicePlayer::Voice::volume_changed ()
130 element->set_volume(get_volume());
134 FGVoicePlayer::Voice::update ()
138 if (! element->is_playing())
140 if (++iter == elements.end())
145 element->play(get_volume());
151 FGVoicePlayer::~FGVoicePlayer ()
153 vector<Voice *>::iterator iter1;
154 for (iter1 = _voices.begin(); iter1 != _voices.end(); iter1++)
161 FGVoicePlayer::bind (SGPropertyNode *node, const char* default_dir_prefix)
163 dir_prefix = node->getStringValue("voice/file-prefix", default_dir_prefix);
168 FGVoicePlayer::init ()
170 SGSoundMgr *smgr = globals->get_soundmgr();
171 _sgr = smgr->find("avionics", true);
172 _sgr->tie_to_listener();
173 speaker.update_configuration();
177 FGVoicePlayer::pause()
190 FGVoicePlayer::resume()
202 FGVoicePlayer::get_sample (const char *name)
205 refname = dev_name + "/" + dir_prefix + name;
207 SGSoundSample *sample = _sgr->find(refname);
210 string filename = dir_prefix + string(name) + ".wav";
213 sample = new SGSoundSample(filename.c_str(), SGPath());
215 catch (const sg_exception &e)
217 SG_LOG(SG_INSTR, SG_ALERT, "Error loading sound sample \"" + filename + "\": " + e.getFormattedMessage());
221 _sgr->add(sample, refname);
222 samples[refname] = sample;
229 FGVoicePlayer::play (Voice *_voice, unsigned int flags)
233 if (test_bits(flags, PLAY_NOW) || ! voice ||
234 (voice->element && voice->element->silence))
240 looped = test_bits(flags, PLAY_LOOPED);
251 next_looped = test_bits(flags, PLAY_LOOPED);
256 FGVoicePlayer::stop (unsigned int flags)
260 voice->stop(test_bits(flags, STOP_NOW));
270 FGVoicePlayer::set_volume (float _volume)
274 voice->volume_changed();
278 FGVoicePlayer::update ()
286 if (! voice->element || voice->element->silence)
289 looped = next_looped;
299 if (! voice->element)