1 // sample.cxx -- Sound sample encapsulation class
3 // Written by Curtis Olson, started April 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
24 # include <simgear_config.h>
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>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/misc/sg_path.hxx>
39 #include <simgear/structure/exception.hxx>
41 #include "sample_openal.hxx"
49 static bool print_openal_error(const string &s = "unknown") {
50 ALuint error = alGetError();
51 if ( error == AL_NO_ERROR ) {
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 );
64 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
70 SGSoundSample::SGSoundSample() :
75 reference_dist(500.0),
78 #ifdef USE_SOFTWARE_DOPPLER
79 doppler_pitch_factor(1),
80 doppler_volume_factor(1),
83 no_Doppler_effect(true)
88 SGSoundSample::SGSoundSample( const char *path, const char *file, bool _no_Doppler_effect ) :
93 reference_dist(500.0),
96 #ifdef USE_SOFTWARE_DOPPLER
97 doppler_pitch_factor(1),
98 doppler_volume_factor(1),
101 no_Doppler_effect(_no_Doppler_effect)
103 SGPath samplepath( path );
104 if ( strlen(file) ) {
105 samplepath.append( file );
107 sample_name = samplepath.str();
109 SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
110 << samplepath.str() );
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;
118 // clear errors from elsewhere?
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.");
127 // Load the sample file
128 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
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))));
140 // pre 1.0 alut version
142 ALvoid* data = load_file(path, file);
144 // Copy data to the internal OpenAL buffer
145 alBufferData( buffer, format, data, size, freq );
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.");
152 alutUnloadWAV( format, data, size, freq );
155 print_openal_error("constructor return");
159 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq, bool _no_Doppler_effect ) :
164 reference_dist(500.0),
167 #ifdef USE_SOFTWARE_DOPPLER
168 doppler_pitch_factor(1),
169 doppler_volume_factor(1),
172 no_Doppler_effect(_no_Doppler_effect)
174 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
176 sample_name = "unknown, generated from data";
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;
184 // clear errors from elsewhere?
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." );
193 format = AL_FORMAT_MONO8;
197 alBufferData( buffer, format, _data, size, freq );
198 if ( print_openal_error("constructor (alBufferData)") ) {
199 throw sg_exception("Failed to buffer data.");
202 print_openal_error("constructor return");
207 SGSoundSample::~SGSoundSample() {
208 SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
210 alDeleteBuffers(1, &buffer);
215 void SGSoundSample::play( bool _loop ) {
218 alSourceStop( source );
221 playing = bind_source();
225 alSourcei( source, AL_LOOPING, loop );
226 alSourcePlay( source );
228 print_openal_error("play (alSourcePlay)");
233 // stop playing the sample
234 void SGSoundSample::stop() {
236 alSourceStop( source );
237 alDeleteSources(1, &source);
239 print_openal_error("stop (alDeleteSources)");
244 // Generate sound source
246 SGSoundSample::bind_source() {
255 // Bind buffer with a source.
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.");
265 alSourcei( source, AL_BUFFER, buffer );
266 #ifndef USE_SOFTWARE_DOPPLER
267 alSourcef( source, AL_PITCH, pitch );
268 alSourcef( source, AL_GAIN, volume );
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 );
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 );
283 alSourcei( source, AL_LOOPING, loop );
285 alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
286 alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
287 alSourcef( source, AL_MAX_DISTANCE, max_dist );
289 print_openal_error("bind_sources return");
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; }
301 #ifndef USE_SOFTWARE_DOPPLER
302 alSourcef( source, AL_PITCH, pitch );
303 print_openal_error("set_pitch");
305 alSourcef( source, AL_PITCH, pitch * doppler_pitch_factor );
306 alGetError(); // ignore if the pitch is clamped by the driver
312 SGSoundSample::set_volume( double v ) {
315 #ifndef USE_SOFTWARE_DOPPLER
316 alSourcef( source, AL_GAIN, volume );
318 alSourcef( source, AL_GAIN, volume * doppler_volume_factor );
320 print_openal_error("set_volume");
326 SGSoundSample::is_playing( ) {
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 );
334 return (result == AL_PLAYING) ;
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];
347 sgAddVec3( final_pos, source_pos, offset_pos );
349 alSourcefv( source, AL_POSITION, final_pos );
350 print_openal_error("set_source_pos");
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];
362 sgAddVec3( final_pos, source_pos, offset_pos );
364 alSourcefv( source, AL_POSITION, final_pos );
365 print_openal_error("set_offset_pos");
370 SGSoundSample::set_orientation( ALfloat *dir, ALfloat inner_angle,
376 outergain = outer_gain;
377 direction[0] = dir[0];
378 direction[1] = dir[1];
379 direction[2] = dir[2];
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 );
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];
395 source_vel[0] = vel[0];
396 source_vel[1] = vel[1];
397 source_vel[2] = vel[2];
399 #ifdef USE_OPEN_AL_DOPPLER
401 alSourcefv( source, AL_VELOCITY, source_vel );
403 #elif defined (USE_OPEN_AL_DOPPLER_WITH_FIXED_LISTENER)
406 sgSubVec3( relative_vel, source_vel, listener_vel );
407 alSourcefv( source, AL_VELOCITY, relative_vel );
410 if (no_Doppler_effect) {
411 doppler_pitch_factor = 1;
412 doppler_volume_factor = 1;
417 sgAddVec3( final_pos, source_pos, offset_pos );
418 mfp = sgLengthVec3(final_pos);
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)
424 doppler = (340 - vls) / (340 - vss);
425 doppler = ( doppler > 0) ? ( ( doppler < 10) ? doppler : 10 ) : 0;
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)
443 vss = min(vss, SS/DF)
444 vls = min(vls, SS/DF)
445 f' = f * (SS - DF*vls) / (SS - DF*vss)
449 doppler_pitch_factor = doppler;
450 doppler_volume_factor = 1;
453 doppler_pitch_factor = (doppler < 11) ? doppler : 11;
454 doppler_volume_factor = (doppler < 11) ? 11-doppler : 0;
458 doppler_pitch_factor = 0.1;
459 doppler_volume_factor = (doppler > 0) ? doppler * 10 : 0;
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
471 SGSoundSample::set_reference_dist( ALfloat dist ) {
472 reference_dist = dist;
474 alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
480 SGSoundSample::set_max_dist( ALfloat dist ) {
483 alSourcef( source, AL_MAX_DISTANCE, max_dist );
488 SGSoundSample::load_file(const char *path, const char *file)
492 SGPath samplepath( path );
493 if ( strlen(file) ) {
494 samplepath.append( file );
497 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
499 data = alutLoadMemoryFromFile(samplepath.c_str(), &format, &size, &freqf );
501 throw sg_io_exception("Failed to load wav file.",
502 sg_location(samplepath.str()));
504 freq = (ALsizei)freqf;
506 # if defined (__APPLE__)
507 alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
508 &format, &data, &size, &freq );
510 alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
511 &format, &data, &size, &freq, &loop );
513 if ( print_openal_error("constructor (alutLoadWAVFile)") ) {
514 throw sg_io_exception("Failed to load wav file.",
515 sg_location(samplepath.str()));