]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
Don't continue parsing after processing version line
[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(), free()
30 #include <cstring>
31 #include <stdio.h>
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/structure/exception.hxx>
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/misc/ResourceManager.hxx>
37
38 #include "soundmgr_openal.hxx"
39 #include "sample_openal.hxx"
40 #include "soundmgr_openal_private.hxx"
41
42 #define AL_FALSE 0
43
44 using std::string;
45
46 //
47 // SGSoundSample
48 //
49
50 // empty constructor
51 SGSoundSample::SGSoundSample() :
52     _format(AL_FORMAT_MONO8),
53     _size(0),
54     _freq(0),
55     _changed(true),
56     _valid_source(false),
57     _source(SGSoundMgr::NO_SOURCE),
58     _absolute_pos(SGVec3d::zeros()),
59     _relative_pos(SGVec3d::zeros()),
60     _direction(SGVec3d::zeros()),
61     _velocity(SGVec3f::zeros()),
62     _orientation(SGQuatd::zeros()),
63     _orivec(SGVec3f::zeros()),
64     _base_pos(SGVec3d::zeros()),
65     _rotation(SGQuatd::zeros()),
66     _refname(random_string()),
67     _data(NULL),
68     _valid_buffer(false),
69     _buffer(SGSoundMgr::NO_BUFFER),
70     _inner_angle(360.0),
71     _outer_angle(360.0),
72     _outer_gain(0.0),
73     _pitch(1.0),
74     _volume(1.0),
75     _master_volume(1.0),
76     _reference_dist(500.0),
77     _max_dist(3000.0),
78     _loop(AL_FALSE),
79     _playing(false),
80     _static_changed(true),
81     _out_of_range(false),
82     _is_file(false),
83     _use_pos_props(false)
84 {
85     _pos_prop[0] = 0;
86     _pos_prop[1] = 0;
87     _pos_prop[2] = 0;
88 }
89
90 // constructor
91 SGSoundSample::SGSoundSample(const char *file, const SGPath& currentDir) :
92     _format(AL_FORMAT_MONO8),
93     _size(0),
94     _freq(0),
95     _changed(true),
96     _valid_source(false),
97     _source(SGSoundMgr::NO_SOURCE),
98     _absolute_pos(SGVec3d::zeros()),
99     _relative_pos(SGVec3d::zeros()),
100     _direction(SGVec3d::zeros()),
101     _velocity(SGVec3f::zeros()),
102     _orientation(SGQuatd::zeros()),
103     _orivec(SGVec3f::zeros()),
104     _base_pos(SGVec3d::zeros()),
105     _rotation(SGQuatd::zeros()),
106     _refname(file),
107     _data(NULL),
108     _valid_buffer(false),
109     _buffer(SGSoundMgr::NO_BUFFER),
110     _inner_angle(360.0),
111     _outer_angle(360.0),
112     _outer_gain(0.0),
113     _pitch(1.0),
114     _volume(1.0),
115     _master_volume(1.0),
116     _reference_dist(500.0),
117     _max_dist(3000.0),
118     _loop(AL_FALSE),
119     _playing(false),
120     _static_changed(true),
121     _out_of_range(false),
122     _is_file(true),
123     _use_pos_props(false)
124 {
125     SGPath p = simgear::ResourceManager::instance()->findPath(file, currentDir);
126     _refname = p.str();
127     _pos_prop[0] = 0;
128     _pos_prop[1] = 0;
129     _pos_prop[2] = 0;
130 }
131
132 // constructor
133 SGSoundSample::SGSoundSample( const unsigned char** data,
134                               int len, int freq, int format ) :
135     _format(format),
136     _size(len),
137     _freq(freq),
138     _changed(true),
139     _valid_source(false),
140     _source(SGSoundMgr::NO_SOURCE),
141     _absolute_pos(SGVec3d::zeros()),
142     _relative_pos(SGVec3d::zeros()),
143     _direction(SGVec3d::zeros()),
144     _velocity(SGVec3f::zeros()),
145     _orientation(SGQuatd::zeros()),
146     _orivec(SGVec3f::zeros()),
147     _base_pos(SGVec3d::zeros()),
148     _rotation(SGQuatd::zeros()),
149     _refname(random_string()),
150     _valid_buffer(false),
151     _buffer(SGSoundMgr::NO_BUFFER),
152     _inner_angle(360.0),
153     _outer_angle(360.0),
154     _outer_gain(0.0),
155     _pitch(1.0),
156     _volume(1.0),
157     _master_volume(1.0),
158     _reference_dist(500.0),
159     _max_dist(3000.0),
160     _loop(AL_FALSE),
161     _playing(false),
162     _static_changed(true),
163     _out_of_range(false),
164     _is_file(false),
165     _use_pos_props(false)
166 {
167     SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
168     _data = (unsigned char*)*data; *data = NULL;
169     _pos_prop[0] = 0;
170     _pos_prop[1] = 0;
171     _pos_prop[2] = 0;
172 }
173
174 // constructor
175 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
176     _format(format),
177     _size(len),
178     _freq(freq),
179     _changed(true),
180     _valid_source(false),
181     _source(SGSoundMgr::NO_SOURCE),
182     _absolute_pos(SGVec3d::zeros()),
183     _relative_pos(SGVec3d::zeros()),
184     _direction(SGVec3d::zeros()),
185     _velocity(SGVec3f::zeros()),
186     _orientation(SGQuatd::zeros()),
187     _orivec(SGVec3f::zeros()),
188     _base_pos(SGVec3d::zeros()),
189     _rotation(SGQuatd::zeros()),
190     _refname(random_string()),
191     _valid_buffer(false),
192     _buffer(SGSoundMgr::NO_BUFFER),
193     _inner_angle(360.0),
194     _outer_angle(360.0),
195     _outer_gain(0.0),
196     _pitch(1.0),
197     _volume(1.0),
198     _master_volume(1.0),
199     _reference_dist(500.0),
200     _max_dist(3000.0),
201     _loop(AL_FALSE),
202     _playing(false),
203     _static_changed(true),
204     _out_of_range(false),
205     _is_file(false),
206     _use_pos_props(false)
207 {
208     SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
209     _data = (unsigned char*)*data; *data = NULL;
210     _pos_prop[0] = 0;
211     _pos_prop[1] = 0;
212     _pos_prop[2] = 0;
213 }
214
215
216 // destructor
217 SGSoundSample::~SGSoundSample() {
218     if ( _data != NULL ) free(_data);
219 }
220
221 void SGSoundSample::update_pos_and_orientation() {
222
223     if (_use_pos_props) {
224         if (_pos_prop[0]) _absolute_pos[0] = _pos_prop[0]->getDoubleValue();
225         if (_pos_prop[1]) _absolute_pos[1] = _pos_prop[1]->getDoubleValue();
226         if (_pos_prop[2]) _absolute_pos[2] = _pos_prop[2]->getDoubleValue();
227     }
228     else {
229         _absolute_pos = _base_pos;
230         if (_relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
231            _absolute_pos += _rotation.rotate( _relative_pos );
232         }
233     }
234
235     _orivec = SGVec3f::zeros();
236     if ( _direction[0] || _direction[1] || _direction[2] ) {
237         _orivec = toVec3f( _rotation.rotate( _direction ) );
238     }
239 }
240
241 string SGSoundSample::random_string() {
242       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
243                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
244       string rstr = "System generated name: ";
245       for (int i=0; i<10; i++) {
246           rstr.push_back( r[rand() % strlen(r)] );
247       }
248
249       return rstr;
250 }
251
252 SGPath SGSoundSample::file_path() const
253 {
254   if (!_is_file) {
255     return SGPath();
256   }
257   
258   return SGPath(_refname);
259 }
260
261 void SGSoundSample::free_data()
262 {
263    if ( _data != NULL ) free( _data ); _data = NULL;
264 }