4 // This is broken, but allows the file to compile without a POSIX
6 static unsigned int sleep(unsigned int secs) { return 0; }
8 #include <unistd.h> // sleep()
11 #if defined( __APPLE__ )
12 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
13 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
14 # include <OpenAL/al.h>
15 # include <OpenAL/alc.h>
16 # include <OpenAL/alut.h>
17 #elif defined(OPENALSDK)
27 #define AUDIOFILE SRC_DIR"/jet.wav"
29 #include <simgear/debug/logstream.hxx>
31 static void print_openal_error( ALuint error ) {
32 if ( error == AL_INVALID_NAME ) {
33 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
34 } else if ( error == AL_ILLEGAL_ENUM ) {
35 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
36 } else if ( error == AL_INVALID_VALUE ) {
37 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
38 } else if ( error == AL_ILLEGAL_COMMAND ) {
39 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
40 } else if ( error == AL_OUT_OF_MEMORY ) {
41 SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
43 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
48 int main( int argc, char *argv[] ) {
54 if ( (dev = alcOpenDevice( NULL )) != NULL
55 && ( context = alcCreateContext( dev, NULL )) != NULL ) {
56 alcMakeContextCurrent( context );
59 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
62 // Position of the listener.
63 ALfloat listener_pos[3];
65 // Velocity of the listener.
66 ALfloat listener_vel[3];
68 // Orientation of the listener. (first 3 elements are "at", second
70 ALfloat listener_ori[6];
72 listener_pos[0] = 0.0;
73 listener_pos[1] = 0.0;
74 listener_pos[2] = 0.0;
76 listener_vel[0] = 0.0;
77 listener_vel[1] = 0.0;
78 listener_vel[2] = 0.0;
80 listener_ori[0] = 0.0;
81 listener_ori[1] = 0.0;
82 listener_ori[2] = -1.0;
83 listener_ori[3] = 0.0;
84 listener_ori[4] = 1.0;
85 listener_ori[5] = 0.0;
87 alListenerfv( AL_POSITION, listener_pos );
88 alListenerfv( AL_VELOCITY, listener_vel );
89 alListenerfv( AL_ORIENTATION, listener_ori );
91 // Buffers hold sound data.
94 // Sources are points emitting sound.
97 // Position of the source sound.
98 ALfloat source_pos[3];
100 // Velocity of the source sound.
101 ALfloat source_vel[3];
103 // configuration values
108 ALboolean loop = false;
110 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
111 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
113 // create an OpenAL buffer handle
114 alGenBuffers(1, &buffer);
115 ALuint error = alGetError();
116 if ( error != AL_NO_ERROR ) {
117 print_openal_error( error );
118 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
120 SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
123 // Load the sample file
124 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
126 buffer = alutCreateBufferFromFile(AUDIOFILE);
127 if (buffer == AL_NONE) {
128 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
132 # if defined (__APPLE__)
133 alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq );
135 alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq, &loop );
137 if (alGetError() != AL_NO_ERROR) {
138 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
141 // Copy data to the internal OpenAL buffer
142 alBufferData( buffer, format, data, size, freq );
143 if (alGetError() != AL_NO_ERROR) {
144 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
147 alutUnloadWAV( format, data, size, freq );
150 alGenSources(1, &source);
151 if (alGetError() != AL_NO_ERROR) {
152 print_openal_error( error );
155 alSourcei( source, AL_BUFFER, buffer );
156 alSourcef( source, AL_PITCH, 1.0 );
157 alSourcef( source, AL_GAIN, 1.0 );
158 alSourcefv( source, AL_POSITION, source_pos );
159 alSourcefv( source, AL_VELOCITY, source_vel );
160 alSourcei( source, AL_LOOPING, loop );
162 alSourcePlay( source );