]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
Prepare for ALUT version 1.0
[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      print_openal_error("constructor (alutCreateBufferFromFile)");
111      throw sg_exception("Failed to load wav file.");
112   }
113
114 #else
115         //
116         // pre 1.0 alut version
117         //
118 # if defined (__APPLE__)
119     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
120                      &format, &data, &size, &freq );
121 # else
122     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
123                      &format, &data, &size, &freq, &loop );
124 # endif
125     if ( print_openal_error("constructor (alutLoadWAVFile)") ) {
126         throw sg_exception("Failed to load wav file.");
127     }
128
129     // Copy data to the internal OpenAL buffer
130     alBufferData( buffer, format, data, size, freq );
131
132     if ( print_openal_error("constructor (alBufferData)") ) {
133         throw sg_exception("Failed to buffer data.");
134     }
135
136     if ( cleanup ) {
137         alutUnloadWAV( format, data, size, freq );
138         data = NULL;
139     }
140 #endif
141
142     print_openal_error("constructor return");
143 }
144
145
146 // constructor
147 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq,
148                               bool cleanup) :
149     data(NULL),
150     buffer(0),
151     source(0),
152     pitch(1.0),
153     volume(1.0),
154     reference_dist(500.0),
155     max_dist(3000.),
156     loop(AL_FALSE),
157     playing(false)
158 {
159     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
160
161     sample_name = "unknown, generated from data";
162
163     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
164     offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
165     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
166     direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
167     inner = outer = 360.0; outergain = 0.0;
168
169     // clear errors from elsewhere?
170     alGetError();
171
172     // Load wav data into a buffer.
173     alGenBuffers(1, &buffer);
174     if ( print_openal_error("constructor (alGenBuffers)") ) {
175         throw sg_exception("Failed to gen buffer." );
176     }
177
178     format = AL_FORMAT_MONO8;
179     size = len;
180     data = _data;
181     freq = _freq;
182
183     alBufferData( buffer, format, data, size, freq );
184     if ( print_openal_error("constructor (alBufferData)") ) {
185         throw sg_exception("Failed to buffer data.");
186     }
187
188     if ( cleanup ) {
189         alutUnloadWAV( format, data, size, freq );
190         data = NULL;
191     }
192
193     print_openal_error("constructor return");
194 }
195
196
197 // destructor
198 SGSoundSample::~SGSoundSample() {
199     SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
200     alDeleteBuffers(1, &buffer);
201 }
202
203
204 // play the sample
205 void SGSoundSample::play( bool _loop ) {
206
207     if ( source ) {
208         alSourceStop( source );
209     }
210
211     playing = bind_source();
212     if ( playing ) {
213         loop = _loop;
214     
215         alSourcei( source, AL_LOOPING, loop );
216         alSourcePlay( source );
217
218         print_openal_error("play (alSourcePlay)");
219     }
220 }
221
222
223 // stop playing the sample
224 void SGSoundSample::stop() {
225     if (playing) {
226         alSourceStop( source );
227         alDeleteSources(1, &source);
228         source = 0;
229         print_openal_error("stop (alDeleteSources)");
230     }
231     playing = false;
232 }
233
234 // Generate sound source
235 bool
236 SGSoundSample::bind_source() {
237
238     if ( playing ) {
239         return true;
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     }
320 }
321
322 void
323 SGSoundSample::set_offset_pos( ALfloat *pos ) {
324     offset_pos[0] = pos[0];
325     offset_pos[1] = pos[1];
326     offset_pos[2] = pos[2];
327
328     if (playing) {
329         sgVec3 final_pos;
330         sgAddVec3( final_pos, source_pos, offset_pos );
331
332         alSourcefv( source, AL_POSITION, final_pos );
333     }
334 }
335
336 void
337 SGSoundSample::set_orientation( ALfloat *dir, ALfloat inner_angle,
338                                            ALfloat outer_angle,
339                                            ALfloat outer_gain)
340 {
341     inner = inner_angle;
342     outer = outer_angle;
343     outergain = outer_gain;
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 }