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().data()),
43 _relative_pos(SGVec3f::zeros().data()),
44 _base_pos(SGVec3d::zeros().data()),
45 _orientation(SGVec3f::zeros().data()),
46 _direction(SGVec3f::zeros().data()),
47 _velocity(SGVec3f::zeros().data()),
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().data()),
76 _relative_pos(SGVec3f::zeros().data()),
77 _base_pos(SGVec3d::zeros().data()),
78 _orientation(SGVec3f::zeros().data()),
79 _direction(SGVec3f::zeros().data()),
80 _velocity(SGVec3f::zeros().data()),
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().data()),
115 _relative_pos(SGVec3f::zeros().data()),
116 _base_pos(SGVec3d::zeros().data()),
117 _orientation(SGVec3f::zeros().data()),
118 _direction(SGVec3f::zeros().data()),
119 _velocity(SGVec3f::zeros().data()),
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 float *SGSoundSample::get_orientation() {
153 SGQuatf quat = SGQuatf::fromAngleAxis(_orientation);
154 SGVec3f orient = quat.transform(_direction);
155 return orient.data();
157 return _orientation.data();
161 void SGSoundSample::set_base_position( SGVec3d pos ) {
163 update_absolute_position();
167 void SGSoundSample::set_relative_position( SGVec3f pos ) {
169 update_absolute_position();
173 void SGSoundSample::set_orientation( SGVec3f ori ) {
175 update_absolute_position();
179 void SGSoundSample::set_direction( SGVec3f dir ) {
181 update_absolute_position();
185 void SGSoundSample::update_absolute_position() {
187 SGQuatf orient = SGQuatf::fromAngleAxis(_orientation);
188 SGVec3f modified_relative_pos = orient.transform(_relative_pos);
189 _absolute_pos = _base_pos + toVec3d(modified_relative_pos);
191 _absolute_pos = _base_pos;