]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
math: Move lerp function into SGMisc.
[simgear.git] / simgear / sound / sample_openal.cxx
1 // sample_openal.cxx -- Audio sample encapsulation class
2 // 
3 // Written by Curtis Olson, started April 2004.
4 // Modified to match the new SoundSystem by Erik Hofman, October 2009
5 //
6 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
7 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
8 //
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.
13 //
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.
18 //
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.
22 //
23 // $Id$
24
25 #ifdef HAVE_CONFIG_H
26 #  include <simgear_config.h>
27 #endif
28
29 #include <stdlib.h>     // rand()
30 #include <cstring>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/structure/exception.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/math/SGMath.hxx>
36 #include <simgear/misc/ResourceManager.hxx>
37
38 #include "soundmgr_openal.hxx"
39 #include "sample_openal.hxx"
40
41
42 //
43 // SGSoundSample
44 //
45
46 // empty constructor
47 SGSoundSample::SGSoundSample() :
48     _absolute_pos(SGVec3d::zeros()),
49     _relative_pos(SGVec3d::zeros()),
50     _direction(SGVec3d::zeros()),
51     _velocity(SGVec3f::zeros()),
52     _orientation(SGQuatd::zeros()),
53     _orivec(SGVec3f::zeros()),
54     _base_pos(SGVec3d::zeros()),
55     _rotation(SGQuatd::zeros()),
56     _refname(random_string()),
57     _data(NULL),
58     _format(AL_FORMAT_MONO8),
59     _size(0),
60     _freq(0),
61     _valid_buffer(false),
62     _buffer(SGSoundMgr::NO_BUFFER),
63     _valid_source(false),
64     _source(SGSoundMgr::NO_SOURCE),
65     _inner_angle(360.0),
66     _outer_angle(360.0),
67     _outer_gain(0.0),
68     _pitch(1.0),
69     _volume(1.0),
70     _master_volume(1.0),
71     _reference_dist(500.0),
72     _max_dist(3000.0),
73     _loop(AL_FALSE),
74     _playing(false),
75     _changed(true),
76     _static_changed(true),
77     _out_of_range(false),
78     _is_file(false)
79 {
80 }
81
82 // constructor
83 SGSoundSample::SGSoundSample(const char *file, const SGPath& currentDir) :
84     _absolute_pos(SGVec3d::zeros()),
85     _relative_pos(SGVec3d::zeros()),
86     _direction(SGVec3d::zeros()),
87     _velocity(SGVec3f::zeros()),
88     _orientation(SGQuatd::zeros()),
89     _orivec(SGVec3f::zeros()),
90     _base_pos(SGVec3d::zeros()),
91     _rotation(SGQuatd::zeros()),
92     _refname(file),
93     _data(NULL),
94     _format(AL_FORMAT_MONO8),
95     _size(0),
96     _freq(0),
97     _valid_buffer(false),
98     _buffer(SGSoundMgr::NO_BUFFER),
99     _valid_source(false),
100     _source(SGSoundMgr::NO_SOURCE),
101     _inner_angle(360.0),
102     _outer_angle(360.0),
103     _outer_gain(0.0),
104     _pitch(1.0),
105     _volume(1.0),
106     _master_volume(1.0),
107     _reference_dist(500.0),
108     _max_dist(3000.0),
109     _loop(AL_FALSE),
110     _playing(false),
111     _changed(true),
112     _static_changed(true),
113     _out_of_range(false),
114     _is_file(true)
115 {
116     SGPath p = simgear::ResourceManager::instance()->findPath(file, currentDir);
117     _refname = p.str();
118 }
119
120 // constructor
121 SGSoundSample::SGSoundSample( const unsigned char** data,
122                               int len, int freq, int format ) :
123     _absolute_pos(SGVec3d::zeros()),
124     _relative_pos(SGVec3d::zeros()),
125     _direction(SGVec3d::zeros()),
126     _velocity(SGVec3f::zeros()),
127     _orientation(SGQuatd::zeros()),
128     _orivec(SGVec3f::zeros()),
129     _base_pos(SGVec3d::zeros()),
130     _rotation(SGQuatd::zeros()),
131     _refname(random_string()),
132     _format(format),
133     _size(len),
134     _freq(freq),
135     _valid_buffer(false),
136     _buffer(SGSoundMgr::NO_BUFFER),
137     _valid_source(false),
138     _source(SGSoundMgr::NO_SOURCE),
139     _inner_angle(360.0),
140     _outer_angle(360.0),
141     _outer_gain(0.0),
142     _pitch(1.0),
143     _volume(1.0),
144     _master_volume(1.0),
145     _reference_dist(500.0),
146     _max_dist(3000.0),
147     _loop(AL_FALSE),
148     _playing(false),
149     _changed(true),
150     _static_changed(true),
151     _out_of_range(false),
152     _is_file(false)
153 {
154     SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
155     _data = (unsigned char*)*data; *data = NULL;
156 }
157
158 // constructor
159 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
160     _absolute_pos(SGVec3d::zeros()),
161     _relative_pos(SGVec3d::zeros()),
162     _direction(SGVec3d::zeros()),
163     _velocity(SGVec3f::zeros()),
164     _orientation(SGQuatd::zeros()),
165     _orivec(SGVec3f::zeros()),
166     _base_pos(SGVec3d::zeros()),
167     _rotation(SGQuatd::zeros()),
168     _refname(random_string()),
169     _format(format),
170     _size(len),
171     _freq(freq),
172     _valid_buffer(false),
173     _buffer(SGSoundMgr::NO_BUFFER),
174     _valid_source(false),
175     _source(SGSoundMgr::NO_SOURCE),
176     _inner_angle(360.0),
177     _outer_angle(360.0),
178     _outer_gain(0.0),
179     _pitch(1.0),
180     _volume(1.0),
181     _master_volume(1.0),
182     _reference_dist(500.0),
183     _max_dist(3000.0),
184     _loop(AL_FALSE),
185     _playing(false),
186     _changed(true),
187     _static_changed(true),
188     _out_of_range(false),
189     _is_file(false)
190 {
191     SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
192     _data = (unsigned char*)*data; *data = NULL;
193 }
194
195
196 // destructor
197 SGSoundSample::~SGSoundSample() {
198     if ( _data != NULL ) free(_data);
199 }
200
201 void SGSoundSample::update_pos_and_orientation() {
202
203     _absolute_pos = _base_pos;
204     if (_relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
205        _absolute_pos += _rotation.rotate( _relative_pos );
206     }
207
208     _orivec = SGVec3f::zeros();
209     if ( _direction[0] || _direction[1] || _direction[2] ) {
210         _orivec = toVec3f( _rotation.rotate( _direction ) );
211     }
212 }
213
214 string SGSoundSample::random_string() {
215       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
216                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
217       string rstr = "System generated name: ";
218       for (int i=0; i<10; i++) {
219           rstr.push_back( r[rand() % strlen(r)] );
220       }
221
222       return rstr;
223 }
224
225 SGPath SGSoundSample::file_path() const
226 {
227   if (!_is_file) {
228     return SGPath();
229   }
230   
231   return SGPath(_refname);
232 }
233
234