]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
ef6db1d8feaae001a0f936ad7a4a5f66b03e40f1
[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     inner = outer = 360.0; outergain = 0.0;
87
88     // clear errors from elsewhere?
89     alGetError();
90
91     // create an OpenAL buffer handle
92     alGenBuffers(1, &buffer);
93     ALuint error = alGetError();
94     if ( error != AL_NO_ERROR ) {
95         print_openal_error( error );
96         throw sg_exception("Failed to gen OpenAL buffer.");
97     }
98
99     // Load the sample file
100 #if defined (__APPLE__)
101     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
102                      &format, &data, &size, &freq );
103 #else
104     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
105                      &format, &data, &size, &freq, &loop );
106 #endif
107     if (alGetError() != AL_NO_ERROR) {
108         throw sg_exception("Failed to load wav file.");
109     }
110
111     // Copy data to the internal OpenAL buffer
112     alBufferData( buffer, format, data, size, freq );
113     if (alGetError() != AL_NO_ERROR) {
114         throw sg_exception("Failed to buffer data.");
115     }
116
117     if ( cleanup ) {
118         alutUnloadWAV( format, data, size, freq );
119         data = NULL;
120     }
121
122     // Bind buffer with a source.
123     alGenSources(1, &source);
124     if (alGetError() != AL_NO_ERROR) {
125         throw sg_exception("Failed to gen source.");
126     }
127
128     alSourcei( source, AL_BUFFER, buffer );
129     alSourcef( source, AL_PITCH, pitch );
130     alSourcef( source, AL_GAIN, volume );
131     alSourcefv( source, AL_POSITION, source_pos );
132     alSourcefv( source, AL_DIRECTION, direction );
133     alSourcef( source, AL_CONE_INNER_ANGLE, inner );
134     alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
135     alSourcef( source, AL_CONE_OUTER_GAIN, outergain);
136     alSourcefv( source, AL_VELOCITY, source_vel );
137     alSourcei( source, AL_LOOPING, loop );
138
139     alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
140     alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
141     alSourcef( source, AL_MAX_DISTANCE, max_dist );
142 }
143
144
145 // constructor
146 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq,
147                               bool cleanup) :
148     data(NULL),
149     pitch(1.0),
150     volume(1.0),
151     reference_dist(500.0),
152     max_dist(3000.),
153     loop(AL_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     inner = outer = 360.0; outergain = 0.0;
163
164     // clear errors from elsewhere?
165     alGetError();
166
167     // Load wav data into a buffer.
168     alGenBuffers(1, &buffer);
169     ALuint error = alGetError();
170     if ( error != AL_NO_ERROR ) {
171         print_openal_error( error );
172         throw sg_exception("Failed to gen buffer." );
173         return;
174     }
175
176     format = AL_FORMAT_MONO8;
177     size = len;
178     data = _data;
179     freq = _freq;
180
181     alBufferData( buffer, format, data, size, freq );
182     if (alGetError() != AL_NO_ERROR) {
183         throw sg_exception("Failed to buffer data.");
184     }
185
186     if ( cleanup ) {
187         alutUnloadWAV( format, data, size, freq );
188         data = NULL;
189     }
190
191     // Bind buffer with a source.
192     alGenSources(1, &source);
193     if (alGetError() != AL_NO_ERROR) {
194         throw sg_exception("Failed to gen source.");
195     }
196
197     alSourcei( source, AL_BUFFER, buffer );
198     alSourcef( source, AL_PITCH, pitch );
199     alSourcef( source, AL_GAIN, volume );
200     alSourcefv( source, AL_POSITION, source_pos );
201     alSourcefv( source, AL_DIRECTION, direction );
202     alSourcef( source, AL_CONE_INNER_ANGLE, inner );
203     alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
204     alSourcef( source, AL_CONE_OUTER_GAIN, outergain );
205     alSourcefv( source, AL_VELOCITY, source_vel );
206     alSourcei( source, AL_LOOPING, loop );
207
208     alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
209     alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
210     alSourcef( source, AL_MAX_DISTANCE, max_dist );
211 }
212
213
214 // destructor
215 SGSoundSample::~SGSoundSample() {
216     SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
217     alDeleteSources(1, &source);
218     alDeleteBuffers(1, &buffer);
219 }
220
221
222 // play the sample
223 void SGSoundSample::play( bool _loop ) {
224     loop = _loop;
225     
226     // make sure sound isn't already playing
227     alSourceStop( source );
228
229     alSourcei( source, AL_LOOPING, loop );
230     alSourcePlay( source );
231 }
232
233
234 // stop playing the sample
235 void SGSoundSample::stop() {
236     alSourceStop( source );
237 }