]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
Merge branch 'timoore/effects'
[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/SGMath.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     _orivec(SGVec3f::zeros()),
52     _base_pos(SGVec3d::zeros()),
53     _rotation(SGQuatd::zeros()),
54     _refname(random_string()),
55     _data(NULL),
56     _format(AL_FORMAT_MONO8),
57     _size(0),
58     _freq(0),
59     _valid_buffer(false),
60     _buffer(SGSoundMgr::NO_BUFFER),
61     _valid_source(false),
62     _source(SGSoundMgr::NO_SOURCE),
63     _inner_angle(360.0),
64     _outer_angle(360.0),
65     _outer_gain(0.0),
66     _pitch(1.0),
67     _volume(1.0),
68     _master_volume(1.0),
69     _reference_dist(500.0),
70     _max_dist(3000.0),
71     _loop(AL_FALSE),
72     _playing(false),
73     _changed(true),
74     _static_changed(true),
75     _is_file(false)
76 {
77 }
78
79 // constructor
80 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
81     _absolute_pos(SGVec3d::zeros()),
82     _relative_pos(SGVec3d::zeros()),
83     _direction(SGVec3d::zeros()),
84     _velocity(SGVec3f::zeros()),
85     _orientation(SGQuatd::zeros()),
86     _orivec(SGVec3f::zeros()),
87     _base_pos(SGVec3d::zeros()),
88     _rotation(SGQuatd::zeros()),
89     _refname(file),
90     _data(NULL),
91     _format(AL_FORMAT_MONO8),
92     _size(0),
93     _freq(0),
94     _valid_buffer(false),
95     _buffer(SGSoundMgr::NO_BUFFER),
96     _valid_source(false),
97     _source(SGSoundMgr::NO_SOURCE),
98     _inner_angle(360.0),
99     _outer_angle(360.0),
100     _outer_gain(0.0),
101     _pitch(1.0),
102     _volume(1.0),
103     _master_volume(1.0),
104     _reference_dist(500.0),
105     _max_dist(3000.0),
106     _loop(AL_FALSE),
107     _playing(false),
108     _changed(true),
109     _static_changed(true),
110     _is_file(true)
111 {
112     SGPath samplepath( path );
113     if ( strlen(file) ) {
114         samplepath.append( file );
115     }
116     _refname = samplepath.str();
117 }
118
119 // constructor
120 SGSoundSample::SGSoundSample( const unsigned char** data,
121                               int len, int freq, int format ) :
122     _absolute_pos(SGVec3d::zeros()),
123     _relative_pos(SGVec3d::zeros()),
124     _direction(SGVec3d::zeros()),
125     _velocity(SGVec3f::zeros()),
126     _orientation(SGQuatd::zeros()),
127     _orivec(SGVec3f::zeros()),
128     _base_pos(SGVec3d::zeros()),
129     _rotation(SGQuatd::zeros()),
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(SGVec3f::zeros()),
162     _orientation(SGQuatd::zeros()),
163     _orivec(SGVec3f::zeros()),
164     _base_pos(SGVec3d::zeros()),
165     _rotation(SGQuatd::zeros()),
166     _refname(random_string()),
167     _format(format),
168     _size(len),
169     _freq(freq),
170     _valid_buffer(false),
171     _buffer(SGSoundMgr::NO_BUFFER),
172     _valid_source(false),
173     _source(SGSoundMgr::NO_SOURCE),
174     _inner_angle(360.0),
175     _outer_angle(360.0),
176     _outer_gain(0.0),
177     _pitch(1.0),
178     _volume(1.0),
179     _master_volume(1.0),
180     _reference_dist(500.0),
181     _max_dist(3000.0),
182     _loop(AL_FALSE),
183     _playing(false),
184     _changed(true),
185     _static_changed(true),
186     _is_file(false)
187 {
188     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
189     _data = (unsigned char*)*data; *data = NULL;
190 }
191
192
193 // destructor
194 SGSoundSample::~SGSoundSample() {
195     if ( _data != NULL ) free(_data);
196 }
197
198 void SGSoundSample::update_pos_and_orientation() {
199
200     _absolute_pos = _base_pos;
201     if (_relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
202        _absolute_pos += _rotation.rotate( _relative_pos );
203     }
204
205     _orivec = SGVec3f::zeros();
206     if ( _direction[0] || _direction[1] || _direction[2] ) {
207         _orivec = toVec3f( _rotation.rotate( _direction ) );
208     }
209 }
210
211 string SGSoundSample::random_string() {
212       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
213                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
214       string rstr = "System generated name: ";
215       for (int i=0; i<10; i++) {
216           rstr.push_back( r[rand() % strlen(r)] );
217       }
218
219       return rstr;
220 }
221