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