1 // sample.cxx -- Sound sample encapsulation class
3 // Written by Curtis Olson, started April 2004.
5 // Copyright (C) 2004 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
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>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/structure/exception.hxx>
38 #include "sample_openal.hxx"
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" );
58 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
64 SGSoundSample::SGSoundSample( const char *path, const char *file,
69 reference_dist(500.0),
73 SGPath samplepath( path );
75 samplepath.append( file );
78 sample_name = samplepath.str();
80 SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
81 << samplepath.str() );
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;
87 // clear errors from elsewhere?
90 // create an OpenAL buffer handle
91 alGenBuffers(1, &buffer);
92 ALuint error = alGetError();
93 if ( error != AL_NO_ERROR ) {
94 print_openal_error( error );
95 throw sg_exception("Failed to gen OpenAL buffer.");
98 // Load the sample file
99 #if defined (__APPLE__)
100 alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
101 &format, &data, &size, &freq );
103 alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
104 &format, &data, &size, &freq, &loop );
106 if (alGetError() != AL_NO_ERROR) {
107 throw sg_exception("Failed to load wav file.");
110 // Copy data to the internal OpenAL buffer
111 alBufferData( buffer, format, data, size, freq );
112 if (alGetError() != AL_NO_ERROR) {
113 throw sg_exception("Failed to buffer data.");
117 alutUnloadWAV( format, data, size, freq );
121 // Bind buffer with a source.
122 alGenSources(1, &source);
123 if (alGetError() != AL_NO_ERROR) {
124 throw sg_exception("Failed to gen source.");
127 alSourcei( source, AL_BUFFER, buffer );
128 alSourcef( source, AL_PITCH, pitch );
129 alSourcef( source, AL_GAIN, volume );
130 alSourcefv( source, AL_POSITION, source_pos );
131 alSourcefv( source, AL_VELOCITY, source_vel );
132 alSourcei( source, AL_LOOPING, loop );
134 alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
135 alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
136 alSourcef( source, AL_MAX_DISTANCE, max_dist );
141 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq,
146 reference_dist(500.0),
150 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
152 sample_name = "unknown, generated from data";
154 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
155 offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0;
156 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
158 // clear errors from elsewhere?
161 // Load wav data into a buffer.
162 alGenBuffers(1, &buffer);
163 ALuint error = alGetError();
164 if ( error != AL_NO_ERROR ) {
165 print_openal_error( error );
166 throw sg_exception("Failed to gen buffer." );
170 format = AL_FORMAT_MONO8;
175 alBufferData( buffer, format, data, size, freq );
176 if (alGetError() != AL_NO_ERROR) {
177 throw sg_exception("Failed to buffer data.");
181 alutUnloadWAV( format, data, size, freq );
185 // Bind buffer with a source.
186 alGenSources(1, &source);
187 if (alGetError() != AL_NO_ERROR) {
188 throw sg_exception("Failed to gen source.");
191 alSourcei( source, AL_BUFFER, buffer );
192 alSourcef( source, AL_PITCH, pitch );
193 alSourcef( source, AL_GAIN, volume );
194 alSourcefv( source, AL_POSITION, source_pos );
195 alSourcefv( source, AL_VELOCITY, source_vel );
196 alSourcei( source, AL_LOOPING, loop );
198 alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
199 alSourcef( source, AL_REFERENCE_DISTANCE, reference_dist );
200 alSourcef( source, AL_MAX_DISTANCE, max_dist );
205 SGSoundSample::~SGSoundSample() {
206 SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
207 alDeleteSources(1, &source);
208 alDeleteBuffers(1, &buffer);
213 void SGSoundSample::play( bool _loop ) {
216 // make sure sound isn't already playing
217 alSourceStop( source );
219 alSourcei( source, AL_LOOPING, loop );
220 alSourcePlay( source );
224 // stop playing the sample
225 void SGSoundSample::stop() {
226 alSourceStop( source );