3 #if defined( __APPLE__ )
4 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
5 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
6 # include <OpenAL/al.h>
7 # include <OpenAL/alut.h>
13 #include <simgear/debug/logstream.hxx>
15 static void print_openal_error( ALuint error ) {
16 if ( error == AL_INVALID_NAME ) {
17 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
18 } else if ( error == AL_ILLEGAL_ENUM ) {
19 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
20 } else if ( error == AL_INVALID_VALUE ) {
21 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
22 } else if ( error == AL_ILLEGAL_COMMAND ) {
23 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
24 } else if ( error == AL_OUT_OF_MEMORY ) {
25 SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
27 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
32 int main( int argc, char *argv[] ) {
36 if ( alGetError() != AL_NO_ERROR) {
37 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
40 // Position of the listener.
41 ALfloat listener_pos[3];
43 // Velocity of the listener.
44 ALfloat listener_vel[3];
46 // Orientation of the listener. (first 3 elements are "at", second
48 ALfloat listener_ori[6];
50 listener_pos[0] = 0.0;
51 listener_pos[1] = 0.0;
52 listener_pos[2] = 0.0;
54 listener_vel[0] = 0.0;
55 listener_vel[1] = 0.0;
56 listener_vel[2] = 0.0;
58 listener_ori[0] = 0.0;
59 listener_ori[1] = 0.0;
60 listener_ori[2] = -1.0;
61 listener_ori[3] = 0.0;
62 listener_ori[4] = 1.0;
63 listener_ori[5] = 0.0;
65 alListenerfv( AL_POSITION, listener_pos );
66 alListenerfv( AL_VELOCITY, listener_vel );
67 alListenerfv( AL_ORIENTATION, listener_ori );
69 // Buffers hold sound data.
72 // Sources are points emitting sound.
75 // Position of the source sound.
76 ALfloat source_pos[3];
78 // Velocity of the source sound.
79 ALfloat source_vel[3];
81 // configuration values
88 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
89 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
91 // create an OpenAL buffer handle
92 alGenBuffers(1, &buffer);
93 ALuint error = alGetError();
94 if ( error != AL_NO_ERROR ) {
95 print_openal_error( error );
96 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
98 SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
101 // Load the sample file
102 #if defined (__APPLE__)
103 alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq );
105 alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq, &loop );
107 if (alGetError() != AL_NO_ERROR) {
108 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
111 // Copy data to the internal OpenAL buffer
112 alBufferData( buffer, format, data, size, freq );
113 if (alGetError() != AL_NO_ERROR) {
114 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
117 alutUnloadWAV( format, data, size, freq );
119 alGenSources(1, &source);
120 if (alGetError() != AL_NO_ERROR) {
121 print_openal_error( error );
124 alSourcei( source, AL_BUFFER, buffer );
125 alSourcef( source, AL_PITCH, 1.0 );
126 alSourcef( source, AL_GAIN, 1.0 );
127 alSourcefv( source, AL_POSITION, source_pos );
128 alSourcefv( source, AL_VELOCITY, source_vel );
129 alSourcei( source, AL_LOOPING, loop );
131 alSourcePlay( source );