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