]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
ec494392751a0578454b829eea4a14d7a66c1c62
[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;
67 }
68
69 // empry 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     playing(false),
79     no_Doppler_effect(true)
80 {
81 }
82
83 // constructor
84 SGSoundSample::SGSoundSample( const char *path, const char *file , bool _no_Doppler_effect ) :
85     buffer(0),
86     source(0),
87     pitch(1.0),
88     volume(1.0),
89     reference_dist(500.0),
90     max_dist(3000.),
91     loop(AL_FALSE),
92     playing(false),
93     no_Doppler_effect(_no_Doppler_effect)
94     {
95     SGPath samplepath( path );
96     if ( strlen(file) ) {
97         samplepath.append( file );
98     }
99     sample_name = samplepath.str();
100
101     SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
102             << samplepath.str() );
103
104     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
105     offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
106     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
107     direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
108     inner = outer = 360.0; outergain = 0.0;
109
110     // clear errors from elsewhere?
111     alGetError();
112
113     // create an OpenAL buffer handle
114     alGenBuffers(1, &buffer);
115     if ( print_openal_error("constructor (alGenBuffers)") ) {
116         throw sg_exception("Failed to gen OpenAL buffer.");
117     }
118
119     // Load the sample file
120 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
121
122   buffer = alutCreateBufferFromFile(samplepath.c_str());
123   if (buffer == AL_NONE) {
124      ALenum error = alutGetError ();
125      print_openal_error("constructor (alutCreateBufferFromFile)");
126      throw sg_io_exception("Failed to load wav file: ",
127                          sg_location(string(alutGetErrorString (error))));
128   }
129
130 #else
131         //
132         // pre 1.0 alut version
133         //
134     ALvoid* data = load_file(path, file);
135
136     // Copy data to the internal OpenAL buffer
137     alBufferData( buffer, format, data, size, freq );
138
139     if ( print_openal_error("constructor (alBufferData)") ) {
140         throw sg_exception("Failed to buffer data.");
141     }
142
143     alutUnloadWAV( format, data, size, freq );
144 #endif
145
146     print_openal_error("constructor return");
147 }
148
149 // constructor
150 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq , bool _no_Doppler_effect ) :
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     no_Doppler_effect(_no_Doppler_effect)
160 {
161     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
162
163     sample_name = "unknown, generated from data";
164
165     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
166     offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
167     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
168     direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
169     inner = outer = 360.0; outergain = 0.0;
170
171     // clear errors from elsewhere?
172     alGetError();
173
174     // Load wav data into a buffer.
175     alGenBuffers(1, &buffer);
176     if ( print_openal_error("constructor (alGenBuffers)") ) {
177         throw sg_exception("Failed to gen buffer." );
178     }
179
180     format = AL_FORMAT_MONO8;
181     size = len;
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     print_openal_error("constructor return");
190 }
191
192
193 // destructor
194 SGSoundSample::~SGSoundSample() {
195     SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
196     if (buffer)
197         alDeleteBuffers(1, &buffer);
198 }
199
200
201 // play the sample
202 void SGSoundSample::play( bool _loop ) {
203
204     if ( source ) {
205         alSourceStop( source );
206     }
207
208     playing = bind_source();
209     if ( playing ) {
210         loop = _loop;
211     
212         alSourcei( source, AL_LOOPING, loop );
213         alSourcePlay( source );
214
215         print_openal_error("play (alSourcePlay)");
216     }
217 }
218
219
220 // stop playing the sample
221 void SGSoundSample::stop() {
222     if (playing) {
223         alSourceStop( source );
224         alDeleteSources(1, &source);
225         source = 0;
226         print_openal_error("stop (alDeleteSources)");
227     }
228     playing = false;
229 }
230
231 // Generate sound source
232 bool
233 SGSoundSample::bind_source() {
234
235     if ( playing ) {
236         return true;
237     }
238     if ( buffer == 0 ) {
239         return false;
240     }
241
242     // Bind buffer with a source.
243     alGetError();
244     alGenSources(1, &source);
245     if ( print_openal_error("bind_source (alGenSources)") ) {
246         // No biggy, better luck next time.
247         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to generate audio source.");
248         // SG_LOG( SG_GENERAL, SG_ALERT, "Please update your sound driver and try again.");
249         return false;
250     }
251
252     alSourcei( source, AL_BUFFER, buffer );
253     alSourcef( source, AL_PITCH, pitch );
254     alSourcef( source, AL_GAIN, volume );
255     alSourcefv( source, AL_POSITION, source_pos );
256     alSourcefv( source, AL_DIRECTION, direction );
257     alSourcef( source, AL_CONE_INNER_ANGLE, inner );
258     alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
259     alSourcef( source, AL_CONE_OUTER_GAIN, outergain);
260     alSourcefv( source, AL_VELOCITY, source_vel );
261     alSourcei( source, AL_LOOPING, loop );
262
263     alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
264     alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
265     alSourcef( source, AL_MAX_DISTANCE, max_dist );
266
267     print_openal_error("bind_sources return");
268
269     return true;
270 }
271
272 void
273 SGSoundSample::set_pitch( double p ) {
274     // clamp in the range of 0.01 to 2.0
275     if ( p < 0.01 ) { p = 0.01; }
276     if ( p > 2.0 ) { p = 2.0; }
277     pitch = p;
278     if (playing) {
279         alSourcef( source, AL_PITCH, pitch );
280         print_openal_error("set_pitch");
281     }
282 }
283
284 void
285 SGSoundSample::set_volume( double v ) {
286     volume = v;
287     if (playing) {
288         alSourcef( source, AL_GAIN, volume );
289         print_openal_error("set_volume");
290     }
291 }
292
293
294 bool
295 SGSoundSample::is_playing( ) {
296     if (playing) {
297         ALint result;
298         alGetSourcei( source, AL_SOURCE_STATE, &result );
299         if ( alGetError() != AL_NO_ERROR) {
300             SG_LOG( SG_GENERAL, SG_ALERT,
301                 "Oops AL error in sample is_playing(): " << sample_name );
302         }
303         return (result == AL_PLAYING) ;
304     } else
305         return false;
306 }
307
308 void
309 SGSoundSample::set_source_pos( ALfloat *pos ) {
310     source_pos[0] = pos[0];
311     source_pos[1] = pos[1];
312     source_pos[2] = pos[2];
313
314     if (playing) {
315         sgVec3 final_pos;
316         sgAddVec3( final_pos, source_pos, offset_pos );
317
318         alSourcefv( source, AL_POSITION, final_pos );
319         print_openal_error("set_source_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         print_openal_error("set_offset_pos");
335     }
336 }
337
338 void
339 SGSoundSample::set_orientation( ALfloat *dir, ALfloat inner_angle,
340                                            ALfloat outer_angle,
341                                            ALfloat outer_gain)
342 {
343     inner = inner_angle;
344     outer = outer_angle;
345     outergain = outer_gain;
346     direction[0] = dir[0];
347     direction[1] = dir[1];
348     direction[2] = dir[2];
349     if (playing) {
350         alSourcefv( source, AL_DIRECTION, dir);
351         alSourcef( source, AL_CONE_INNER_ANGLE, inner );
352         alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
353         alSourcef( source, AL_CONE_OUTER_GAIN, outergain );
354     }
355 }
356
357 void
358 SGSoundSample::set_source_vel( ALfloat *vel , ALfloat *listener_vel ) {
359     if (no_Doppler_effect) {
360         source_vel[0] = listener_vel[0];
361         source_vel[1] = listener_vel[1];
362         source_vel[2] = listener_vel[2];
363     } else {
364         source_vel[0] = vel[0];
365         source_vel[1] = vel[1];
366         source_vel[2] = vel[2];
367     }
368     if (playing) {
369         alSourcefv( source, AL_VELOCITY, source_vel );
370     }
371 }
372
373 void
374 SGSoundSample::set_reference_dist( ALfloat dist ) {
375     reference_dist = dist;
376     if (playing) {
377         alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
378     }
379 }
380
381
382 void
383 SGSoundSample::set_max_dist( ALfloat dist ) {
384     max_dist = dist;
385     if (playing) {
386         alSourcef( source, AL_MAX_DISTANCE, max_dist );
387     }
388 }
389
390 ALvoid *
391 SGSoundSample::load_file(const char *path, const char *file)
392 {
393     ALvoid* data = 0;
394
395     SGPath samplepath( path );
396     if ( strlen(file) ) {
397         samplepath.append( file );
398     }
399
400 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
401     ALfloat freqf;
402     data = alutLoadMemoryFromFile(samplepath.c_str(), &format, &size, &freqf );
403     if (data == NULL) {
404         throw sg_io_exception("Failed to load wav file.",
405                                         sg_location(samplepath.str()));
406     }
407     freq = (ALsizei)freqf;
408 #else
409 # if defined (__APPLE__)
410     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
411                      &format, &data, &size, &freq );
412 # else
413     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
414                      &format, &data, &size, &freq, &loop );
415 # endif
416     if ( print_openal_error("constructor (alutLoadWAVFile)") ) {
417         throw sg_io_exception("Failed to load wav file.",
418                                         sg_location(samplepath.str()));
419     }
420 #endif
421
422     return data;
423 }
424