]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.cxx
124cac797db925c0869a27f90f57bba66c00f594
[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     pitch(1.0),
66     volume(1.0),
67     loop(AL_FALSE)
68 {
69     SGPath samplepath( path );
70     if ( strlen(file) ) {
71         samplepath.append( file );
72     }
73      
74     SG_LOG( SG_GENERAL, SG_ALERT, "From file sounds sample = "
75             << samplepath.str() );
76
77     ALuint error;
78
79     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
80     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
81
82     // clear errors from elsewhere?
83     alGetError();
84
85     // create an OpenAL buffer handle
86     alGenBuffers(1, &buffer);
87     error = alGetError();
88     if ( error != AL_NO_ERROR ) {
89         print_openal_error( error );
90         throw sg_exception("Failed to gen OpenAL buffer.");
91     } else {
92         SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
93     }
94
95     // Load the sample file
96 #if defined (__APPLE__)
97     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
98                      &format, &data, &size, &freq );
99 #else
100     alutLoadWAVFile( (ALbyte *)samplepath.c_str(),
101                      &format, &data, &size, &freq, &loop );
102 #endif
103     if (alGetError() != AL_NO_ERROR) {
104         throw sg_exception("Failed to load wav file.");
105     }
106
107     // Copy data to the internal OpenAL buffer
108     alBufferData( buffer, format, data, size, freq );
109     if (alGetError() != AL_NO_ERROR) {
110         throw sg_exception("Failed to buffer data.");
111     }
112
113     alutUnloadWAV( format, data, size, freq );
114
115     // Bind buffer with a source.
116     alGenSources(1, &source);
117     if (alGetError() != AL_NO_ERROR) {
118         throw sg_exception("Failed to gen source.");
119     }
120
121     alSourcei( source, AL_BUFFER, buffer );
122     alSourcef( source, AL_PITCH, pitch );
123     alSourcef( source, AL_GAIN, volume );
124     alSourcefv( source, AL_POSITION, source_pos );
125     alSourcefv( source, AL_VELOCITY, source_vel );
126     alSourcei( source, AL_LOOPING, loop );
127 }
128
129
130 // constructor
131 SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) :
132     pitch(1.0),
133     volume(1.0),
134     loop(AL_FALSE)
135 {
136     SG_LOG( SG_GENERAL, SG_ALERT, "In memory sounds sample" );
137
138     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
139     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
140
141     // Load wav data into a buffer.
142     alGenBuffers(1, &buffer);
143     if (alGetError() != AL_NO_ERROR) {
144         SG_LOG( SG_GENERAL, SG_ALERT, "Error in alGenBuffers()" );
145         return;
146     }
147
148     format = AL_FORMAT_MONO8;
149     size = len;
150     data = _data;
151     freq = _freq;
152
153     alBufferData( buffer, format, data, size, freq );
154
155     // Bind buffer with a source.
156     alGenSources(1, &source);
157     if (alGetError() != AL_NO_ERROR) {
158         throw sg_exception("Failed to gen source.");
159     }
160
161     alSourcei( source, AL_BUFFER, buffer );
162     alSourcef( source, AL_PITCH, pitch );
163     alSourcef( source, AL_GAIN, volume );
164     alSourcefv( source, AL_POSITION, source_pos );
165     alSourcefv( source, AL_VELOCITY, source_vel );
166     alSourcei( source, AL_LOOPING, loop );
167 }
168
169
170 // destructor
171 SGSoundSample::~SGSoundSample() {
172     SG_LOG( SG_GENERAL, SG_ALERT, "Deleting a sample" );
173     alDeleteSources(1, &source);
174     alDeleteBuffers(1, &buffer);
175 }
176
177
178 // play the sample
179 void SGSoundSample::play( bool _loop ) {
180     loop = _loop;
181     
182     // make sure sound isn't already playing
183     alSourceStop( source );
184
185     alSourcei( source, AL_LOOPING, loop );
186     alSourcePlay( source );
187 }
188
189
190 // stop playing the sample
191 void SGSoundSample::stop() {
192     alSourceStop( source );
193 }