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(SGVec3f::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),
74 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
75 _absolute_pos(SGVec3d::zeros()),
76 _relative_pos(SGVec3f::zeros()),
77 _direction(SGVec3d::zeros()),
78 _velocity(SGVec3d::zeros()),
80 _orientation(SGQuatd::zeros()),
81 _format(AL_FORMAT_MONO8),
85 _buffer(SGSoundMgr::NO_BUFFER),
87 _source(SGSoundMgr::NO_SOURCE),
94 _reference_dist(500.0),
99 _static_changed(true),
102 SGPath samplepath( path );
103 if ( strlen(file) ) {
104 samplepath.append( file );
106 _sample_name = samplepath.str();
108 SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
109 << samplepath.str() );
113 SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format ) :
114 _absolute_pos(SGVec3d::zeros()),
115 _relative_pos(SGVec3f::zeros()),
116 _direction(SGVec3d::zeros()),
117 _velocity(SGVec3d::zeros()),
119 _orientation(SGQuatd::zeros()),
124 _valid_buffer(false),
125 _buffer(SGSoundMgr::NO_BUFFER),
126 _valid_source(false),
127 _source(SGSoundMgr::NO_SOURCE),
134 _reference_dist(500.0),
139 _static_changed(true),
142 _sample_name = "unknown, data supplied by caller";
143 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
148 SGSoundSample::~SGSoundSample() {
151 void SGSoundSample::set_orientation( SGQuatd ori ) {
153 update_absolute_position();
157 void SGSoundSample::set_direction( SGVec3d dir ) {
159 update_absolute_position();
163 float *SGSoundSample::get_orientation() {
164 SGQuatd orient = SGQuatd::fromLonLat(_base_pos) * _orientation;
165 SGVec3d orivec = -orient.rotate(_direction);
166 return toVec3f(orivec).data();
169 void SGSoundSample::set_relative_position( SGVec3f pos ) {
171 update_absolute_position();
175 void SGSoundSample::set_position( SGGeod pos ) {
177 update_absolute_position();
181 void SGSoundSample::update_absolute_position() {
182 _absolute_pos = -SGVec3d::fromGeod(_base_pos);
183 // TODO: add relative position