]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
Use shared pointers for any reference to SGSoundSample, fix the constructor of SGSoun...
[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
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/structure/exception.hxx>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/math/SGQuat.hxx>
35
36 #include "soundmgr_openal.hxx"
37 #include "sample_openal.hxx"
38
39
40 //
41 // SGSoundSample
42 //
43
44 // empty constructor
45 SGSoundSample::SGSoundSample() :
46     _absolute_pos(SGVec3d::zeros()),
47     _relative_pos(SGVec3d::zeros()),
48     _direction(SGVec3d::zeros()),
49     _velocity(SGVec3d::zeros()),
50     _orientation(SGQuatd::zeros()),
51     _orivec(SGVec3f::zeros()),
52     _base_pos(SGGeod()),
53     _refname(random_string()),
54     _data(NULL),
55     _format(AL_FORMAT_MONO8),
56     _size(0),
57     _freq(0),
58     _valid_buffer(false),
59     _buffer(SGSoundMgr::NO_BUFFER),
60     _valid_source(false),
61     _source(SGSoundMgr::NO_SOURCE),
62     _inner_angle(360.0),
63     _outer_angle(360.0),
64     _outer_gain(0.0),
65     _pitch(1.0),
66     _volume(1.0),
67     _master_volume(1.0),
68     _reference_dist(500.0),
69     _max_dist(3000.0),
70     _loop(AL_FALSE),
71     _playing(false),
72     _changed(true),
73     _static_changed(true),
74     _is_file(false)
75 {
76 }
77
78 // constructor
79 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
80     _absolute_pos(SGVec3d::zeros()),
81     _relative_pos(SGVec3d::zeros()),
82     _direction(SGVec3d::zeros()),
83     _velocity(SGVec3d::zeros()),
84     _orientation(SGQuatd::zeros()),
85     _orivec(SGVec3f::zeros()),
86     _base_pos(SGGeod()),
87     _refname(file),
88     _data(NULL),
89     _format(AL_FORMAT_MONO8),
90     _size(0),
91     _freq(0),
92     _valid_buffer(false),
93     _buffer(SGSoundMgr::NO_BUFFER),
94     _valid_source(false),
95     _source(SGSoundMgr::NO_SOURCE),
96     _inner_angle(360.0),
97     _outer_angle(360.0),
98     _outer_gain(0.0),
99     _pitch(1.0),
100     _volume(1.0),
101     _master_volume(1.0),
102     _reference_dist(500.0),
103     _max_dist(3000.0),
104     _loop(AL_FALSE),
105     _playing(false),
106     _changed(true),
107     _static_changed(true),
108     _is_file(true)
109 {
110     SGPath samplepath( path );
111     if ( strlen(file) ) {
112         samplepath.append( file );
113     }
114     _refname = samplepath.str();
115
116      SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
117             << samplepath.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(SGVec3d::zeros()),
127     _orientation(SGQuatd::zeros()),
128     _orivec(SGVec3f::zeros()),
129     _base_pos(SGGeod()),
130     _refname(random_string()),
131     _format(format),
132     _size(len),
133     _freq(freq),
134     _valid_buffer(false),
135     _buffer(SGSoundMgr::NO_BUFFER),
136     _valid_source(false),
137     _source(SGSoundMgr::NO_SOURCE),
138     _inner_angle(360.0),
139     _outer_angle(360.0),
140     _outer_gain(0.0),
141     _pitch(1.0),
142     _volume(1.0),
143     _master_volume(1.0),
144     _reference_dist(500.0),
145     _max_dist(3000.0),
146     _loop(AL_FALSE),
147     _playing(false),
148     _changed(true),
149     _static_changed(true),
150     _is_file(false)
151 {
152     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
153     _data = (unsigned char*)*data; *data = NULL;
154 }
155
156 // constructor
157 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
158     _absolute_pos(SGVec3d::zeros()),
159     _relative_pos(SGVec3d::zeros()),
160     _direction(SGVec3d::zeros()),
161     _velocity(SGVec3d::zeros()),
162     _orientation(SGQuatd::zeros()),
163     _orivec(SGVec3f::zeros()),
164     _base_pos(SGGeod()),
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) free( _data );
195     _data = NULL;
196 }
197
198 void SGSoundSample::set_orientation( const SGQuatd& ori ) {
199     _orientation = ori;
200     update_absolute_position();
201     _changed = true;
202 }
203
204 void SGSoundSample::set_direction( const SGVec3d& dir ) {
205     _direction = dir;
206     update_absolute_position();
207     _changed = true;
208 }
209
210 void SGSoundSample::set_relative_position( const SGVec3f& pos ) {
211     _relative_pos = toVec3d(pos);
212     update_absolute_position();
213     _changed = true;
214 }
215
216 void SGSoundSample::set_position( const SGGeod& pos ) {
217     _base_pos = pos;
218     update_absolute_position();
219     _changed = true;
220 }
221
222 void SGSoundSample::update_absolute_position() {
223     SGQuatd orient = SGQuatd::fromLonLat(_base_pos) * _orientation;
224     _orivec = -toVec3f(orient.rotate(-SGVec3d::e1()));
225
226     orient = SGQuatd::fromRealImag(0, _relative_pos) * _orientation;
227     _absolute_pos = -SGVec3d::fromGeod(_base_pos); // -orient.rotate(SGVec3d::e1());
228 }
229
230 string SGSoundSample::random_string() {
231       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
232                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
233       string rstr;
234       for (int i=0; i<10; i++) {
235           rstr.push_back( r[rand() % strlen(r)] );
236       }
237
238       return rstr;
239 }