]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
Alut < 1.0 fixes and finaly fix the sound orientation
[simgear.git] / simgear / sound / sample_openal.cxx
1 // sample.cxx -- Sound sample encapsulation class
2 // 
3 // Written by Curtis Olson, started April 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/structure/exception.hxx>
29 #include <simgear/misc/sg_path.hxx>
30 #include <simgear/math/SGQuat.hxx>
31
32 #include "soundmgr_openal.hxx"
33 #include "sample_openal.hxx"
34
35
36 //
37 // SGSoundSample
38 //
39
40 // empty constructor
41 SGSoundSample::SGSoundSample() :
42     _absolute_pos(SGVec3d::zeros()),
43     _relative_pos(SGVec3f::zeros()),
44     _direction(SGVec3d::zeros()),
45     _velocity(SGVec3d::zeros()),
46     _base_pos(SGGeod()),
47     _orientation(SGQuatd::zeros()),
48     _sample_name(""),
49     _data(NULL),
50     _format(AL_FORMAT_MONO8),
51     _size(0),
52     _freq(0),
53     _valid_buffer(false),
54     _buffer(SGSoundMgr::NO_BUFFER),
55     _valid_source(false),
56     _source(SGSoundMgr::NO_SOURCE),
57     _inner_angle(360.0),
58     _outer_angle(360.0),
59     _outer_gain(0.0),
60     _pitch(1.0),
61     _volume(1.0),
62     _master_volume(1.0),
63     _reference_dist(500.0),
64     _max_dist(3000.0),
65     _loop(AL_FALSE),
66     _playing(false),
67     _changed(true),
68     _static_changed(true),
69     _is_file(false),
70     _orivec(SGVec3f::zeros())
71 {
72 }
73
74 // constructor
75 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
76     _absolute_pos(SGVec3d::zeros()),
77     _relative_pos(SGVec3f::zeros()),
78     _direction(SGVec3d::zeros()),
79     _velocity(SGVec3d::zeros()),
80     _base_pos(SGGeod()),
81     _orientation(SGQuatd::zeros()),
82     _format(AL_FORMAT_MONO8),
83     _size(0),
84     _freq(0),
85     _valid_buffer(false),
86     _buffer(SGSoundMgr::NO_BUFFER),
87     _valid_source(false),
88     _source(SGSoundMgr::NO_SOURCE),
89     _inner_angle(360.0),
90     _outer_angle(360.0),
91     _outer_gain(0.0),
92     _pitch(1.0),
93     _volume(1.0),
94     _master_volume(1.0),
95     _reference_dist(500.0),
96     _max_dist(3000.0),
97     _loop(AL_FALSE),
98     _playing(false),
99     _changed(true),
100     _static_changed(true),
101     _is_file(true),
102     _orivec(SGVec3f::zeros())
103 {
104     SGPath samplepath( path );
105     if ( strlen(file) ) {
106         samplepath.append( file );
107     }
108     _sample_name = samplepath.str();
109
110      SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
111             << samplepath.str() );
112 }
113
114 // constructor
115 SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format ) :
116     _absolute_pos(SGVec3d::zeros()),
117     _relative_pos(SGVec3f::zeros()),
118     _direction(SGVec3d::zeros()),
119     _velocity(SGVec3d::zeros()),
120     _base_pos(SGGeod()),
121     _orientation(SGQuatd::zeros()),
122     _data(data),
123     _format(format),
124     _size(len),
125     _freq(freq),
126     _valid_buffer(false),
127     _buffer(SGSoundMgr::NO_BUFFER),
128     _valid_source(false),
129     _source(SGSoundMgr::NO_SOURCE),
130     _inner_angle(360.0),
131     _outer_angle(360.0),
132     _outer_gain(0.0),
133     _pitch(1.0),
134     _volume(1.0),
135     _master_volume(1.0),
136     _reference_dist(500.0),
137     _max_dist(3000.0),
138     _loop(AL_FALSE),
139     _playing(false),
140     _changed(true),
141     _static_changed(true),
142     _is_file(false),
143     _orivec(SGVec3f::zeros())
144 {
145     _sample_name = "unknown, data supplied by caller";
146     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
147 }
148
149
150 // destructor
151 SGSoundSample::~SGSoundSample() {
152 }
153
154 void SGSoundSample::set_orientation( SGQuatd ori ) {
155     _orientation = ori;
156     update_absolute_position();
157     _changed = true;
158 }
159
160 void SGSoundSample::set_direction( SGVec3d dir ) {
161     _direction = dir;
162     update_absolute_position();
163     _changed = true;
164 }
165
166 void SGSoundSample::set_relative_position( SGVec3f pos ) {
167     _relative_pos = pos;
168     update_absolute_position();
169     _changed = true;
170 }
171
172 void SGSoundSample::set_position( SGGeod pos ) {
173     _base_pos = pos;
174     update_absolute_position();
175     _changed = true;
176 }
177
178 void SGSoundSample::update_absolute_position() {
179     SGQuatd orient = SGQuatd::fromLonLat(_base_pos) * _orientation;
180     _orivec = -toVec3f(orient.rotate(_direction));
181
182     _absolute_pos = -SGVec3d::fromGeod(_base_pos);
183     // TODO: add relative position
184 }