]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
Oops, ALUT 1.0 requires a little more work than expected.
[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(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
107
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)));
113   }
114
115 #else
116         //
117         // pre 1.0 alut version
118         //
119 # if defined (__APPLE__)
120     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
121                      &format, &data, &size, &freq );
122 # else
123     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
124                      &format, &data, &size, &freq, &loop );
125 # endif
126     if ( print_openal_error("constructor (alutLoadWAVFile)") ) {
127         throw sg_exception("Failed to load wav file.");
128     }
129
130     // Copy data to the internal OpenAL buffer
131     alBufferData( buffer, format, data, size, freq );
132
133     if ( print_openal_error("constructor (alBufferData)") ) {
134         throw sg_exception("Failed to buffer data.");
135     }
136
137     if ( cleanup ) {
138         alutUnloadWAV( format, data, size, freq );
139         data = NULL;
140     }
141 #endif
142
143     print_openal_error("constructor return");
144 }
145
146
147 // constructor
148 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq,
149                               bool cleanup) :
150     data(NULL),
151     buffer(0),
152     source(0),
153     pitch(1.0),
154     volume(1.0),
155     reference_dist(500.0),
156     max_dist(3000.),
157     loop(AL_FALSE),
158     playing(false)
159 {
160     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
161
162     sample_name = "unknown, generated from data";
163
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;
169
170     // clear errors from elsewhere?
171     alGetError();
172
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." );
177     }
178
179     format = AL_FORMAT_MONO8;
180     size = len;
181     data = _data;
182     freq = _freq;
183
184     alBufferData( buffer, format, data, size, freq );
185     if ( print_openal_error("constructor (alBufferData)") ) {
186         throw sg_exception("Failed to buffer data.");
187     }
188
189     if ( cleanup ) {
190         alutUnloadWAV( format, data, size, freq );
191         data = NULL;
192     }
193
194     print_openal_error("constructor return");
195 }
196
197
198 // destructor
199 SGSoundSample::~SGSoundSample() {
200     SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
201     alDeleteBuffers(1, &buffer);
202 }
203
204
205 // play the sample
206 void SGSoundSample::play( bool _loop ) {
207
208     if ( source ) {
209         alSourceStop( source );
210     }
211
212     playing = bind_source();
213     if ( playing ) {
214         loop = _loop;
215     
216         alSourcei( source, AL_LOOPING, loop );
217         alSourcePlay( source );
218
219         print_openal_error("play (alSourcePlay)");
220     }
221 }
222
223
224 // stop playing the sample
225 void SGSoundSample::stop() {
226     if (playing) {
227         alSourceStop( source );
228         alDeleteSources(1, &source);
229         source = 0;
230         print_openal_error("stop (alDeleteSources)");
231     }
232     playing = false;
233 }
234
235 // Generate sound source
236 bool
237 SGSoundSample::bind_source() {
238
239     if ( playing ) {
240         return true;
241     }
242
243     // Bind buffer with a source.
244     alGetError();
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.");
250         return false;
251     }
252
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 );
263
264     alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
265     alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
266     alSourcef( source, AL_MAX_DISTANCE, max_dist );
267
268     print_openal_error("bind_sources return");
269
270     return true;
271 }
272
273 void
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; }
278     pitch = p;
279     if (playing) {
280         alSourcef( source, AL_PITCH, pitch );
281         print_openal_error("set_pitch");
282     }
283 }
284
285 void
286 SGSoundSample::set_volume( double v ) {
287     volume = v;
288     if (playing) {
289         alSourcef( source, AL_GAIN, volume );
290         print_openal_error("set_volume");
291     }
292 }
293
294
295 bool
296 SGSoundSample::is_playing( ) {
297     if (playing) {
298         ALint result;
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 );
303         }
304         return (result == AL_PLAYING) ;
305     } else
306         return false;
307 }
308
309 void
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];
314
315     if (playing) {
316         sgVec3 final_pos;
317         sgAddVec3( final_pos, source_pos, offset_pos );
318
319         alSourcefv( source, AL_POSITION, final_pos );
320     }
321 }
322
323 void
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];
328
329     if (playing) {
330         sgVec3 final_pos;
331         sgAddVec3( final_pos, source_pos, offset_pos );
332
333         alSourcefv( source, AL_POSITION, final_pos );
334     }
335 }
336
337 void
338 SGSoundSample::set_orientation( ALfloat *dir, ALfloat inner_angle,
339                                            ALfloat outer_angle,
340                                            ALfloat outer_gain)
341 {
342     inner = inner_angle;
343     outer = outer_angle;
344     outergain = outer_gain;
345     if (playing) {
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 );
350     }
351 }
352
353 void
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];
358     if (playing) {
359         alSourcefv( source, AL_VELOCITY, source_vel );
360     }
361 }
362
363 void
364 SGSoundSample::set_reference_dist( ALfloat dist ) {
365     reference_dist = dist;
366     if (playing) {
367         alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
368     }
369 }
370
371
372 void
373 SGSoundSample::set_max_dist( ALfloat dist ) {
374     max_dist = dist;
375     if (playing) {
376         alSourcef( source, AL_MAX_DISTANCE, max_dist );
377     }
378 }