6 #include <simgear/debug/logstream.hxx>
8 static void print_openal_error( ALuint error ) {
9 if ( error == AL_INVALID_NAME ) {
10 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
11 } else if ( error == AL_ILLEGAL_ENUM ) {
12 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
13 } else if ( error == AL_INVALID_VALUE ) {
14 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
15 } else if ( error == AL_ILLEGAL_COMMAND ) {
16 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
17 } else if ( error == AL_OUT_OF_MEMORY ) {
18 SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
20 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
25 int main( int argc, char *argv[] ) {
29 if ( alGetError() != AL_NO_ERROR) {
30 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
33 // Position of the listener.
34 ALfloat listener_pos[3];
36 // Velocity of the listener.
37 ALfloat listener_vel[3];
39 // Orientation of the listener. (first 3 elements are "at", second
41 ALfloat listener_ori[6];
43 listener_pos[0] = 0.0;
44 listener_pos[1] = 0.0;
45 listener_pos[2] = 0.0;
47 listener_vel[0] = 0.0;
48 listener_vel[1] = 0.0;
49 listener_vel[2] = 0.0;
51 listener_ori[0] = 0.0;
52 listener_ori[1] = 0.0;
53 listener_ori[2] = -1.0;
54 listener_ori[3] = 0.0;
55 listener_ori[4] = 1.0;
56 listener_ori[5] = 0.0;
58 alListenerfv( AL_POSITION, listener_pos );
59 alListenerfv( AL_VELOCITY, listener_vel );
60 alListenerfv( AL_ORIENTATION, listener_ori );
62 // Buffers hold sound data.
65 // Sources are points emitting sound.
68 // Position of the source sound.
69 ALfloat source_pos[3];
71 // Velocity of the source sound.
72 ALfloat source_vel[3];
74 // configuration values
81 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
82 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
84 // create an OpenAL buffer handle
85 alGenBuffers(1, &buffer);
86 ALuint error = alGetError();
87 if ( error != AL_NO_ERROR ) {
88 print_openal_error( error );
89 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
91 SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
94 // Load the sample file
95 alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq, &loop );
96 if (alGetError() != AL_NO_ERROR) {
97 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
100 // Copy data to the internal OpenAL buffer
101 alBufferData( buffer, format, data, size, freq );
102 if (alGetError() != AL_NO_ERROR) {
103 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
106 alutUnloadWAV( format, data, size, freq );
108 alGenSources(1, &source);
109 if (alGetError() != AL_NO_ERROR) {
110 print_openal_error( error );
113 alSourcei( source, AL_BUFFER, buffer );
114 alSourcef( source, AL_PITCH, 1.0 );
115 alSourcef( source, AL_GAIN, 1.0 );
116 alSourcefv( source, AL_POSITION, source_pos );
117 alSourcefv( source, AL_VELOCITY, source_vel );
118 alSourcei( source, AL_LOOPING, loop );
120 alSourcePlay( source );