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