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 );
68 SGSoundSample::SGSoundSample( const char *path, const char *file,
75 reference_dist(500.0),
80 SGPath samplepath( path );
82 samplepath.append( file );
85 sample_name = samplepath.str();
87 SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
88 << samplepath.str() );
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;
96 // clear errors from elsewhere?
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.");
105 // Load the sample file
106 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
108 buffer = alutCreateBufferFromFile(samplepath.c_str());
109 if (buffer == AL_NONE) {
110 ALenum error = alutGetError ();
111 print_openal_error("constructor (alutCreateBufferFromFile)");
112 throw sg_exception("Failed to load wav file: "+string(alutGetErrorString (error)));
117 // pre 1.0 alut version
119 # if defined (__APPLE__)
120 alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
121 &format, &data, &size, &freq );
123 alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
124 &format, &data, &size, &freq, &loop );
126 if ( print_openal_error("constructor (alutLoadWAVFile)") ) {
127 throw sg_exception("Failed to load wav 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.");
138 alutUnloadWAV( format, data, size, freq );
143 print_openal_error("constructor return");
148 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq,
155 reference_dist(500.0),
160 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
162 sample_name = "unknown, generated from data";
164 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
165 offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
166 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
167 direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
168 inner = outer = 360.0; outergain = 0.0;
170 // clear errors from elsewhere?
173 // Load wav data into a buffer.
174 alGenBuffers(1, &buffer);
175 if ( print_openal_error("constructor (alGenBuffers)") ) {
176 throw sg_exception("Failed to gen buffer." );
179 format = AL_FORMAT_MONO8;
184 alBufferData( buffer, format, data, size, freq );
185 if ( print_openal_error("constructor (alBufferData)") ) {
186 throw sg_exception("Failed to buffer data.");
190 alutUnloadWAV( format, data, size, freq );
194 print_openal_error("constructor return");
199 SGSoundSample::~SGSoundSample() {
200 SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
201 alDeleteBuffers(1, &buffer);
206 void SGSoundSample::play( bool _loop ) {
209 alSourceStop( source );
212 playing = bind_source();
216 alSourcei( source, AL_LOOPING, loop );
217 alSourcePlay( source );
219 print_openal_error("play (alSourcePlay)");
224 // stop playing the sample
225 void SGSoundSample::stop() {
227 alSourceStop( source );
228 alDeleteSources(1, &source);
230 print_openal_error("stop (alDeleteSources)");
235 // Generate sound source
237 SGSoundSample::bind_source() {
243 // Bind buffer with a source.
245 alGenSources(1, &source);
246 if ( print_openal_error("bind_source (alGenSources)") ) {
247 // No biggy, better luck next time.
248 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to generate audio source.");
249 // SG_LOG( SG_GENERAL, SG_ALERT, "Please update your sound driver and try again.");
253 alSourcei( source, AL_BUFFER, buffer );
254 alSourcef( source, AL_PITCH, pitch );
255 alSourcef( source, AL_GAIN, volume );
256 alSourcefv( source, AL_POSITION, source_pos );
257 alSourcefv( source, AL_DIRECTION, direction );
258 alSourcef( source, AL_CONE_INNER_ANGLE, inner );
259 alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
260 alSourcef( source, AL_CONE_OUTER_GAIN, outergain);
261 alSourcefv( source, AL_VELOCITY, source_vel );
262 alSourcei( source, AL_LOOPING, loop );
264 alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
265 alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
266 alSourcef( source, AL_MAX_DISTANCE, max_dist );
268 print_openal_error("bind_sources return");
274 SGSoundSample::set_pitch( double p ) {
275 // clamp in the range of 0.01 to 2.0
276 if ( p < 0.01 ) { p = 0.01; }
277 if ( p > 2.0 ) { p = 2.0; }
280 alSourcef( source, AL_PITCH, pitch );
281 print_openal_error("set_pitch");
286 SGSoundSample::set_volume( double v ) {
289 alSourcef( source, AL_GAIN, volume );
290 print_openal_error("set_volume");
296 SGSoundSample::is_playing( ) {
299 alGetSourcei( source, AL_SOURCE_STATE, &result );
300 if ( alGetError() != AL_NO_ERROR) {
301 SG_LOG( SG_GENERAL, SG_ALERT,
302 "Oops AL error in sample is_playing(): " << sample_name );
304 return (result == AL_PLAYING) ;
310 SGSoundSample::set_source_pos( ALfloat *pos ) {
311 source_pos[0] = pos[0];
312 source_pos[1] = pos[1];
313 source_pos[2] = pos[2];
317 sgAddVec3( final_pos, source_pos, offset_pos );
319 alSourcefv( source, AL_POSITION, final_pos );
324 SGSoundSample::set_offset_pos( ALfloat *pos ) {
325 offset_pos[0] = pos[0];
326 offset_pos[1] = pos[1];
327 offset_pos[2] = pos[2];
331 sgAddVec3( final_pos, source_pos, offset_pos );
333 alSourcefv( source, AL_POSITION, final_pos );
338 SGSoundSample::set_orientation( ALfloat *dir, ALfloat inner_angle,
344 outergain = outer_gain;
346 alSourcefv( source, AL_DIRECTION, dir);
347 alSourcef( source, AL_CONE_INNER_ANGLE, inner );
348 alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
349 alSourcef( source, AL_CONE_OUTER_GAIN, outergain );
354 SGSoundSample::set_source_vel( ALfloat *vel ) {
355 source_vel[0] = vel[0];
356 source_vel[1] = vel[1];
357 source_vel[2] = vel[2];
359 alSourcefv( source, AL_VELOCITY, source_vel );
364 SGSoundSample::set_reference_dist( ALfloat dist ) {
365 reference_dist = dist;
367 alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
373 SGSoundSample::set_max_dist( ALfloat dist ) {
376 alSourcef( source, AL_MAX_DISTANCE, max_dist );