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()
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/structure/exception.hxx>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/math/SGQuat.hxx>
36 #include "soundmgr_openal.hxx"
37 #include "sample_openal.hxx"
45 SGSoundSample::SGSoundSample() :
46 _absolute_pos(SGVec3d::zeros()),
47 _relative_pos(SGVec3d::zeros()),
48 _direction(SGVec3d::zeros()),
49 _velocity(SGVec3f::zeros()),
50 _orientation(SGQuatd::zeros()),
51 _orivec(SGVec3f::zeros()),
52 _base_pos(SGVec3d::zeros()),
53 _refname(random_string()),
55 _format(AL_FORMAT_MONO8),
59 _buffer(SGSoundMgr::NO_BUFFER),
61 _source(SGSoundMgr::NO_SOURCE),
68 _reference_dist(500.0),
73 _static_changed(true),
79 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
80 _absolute_pos(SGVec3d::zeros()),
81 _relative_pos(SGVec3d::zeros()),
82 _direction(SGVec3d::zeros()),
83 _velocity(SGVec3f::zeros()),
84 _orientation(SGQuatd::zeros()),
85 _orivec(SGVec3f::zeros()),
86 _base_pos(SGVec3d::zeros()),
89 _format(AL_FORMAT_MONO8),
93 _buffer(SGSoundMgr::NO_BUFFER),
95 _source(SGSoundMgr::NO_SOURCE),
102 _reference_dist(500.0),
107 _static_changed(true),
110 SGPath samplepath( path );
111 if ( strlen(file) ) {
112 samplepath.append( file );
114 _refname = samplepath.str();
118 SGSoundSample::SGSoundSample( const unsigned char** data,
119 int len, int freq, int format ) :
120 _absolute_pos(SGVec3d::zeros()),
121 _relative_pos(SGVec3d::zeros()),
122 _direction(SGVec3d::zeros()),
123 _velocity(SGVec3f::zeros()),
124 _orientation(SGQuatd::zeros()),
125 _orivec(SGVec3f::zeros()),
126 _base_pos(SGVec3d::zeros()),
127 _refname(random_string()),
131 _valid_buffer(false),
132 _buffer(SGSoundMgr::NO_BUFFER),
133 _valid_source(false),
134 _source(SGSoundMgr::NO_SOURCE),
141 _reference_dist(500.0),
146 _static_changed(true),
149 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
150 _data = (unsigned char*)*data; *data = NULL;
154 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
155 _absolute_pos(SGVec3d::zeros()),
156 _relative_pos(SGVec3d::zeros()),
157 _direction(SGVec3d::zeros()),
158 _velocity(SGVec3f::zeros()),
159 _orientation(SGQuatd::zeros()),
160 _orivec(SGVec3f::zeros()),
161 _base_pos(SGVec3d::zeros()),
162 _refname(random_string()),
166 _valid_buffer(false),
167 _buffer(SGSoundMgr::NO_BUFFER),
168 _valid_source(false),
169 _source(SGSoundMgr::NO_SOURCE),
176 _reference_dist(500.0),
181 _static_changed(true),
184 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
185 _data = (unsigned char*)*data; *data = NULL;
190 SGSoundSample::~SGSoundSample() {
191 if (_data) free(_data);
194 void SGSoundSample::update_pos_and_orientation() {
196 _absolute_pos = _base_pos;
197 if ( _relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
198 _absolute_pos += _orientation.backTransform(_relative_pos);
201 if ( _direction[0] || _direction[1] || _direction[2] ) {
202 _orivec = toVec3f( _orientation.rotate(_direction) );
205 _orivec = SGVec3f::zeros();
208 string SGSoundSample::random_string() {
209 static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
210 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
211 string rstr = "System generated name: ";
212 for (int i=0; i<10; i++) {
213 rstr.push_back( r[rand() % strlen(r)] );