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 <simgear/debug/logstream.hxx>
30 #include <simgear/structure/exception.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/math/SGQuat.hxx>
34 #include "soundmgr_openal.hxx"
35 #include "sample_openal.hxx"
43 SGSoundSample::SGSoundSample() :
44 _absolute_pos(SGVec3d::zeros()),
45 _relative_pos(SGVec3d::zeros()),
46 _direction(SGVec3d::zeros()),
47 _velocity(SGVec3d::zeros()),
48 _orientation(SGQuatd::zeros()),
49 _orivec(SGVec3f::zeros()),
53 _format(AL_FORMAT_MONO8),
57 _buffer(SGSoundMgr::NO_BUFFER),
59 _source(SGSoundMgr::NO_SOURCE),
66 _reference_dist(500.0),
71 _static_changed(true),
77 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
78 _absolute_pos(SGVec3d::zeros()),
79 _relative_pos(SGVec3d::zeros()),
80 _direction(SGVec3d::zeros()),
81 _velocity(SGVec3d::zeros()),
82 _orientation(SGQuatd::zeros()),
83 _orivec(SGVec3f::zeros()),
85 _format(AL_FORMAT_MONO8),
89 _buffer(SGSoundMgr::NO_BUFFER),
91 _source(SGSoundMgr::NO_SOURCE),
98 _reference_dist(500.0),
103 _static_changed(true),
106 SGPath samplepath( path );
107 if ( strlen(file) ) {
108 samplepath.append( file );
110 _refname = samplepath.str();
112 SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
113 << samplepath.str() );
117 SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format ) :
118 _absolute_pos(SGVec3d::zeros()),
119 _relative_pos(SGVec3d::zeros()),
120 _direction(SGVec3d::zeros()),
121 _velocity(SGVec3d::zeros()),
122 _orientation(SGQuatd::zeros()),
123 _orivec(SGVec3f::zeros()),
129 _valid_buffer(false),
130 _buffer(SGSoundMgr::NO_BUFFER),
131 _valid_source(false),
132 _source(SGSoundMgr::NO_SOURCE),
139 _reference_dist(500.0),
144 _static_changed(true),
147 _refname = "unknown, data supplied by caller";
148 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
153 SGSoundSample::~SGSoundSample() {
156 void SGSoundSample::set_orientation( const SGQuatd& ori ) {
158 update_absolute_position();
162 void SGSoundSample::set_direction( const SGVec3d& dir ) {
164 update_absolute_position();
168 void SGSoundSample::set_relative_position( const SGVec3f& pos ) {
169 _relative_pos = toVec3d(pos);
170 update_absolute_position();
174 void SGSoundSample::set_position( const SGGeod& pos ) {
176 update_absolute_position();
180 void SGSoundSample::update_absolute_position() {
181 SGQuatd orient = SGQuatd::fromLonLat(_base_pos) * _orientation;
182 _orivec = -toVec3f(orient.rotate(_direction));
184 orient = SGQuatd::fromRealImag(0, _relative_pos) * _orientation;
185 _absolute_pos = -SGVec3d::fromGeod(_base_pos) -orient.rotate(SGVec3d::e1());