]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
Don't delete the sample data if it wasn't constructed as a file. It's now deleted...
[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 <simgear/debug/logstream.hxx>
30 #include <simgear/structure/exception.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/math/SGQuat.hxx>
33
34 #include "soundmgr_openal.hxx"
35 #include "sample_openal.hxx"
36
37
38 //
39 // SGSoundSample
40 //
41
42 // empty constructor
43 SGSoundSample::SGSoundSample() :
44     _absolute_pos(SGVec3d::zeros()),
45     _relative_pos(SGVec3d::zeros()),
46     _direction(SGVec3d::zeros()),
47     _velocity(SGVec3d::zeros()),
48     _orientation(SGQuatd::zeros()),
49     _orivec(SGVec3f::zeros()),
50     _base_pos(SGGeod()),
51     _refname(""),
52     _data(NULL),
53     _format(AL_FORMAT_MONO8),
54     _size(0),
55     _freq(0),
56     _valid_buffer(false),
57     _buffer(SGSoundMgr::NO_BUFFER),
58     _valid_source(false),
59     _source(SGSoundMgr::NO_SOURCE),
60     _inner_angle(360.0),
61     _outer_angle(360.0),
62     _outer_gain(0.0),
63     _pitch(1.0),
64     _volume(1.0),
65     _master_volume(1.0),
66     _reference_dist(500.0),
67     _max_dist(3000.0),
68     _loop(AL_FALSE),
69     _playing(false),
70     _changed(true),
71     _static_changed(true),
72     _is_file(false)
73 {
74 }
75
76 // constructor
77 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
78     _absolute_pos(SGVec3d::zeros()),
79     _relative_pos(SGVec3d::zeros()),
80     _direction(SGVec3d::zeros()),
81     _velocity(SGVec3d::zeros()),
82     _orientation(SGQuatd::zeros()),
83     _orivec(SGVec3f::zeros()),
84     _base_pos(SGGeod()),
85     _format(AL_FORMAT_MONO8),
86     _size(0),
87     _freq(0),
88     _valid_buffer(false),
89     _buffer(SGSoundMgr::NO_BUFFER),
90     _valid_source(false),
91     _source(SGSoundMgr::NO_SOURCE),
92     _inner_angle(360.0),
93     _outer_angle(360.0),
94     _outer_gain(0.0),
95     _pitch(1.0),
96     _volume(1.0),
97     _master_volume(1.0),
98     _reference_dist(500.0),
99     _max_dist(3000.0),
100     _loop(AL_FALSE),
101     _playing(false),
102     _changed(true),
103     _static_changed(true),
104     _is_file(true)
105 {
106     SGPath samplepath( path );
107     if ( strlen(file) ) {
108         samplepath.append( file );
109     }
110     _refname = samplepath.str();
111
112      SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
113             << samplepath.str() );
114 }
115
116 // constructor
117 SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format ) :
118     _absolute_pos(SGVec3d::zeros()),
119     _relative_pos(SGVec3d::zeros()),
120     _direction(SGVec3d::zeros()),
121     _velocity(SGVec3d::zeros()),
122     _orientation(SGQuatd::zeros()),
123     _orivec(SGVec3f::zeros()),
124     _base_pos(SGGeod()),
125     _data(data),
126     _format(format),
127     _size(len),
128     _freq(freq),
129     _valid_buffer(false),
130     _buffer(SGSoundMgr::NO_BUFFER),
131     _valid_source(false),
132     _source(SGSoundMgr::NO_SOURCE),
133     _inner_angle(360.0),
134     _outer_angle(360.0),
135     _outer_gain(0.0),
136     _pitch(1.0),
137     _volume(1.0),
138     _master_volume(1.0),
139     _reference_dist(500.0),
140     _max_dist(3000.0),
141     _loop(AL_FALSE),
142     _playing(false),
143     _changed(true),
144     _static_changed(true),
145     _is_file(false)
146 {
147     _refname = "unknown, data supplied by caller";
148     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
149 }
150
151
152 // destructor
153 SGSoundSample::~SGSoundSample() {
154     if (_data != NULL) {
155         delete _data;
156         _data = NULL;
157     }
158 }
159
160 void SGSoundSample::set_orientation( const SGQuatd& ori ) {
161     _orientation = ori;
162     update_absolute_position();
163     _changed = true;
164 }
165
166 void SGSoundSample::set_direction( const SGVec3d& dir ) {
167     _direction = dir;
168     update_absolute_position();
169     _changed = true;
170 }
171
172 void SGSoundSample::set_relative_position( const SGVec3f& pos ) {
173     _relative_pos = toVec3d(pos);
174     update_absolute_position();
175     _changed = true;
176 }
177
178 void SGSoundSample::set_position( const SGGeod& pos ) {
179     _base_pos = pos;
180     update_absolute_position();
181     _changed = true;
182 }
183
184 void SGSoundSample::update_absolute_position() {
185     SGQuatd orient = SGQuatd::fromLonLat(_base_pos) * _orientation;
186     _orivec = -toVec3f(orient.rotate(_direction));
187
188      orient = SGQuatd::fromRealImag(0, _relative_pos) * _orientation;
189     _absolute_pos = -SGVec3d::fromGeod(_base_pos) -orient.rotate(SGVec3d::e1());
190
191     float vel = length(_velocity);
192     _velocity = toVec3d(_orivec * vel);
193 }