]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
Make SGPath cache stat() information, cheers Fred
[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/math/SGMath.hxx>
36
37 #include "soundmgr_openal.hxx"
38 #include "sample_openal.hxx"
39
40
41 //
42 // SGSoundSample
43 //
44
45 // empty constructor
46 SGSoundSample::SGSoundSample() :
47     _absolute_pos(SGVec3d::zeros()),
48     _relative_pos(SGVec3d::zeros()),
49     _direction(SGVec3d::zeros()),
50     _velocity(SGVec3f::zeros()),
51     _orientation(SGQuatd::zeros()),
52     _orivec(SGVec3f::zeros()),
53     _base_pos(SGVec3d::zeros()),
54     _rotation(SGQuatd::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     _orivec(SGVec3f::zeros()),
88     _base_pos(SGVec3d::zeros()),
89     _rotation(SGQuatd::zeros()),
90     _refname(file),
91     _data(NULL),
92     _format(AL_FORMAT_MONO8),
93     _size(0),
94     _freq(0),
95     _valid_buffer(false),
96     _buffer(SGSoundMgr::NO_BUFFER),
97     _valid_source(false),
98     _source(SGSoundMgr::NO_SOURCE),
99     _inner_angle(360.0),
100     _outer_angle(360.0),
101     _outer_gain(0.0),
102     _pitch(1.0),
103     _volume(1.0),
104     _master_volume(1.0),
105     _reference_dist(500.0),
106     _max_dist(3000.0),
107     _loop(AL_FALSE),
108     _playing(false),
109     _changed(true),
110     _static_changed(true),
111     _is_file(true)
112 {
113     SGPath samplepath( path );
114     if ( strlen(file) ) {
115         samplepath.append( file );
116     }
117     _refname = 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(SGVec3f::zeros()),
127     _orientation(SGQuatd::zeros()),
128     _orivec(SGVec3f::zeros()),
129     _base_pos(SGVec3d::zeros()),
130     _rotation(SGQuatd::zeros()),
131     _refname(random_string()),
132     _format(format),
133     _size(len),
134     _freq(freq),
135     _valid_buffer(false),
136     _buffer(SGSoundMgr::NO_BUFFER),
137     _valid_source(false),
138     _source(SGSoundMgr::NO_SOURCE),
139     _inner_angle(360.0),
140     _outer_angle(360.0),
141     _outer_gain(0.0),
142     _pitch(1.0),
143     _volume(1.0),
144     _master_volume(1.0),
145     _reference_dist(500.0),
146     _max_dist(3000.0),
147     _loop(AL_FALSE),
148     _playing(false),
149     _changed(true),
150     _static_changed(true),
151     _is_file(false)
152 {
153     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
154     _data = (unsigned char*)*data; *data = NULL;
155 }
156
157 // constructor
158 SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) :
159     _absolute_pos(SGVec3d::zeros()),
160     _relative_pos(SGVec3d::zeros()),
161     _direction(SGVec3d::zeros()),
162     _velocity(SGVec3f::zeros()),
163     _orientation(SGQuatd::zeros()),
164     _orivec(SGVec3f::zeros()),
165     _base_pos(SGVec3d::zeros()),
166     _rotation(SGQuatd::zeros()),
167     _refname(random_string()),
168     _format(format),
169     _size(len),
170     _freq(freq),
171     _valid_buffer(false),
172     _buffer(SGSoundMgr::NO_BUFFER),
173     _valid_source(false),
174     _source(SGSoundMgr::NO_SOURCE),
175     _inner_angle(360.0),
176     _outer_angle(360.0),
177     _outer_gain(0.0),
178     _pitch(1.0),
179     _volume(1.0),
180     _master_volume(1.0),
181     _reference_dist(500.0),
182     _max_dist(3000.0),
183     _loop(AL_FALSE),
184     _playing(false),
185     _changed(true),
186     _static_changed(true),
187     _is_file(false)
188 {
189     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
190     _data = (unsigned char*)*data; *data = NULL;
191 }
192
193
194 // destructor
195 SGSoundSample::~SGSoundSample() {
196     if ( _data != NULL ) free(_data);
197 }
198
199 void SGSoundSample::update_pos_and_orientation() {
200
201     _absolute_pos = _base_pos;
202     if (_relative_pos[0] || _relative_pos[1] || _relative_pos[2] ) {
203        _absolute_pos += _rotation.rotate( _relative_pos );
204     }
205
206     _orivec = SGVec3f::zeros();
207     if ( _direction[0] || _direction[1] || _direction[2] ) {
208         _orivec = toVec3f( _rotation.rotate( _direction ) );
209     }
210 }
211
212 string SGSoundSample::random_string() {
213       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
214                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
215       string rstr = "System generated name: ";
216       for (int i=0; i<10; i++) {
217           rstr.push_back( r[rand() % strlen(r)] );
218       }
219
220       return rstr;
221 }
222