]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
526e27c77c5f5383ea1bb1b114def67bd5b596ec
[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 {
71 }
72
73 // constructor
74 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
75     _absolute_pos(SGVec3d::zeros()),
76     _relative_pos(SGVec3f::zeros()),
77     _direction(SGVec3d::zeros()),
78     _velocity(SGVec3d::zeros()),
79     _base_pos(SGGeod()),
80     _orientation(SGQuatd::zeros()),
81     _format(AL_FORMAT_MONO8),
82     _size(0),
83     _freq(0),
84     _valid_buffer(false),
85     _buffer(SGSoundMgr::NO_BUFFER),
86     _valid_source(false),
87     _source(SGSoundMgr::NO_SOURCE),
88     _inner_angle(360.0),
89     _outer_angle(360.0),
90     _outer_gain(0.0),
91     _pitch(1.0),
92     _volume(1.0),
93     _master_volume(1.0),
94     _reference_dist(500.0),
95     _max_dist(3000.0),
96     _loop(AL_FALSE),
97     _playing(false),
98     _changed(true),
99     _static_changed(true),
100     _is_file(true)
101 {
102     SGPath samplepath( path );
103     if ( strlen(file) ) {
104         samplepath.append( file );
105     }
106     _sample_name = samplepath.str();
107
108      SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
109             << samplepath.str() );
110 }
111
112 // constructor
113 SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format ) :
114     _absolute_pos(SGVec3d::zeros()),
115     _relative_pos(SGVec3f::zeros()),
116     _direction(SGVec3d::zeros()),
117     _velocity(SGVec3d::zeros()),
118     _base_pos(SGGeod()),
119     _orientation(SGQuatd::zeros()),
120     _data(data),
121     _format(format),
122     _size(len),
123     _freq(freq),
124     _valid_buffer(false),
125     _buffer(SGSoundMgr::NO_BUFFER),
126     _valid_source(false),
127     _source(SGSoundMgr::NO_SOURCE),
128     _inner_angle(360.0),
129     _outer_angle(360.0),
130     _outer_gain(0.0),
131     _pitch(1.0),
132     _volume(1.0),
133     _master_volume(1.0),
134     _reference_dist(500.0),
135     _max_dist(3000.0),
136     _loop(AL_FALSE),
137     _playing(false),
138     _changed(true),
139     _static_changed(true),
140     _is_file(false)
141 {
142     _sample_name = "unknown, data supplied by caller";
143     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
144 }
145
146
147 // destructor
148 SGSoundSample::~SGSoundSample() {
149 }
150
151 void SGSoundSample::set_orientation( SGQuatd ori ) {
152     _orientation = ori;
153     update_absolute_position();
154     _changed = true;
155 }
156
157 void SGSoundSample::set_direction( SGVec3d dir ) {
158     _direction = dir;
159     update_absolute_position();
160     _changed = true;
161 }
162
163 float *SGSoundSample::get_orientation() {
164     SGQuatd orient = SGQuatd::fromLonLat(_base_pos) * _orientation;
165     SGVec3d orivec = -orient.rotate(_direction);
166     return toVec3f(orivec).data();
167 }
168
169 void SGSoundSample::set_relative_position( SGVec3f pos ) {
170     _relative_pos = pos;
171     update_absolute_position();
172     _changed = true;
173 }
174
175 void SGSoundSample::set_position( SGGeod pos ) {
176     _base_pos = pos;
177     update_absolute_position();
178     _changed = true;
179 }
180
181 void SGSoundSample::update_absolute_position() {
182     _absolute_pos = -SGVec3d::fromGeod(_base_pos);
183     // TODO: add relative position
184 }