1 // sample.cxx -- Sound sample encapsulation class
3 // Written by Curtis Olson, started April 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
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 Foundation,
19 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 # include <simgear_config.h>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/structure/exception.hxx>
29 #include <simgear/misc/sg_path.hxx>
30 #include <simgear/math/SGQuat.hxx>
32 #include "soundmgr_openal.hxx"
33 #include "sample_openal.hxx"
41 SGSoundSample::SGSoundSample() :
42 _absolute_pos(SGVec3d::zeros()),
43 _relative_pos(SGVec3d::zeros()),
44 _direction(SGVec3d::zeros()),
45 _velocity(SGVec3d::zeros()),
47 _orientation(SGQuatd::zeros()),
50 _format(AL_FORMAT_MONO8),
54 _buffer(SGSoundMgr::NO_BUFFER),
56 _source(SGSoundMgr::NO_SOURCE),
63 _reference_dist(500.0),
68 _static_changed(true),
70 _orivec(SGVec3f::zeros())
75 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
76 _absolute_pos(SGVec3d::zeros()),
77 _relative_pos(SGVec3d::zeros()),
78 _direction(SGVec3d::zeros()),
79 _velocity(SGVec3d::zeros()),
81 _orientation(SGQuatd::zeros()),
82 _format(AL_FORMAT_MONO8),
86 _buffer(SGSoundMgr::NO_BUFFER),
88 _source(SGSoundMgr::NO_SOURCE),
95 _reference_dist(500.0),
100 _static_changed(true),
102 _orivec(SGVec3f::zeros())
104 SGPath samplepath( path );
105 if ( strlen(file) ) {
106 samplepath.append( file );
108 _sample_name = samplepath.str();
110 SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
111 << samplepath.str() );
115 SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format ) :
116 _absolute_pos(SGVec3d::zeros()),
117 _relative_pos(SGVec3d::zeros()),
118 _direction(SGVec3d::zeros()),
119 _velocity(SGVec3d::zeros()),
121 _orientation(SGQuatd::zeros()),
126 _valid_buffer(false),
127 _buffer(SGSoundMgr::NO_BUFFER),
128 _valid_source(false),
129 _source(SGSoundMgr::NO_SOURCE),
136 _reference_dist(500.0),
141 _static_changed(true),
143 _orivec(SGVec3f::zeros())
145 _sample_name = "unknown, data supplied by caller";
146 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
151 SGSoundSample::~SGSoundSample() {
154 void SGSoundSample::set_orientation( SGQuatd ori ) {
156 update_absolute_position();
160 void SGSoundSample::set_direction( SGVec3d dir ) {
162 update_absolute_position();
166 void SGSoundSample::set_relative_position( SGVec3f pos ) {
167 _relative_pos = toVec3d(pos);
168 update_absolute_position();
172 void SGSoundSample::set_position( SGGeod pos ) {
174 update_absolute_position();
178 void SGSoundSample::update_absolute_position() {
179 SGQuatd orient = SGQuatd::fromLonLat(_base_pos) * _orientation;
180 _orivec = -toVec3f(orient.rotate(_direction));
182 orient = SGQuatd::fromRealImag(0, _relative_pos) * _orientation;
183 _absolute_pos = -SGVec3d::fromGeod(_base_pos) -orient.rotate(SGVec3d::e1());