]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
5c20c1c9493a4180428ebff8bf1d9e27b0c88f7e
[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(SGVec3f::zeros()),
50     _orientation(SGQuatd::zeros()),
51     _rotation(SGQuatd::zeros()),
52     _orivec(SGVec3f::zeros()),
53     _base_pos(SGVec3d::zeros()),
54     _base_offs(SGVec3d::zeros()),
55     _refname(random_string()),
56     _data(NULL),
57     _format(AL_FORMAT_MONO8),
58     _size(0),
59     _freq(0),
60     _valid_buffer(false),
61     _buffer(SGSoundMgr::NO_BUFFER),
62     _valid_source(false),
63     _source(SGSoundMgr::NO_SOURCE),
64     _inner_angle(360.0),
65     _outer_angle(360.0),
66     _outer_gain(0.0),
67     _pitch(1.0),
68     _volume(1.0),
69     _master_volume(1.0),
70     _reference_dist(500.0),
71     _max_dist(3000.0),
72     _loop(AL_FALSE),
73     _playing(false),
74     _changed(true),
75     _static_changed(true),
76     _is_file(false)
77 {
78 }
79
80 // constructor
81 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
82     _absolute_pos(SGVec3d::zeros()),
83     _relative_pos(SGVec3d::zeros()),
84     _direction(SGVec3d::zeros()),
85     _velocity(SGVec3f::zeros()),
86     _orientation(SGQuatd::zeros()),
87     _rotation(SGQuatd::zeros()),
88     _orivec(SGVec3f::zeros()),
89     _base_pos(SGVec3d::zeros()),
90     _base_offs(SGVec3d::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 samplepath( path );
115     if ( strlen(file) ) {
116         samplepath.append( file );
117     }
118     _refname = samplepath.str();
119 }
120
121 // constructor
122 SGSoundSample::SGSoundSample( const unsigned char** data,
123                               int len, int freq, int format ) :
124     _absolute_pos(SGVec3d::zeros()),
125     _relative_pos(SGVec3d::zeros()),
126     _direction(SGVec3d::zeros()),
127     _velocity(SGVec3f::zeros()),
128     _orientation(SGQuatd::zeros()),
129     _rotation(SGQuatd::zeros()),
130     _orivec(SGVec3f::zeros()),
131     _base_pos(SGVec3d::zeros()),
132     _base_offs(SGVec3d::zeros()),
133     _refname(random_string()),
134     _format(format),
135     _size(len),
136     _freq(freq),
137     _valid_buffer(false),
138     _buffer(SGSoundMgr::NO_BUFFER),
139     _valid_source(false),
140     _source(SGSoundMgr::NO_SOURCE),
141     _inner_angle(360.0),
142     _outer_angle(360.0),
143     _outer_gain(0.0),
144     _pitch(1.0),
145     _volume(1.0),
146     _master_volume(1.0),
147     _reference_dist(500.0),
148     _max_dist(3000.0),
149     _loop(AL_FALSE),
150     _playing(false),
151     _changed(true),
152     _static_changed(true),
153     _is_file(false)
154 {
155     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
156     _data = (unsigned char*)*data; *data = NULL;
157 }
158
159 // constructor
160 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
161     _absolute_pos(SGVec3d::zeros()),
162     _relative_pos(SGVec3d::zeros()),
163     _direction(SGVec3d::zeros()),
164     _velocity(SGVec3f::zeros()),
165     _orientation(SGQuatd::zeros()),
166     _rotation(SGQuatd::zeros()),
167     _orivec(SGVec3f::zeros()),
168     _base_pos(SGVec3d::zeros()),
169     _base_offs(SGVec3d::zeros()),
170     _refname(random_string()),
171     _format(format),
172     _size(len),
173     _freq(freq),
174     _valid_buffer(false),
175     _buffer(SGSoundMgr::NO_BUFFER),
176     _valid_source(false),
177     _source(SGSoundMgr::NO_SOURCE),
178     _inner_angle(360.0),
179     _outer_angle(360.0),
180     _outer_gain(0.0),
181     _pitch(1.0),
182     _volume(1.0),
183     _master_volume(1.0),
184     _reference_dist(500.0),
185     _max_dist(3000.0),
186     _loop(AL_FALSE),
187     _playing(false),
188     _changed(true),
189     _static_changed(true),
190     _is_file(false)
191 {
192     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
193     _data = (unsigned char*)*data; *data = NULL;
194 }
195
196
197 // destructor
198 SGSoundSample::~SGSoundSample() {
199     if (_data) free(_data);
200 }
201
202 void SGSoundSample::update_pos_and_orientation() {
203
204     _absolute_pos = _base_pos - _base_offs;
205     if ( _relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
206         _absolute_pos += _rotation.backTransform(_relative_pos);
207     }
208
209     if ( _direction[0] || _direction[1] || _direction[2] ) {
210         _orivec = toVec3f( _rotation.rotate(_direction) );
211     }
212     else
213         _orivec = SGVec3f::zeros();
214 }
215
216 string SGSoundSample::random_string() {
217       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
218                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
219       string rstr = "System generated name: ";
220       for (int i=0; i<10; i++) {
221           rstr.push_back( r[rand() % strlen(r)] );
222       }
223
224       return rstr;
225 }
226