1 // sample_openal.cxx -- Audio sample encapsulation class
3 // Written by Curtis Olson, started April 2004.
4 // Modified to match the new SoundSystem by Erik Hofman, October 2009
6 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
7 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software Foundation,
21 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 # include <simgear_config.h>
29 #include <stdlib.h> // rand(), free()
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/structure/exception.hxx>
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/misc/ResourceManager.hxx>
38 #include "soundmgr_openal.hxx"
39 #include "sample_openal.hxx"
40 #include "soundmgr_openal_private.hxx"
51 SGSoundSample::SGSoundSample() :
52 _format(AL_FORMAT_MONO8),
57 _source(SGSoundMgr::NO_SOURCE),
58 _absolute_pos(SGVec3d::zeros()),
59 _relative_pos(SGVec3d::zeros()),
60 _direction(SGVec3d::zeros()),
61 _velocity(SGVec3f::zeros()),
62 _orientation(SGQuatd::zeros()),
63 _orivec(SGVec3f::zeros()),
64 _base_pos(SGVec3d::zeros()),
65 _rotation(SGQuatd::zeros()),
66 _refname(random_string()),
69 _buffer(SGSoundMgr::NO_BUFFER),
76 _reference_dist(500.0),
80 _static_changed(true),
91 SGSoundSample::SGSoundSample(const char *file, const SGPath& currentDir) :
92 _format(AL_FORMAT_MONO8),
97 _source(SGSoundMgr::NO_SOURCE),
98 _absolute_pos(SGVec3d::zeros()),
99 _relative_pos(SGVec3d::zeros()),
100 _direction(SGVec3d::zeros()),
101 _velocity(SGVec3f::zeros()),
102 _orientation(SGQuatd::zeros()),
103 _orivec(SGVec3f::zeros()),
104 _base_pos(SGVec3d::zeros()),
105 _rotation(SGQuatd::zeros()),
108 _valid_buffer(false),
109 _buffer(SGSoundMgr::NO_BUFFER),
116 _reference_dist(500.0),
120 _static_changed(true),
121 _out_of_range(false),
123 _use_pos_props(false)
125 SGPath p = simgear::ResourceManager::instance()->findPath(file, currentDir);
133 SGSoundSample::SGSoundSample( const unsigned char** data,
134 int len, int freq, int format ) :
139 _valid_source(false),
140 _source(SGSoundMgr::NO_SOURCE),
141 _absolute_pos(SGVec3d::zeros()),
142 _relative_pos(SGVec3d::zeros()),
143 _direction(SGVec3d::zeros()),
144 _velocity(SGVec3f::zeros()),
145 _orientation(SGQuatd::zeros()),
146 _orivec(SGVec3f::zeros()),
147 _base_pos(SGVec3d::zeros()),
148 _rotation(SGQuatd::zeros()),
149 _refname(random_string()),
150 _valid_buffer(false),
151 _buffer(SGSoundMgr::NO_BUFFER),
158 _reference_dist(500.0),
162 _static_changed(true),
163 _out_of_range(false),
165 _use_pos_props(false)
167 SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
168 _data = (unsigned char*)*data; *data = NULL;
175 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
180 _valid_source(false),
181 _source(SGSoundMgr::NO_SOURCE),
182 _absolute_pos(SGVec3d::zeros()),
183 _relative_pos(SGVec3d::zeros()),
184 _direction(SGVec3d::zeros()),
185 _velocity(SGVec3f::zeros()),
186 _orientation(SGQuatd::zeros()),
187 _orivec(SGVec3f::zeros()),
188 _base_pos(SGVec3d::zeros()),
189 _rotation(SGQuatd::zeros()),
190 _refname(random_string()),
191 _valid_buffer(false),
192 _buffer(SGSoundMgr::NO_BUFFER),
199 _reference_dist(500.0),
203 _static_changed(true),
204 _out_of_range(false),
206 _use_pos_props(false)
208 SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
209 _data = (unsigned char*)*data; *data = NULL;
217 SGSoundSample::~SGSoundSample() {
218 if ( _data != NULL ) free(_data);
221 void SGSoundSample::update_pos_and_orientation() {
223 if (_use_pos_props) {
224 if (_pos_prop[0]) _absolute_pos[0] = _pos_prop[0]->getDoubleValue();
225 if (_pos_prop[1]) _absolute_pos[1] = _pos_prop[1]->getDoubleValue();
226 if (_pos_prop[2]) _absolute_pos[2] = _pos_prop[2]->getDoubleValue();
229 _absolute_pos = _base_pos;
230 if (_relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
231 _absolute_pos += _rotation.rotate( _relative_pos );
235 _orivec = SGVec3f::zeros();
236 if ( _direction[0] || _direction[1] || _direction[2] ) {
237 _orivec = toVec3f( _rotation.rotate( _direction ) );
241 string SGSoundSample::random_string() {
242 static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
243 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
244 string rstr = "System generated name: ";
245 for (int i=0; i<10; i++) {
246 rstr.push_back( r[rand() % strlen(r)] );
252 SGPath SGSoundSample::file_path() const
258 return SGPath(_refname);
261 void SGSoundSample::free_data()
263 if ( _data != NULL ) free( _data ); _data = NULL;