]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
0423f2c463975b55c6c4a53b48f376917023eda8
[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()),
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()),
87     _format(AL_FORMAT_MONO8),
88     _size(0),
89     _freq(0),
90     _valid_buffer(false),
91     _buffer(SGSoundMgr::NO_BUFFER),
92     _valid_source(false),
93     _source(SGSoundMgr::NO_SOURCE),
94     _inner_angle(360.0),
95     _outer_angle(360.0),
96     _outer_gain(0.0),
97     _pitch(1.0),
98     _volume(1.0),
99     _master_volume(1.0),
100     _reference_dist(500.0),
101     _max_dist(3000.0),
102     _loop(AL_FALSE),
103     _playing(false),
104     _changed(true),
105     _static_changed(true),
106     _is_file(true)
107 {
108     SGPath samplepath( path );
109     if ( strlen(file) ) {
110         samplepath.append( file );
111     }
112     _refname = samplepath.str();
113
114      SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
115             << samplepath.str() );
116 }
117
118 // constructor
119 SGSoundSample::SGSoundSample( unsigned char *data, 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()),
127     _refname(random_string()),
128     _data(data),
129     _format(format),
130     _size(len),
131     _freq(freq),
132     _valid_buffer(false),
133     _buffer(SGSoundMgr::NO_BUFFER),
134     _valid_source(false),
135     _source(SGSoundMgr::NO_SOURCE),
136     _inner_angle(360.0),
137     _outer_angle(360.0),
138     _outer_gain(0.0),
139     _pitch(1.0),
140     _volume(1.0),
141     _master_volume(1.0),
142     _reference_dist(500.0),
143     _max_dist(3000.0),
144     _loop(AL_FALSE),
145     _playing(false),
146     _changed(true),
147     _static_changed(true),
148     _is_file(false)
149 {
150     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
151 }
152
153
154 // destructor
155 SGSoundSample::~SGSoundSample() {
156     if (_data != NULL) {
157         delete _data;
158         _data = NULL;
159     }
160 }
161
162 void SGSoundSample::set_orientation( const SGQuatd& ori ) {
163     _orientation = ori;
164     update_absolute_position();
165     _changed = true;
166 }
167
168 void SGSoundSample::set_direction( const SGVec3d& dir ) {
169     _direction = dir;
170     update_absolute_position();
171     _changed = true;
172 }
173
174 void SGSoundSample::set_relative_position( const SGVec3f& pos ) {
175     _relative_pos = toVec3d(pos);
176     update_absolute_position();
177     _changed = true;
178 }
179
180 void SGSoundSample::set_position( const SGGeod& pos ) {
181     _base_pos = pos;
182     update_absolute_position();
183     _changed = true;
184 }
185
186 void SGSoundSample::update_absolute_position() {
187     SGQuatd orient = SGQuatd::fromLonLat(_base_pos) * _orientation;
188     _orivec = -toVec3f(orient.rotate(_direction));
189
190      orient = SGQuatd::fromRealImag(0, _relative_pos) * _orientation;
191     _absolute_pos = -SGVec3d::fromGeod(_base_pos); // -orient.rotate(SGVec3d::e1());
192 }
193
194 string SGSoundSample::random_string() {
195       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
196                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
197       string rstr;
198       for (int i=0; i<10; i++) {
199           rstr.push_back( r[rand() % strlen(r)] );
200       }
201
202       return rstr;
203 }