]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
(try to) properly align model and viewer
[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     _direction(SGVec3f::zeros().data()),
46     _velocity(SGVec3f::zeros().data()),
47     _sample_name(""),
48     _data(NULL),
49     _format(AL_FORMAT_MONO8),
50     _size(0),
51     _freq(0),
52     _valid_buffer(false),
53     _buffer(SGSoundMgr::NO_BUFFER),
54     _valid_source(false),
55     _source(SGSoundMgr::NO_SOURCE),
56     _inner_angle(360.0),
57     _outer_angle(360.0),
58     _outer_gain(0.0),
59     _pitch(1.0),
60     _volume(1.0),
61     _master_volume(1.0),
62     _reference_dist(500.0),
63     _max_dist(3000.0),
64     _loop(AL_FALSE),
65     _playing(false),
66     _changed(true),
67     _static_changed(true),
68     _is_file(false)
69 {
70 }
71
72 // constructor
73 SGSoundSample::SGSoundSample( const char *path, const char *file ) :
74     _absolute_pos(SGVec3d::zeros().data()),
75     _relative_pos(SGVec3f::zeros().data()),
76     _base_pos(SGVec3d::zeros().data()),
77     _direction(SGVec3f::zeros().data()),
78     _velocity(SGVec3f::zeros().data()),
79     _format(AL_FORMAT_MONO8),
80     _size(0),
81     _freq(0),
82     _valid_buffer(false),
83     _buffer(SGSoundMgr::NO_BUFFER),
84     _valid_source(false),
85     _source(SGSoundMgr::NO_SOURCE),
86     _inner_angle(360.0),
87     _outer_angle(360.0),
88     _outer_gain(0.0),
89     _pitch(1.0),
90     _volume(1.0),
91     _master_volume(1.0),
92     _reference_dist(500.0),
93     _max_dist(3000.0),
94     _loop(AL_FALSE),
95     _playing(false),
96     _changed(true),
97     _static_changed(true),
98     _is_file(true)
99 {
100     SGPath samplepath( path );
101     if ( strlen(file) ) {
102         samplepath.append( file );
103     }
104     _sample_name = samplepath.str();
105
106      SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
107             << samplepath.str() );
108 }
109
110 // constructor
111 SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format ) :
112     _absolute_pos(SGVec3d::zeros().data()),
113     _relative_pos(SGVec3f::zeros().data()),
114     _base_pos(SGVec3d::zeros().data()),
115     _direction(SGVec3f::zeros().data()),
116     _velocity(SGVec3f::zeros().data()),
117     _data(data),
118     _format(format),
119     _size(len),
120     _freq(freq),
121     _valid_buffer(false),
122     _buffer(SGSoundMgr::NO_BUFFER),
123     _valid_source(false),
124     _source(SGSoundMgr::NO_SOURCE),
125     _inner_angle(360.0),
126     _outer_angle(360.0),
127     _outer_gain(0.0),
128     _pitch(1.0),
129     _volume(1.0),
130     _master_volume(1.0),
131     _reference_dist(500.0),
132     _max_dist(3000.0),
133     _loop(AL_FALSE),
134     _playing(false),
135     _changed(true),
136     _static_changed(true),
137     _is_file(false)
138 {
139     _sample_name = "unknown, data supplied by caller";
140     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
141 }
142
143
144 // destructor
145 SGSoundSample::~SGSoundSample() {
146 }
147
148 void SGSoundSample::set_base_position( SGVec3d pos ) {
149     _base_pos = pos;
150     update_absolute_position();
151     _changed = true;
152 }
153
154 void SGSoundSample::set_relative_position( SGVec3f pos ) {
155     _relative_pos = pos;
156     update_absolute_position();
157     _changed = true;
158 }
159
160 void SGSoundSample::set_orientation( SGVec3f dir ) {
161     _direction = dir;
162     update_absolute_position();
163     _changed = true;
164 }
165
166 void SGSoundSample::update_absolute_position() {
167     SGQuatf orient = SGQuatf::fromAngleAxis(_direction);
168     SGVec3f modified_relative_pos = orient.transform(_relative_pos);
169     _absolute_pos = _base_pos + toVec3d(modified_relative_pos);
170 }