]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
direction vector needs to be initialized, otherwise garbage data could cause
[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 - curt@flightgear.org
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 void print_openal_error( ALuint error ) {
47     if ( error == AL_INVALID_NAME ) {
48         SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
49     } else if ( error == AL_ILLEGAL_ENUM ) {
50         SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
51     } else if ( error == AL_INVALID_VALUE ) {
52         SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
53     } else if ( error == AL_ILLEGAL_COMMAND ) {
54         SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
55     } else if ( error == AL_OUT_OF_MEMORY ) {
56         SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
57     } else {
58         SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
59     }
60 }
61
62
63 // constructor
64 SGSoundSample::SGSoundSample( const char *path, const char *file,
65                               bool cleanup ) :
66     data(NULL),
67     pitch(1.0),
68     volume(1.0),
69     reference_dist(500.0),
70     max_dist(3000.),
71     loop(AL_FALSE)
72 {
73     SGPath samplepath( path );
74     if ( strlen(file) ) {
75         samplepath.append( file );
76     }
77
78     sample_name = samplepath.str();
79
80     SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
81             << samplepath.str() );
82
83     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
84     offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
85     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
86     direction[0] = 0.0; direction[1] = 0.0; direction[2] = 0.0;
87     inner = outer = 360.0; outergain = 0.0;
88
89     // clear errors from elsewhere?
90     alGetError();
91
92     // create an OpenAL buffer handle
93     alGenBuffers(1, &buffer);
94     ALuint error = alGetError();
95     if ( error != AL_NO_ERROR ) {
96         print_openal_error( error );
97         throw sg_exception("Failed to gen OpenAL buffer.");
98     }
99
100     // Load the sample file
101 #if defined (__APPLE__)
102     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
103                      &format, &data, &size, &freq );
104 #else
105     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
106                      &format, &data, &size, &freq, &loop );
107 #endif
108     if (alGetError() != AL_NO_ERROR) {
109         throw sg_exception("Failed to load wav file.");
110     }
111
112     // Copy data to the internal OpenAL buffer
113     alBufferData( buffer, format, data, size, freq );
114     if (alGetError() != AL_NO_ERROR) {
115         throw sg_exception("Failed to buffer data.");
116     }
117
118     if ( cleanup ) {
119         alutUnloadWAV( format, data, size, freq );
120         data = NULL;
121     }
122
123     // Bind buffer with a source.
124     alGenSources(1, &source);
125     if (alGetError() != AL_NO_ERROR) {
126         throw sg_exception("Failed to gen source.");
127     }
128
129     alSourcei( source, AL_BUFFER, buffer );
130     alSourcef( source, AL_PITCH, pitch );
131     alSourcef( source, AL_GAIN, volume );
132     alSourcefv( source, AL_POSITION, source_pos );
133     alSourcefv( source, AL_DIRECTION, direction );
134     alSourcef( source, AL_CONE_INNER_ANGLE, inner );
135     alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
136     alSourcef( source, AL_CONE_OUTER_GAIN, outergain);
137     alSourcefv( source, AL_VELOCITY, source_vel );
138     alSourcei( source, AL_LOOPING, loop );
139
140     alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
141     alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
142     alSourcef( source, AL_MAX_DISTANCE, max_dist );
143 }
144
145
146 // constructor
147 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq,
148                               bool cleanup) :
149     data(NULL),
150     pitch(1.0),
151     volume(1.0),
152     reference_dist(500.0),
153     max_dist(3000.),
154     loop(AL_FALSE)
155 {
156     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
157
158     sample_name = "unknown, generated from data";
159
160     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
161     offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
162     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[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     ALuint error = alGetError();
171     if ( error != AL_NO_ERROR ) {
172         print_openal_error( error );
173         throw sg_exception("Failed to gen buffer." );
174         return;
175     }
176
177     format = AL_FORMAT_MONO8;
178     size = len;
179     data = _data;
180     freq = _freq;
181
182     alBufferData( buffer, format, data, size, freq );
183     if (alGetError() != AL_NO_ERROR) {
184         throw sg_exception("Failed to buffer data.");
185     }
186
187     if ( cleanup ) {
188         alutUnloadWAV( format, data, size, freq );
189         data = NULL;
190     }
191
192     // Bind buffer with a source.
193     alGenSources(1, &source);
194     if (alGetError() != AL_NO_ERROR) {
195         throw sg_exception("Failed to gen source.");
196     }
197
198     alSourcei( source, AL_BUFFER, buffer );
199     alSourcef( source, AL_PITCH, pitch );
200     alSourcef( source, AL_GAIN, volume );
201     alSourcefv( source, AL_POSITION, source_pos );
202     alSourcefv( source, AL_DIRECTION, direction );
203     alSourcef( source, AL_CONE_INNER_ANGLE, inner );
204     alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
205     alSourcef( source, AL_CONE_OUTER_GAIN, outergain );
206     alSourcefv( source, AL_VELOCITY, source_vel );
207     alSourcei( source, AL_LOOPING, loop );
208
209     alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
210     alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
211     alSourcef( source, AL_MAX_DISTANCE, max_dist );
212 }
213
214
215 // destructor
216 SGSoundSample::~SGSoundSample() {
217     SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
218     alDeleteSources(1, &source);
219     alDeleteBuffers(1, &buffer);
220 }
221
222
223 // play the sample
224 void SGSoundSample::play( bool _loop ) {
225     loop = _loop;
226     
227     // make sure sound isn't already playing
228     alSourceStop( source );
229
230     alSourcei( source, AL_LOOPING, loop );
231     alSourcePlay( source );
232 }
233
234
235 // stop playing the sample
236 void SGSoundSample::stop() {
237     alSourceStop( source );
238 }