]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
Correct (and verrified) position, orientation and velocity vector. Todo: proper sound...
[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().data()),
43     _relative_pos(SGVec3f::zeros().data()),
44     _base_pos(SGVec3d::zeros().data()),
45     _orientation(SGVec3f::zeros().data()),
46     _direction(SGVec3f::zeros().data()),
47     _velocity(SGVec3f::zeros().data()),
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().data()),
76     _relative_pos(SGVec3f::zeros().data()),
77     _base_pos(SGVec3d::zeros().data()),
78     _orientation(SGVec3f::zeros().data()),
79     _direction(SGVec3f::zeros().data()),
80     _velocity(SGVec3f::zeros().data()),
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().data()),
115     _relative_pos(SGVec3f::zeros().data()),
116     _base_pos(SGVec3d::zeros().data()),
117     _orientation(SGVec3f::zeros().data()),
118     _direction(SGVec3f::zeros().data()),
119     _velocity(SGVec3f::zeros().data()),
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 float *SGSoundSample::get_orientation() {
152 #if 0
153     SGQuatf quat = SGQuatf::fromAngleAxis(_orientation);
154     SGVec3f orient = quat.transform(_direction);
155     return orient.data();
156 #else
157     return _orientation.data();
158 #endif
159 }
160
161 void SGSoundSample::set_base_position( SGVec3d pos ) {
162     _base_pos = pos;
163     update_absolute_position();
164     _changed = true;
165 }
166
167 void SGSoundSample::set_relative_position( SGVec3f pos ) {
168     _relative_pos = pos;
169     update_absolute_position();
170     _changed = true;
171 }
172
173 void SGSoundSample::set_orientation( SGVec3f ori ) {
174     _orientation = ori;
175     update_absolute_position();
176     _changed = true;
177 }
178
179 void SGSoundSample::set_direction( SGVec3f dir ) {
180     _direction = dir;
181     update_absolute_position();
182     _changed = true;
183 }
184
185 void SGSoundSample::update_absolute_position() {
186 #if 0
187     SGQuatf orient = SGQuatf::fromAngleAxis(_orientation);
188     SGVec3f modified_relative_pos = orient.transform(_relative_pos);
189     _absolute_pos = _base_pos + toVec3d(modified_relative_pos);
190 #else
191     _absolute_pos = _base_pos;
192 #endif
193 }