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., 675 Mass Ave, Cambridge, MA 02139, USA.
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>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/structure/exception.hxx>
38 #include "sample_openal.hxx"
46 static bool print_openal_error(const string &s = "unknown") {
47 ALuint error = alGetError();
48 if ( error == AL_NO_ERROR ) {
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 );
61 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
67 SGSoundSample::SGSoundSample() :
72 reference_dist(500.0),
80 SGSoundSample::SGSoundSample( const char *path, const char *file) :
85 reference_dist(500.0),
90 SGPath samplepath( path );
92 samplepath.append( file );
94 sample_name = samplepath.str();
96 SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
97 << samplepath.str() );
99 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
100 offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
101 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
102 direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
103 inner = outer = 360.0; outergain = 0.0;
105 // clear errors from elsewhere?
108 // create an OpenAL buffer handle
109 alGenBuffers(1, &buffer);
110 if ( print_openal_error("constructor (alGenBuffers)") ) {
111 throw sg_exception("Failed to gen OpenAL buffer.");
114 // Load the sample file
115 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
117 buffer = alutCreateBufferFromFile(samplepath.c_str());
118 if (buffer == AL_NONE) {
119 ALenum error = alutGetError ();
120 print_openal_error("constructor (alutCreateBufferFromFile)");
121 throw sg_exception("Failed to load wav file: "+string(alutGetErrorString (error)));
126 // pre 1.0 alut version
128 ALvoid* data = load_file(path, file);
130 // Copy data to the internal OpenAL buffer
131 alBufferData( buffer, format, data, size, freq );
133 if ( print_openal_error("constructor (alBufferData)") ) {
134 throw sg_exception("Failed to buffer data.");
137 alutUnloadWAV( format, data, size, freq );
140 print_openal_error("constructor return");
144 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) :
149 reference_dist(500.0),
154 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
156 sample_name = "unknown, generated from data";
158 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
159 offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
160 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
161 direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
162 inner = outer = 360.0; outergain = 0.0;
164 // clear errors from elsewhere?
167 // Load wav data into a buffer.
168 alGenBuffers(1, &buffer);
169 if ( print_openal_error("constructor (alGenBuffers)") ) {
170 throw sg_exception("Failed to gen buffer." );
173 format = AL_FORMAT_MONO8;
177 alBufferData( buffer, format, _data, size, freq );
178 if ( print_openal_error("constructor (alBufferData)") ) {
179 throw sg_exception("Failed to buffer data.");
184 print_openal_error("constructor return");
189 SGSoundSample::~SGSoundSample() {
190 SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
192 alDeleteBuffers(1, &buffer);
197 void SGSoundSample::play( bool _loop ) {
200 alSourceStop( source );
203 playing = bind_source();
207 alSourcei( source, AL_LOOPING, loop );
208 alSourcePlay( source );
210 print_openal_error("play (alSourcePlay)");
215 // stop playing the sample
216 void SGSoundSample::stop() {
218 alSourceStop( source );
219 alDeleteSources(1, &source);
221 print_openal_error("stop (alDeleteSources)");
226 // Generate sound source
228 SGSoundSample::bind_source() {
237 // Bind buffer with a source.
239 alGenSources(1, &source);
240 if ( print_openal_error("bind_source (alGenSources)") ) {
241 // No biggy, better luck next time.
242 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to generate audio source.");
243 // SG_LOG( SG_GENERAL, SG_ALERT, "Please update your sound driver and try again.");
247 alSourcei( source, AL_BUFFER, buffer );
248 alSourcef( source, AL_PITCH, pitch );
249 alSourcef( source, AL_GAIN, volume );
250 alSourcefv( source, AL_POSITION, source_pos );
251 alSourcefv( source, AL_DIRECTION, direction );
252 alSourcef( source, AL_CONE_INNER_ANGLE, inner );
253 alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
254 alSourcef( source, AL_CONE_OUTER_GAIN, outergain);
255 alSourcefv( source, AL_VELOCITY, source_vel );
256 alSourcei( source, AL_LOOPING, loop );
258 alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
259 alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
260 alSourcef( source, AL_MAX_DISTANCE, max_dist );
262 print_openal_error("bind_sources return");
268 SGSoundSample::set_pitch( double p ) {
269 // clamp in the range of 0.01 to 2.0
270 if ( p < 0.01 ) { p = 0.01; }
271 if ( p > 2.0 ) { p = 2.0; }
274 alSourcef( source, AL_PITCH, pitch );
275 print_openal_error("set_pitch");
280 SGSoundSample::set_volume( double v ) {
283 alSourcef( source, AL_GAIN, volume );
284 print_openal_error("set_volume");
290 SGSoundSample::is_playing( ) {
293 alGetSourcei( source, AL_SOURCE_STATE, &result );
294 if ( alGetError() != AL_NO_ERROR) {
295 SG_LOG( SG_GENERAL, SG_ALERT,
296 "Oops AL error in sample is_playing(): " << sample_name );
298 return (result == AL_PLAYING) ;
304 SGSoundSample::set_source_pos( ALfloat *pos ) {
305 source_pos[0] = pos[0];
306 source_pos[1] = pos[1];
307 source_pos[2] = pos[2];
311 sgAddVec3( final_pos, source_pos, offset_pos );
313 alSourcefv( source, AL_POSITION, final_pos );
318 SGSoundSample::set_offset_pos( ALfloat *pos ) {
319 offset_pos[0] = pos[0];
320 offset_pos[1] = pos[1];
321 offset_pos[2] = pos[2];
325 sgAddVec3( final_pos, source_pos, offset_pos );
327 alSourcefv( source, AL_POSITION, final_pos );
332 SGSoundSample::set_orientation( ALfloat *dir, ALfloat inner_angle,
338 outergain = outer_gain;
340 alSourcefv( source, AL_DIRECTION, dir);
341 alSourcef( source, AL_CONE_INNER_ANGLE, inner );
342 alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
343 alSourcef( source, AL_CONE_OUTER_GAIN, outergain );
348 SGSoundSample::set_source_vel( ALfloat *vel ) {
349 source_vel[0] = vel[0];
350 source_vel[1] = vel[1];
351 source_vel[2] = vel[2];
353 alSourcefv( source, AL_VELOCITY, source_vel );
358 SGSoundSample::set_reference_dist( ALfloat dist ) {
359 reference_dist = dist;
361 alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
367 SGSoundSample::set_max_dist( ALfloat dist ) {
370 alSourcef( source, AL_MAX_DISTANCE, max_dist );
375 SGSoundSample::load_file(const char *path, const char *file)
379 SGPath samplepath( path );
380 if ( strlen(file) ) {
381 samplepath.append( file );
384 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
386 data = alutLoadMemoryFromFile(samplepath.c_str(), &format, &size, &freqf );
388 throw sg_exception("Failed to load wav file.");
390 freq = (ALsizei)freqf;
392 # if defined (__APPLE__)
393 alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
394 &format, &data, &size, &freq );
396 alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
397 &format, &data, &size, &freq, &loop );
399 if ( print_openal_error("constructor (alutLoadWAVFile)") ) {
400 throw sg_exception("Failed to load wav file.");