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()
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"
46 SGSoundSample::SGSoundSample() :
47 _absolute_pos(SGVec3d::zeros()),
48 _relative_pos(SGVec3d::zeros()),
49 _direction(SGVec3d::zeros()),
50 _velocity(SGVec3f::zeros()),
51 _orientation(SGQuatd::zeros()),
52 _orivec(SGVec3f::zeros()),
53 _base_pos(SGVec3d::zeros()),
54 _rotation(SGQuatd::zeros()),
55 _refname(random_string()),
57 _format(AL_FORMAT_MONO8),
61 _buffer(SGSoundMgr::NO_BUFFER),
63 _source(SGSoundMgr::NO_SOURCE),
70 _reference_dist(500.0),
75 _static_changed(true),
82 SGSoundSample::SGSoundSample(const char *file, const SGPath& currentDir) :
83 _absolute_pos(SGVec3d::zeros()),
84 _relative_pos(SGVec3d::zeros()),
85 _direction(SGVec3d::zeros()),
86 _velocity(SGVec3f::zeros()),
87 _orientation(SGQuatd::zeros()),
88 _orivec(SGVec3f::zeros()),
89 _base_pos(SGVec3d::zeros()),
90 _rotation(SGQuatd::zeros()),
93 _format(AL_FORMAT_MONO8),
97 _buffer(SGSoundMgr::NO_BUFFER),
99 _source(SGSoundMgr::NO_SOURCE),
106 _reference_dist(500.0),
111 _static_changed(true),
112 _out_of_range(false),
115 SGPath p = simgear::ResourceManager::instance()->findPath(file, currentDir);
120 SGSoundSample::SGSoundSample( const unsigned char** data,
121 int len, int freq, int format ) :
122 _absolute_pos(SGVec3d::zeros()),
123 _relative_pos(SGVec3d::zeros()),
124 _direction(SGVec3d::zeros()),
125 _velocity(SGVec3f::zeros()),
126 _orientation(SGQuatd::zeros()),
127 _orivec(SGVec3f::zeros()),
128 _base_pos(SGVec3d::zeros()),
129 _rotation(SGQuatd::zeros()),
130 _refname(random_string()),
134 _valid_buffer(false),
135 _buffer(SGSoundMgr::NO_BUFFER),
136 _valid_source(false),
137 _source(SGSoundMgr::NO_SOURCE),
144 _reference_dist(500.0),
149 _static_changed(true),
150 _out_of_range(false),
153 SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
154 _data = (unsigned char*)*data; *data = NULL;
158 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
159 _absolute_pos(SGVec3d::zeros()),
160 _relative_pos(SGVec3d::zeros()),
161 _direction(SGVec3d::zeros()),
162 _velocity(SGVec3f::zeros()),
163 _orientation(SGQuatd::zeros()),
164 _orivec(SGVec3f::zeros()),
165 _base_pos(SGVec3d::zeros()),
166 _rotation(SGQuatd::zeros()),
167 _refname(random_string()),
171 _valid_buffer(false),
172 _buffer(SGSoundMgr::NO_BUFFER),
173 _valid_source(false),
174 _source(SGSoundMgr::NO_SOURCE),
181 _reference_dist(500.0),
186 _static_changed(true),
187 _out_of_range(false),
190 SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
191 _data = (unsigned char*)*data; *data = NULL;
196 SGSoundSample::~SGSoundSample() {
197 if ( _data != NULL ) free(_data);
200 void SGSoundSample::update_pos_and_orientation() {
202 _absolute_pos = _base_pos;
203 if (_relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
204 _absolute_pos += _rotation.rotate( _relative_pos );
207 _orivec = SGVec3f::zeros();
208 if ( _direction[0] || _direction[1] || _direction[2] ) {
209 _orivec = toVec3f( _rotation.rotate( _direction ) );
213 string SGSoundSample::random_string() {
214 static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
215 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
216 string rstr = "System generated name: ";
217 for (int i=0; i<10; i++) {
218 rstr.push_back( r[rand() % strlen(r)] );
224 SGPath SGSoundSample::file_path() const
230 return SGPath(_refname);