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,
70 SGPath samplepath( path );
72 samplepath.append( file );
75 sample_name = samplepath.str();
77 SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = "
78 << samplepath.str() );
82 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
83 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
85 // clear errors from elsewhere?
88 // create an OpenAL buffer handle
89 alGenBuffers(1, &buffer);
91 if ( error != AL_NO_ERROR ) {
92 print_openal_error( error );
93 throw sg_exception("Failed to gen OpenAL buffer.");
96 // Load the sample file
97 #if defined (__APPLE__)
98 alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
99 &format, &data, &size, &freq );
101 alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
102 &format, &data, &size, &freq, &loop );
104 if (alGetError() != AL_NO_ERROR) {
105 throw sg_exception("Failed to load wav file.");
108 // Copy data to the internal OpenAL buffer
109 alBufferData( buffer, format, data, size, freq );
110 if (alGetError() != AL_NO_ERROR) {
111 throw sg_exception("Failed to buffer data.");
115 alutUnloadWAV( format, data, size, freq );
119 // Bind buffer with a source.
120 alGenSources(1, &source);
121 if (alGetError() != AL_NO_ERROR) {
122 throw sg_exception("Failed to gen source.");
125 alSourcei( source, AL_BUFFER, buffer );
126 alSourcef( source, AL_PITCH, pitch );
127 alSourcef( source, AL_GAIN, volume );
128 alSourcefv( source, AL_POSITION, source_pos );
129 alSourcefv( source, AL_VELOCITY, source_vel );
130 alSourcei( source, AL_LOOPING, loop );
135 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) :
140 SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
142 sample_name = "unknown, generated from data";
144 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
145 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
147 // Load wav data into a buffer.
148 alGenBuffers(1, &buffer);
149 if (alGetError() != AL_NO_ERROR) {
150 throw sg_exception("Failed to gen buffer." );
154 format = AL_FORMAT_MONO8;
159 alBufferData( buffer, format, data, size, freq );
161 // Bind buffer with a source.
162 alGenSources(1, &source);
163 if (alGetError() != AL_NO_ERROR) {
164 throw sg_exception("Failed to gen source.");
167 alSourcei( source, AL_BUFFER, buffer );
168 alSourcef( source, AL_PITCH, pitch );
169 alSourcef( source, AL_GAIN, volume );
170 alSourcefv( source, AL_POSITION, source_pos );
171 alSourcefv( source, AL_VELOCITY, source_vel );
172 alSourcei( source, AL_LOOPING, loop );
177 SGSoundSample::~SGSoundSample() {
178 SG_LOG( SG_GENERAL, SG_INFO, "Deleting a sample" );
179 if ( data != NULL ) {
182 alDeleteSources(1, &source);
183 alDeleteBuffers(1, &buffer);
188 void SGSoundSample::play( bool _loop ) {
191 // make sure sound isn't already playing
192 alSourceStop( source );
194 alSourcei( source, AL_LOOPING, loop );
195 alSourcePlay( source );
199 // stop playing the sample
200 void SGSoundSample::stop() {
201 alSourceStop( source );