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/alut.h>
21 #include <simgear/debug/logstream.hxx>
23 static void print_openal_error( ALuint error ) {
24 if ( error == AL_INVALID_NAME ) {
25 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
26 } else if ( error == AL_ILLEGAL_ENUM ) {
27 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
28 } else if ( error == AL_INVALID_VALUE ) {
29 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
30 } else if ( error == AL_ILLEGAL_COMMAND ) {
31 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
32 } else if ( error == AL_OUT_OF_MEMORY ) {
33 SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
35 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
40 int main( int argc, char *argv[] ) {
44 if ( alGetError() != AL_NO_ERROR) {
45 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
48 // Position of the listener.
49 ALfloat listener_pos[3];
51 // Velocity of the listener.
52 ALfloat listener_vel[3];
54 // Orientation of the listener. (first 3 elements are "at", second
56 ALfloat listener_ori[6];
58 listener_pos[0] = 0.0;
59 listener_pos[1] = 0.0;
60 listener_pos[2] = 0.0;
62 listener_vel[0] = 0.0;
63 listener_vel[1] = 0.0;
64 listener_vel[2] = 0.0;
66 listener_ori[0] = 0.0;
67 listener_ori[1] = 0.0;
68 listener_ori[2] = -1.0;
69 listener_ori[3] = 0.0;
70 listener_ori[4] = 1.0;
71 listener_ori[5] = 0.0;
73 alListenerfv( AL_POSITION, listener_pos );
74 alListenerfv( AL_VELOCITY, listener_vel );
75 alListenerfv( AL_ORIENTATION, listener_ori );
77 // Buffers hold sound data.
80 // Sources are points emitting sound.
83 // Position of the source sound.
84 ALfloat source_pos[3];
86 // Velocity of the source sound.
87 ALfloat source_vel[3];
89 // configuration values
96 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
97 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
99 // create an OpenAL buffer handle
100 alGenBuffers(1, &buffer);
101 ALuint error = alGetError();
102 if ( error != AL_NO_ERROR ) {
103 print_openal_error( error );
104 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
106 SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
109 // Load the sample file
110 #if defined (__APPLE__)
111 alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq );
113 alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq, &loop );
115 if (alGetError() != AL_NO_ERROR) {
116 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
119 // Copy data to the internal OpenAL buffer
120 alBufferData( buffer, format, data, size, freq );
121 if (alGetError() != AL_NO_ERROR) {
122 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
125 alutUnloadWAV( format, data, size, freq );
127 alGenSources(1, &source);
128 if (alGetError() != AL_NO_ERROR) {
129 print_openal_error( error );
132 alSourcei( source, AL_BUFFER, buffer );
133 alSourcef( source, AL_PITCH, 1.0 );
134 alSourcef( source, AL_GAIN, 1.0 );
135 alSourcefv( source, AL_POSITION, source_pos );
136 alSourcefv( source, AL_VELOCITY, source_vel );
137 alSourcei( source, AL_LOOPING, loop );
139 alSourcePlay( source );