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 <stdlib.h> // rand(), free()
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/structure/exception.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/misc/ResourceManager.hxx>
37 #include "soundmgr_openal.hxx"
38 #include "sample_openal.hxx"
39 #include "soundmgr_openal_private.hxx"
50 SGSoundSample::SGSoundSample() :
51 _format(AL_FORMAT_MONO8),
56 _source(SGSoundMgr::NO_SOURCE),
57 _absolute_pos(SGVec3d::zeros()),
58 _relative_pos(SGVec3d::zeros()),
59 _direction(SGVec3d::zeros()),
60 _velocity(SGVec3f::zeros()),
61 _orientation(SGQuatd::zeros()),
62 _orivec(SGVec3f::zeros()),
63 _base_pos(SGVec3d::zeros()),
64 _rotation(SGQuatd::zeros()),
65 _refname(random_string()),
68 _buffer(SGSoundMgr::NO_BUFFER),
75 _reference_dist(500.0),
79 _static_changed(true),
86 SGSoundSample::SGSoundSample(const char *file, const SGPath& currentDir) :
87 _format(AL_FORMAT_MONO8),
92 _source(SGSoundMgr::NO_SOURCE),
93 _absolute_pos(SGVec3d::zeros()),
94 _relative_pos(SGVec3d::zeros()),
95 _direction(SGVec3d::zeros()),
96 _velocity(SGVec3f::zeros()),
97 _orientation(SGQuatd::zeros()),
98 _orivec(SGVec3f::zeros()),
99 _base_pos(SGVec3d::zeros()),
100 _rotation(SGQuatd::zeros()),
103 _valid_buffer(false),
104 _buffer(SGSoundMgr::NO_BUFFER),
111 _reference_dist(500.0),
115 _static_changed(true),
116 _out_of_range(false),
119 SGPath p = simgear::ResourceManager::instance()->findPath(file, currentDir);
124 SGSoundSample::SGSoundSample( const unsigned char** data,
125 int len, int freq, int format ) :
130 _valid_source(false),
131 _source(SGSoundMgr::NO_SOURCE),
132 _absolute_pos(SGVec3d::zeros()),
133 _relative_pos(SGVec3d::zeros()),
134 _direction(SGVec3d::zeros()),
135 _velocity(SGVec3f::zeros()),
136 _orientation(SGQuatd::zeros()),
137 _orivec(SGVec3f::zeros()),
138 _base_pos(SGVec3d::zeros()),
139 _rotation(SGQuatd::zeros()),
140 _refname(random_string()),
141 _valid_buffer(false),
142 _buffer(SGSoundMgr::NO_BUFFER),
149 _reference_dist(500.0),
153 _static_changed(true),
154 _out_of_range(false),
157 SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
158 _data = (unsigned char*)*data; *data = NULL;
162 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
167 _valid_source(false),
168 _source(SGSoundMgr::NO_SOURCE),
169 _absolute_pos(SGVec3d::zeros()),
170 _relative_pos(SGVec3d::zeros()),
171 _direction(SGVec3d::zeros()),
172 _velocity(SGVec3f::zeros()),
173 _orientation(SGQuatd::zeros()),
174 _orivec(SGVec3f::zeros()),
175 _base_pos(SGVec3d::zeros()),
176 _rotation(SGQuatd::zeros()),
177 _refname(random_string()),
178 _valid_buffer(false),
179 _buffer(SGSoundMgr::NO_BUFFER),
186 _reference_dist(500.0),
190 _static_changed(true),
191 _out_of_range(false),
194 SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
195 _data = (unsigned char*)*data; *data = NULL;
200 SGSoundSample::~SGSoundSample() {
201 if ( _data != NULL ) free(_data);
204 void SGSoundSample::update_pos_and_orientation() {
206 _absolute_pos = _base_pos;
207 if (_relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
208 _absolute_pos += _rotation.rotate( _relative_pos );
211 _orivec = SGVec3f::zeros();
212 if ( _direction[0] || _direction[1] || _direction[2] ) {
213 _orivec = toVec3f( _rotation.rotate( _direction ) );
217 string SGSoundSample::random_string() {
218 static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
219 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
220 string rstr = "System generated name: ";
221 for (int i=0; i<10; i++) {
222 rstr.push_back( r[rand() % strlen(r)] );
228 SGPath SGSoundSample::file_path() const
234 return SGPath(_refname);
237 void SGSoundSample::free_data()
239 if ( _data != NULL ) free( _data ); _data = NULL;