2 #include <unistd.h> // sleep()
4 #if defined( __APPLE__ )
5 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
6 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
7 # include <OpenAL/al.h>
8 # include <OpenAL/alut.h>
14 #include <simgear/debug/logstream.hxx>
16 static void print_openal_error( ALuint error ) {
17 if ( error == AL_INVALID_NAME ) {
18 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
19 } else if ( error == AL_ILLEGAL_ENUM ) {
20 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
21 } else if ( error == AL_INVALID_VALUE ) {
22 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
23 } else if ( error == AL_ILLEGAL_COMMAND ) {
24 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
25 } else if ( error == AL_OUT_OF_MEMORY ) {
26 SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
28 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
33 int main( int argc, char *argv[] ) {
37 if ( alGetError() != AL_NO_ERROR) {
38 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
41 // Position of the listener.
42 ALfloat listener_pos[3];
44 // Velocity of the listener.
45 ALfloat listener_vel[3];
47 // Orientation of the listener. (first 3 elements are "at", second
49 ALfloat listener_ori[6];
51 listener_pos[0] = 0.0;
52 listener_pos[1] = 0.0;
53 listener_pos[2] = 0.0;
55 listener_vel[0] = 0.0;
56 listener_vel[1] = 0.0;
57 listener_vel[2] = 0.0;
59 listener_ori[0] = 0.0;
60 listener_ori[1] = 0.0;
61 listener_ori[2] = -1.0;
62 listener_ori[3] = 0.0;
63 listener_ori[4] = 1.0;
64 listener_ori[5] = 0.0;
66 alListenerfv( AL_POSITION, listener_pos );
67 alListenerfv( AL_VELOCITY, listener_vel );
68 alListenerfv( AL_ORIENTATION, listener_ori );
70 // Buffers hold sound data.
73 // Sources are points emitting sound.
76 // Position of the source sound.
77 ALfloat source_pos[3];
79 // Velocity of the source sound.
80 ALfloat source_vel[3];
82 // configuration values
89 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
90 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
92 // create an OpenAL buffer handle
93 alGenBuffers(1, &buffer);
94 ALuint error = alGetError();
95 if ( error != AL_NO_ERROR ) {
96 print_openal_error( error );
97 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
99 SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
102 // Load the sample file
103 #if defined (__APPLE__)
104 alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq );
106 alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq, &loop );
108 if (alGetError() != AL_NO_ERROR) {
109 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
112 // Copy data to the internal OpenAL buffer
113 alBufferData( buffer, format, data, size, freq );
114 if (alGetError() != AL_NO_ERROR) {
115 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
118 alutUnloadWAV( format, data, size, freq );
120 alGenSources(1, &source);
121 if (alGetError() != AL_NO_ERROR) {
122 print_openal_error( error );
125 alSourcei( source, AL_BUFFER, buffer );
126 alSourcef( source, AL_PITCH, 1.0 );
127 alSourcef( source, AL_GAIN, 1.0 );
128 alSourcefv( source, AL_POSITION, source_pos );
129 alSourcefv( source, AL_VELOCITY, source_vel );
130 alSourcei( source, AL_LOOPING, loop );
132 alSourcePlay( source );