00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef HAVE_CONFIG_H
00026 # include <simgear_config.h>
00027 #endif
00028
00029 #include <stdlib.h>
00030
00031 #include <simgear/debug/logstream.hxx>
00032 #include <simgear/structure/exception.hxx>
00033 #include <simgear/misc/sg_path.hxx>
00034 #include <simgear/math/SGMath.hxx>
00035
00036 #include "soundmgr_openal.hxx"
00037 #include "sample_openal.hxx"
00038
00039
00040
00041
00042
00043
00044
00045 SGSoundSample::SGSoundSample() :
00046 _absolute_pos(SGVec3d::zeros()),
00047 _relative_pos(SGVec3d::zeros()),
00048 _direction(SGVec3d::zeros()),
00049 _velocity(SGVec3f::zeros()),
00050 _orientation(SGQuatd::zeros()),
00051 _orivec(SGVec3f::zeros()),
00052 _base_pos(SGVec3d::zeros()),
00053 _rotation(SGQuatd::zeros()),
00054 _refname(random_string()),
00055 _data(NULL),
00056 _format(AL_FORMAT_MONO8),
00057 _size(0),
00058 _freq(0),
00059 _valid_buffer(false),
00060 _buffer(SGSoundMgr::NO_BUFFER),
00061 _valid_source(false),
00062 _source(SGSoundMgr::NO_SOURCE),
00063 _inner_angle(360.0),
00064 _outer_angle(360.0),
00065 _outer_gain(0.0),
00066 _pitch(1.0),
00067 _volume(1.0),
00068 _master_volume(1.0),
00069 _reference_dist(500.0),
00070 _max_dist(3000.0),
00071 _loop(AL_FALSE),
00072 _playing(false),
00073 _changed(true),
00074 _static_changed(true),
00075 _is_file(false)
00076 {
00077 }
00078
00079
00080 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
00081 _absolute_pos(SGVec3d::zeros()),
00082 _relative_pos(SGVec3d::zeros()),
00083 _direction(SGVec3d::zeros()),
00084 _velocity(SGVec3f::zeros()),
00085 _orientation(SGQuatd::zeros()),
00086 _orivec(SGVec3f::zeros()),
00087 _base_pos(SGVec3d::zeros()),
00088 _rotation(SGQuatd::zeros()),
00089 _refname(file),
00090 _data(NULL),
00091 _format(AL_FORMAT_MONO8),
00092 _size(0),
00093 _freq(0),
00094 _valid_buffer(false),
00095 _buffer(SGSoundMgr::NO_BUFFER),
00096 _valid_source(false),
00097 _source(SGSoundMgr::NO_SOURCE),
00098 _inner_angle(360.0),
00099 _outer_angle(360.0),
00100 _outer_gain(0.0),
00101 _pitch(1.0),
00102 _volume(1.0),
00103 _master_volume(1.0),
00104 _reference_dist(500.0),
00105 _max_dist(3000.0),
00106 _loop(AL_FALSE),
00107 _playing(false),
00108 _changed(true),
00109 _static_changed(true),
00110 _is_file(true)
00111 {
00112 SGPath samplepath( path );
00113 if ( strlen(file) ) {
00114 samplepath.append( file );
00115 }
00116 _refname = samplepath.str();
00117 }
00118
00119
00120 SGSoundSample::SGSoundSample( const unsigned char** data,
00121 int len, int freq, int format ) :
00122 _absolute_pos(SGVec3d::zeros()),
00123 _relative_pos(SGVec3d::zeros()),
00124 _direction(SGVec3d::zeros()),
00125 _velocity(SGVec3f::zeros()),
00126 _orientation(SGQuatd::zeros()),
00127 _orivec(SGVec3f::zeros()),
00128 _base_pos(SGVec3d::zeros()),
00129 _rotation(SGQuatd::zeros()),
00130 _refname(random_string()),
00131 _format(format),
00132 _size(len),
00133 _freq(freq),
00134 _valid_buffer(false),
00135 _buffer(SGSoundMgr::NO_BUFFER),
00136 _valid_source(false),
00137 _source(SGSoundMgr::NO_SOURCE),
00138 _inner_angle(360.0),
00139 _outer_angle(360.0),
00140 _outer_gain(0.0),
00141 _pitch(1.0),
00142 _volume(1.0),
00143 _master_volume(1.0),
00144 _reference_dist(500.0),
00145 _max_dist(3000.0),
00146 _loop(AL_FALSE),
00147 _playing(false),
00148 _changed(true),
00149 _static_changed(true),
00150 _is_file(false)
00151 {
00152 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
00153 _data = (unsigned char*)*data; *data = NULL;
00154 }
00155
00156
00157 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
00158 _absolute_pos(SGVec3d::zeros()),
00159 _relative_pos(SGVec3d::zeros()),
00160 _direction(SGVec3d::zeros()),
00161 _velocity(SGVec3f::zeros()),
00162 _orientation(SGQuatd::zeros()),
00163 _orivec(SGVec3f::zeros()),
00164 _base_pos(SGVec3d::zeros()),
00165 _rotation(SGQuatd::zeros()),
00166 _refname(random_string()),
00167 _format(format),
00168 _size(len),
00169 _freq(freq),
00170 _valid_buffer(false),
00171 _buffer(SGSoundMgr::NO_BUFFER),
00172 _valid_source(false),
00173 _source(SGSoundMgr::NO_SOURCE),
00174 _inner_angle(360.0),
00175 _outer_angle(360.0),
00176 _outer_gain(0.0),
00177 _pitch(1.0),
00178 _volume(1.0),
00179 _master_volume(1.0),
00180 _reference_dist(500.0),
00181 _max_dist(3000.0),
00182 _loop(AL_FALSE),
00183 _playing(false),
00184 _changed(true),
00185 _static_changed(true),
00186 _is_file(false)
00187 {
00188 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
00189 _data = (unsigned char*)*data; *data = NULL;
00190 }
00191
00192
00193
00194 SGSoundSample::~SGSoundSample() {
00195 if ( _data != NULL ) free(_data);
00196 }
00197
00198 void SGSoundSample::update_pos_and_orientation() {
00199
00200 _absolute_pos = _base_pos;
00201 if (_relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
00202 _absolute_pos += _rotation.rotate( _relative_pos );
00203 }
00204
00205 _orivec = SGVec3f::zeros();
00206 if ( _direction[0] || _direction[1] || _direction[2] ) {
00207 _orivec = toVec3f( _rotation.rotate( _direction ) );
00208 }
00209 }
00210
00211 string SGSoundSample::random_string() {
00212 static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
00213 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
00214 string rstr = "System generated name: ";
00215 for (int i=0; i<10; i++) {
00216 rstr.push_back( r[rand() % strlen(r)] );
00217 }
00218
00219 return rstr;
00220 }
00221