]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
another attempt at getting something useful without any result.
[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::fromDeg(0,0)),
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::fromDeg(0,0)),
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
117 // constructor
118 SGSoundSample::SGSoundSample( const unsigned char** data,
119                               int len, int freq, int format ) :
120     _absolute_pos(SGVec3d::zeros()),
121     _relative_pos(SGVec3d::zeros()),
122     _direction(SGVec3d::zeros()),
123     _velocity(SGVec3d::zeros()),
124     _orientation(SGQuatd::zeros()),
125     _orivec(SGVec3f::zeros()),
126     _base_pos(SGGeod::fromDeg(0,0)),
127     _refname(random_string()),
128     _format(format),
129     _size(len),
130     _freq(freq),
131     _valid_buffer(false),
132     _buffer(SGSoundMgr::NO_BUFFER),
133     _valid_source(false),
134     _source(SGSoundMgr::NO_SOURCE),
135     _inner_angle(360.0),
136     _outer_angle(360.0),
137     _outer_gain(0.0),
138     _pitch(1.0),
139     _volume(1.0),
140     _master_volume(1.0),
141     _reference_dist(500.0),
142     _max_dist(3000.0),
143     _loop(AL_FALSE),
144     _playing(false),
145     _changed(true),
146     _static_changed(true),
147     _is_file(false)
148 {
149     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
150     _data = (unsigned char*)*data; *data = NULL;
151 }
152
153 // constructor
154 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
155     _absolute_pos(SGVec3d::zeros()),
156     _relative_pos(SGVec3d::zeros()),
157     _direction(SGVec3d::zeros()),
158     _velocity(SGVec3d::zeros()),
159     _orientation(SGQuatd::zeros()),
160     _orivec(SGVec3f::zeros()),
161     _base_pos(SGGeod::fromDeg(0,0)),
162     _refname(random_string()),
163     _format(format),
164     _size(len),
165     _freq(freq),
166     _valid_buffer(false),
167     _buffer(SGSoundMgr::NO_BUFFER),
168     _valid_source(false),
169     _source(SGSoundMgr::NO_SOURCE),
170     _inner_angle(360.0),
171     _outer_angle(360.0),
172     _outer_gain(0.0),
173     _pitch(1.0),
174     _volume(1.0),
175     _master_volume(1.0),
176     _reference_dist(500.0),
177     _max_dist(3000.0),
178     _loop(AL_FALSE),
179     _playing(false),
180     _changed(true),
181     _static_changed(true),
182     _is_file(false)
183 {
184     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
185     _data = (unsigned char*)*data; *data = NULL;
186 }
187
188
189 // destructor
190 SGSoundSample::~SGSoundSample() {
191     if (_data) free(_data);
192 }
193
194 void SGSoundSample::update_pos_and_orientation() {
195     // The rotation rotating from the earth centerd frame to
196     // the horizontal local frame
197     SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
198
199     // Compute the sounds orientation and position
200     // wrt the earth centered frame - that is global coorinates
201     SGQuatd sc2body = hlOr*_orientation;
202
203     // This is rotates the x-forward, y-right, z-down coordinate system where
204     // simulation runs into the OpenGL camera system with x-right, y-up, z-back.
205     SGQuatd q(-0.5, -0.5, 0.5, 0.5);
206
207     // The cartesian position of the base sound coordinate
208     SGVec3d position = SGVec3d::fromGeod(_base_pos);
209
210     _absolute_pos = position;
211     if ( _relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
212         _absolute_pos += (sc2body*q).backTransform(_relative_pos);
213     }
214     if ( _direction[0] || _direction[1] || _direction[2] ) {
215         _orivec = toVec3f((sc2body*q).backTransform(toVec3d(_direction)));
216     }
217 }
218
219 string SGSoundSample::random_string() {
220       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
221                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
222       string rstr = "System generated name: ";
223       for (int i=0; i<10; i++) {
224           rstr.push_back( r[rand() % strlen(r)] );
225       }
226
227       return rstr;
228 }
229