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