]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
hla: Provide a directly property based api for property data element.
[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     _is_file(false)
78 {
79 }
80
81 // constructor
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()),
91     _refname(file),
92     _data(NULL),
93     _format(AL_FORMAT_MONO8),
94     _size(0),
95     _freq(0),
96     _valid_buffer(false),
97     _buffer(SGSoundMgr::NO_BUFFER),
98     _valid_source(false),
99     _source(SGSoundMgr::NO_SOURCE),
100     _inner_angle(360.0),
101     _outer_angle(360.0),
102     _outer_gain(0.0),
103     _pitch(1.0),
104     _volume(1.0),
105     _master_volume(1.0),
106     _reference_dist(500.0),
107     _max_dist(3000.0),
108     _loop(AL_FALSE),
109     _playing(false),
110     _changed(true),
111     _static_changed(true),
112     _is_file(true)
113 {
114     SGPath p = simgear::ResourceManager::instance()->findPath(file, currentDir);
115     _refname = p.str();
116 }
117
118 // constructor
119 SGSoundSample::SGSoundSample( const unsigned char** data,
120                               int len, int freq, int format ) :
121     _absolute_pos(SGVec3d::zeros()),
122     _relative_pos(SGVec3d::zeros()),
123     _direction(SGVec3d::zeros()),
124     _velocity(SGVec3f::zeros()),
125     _orientation(SGQuatd::zeros()),
126     _orivec(SGVec3f::zeros()),
127     _base_pos(SGVec3d::zeros()),
128     _rotation(SGQuatd::zeros()),
129     _refname(random_string()),
130     _format(format),
131     _size(len),
132     _freq(freq),
133     _valid_buffer(false),
134     _buffer(SGSoundMgr::NO_BUFFER),
135     _valid_source(false),
136     _source(SGSoundMgr::NO_SOURCE),
137     _inner_angle(360.0),
138     _outer_angle(360.0),
139     _outer_gain(0.0),
140     _pitch(1.0),
141     _volume(1.0),
142     _master_volume(1.0),
143     _reference_dist(500.0),
144     _max_dist(3000.0),
145     _loop(AL_FALSE),
146     _playing(false),
147     _changed(true),
148     _static_changed(true),
149     _is_file(false)
150 {
151     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
152     _data = (unsigned char*)*data; *data = NULL;
153 }
154
155 // constructor
156 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
157     _absolute_pos(SGVec3d::zeros()),
158     _relative_pos(SGVec3d::zeros()),
159     _direction(SGVec3d::zeros()),
160     _velocity(SGVec3f::zeros()),
161     _orientation(SGQuatd::zeros()),
162     _orivec(SGVec3f::zeros()),
163     _base_pos(SGVec3d::zeros()),
164     _rotation(SGQuatd::zeros()),
165     _refname(random_string()),
166     _format(format),
167     _size(len),
168     _freq(freq),
169     _valid_buffer(false),
170     _buffer(SGSoundMgr::NO_BUFFER),
171     _valid_source(false),
172     _source(SGSoundMgr::NO_SOURCE),
173     _inner_angle(360.0),
174     _outer_angle(360.0),
175     _outer_gain(0.0),
176     _pitch(1.0),
177     _volume(1.0),
178     _master_volume(1.0),
179     _reference_dist(500.0),
180     _max_dist(3000.0),
181     _loop(AL_FALSE),
182     _playing(false),
183     _changed(true),
184     _static_changed(true),
185     _is_file(false)
186 {
187     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
188     _data = (unsigned char*)*data; *data = NULL;
189 }
190
191
192 // destructor
193 SGSoundSample::~SGSoundSample() {
194     if ( _data != NULL ) free(_data);
195 }
196
197 void SGSoundSample::update_pos_and_orientation() {
198
199     _absolute_pos = _base_pos;
200     if (_relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
201        _absolute_pos += _rotation.rotate( _relative_pos );
202     }
203
204     _orivec = SGVec3f::zeros();
205     if ( _direction[0] || _direction[1] || _direction[2] ) {
206         _orivec = toVec3f( _rotation.rotate( _direction ) );
207     }
208 }
209
210 string SGSoundSample::random_string() {
211       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
212                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
213       string rstr = "System generated name: ";
214       for (int i=0; i<10; i++) {
215           rstr.push_back( r[rand() % strlen(r)] );
216       }
217
218       return rstr;
219 }
220
221 SGPath SGSoundSample::file_path() const
222 {
223   if (!_is_file) {
224     return SGPath();
225   }
226   
227   return SGPath(_refname);
228 }
229
230