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 _direction(SGVec3f::zeros().data()),
46 _velocity(SGVec3f::zeros().data()),
49 _format(AL_FORMAT_MONO8),
53 _buffer(SGSoundMgr::NO_BUFFER),
55 _source(SGSoundMgr::NO_SOURCE),
62 _reference_dist(500.0),
67 _static_changed(true),
73 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
74 _absolute_pos(SGVec3d::zeros().data()),
75 _relative_pos(SGVec3f::zeros().data()),
76 _base_pos(SGVec3d::zeros().data()),
77 _direction(SGVec3f::zeros().data()),
78 _velocity(SGVec3f::zeros().data()),
79 _format(AL_FORMAT_MONO8),
83 _buffer(SGSoundMgr::NO_BUFFER),
85 _source(SGSoundMgr::NO_SOURCE),
92 _reference_dist(500.0),
97 _static_changed(true),
100 SGPath samplepath( path );
101 if ( strlen(file) ) {
102 samplepath.append( file );
104 _sample_name = samplepath.str();
106 SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
107 << samplepath.str() );
111 SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format ) :
112 _absolute_pos(SGVec3d::zeros().data()),
113 _relative_pos(SGVec3f::zeros().data()),
114 _base_pos(SGVec3d::zeros().data()),
115 _direction(SGVec3f::zeros().data()),
116 _velocity(SGVec3f::zeros().data()),
121 _valid_buffer(false),
122 _buffer(SGSoundMgr::NO_BUFFER),
123 _valid_source(false),
124 _source(SGSoundMgr::NO_SOURCE),
131 _reference_dist(500.0),
136 _static_changed(true),
139 _sample_name = "unknown, data supplied by caller";
140 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
145 SGSoundSample::~SGSoundSample() {
148 void SGSoundSample::set_base_position( SGVec3d pos ) {
150 update_absolute_position();
154 void SGSoundSample::set_relative_position( SGVec3f pos ) {
156 update_absolute_position();
160 void SGSoundSample::set_orientation( SGVec3f dir ) {
162 update_absolute_position();
166 void SGSoundSample::update_absolute_position() {
167 SGQuatf orient = SGQuatf::fromAngleAxis(_direction);
168 SGVec3f modified_relative_pos = orient.transform(_relative_pos);
169 _absolute_pos = _base_pos + toVec3d(modified_relative_pos);