]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
52eaea184435b00a3b4e27a5803e3cfbc1ecb526
[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., 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 #if defined( __APPLE__ )
28 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
29 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
30 # include <OpenAL/al.h>
31 # include <OpenAL/alut.h>
32 #else
33 # include <AL/al.h>
34 # include <AL/alut.h>
35 #endif
36
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/misc/sg_path.hxx>
39 #include <simgear/structure/exception.hxx>
40
41 #include "sample_openal.hxx"
42
43
44 //
45 // SGSoundSample
46 //
47
48
49 static bool print_openal_error(const string &s = "unknown") {
50     ALuint error = alGetError();
51     if ( error == AL_NO_ERROR ) {
52        return false;
53     } else if ( error == AL_INVALID_NAME ) {
54         SG_LOG( SG_GENERAL, SG_ALERT, "OpenAL error (AL_INVALID_NAME): " << s );
55     } else if ( error == AL_ILLEGAL_ENUM ) {
56         SG_LOG( SG_GENERAL, SG_ALERT, "OpenAL error (AL_ILLEGAL_ENUM): "  << s );
57     } else if ( error == AL_INVALID_VALUE ) {
58         SG_LOG( SG_GENERAL, SG_ALERT, "OpenAL error (AL_INVALID_VALUE): " << s );
59     } else if ( error == AL_ILLEGAL_COMMAND ) {
60         SG_LOG( SG_GENERAL, SG_ALERT, "OpenAL error (AL_ILLEGAL_COMMAND): " << s );
61     } else if ( error == AL_OUT_OF_MEMORY ) {
62         SG_LOG( SG_GENERAL, SG_ALERT, "OpenAL error (AL_OUT_OF_MEMORY): " << s );
63     } else {
64         SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
65     }
66     return error != 0;
67 }
68
69 // empty constructor
70 SGSoundSample::SGSoundSample() :
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 #ifdef USE_SOFTWARE_DOPPLER
79     doppler_pitch_factor(1),
80     doppler_volume_factor(1),
81 #endif
82     playing(false),
83     no_Doppler_effect(true)
84 {
85 }
86
87 // constructor
88 SGSoundSample::SGSoundSample( const char *path, const char *file, bool _no_Doppler_effect ) :
89     buffer(0),
90     source(0),
91     pitch(1.0),
92     volume(1.0),
93     reference_dist(500.0),
94     max_dist(3000.),
95     loop(AL_FALSE),
96 #ifdef USE_SOFTWARE_DOPPLER
97     doppler_pitch_factor(1),
98     doppler_volume_factor(1),
99 #endif
100     playing(false),
101     no_Doppler_effect(_no_Doppler_effect)
102 {
103     SGPath samplepath( path );
104     if ( strlen(file) ) {
105         samplepath.append( file );
106     }
107     sample_name = samplepath.str();
108
109     SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
110             << samplepath.str() );
111
112     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
113     offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
114     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
115     direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
116     inner = outer = 360.0; outergain = 0.0;
117
118     // clear errors from elsewhere?
119     alGetError();
120
121     // create an OpenAL buffer handle
122     alGenBuffers(1, &buffer);
123     if ( print_openal_error("constructor (alGenBuffers)") ) {
124         throw sg_exception("Failed to gen OpenAL buffer.");
125     }
126
127     // Load the sample file
128 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
129
130   buffer = alutCreateBufferFromFile(samplepath.c_str());
131   if (buffer == AL_NONE) {
132      ALenum error = alutGetError ();
133      print_openal_error("constructor (alutCreateBufferFromFile)");
134      throw sg_io_exception("Failed to load wav file: ",
135                          sg_location(string(alutGetErrorString (error))));
136   }
137
138 #else
139         //
140         // pre 1.0 alut version
141         //
142     ALvoid* data = load_file(path, file);
143
144     // Copy data to the internal OpenAL buffer
145     alBufferData( buffer, format, data, size, freq );
146
147     if ( print_openal_error("constructor (alBufferData)") ) {
148         SG_LOG( SG_GENERAL, SG_ALERT, "Trying to use file " << file );
149         throw sg_exception("Failed to buffer data.");
150     }
151
152     alutUnloadWAV( format, data, size, freq );
153 #endif
154
155     print_openal_error("constructor return");
156 }
157
158 // constructor
159 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq, bool _no_Doppler_effect ) :
160     buffer(0),
161     source(0),
162     pitch(1.0),
163     volume(1.0),
164     reference_dist(500.0),
165     max_dist(3000.),
166     loop(AL_FALSE),
167 #ifdef USE_SOFTWARE_DOPPLER
168     doppler_pitch_factor(1),
169     doppler_volume_factor(1),
170 #endif
171     playing(false),
172     no_Doppler_effect(_no_Doppler_effect)
173 {
174     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
175
176     sample_name = "unknown, generated from data";
177
178     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
179     offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
180     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
181     direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
182     inner = outer = 360.0; outergain = 0.0;
183
184     // clear errors from elsewhere?
185     alGetError();
186
187     // Load wav data into a buffer.
188     alGenBuffers(1, &buffer);
189     if ( print_openal_error("constructor (alGenBuffers)") ) {
190         throw sg_exception("Failed to gen buffer." );
191     }
192
193     format = AL_FORMAT_MONO8;
194     size = len;
195     freq = _freq;
196
197     alBufferData( buffer, format, _data, size, freq );
198     if ( print_openal_error("constructor (alBufferData)") ) {
199         throw sg_exception("Failed to buffer data.");
200     }
201
202     print_openal_error("constructor return");
203 }
204
205
206 // destructor
207 SGSoundSample::~SGSoundSample() {
208     SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
209     if (buffer)
210         alDeleteBuffers(1, &buffer);
211 }
212
213
214 // play the sample
215 void SGSoundSample::play( bool _loop ) {
216
217     if ( source ) {
218         alSourceStop( source );
219     }
220
221     playing = bind_source();
222     if ( playing ) {
223         loop = _loop;
224     
225         alSourcei( source, AL_LOOPING, loop );
226         alSourcePlay( source );
227
228         print_openal_error("play (alSourcePlay)");
229     }
230 }
231
232
233 // stop playing the sample
234 void SGSoundSample::stop() {
235     if (playing) {
236         alSourceStop( source );
237         alDeleteSources(1, &source);
238         source = 0;
239         print_openal_error("stop (alDeleteSources)");
240     }
241     playing = false;
242 }
243
244 // Generate sound source
245 bool
246 SGSoundSample::bind_source() {
247
248     if ( playing ) {
249         return true;
250     }
251     if ( buffer == 0 ) {
252         return false;
253     }
254
255     // Bind buffer with a source.
256     alGetError();
257     alGenSources(1, &source);
258     if ( print_openal_error("bind_source (alGenSources)") ) {
259         // No biggy, better luck next time.
260         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to generate audio source.");
261         // SG_LOG( SG_GENERAL, SG_ALERT, "Please update your sound driver and try again.");
262         return false;
263     }
264
265     alSourcei( source, AL_BUFFER, buffer );
266 #ifndef USE_SOFTWARE_DOPPLER
267     alSourcef( source, AL_PITCH, pitch );
268     alSourcef( source, AL_GAIN, volume );
269 #else
270     print_openal_error("bind_sources return");
271     alSourcef( source, AL_PITCH, pitch * doppler_pitch_factor );
272     alGetError(); // ignore if the pitch is clamped by the driver
273     alSourcef( source, AL_GAIN, volume * doppler_volume_factor );
274 #endif
275     alSourcefv( source, AL_POSITION, source_pos );
276     alSourcefv( source, AL_DIRECTION, direction );
277     alSourcef( source, AL_CONE_INNER_ANGLE, inner );
278     alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
279     alSourcef( source, AL_CONE_OUTER_GAIN, outergain);
280 #ifdef USE_OPEN_AL_DOPPLER
281     alSourcefv( source, AL_VELOCITY, source_vel );
282 #endif
283     alSourcei( source, AL_LOOPING, loop );
284
285     alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
286     alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
287     alSourcef( source, AL_MAX_DISTANCE, max_dist );
288
289     print_openal_error("bind_sources return");
290
291     return true;
292 }
293
294 void
295 SGSoundSample::set_pitch( double p ) {
296     // clamp in the range of 0.01 to 2.0
297     if ( p < 0.01 ) { p = 0.01; }
298     if ( p > 2.0 ) { p = 2.0; }
299     pitch = p;
300     if (playing) {
301 #ifndef USE_SOFTWARE_DOPPLER
302         alSourcef( source, AL_PITCH, pitch );
303         print_openal_error("set_pitch");
304 #else
305         alSourcef( source, AL_PITCH, pitch * doppler_pitch_factor );
306         alGetError(); // ignore if the pitch is clamped by the driver
307 #endif
308     }
309 }
310
311 void
312 SGSoundSample::set_volume( double v ) {
313     volume = v;
314     if (playing) {
315 #ifndef USE_SOFTWARE_DOPPLER
316         alSourcef( source, AL_GAIN, volume );
317 #else
318         alSourcef( source, AL_GAIN, volume * doppler_volume_factor );
319 #endif
320         print_openal_error("set_volume");
321     }
322 }
323
324
325 bool
326 SGSoundSample::is_playing( ) {
327     if (playing) {
328         ALint result;
329         alGetSourcei( source, AL_SOURCE_STATE, &result );
330         if ( alGetError() != AL_NO_ERROR) {
331             SG_LOG( SG_GENERAL, SG_ALERT,
332                 "Oops AL error in sample is_playing(): " << sample_name );
333         }
334         return (result == AL_PLAYING) ;
335     } else
336         return false;
337 }
338
339 void
340 SGSoundSample::set_source_pos( ALfloat *pos ) {
341     source_pos[0] = pos[0];
342     source_pos[1] = pos[1];
343     source_pos[2] = pos[2];
344
345     if (playing) {
346         sgVec3 final_pos;
347         sgAddVec3( final_pos, source_pos, offset_pos );
348
349         alSourcefv( source, AL_POSITION, final_pos );
350         print_openal_error("set_source_pos");
351     }
352 }
353
354 void
355 SGSoundSample::set_offset_pos( ALfloat *pos ) {
356     offset_pos[0] = pos[0];
357     offset_pos[1] = pos[1];
358     offset_pos[2] = pos[2];
359
360     if (playing) {
361         sgVec3 final_pos;
362         sgAddVec3( final_pos, source_pos, offset_pos );
363
364         alSourcefv( source, AL_POSITION, final_pos );
365         print_openal_error("set_offset_pos");
366     }
367 }
368
369 void
370 SGSoundSample::set_orientation( ALfloat *dir, ALfloat inner_angle,
371                                            ALfloat outer_angle,
372                                            ALfloat outer_gain)
373 {
374     inner = inner_angle;
375     outer = outer_angle;
376     outergain = outer_gain;
377     direction[0] = dir[0];
378     direction[1] = dir[1];
379     direction[2] = dir[2];
380     if (playing) {
381         alSourcefv( source, AL_DIRECTION, dir);
382         alSourcef( source, AL_CONE_INNER_ANGLE, inner );
383         alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
384         alSourcef( source, AL_CONE_OUTER_GAIN, outergain );
385     }
386 }
387
388 void
389 SGSoundSample::set_source_vel( ALfloat *vel, ALfloat *listener_vel ) {
390     if (no_Doppler_effect) {
391         source_vel[0] = listener_vel[0];
392         source_vel[1] = listener_vel[1];
393         source_vel[2] = listener_vel[2];
394     } else {
395         source_vel[0] = vel[0];
396         source_vel[1] = vel[1];
397         source_vel[2] = vel[2];
398     }
399 #ifdef USE_OPEN_AL_DOPPLER
400     if (playing) {
401         alSourcefv( source, AL_VELOCITY, source_vel );
402     }
403 #elif defined (USE_OPEN_AL_DOPPLER_WITH_FIXED_LISTENER)
404     if (playing) {
405         sgVec3 relative_vel;
406         sgSubVec3( relative_vel, source_vel, listener_vel );
407         alSourcefv( source, AL_VELOCITY, relative_vel );
408     }
409 #else
410     if (no_Doppler_effect) {
411         doppler_pitch_factor = 1;
412         doppler_volume_factor = 1;
413         return;
414     }
415     double doppler, mfp;
416     sgVec3 final_pos;
417     sgAddVec3( final_pos, source_pos, offset_pos );
418     mfp = sgLengthVec3(final_pos);
419     if (mfp > 1e-6) {
420         double vls = -sgScalarProductVec3( listener_vel, final_pos ) / mfp;
421         double vss = -sgScalarProductVec3( source_vel, final_pos ) / mfp;
422         if (fabs(340 - vss) > 1e-6)
423         {
424             doppler = (340 - vls) / (340 - vss);
425             doppler = ( doppler > 0) ? ( ( doppler < 10) ? doppler : 10 ) : 0;
426         }
427         else
428             doppler = 0;
429     }
430     else
431         doppler = 1;
432     /* the OpenAL documentation of the Doppler calculation
433     SS: AL_SPEED_OF_SOUND = speed of sound (default value 343.3)
434     DF: AL_DOPPLER_FACTOR = Doppler factor (default 1.0)
435     vls: Listener velocity scalar (scalar, projected on source-to-listener vector)
436     vss: Source velocity scalar (scalar, projected on source-to-listener vector)
437     SL = source to listener vector
438     SV = Source Velocity vector
439     LV = Listener Velocity vector
440     vls = DotProduct(SL, LV) / Mag(SL)
441     vss = DotProduct(SL, SV) / Mag(SL)
442     Dopper Calculation:
443     vss = min(vss, SS/DF)
444     vls = min(vls, SS/DF)
445     f' = f * (SS - DF*vls) / (SS - DF*vss)
446     */
447     if (doppler > 0.1) {
448         if (doppler < 10) {
449             doppler_pitch_factor = doppler;
450             doppler_volume_factor = 1;
451         }
452         else {
453             doppler_pitch_factor = (doppler < 11) ? doppler : 11;
454             doppler_volume_factor = (doppler < 11) ? 11-doppler : 0;
455         }
456     }
457     else {
458         doppler_pitch_factor = 0.1;
459         doppler_volume_factor = (doppler > 0) ? doppler * 10 : 0;
460     }
461     if (playing) {
462         alSourcef( source, AL_GAIN, volume * doppler_volume_factor );
463         print_openal_error("set_source_vel: volume");
464         alSourcef( source, AL_PITCH, pitch * doppler_pitch_factor );
465         alGetError(); //ignore if the pitch is clamped
466     }
467 #endif
468 }
469
470 void
471 SGSoundSample::set_reference_dist( ALfloat dist ) {
472     reference_dist = dist;
473     if (playing) {
474         alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
475     }
476 }
477
478
479 void
480 SGSoundSample::set_max_dist( ALfloat dist ) {
481     max_dist = dist;
482     if (playing) {
483         alSourcef( source, AL_MAX_DISTANCE, max_dist );
484     }
485 }
486
487 ALvoid *
488 SGSoundSample::load_file(const char *path, const char *file)
489 {
490     ALvoid* data = 0;
491
492     SGPath samplepath( path );
493     if ( strlen(file) ) {
494         samplepath.append( file );
495     }
496
497 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
498     ALfloat freqf;
499     data = alutLoadMemoryFromFile(samplepath.c_str(), &format, &size, &freqf );
500     if (data == NULL) {
501         throw sg_io_exception("Failed to load wav file.",
502                                         sg_location(samplepath.str()));
503     }
504     freq = (ALsizei)freqf;
505 #else
506 # if defined (__APPLE__)
507     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
508                      &format, &data, &size, &freq );
509 # else
510     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
511                      &format, &data, &size, &freq, &loop );
512 # endif
513     if ( print_openal_error("constructor (alutLoadWAVFile)") ) {
514         throw sg_io_exception("Failed to load wav file.",
515                                         sg_location(samplepath.str()));
516     }
517 #endif
518
519     return data;
520 }
521