]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
MSVC fix.
[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
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #if defined( __APPLE__ )
25 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
26 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
27 # include <OpenAL/al.h>
28 # include <OpenAL/alut.h>
29 #else
30 # include <AL/al.h>
31 # include <AL/alut.h>
32 #endif
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/structure/exception.hxx>
37
38 #include "sample_openal.hxx"
39
40
41 //
42 // SGSoundSample
43 //
44
45
46 static bool print_openal_error(const string &s = "unknown") {
47     ALuint error = alGetError();
48     if ( error == AL_NO_ERROR ) {
49        return false;
50     } else if ( error == AL_INVALID_NAME ) {
51         SG_LOG( SG_GENERAL, SG_ALERT, "OpenAL error (AL_INVALID_NAME): " << s );
52     } else if ( error == AL_ILLEGAL_ENUM ) {
53         SG_LOG( SG_GENERAL, SG_ALERT, "OpenAL error (AL_ILLEGAL_ENUM): "  << s );
54     } else if ( error == AL_INVALID_VALUE ) {
55         SG_LOG( SG_GENERAL, SG_ALERT, "OpenAL error (AL_INVALID_VALUE): " << s );
56     } else if ( error == AL_ILLEGAL_COMMAND ) {
57         SG_LOG( SG_GENERAL, SG_ALERT, "OpenAL error (AL_ILLEGAL_COMMAND): " << s );
58     } else if ( error == AL_OUT_OF_MEMORY ) {
59         SG_LOG( SG_GENERAL, SG_ALERT, "OpenAL error (AL_OUT_OF_MEMORY): " << s );
60     } else {
61         SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
62     }
63     return error;
64 }
65
66
67 // constructor
68 SGSoundSample::SGSoundSample( const char *path, const char *file,
69                               bool cleanup ) :
70     data(NULL),
71     buffer(0),
72     source(0),
73     pitch(1.0),
74     volume(1.0),
75     reference_dist(500.0),
76     max_dist(3000.),
77     loop(AL_FALSE),
78     playing(false)
79 {
80     SGPath samplepath( path );
81     if ( strlen(file) ) {
82         samplepath.append( file );
83     }
84
85     sample_name = samplepath.str();
86
87     SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
88             << samplepath.str() );
89
90     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
91     offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
92     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
93     direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
94     inner = outer = 360.0; outergain = 0.0;
95
96     // clear errors from elsewhere?
97     alGetError();
98
99     // create an OpenAL buffer handle
100     alGenBuffers(1, &buffer);
101     if ( print_openal_error("constructor (alGenBuffers)") ) {
102         throw sg_exception("Failed to gen OpenAL buffer.");
103     }
104
105     // Load the sample file
106 #if defined (__APPLE__)
107     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
108                      &format, &data, &size, &freq );
109 #else
110     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
111                      &format, &data, &size, &freq, &loop );
112 #endif
113     if ( print_openal_error("constructor (alutLoadWAVFile)") ) {
114         throw sg_exception("Failed to load wav file.");
115     }
116
117     // Copy data to the internal OpenAL buffer
118     alBufferData( buffer, format, data, size, freq );
119     if ( print_openal_error("constructor (alBufferData)") ) {
120         throw sg_exception("Failed to buffer data.");
121     }
122
123     if ( cleanup ) {
124         alutUnloadWAV( format, data, size, freq );
125         data = NULL;
126     }
127
128     print_openal_error("constructor return");
129 }
130
131
132 // constructor
133 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq,
134                               bool cleanup) :
135     data(NULL),
136     buffer(0),
137     source(0),
138     pitch(1.0),
139     volume(1.0),
140     reference_dist(500.0),
141     max_dist(3000.),
142     loop(AL_FALSE),
143     playing(false)
144 {
145     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
146
147     sample_name = "unknown, generated from data";
148
149     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
150     offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
151     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
152     direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
153     inner = outer = 360.0; outergain = 0.0;
154
155     // clear errors from elsewhere?
156     alGetError();
157
158     // Load wav data into a buffer.
159     alGenBuffers(1, &buffer);
160     if ( print_openal_error("constructor (alGenBuffers)") ) {
161         throw sg_exception("Failed to gen buffer." );
162     }
163
164     format = AL_FORMAT_MONO8;
165     size = len;
166     data = _data;
167     freq = _freq;
168
169     alBufferData( buffer, format, data, size, freq );
170     if ( print_openal_error("constructor (alBufferData)") ) {
171         throw sg_exception("Failed to buffer data.");
172     }
173
174     if ( cleanup ) {
175         alutUnloadWAV( format, data, size, freq );
176         data = NULL;
177     }
178
179     print_openal_error("constructor return");
180 }
181
182
183 // destructor
184 SGSoundSample::~SGSoundSample() {
185     SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
186     alDeleteBuffers(1, &buffer);
187 }
188
189
190 // play the sample
191 void SGSoundSample::play( bool _loop ) {
192
193     if ( source ) {
194         alSourceStop( source );
195     }
196
197     playing = bind_source();
198     if ( playing ) {
199         loop = _loop;
200     
201         alSourcei( source, AL_LOOPING, loop );
202         alSourcePlay( source );
203
204         print_openal_error("play (alSourcePlay)");
205     }
206 }
207
208
209 // stop playing the sample
210 void SGSoundSample::stop() {
211     if (playing) {
212         alSourceStop( source );
213         alDeleteSources(1, &source);
214         source = 0;
215         print_openal_error("stop (alDeleteSources)");
216     }
217     playing = false;
218 }
219
220 // Generate sound source
221 bool
222 SGSoundSample::bind_source() {
223
224     if ( playing ) {
225         return true;
226     }
227
228     // Bind buffer with a source.
229     alGetError();
230     alGenSources(1, &source);
231     if ( print_openal_error("bind_source (alGenSources)") ) {
232         // No biggy, better luck next time.
233         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to generate audio source.");
234         // SG_LOG( SG_GENERAL, SG_ALERT, "Please update your sound driver and try again.");
235         return false;
236     }
237
238     alSourcei( source, AL_BUFFER, buffer );
239     alSourcef( source, AL_PITCH, pitch );
240     alSourcef( source, AL_GAIN, volume );
241     alSourcefv( source, AL_POSITION, source_pos );
242     alSourcefv( source, AL_DIRECTION, direction );
243     alSourcef( source, AL_CONE_INNER_ANGLE, inner );
244     alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
245     alSourcef( source, AL_CONE_OUTER_GAIN, outergain);
246     alSourcefv( source, AL_VELOCITY, source_vel );
247     alSourcei( source, AL_LOOPING, loop );
248
249     alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
250     alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
251     alSourcef( source, AL_MAX_DISTANCE, max_dist );
252
253     print_openal_error("bind_sources return");
254
255     return true;
256 }
257
258 void
259 SGSoundSample::set_pitch( double p ) {
260     // clamp in the range of 0.01 to 2.0
261     if ( p < 0.01 ) { p = 0.01; }
262     if ( p > 2.0 ) { p = 2.0; }
263     pitch = p;
264     if (playing) {
265         alSourcef( source, AL_PITCH, pitch );
266         print_openal_error("set_pitch");
267     }
268 }
269
270 void
271 SGSoundSample::set_volume( double v ) {
272     volume = v;
273     if (playing) {
274         alSourcef( source, AL_GAIN, volume );
275         print_openal_error("set_volume");
276     }
277 }
278
279
280 bool
281 SGSoundSample::is_playing( ) {
282     if (playing) {
283         ALint result;
284         alGetSourcei( source, AL_SOURCE_STATE, &result );
285         if ( alGetError() != AL_NO_ERROR) {
286             SG_LOG( SG_GENERAL, SG_ALERT,
287                 "Oops AL error in sample is_playing(): " << sample_name );
288         }
289         return (result == AL_PLAYING) ;
290     } else
291         return false;
292 }
293
294 void
295 SGSoundSample::set_source_pos( ALfloat *pos ) {
296     source_pos[0] = pos[0];
297     source_pos[1] = pos[1];
298     source_pos[2] = pos[2];
299
300     if (playing) {
301         sgVec3 final_pos;
302         sgAddVec3( final_pos, source_pos, offset_pos );
303
304         alSourcefv( source, AL_POSITION, final_pos );
305     }
306 }
307
308 void
309 SGSoundSample::set_offset_pos( ALfloat *pos ) {
310     offset_pos[0] = pos[0];
311     offset_pos[1] = pos[1];
312     offset_pos[2] = pos[2];
313
314     if (playing) {
315         sgVec3 final_pos;
316         sgAddVec3( final_pos, source_pos, offset_pos );
317
318         alSourcefv( source, AL_POSITION, final_pos );
319     }
320 }
321
322 void
323 SGSoundSample::set_orientation( ALfloat *dir, ALfloat inner_angle,
324                                            ALfloat outer_angle,
325                                            ALfloat outer_gain)
326 {
327     inner = inner_angle;
328     outer = outer_angle;
329     outergain = outer_gain;
330     if (playing) {
331         alSourcefv( source, AL_DIRECTION, dir);
332         alSourcef( source, AL_CONE_INNER_ANGLE, inner );
333         alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
334         alSourcef( source, AL_CONE_OUTER_GAIN, outergain );
335     }
336 }
337
338 void
339 SGSoundSample::set_source_vel( ALfloat *vel ) {
340     source_vel[0] = vel[0];
341     source_vel[1] = vel[1];
342     source_vel[2] = vel[2];
343     if (playing) {
344         alSourcefv( source, AL_VELOCITY, source_vel );
345     }
346 }
347
348 void
349 SGSoundSample::set_reference_dist( ALfloat dist ) {
350     reference_dist = dist;
351     if (playing) {
352         alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
353     }
354 }
355
356
357 void
358 SGSoundSample::set_max_dist( ALfloat dist ) {
359     max_dist = dist;
360     if (playing) {
361         alSourcef( source, AL_MAX_DISTANCE, max_dist );
362     }
363 }