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