]> git.mxchange.org Git - simgear.git/blob - simgear/sound/openal_test1.cxx
Make sure the prototype definition of sleep() is found (at least for IRIX).
[simgear.git] / simgear / sound / openal_test1.cxx
1 #include <stdio.h>
2 #include <unistd.h>     // sleep()
3
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>
9 #else
10 # include <AL/al.h>
11 # include <AL/alut.h>
12 #endif
13
14 #include <simgear/debug/logstream.hxx>
15
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" );
27     } else {
28         SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
29     }
30 }
31
32
33 int main( int argc, char *argv[] ) {
34     // initialize OpenAL
35     alutInit( 0, NULL );
36     alGetError();
37     if ( alGetError() != AL_NO_ERROR) {
38         SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
39     }
40
41     // Position of the listener.
42     ALfloat listener_pos[3];
43
44     // Velocity of the listener.
45     ALfloat listener_vel[3];
46
47     // Orientation of the listener. (first 3 elements are "at", second
48     // 3 are "up")
49     ALfloat listener_ori[6];
50
51     listener_pos[0] = 0.0;
52     listener_pos[1] = 0.0;
53     listener_pos[2] = 0.0;
54
55     listener_vel[0] = 0.0;
56     listener_vel[1] = 0.0;
57     listener_vel[2] = 0.0;
58     
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;
65
66     alListenerfv( AL_POSITION, listener_pos );
67     alListenerfv( AL_VELOCITY, listener_vel );
68     alListenerfv( AL_ORIENTATION, listener_ori );
69
70     // Buffers hold sound data.
71     ALuint buffer;
72
73     // Sources are points emitting sound.
74     ALuint source;
75
76     // Position of the source sound.
77     ALfloat source_pos[3];
78
79     // Velocity of the source sound.
80     ALfloat source_vel[3];
81
82     // configuration values
83     ALenum format;
84     ALsizei size;
85     ALvoid* data;
86     ALsizei freq;
87     ALboolean loop;
88
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;
91
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." );
98     } else {
99         SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
100     }
101
102     // Load the sample file
103 #if defined (__APPLE__)
104     alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq );
105 #else
106     alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq, &loop );
107 #endif
108     if (alGetError() != AL_NO_ERROR) {
109         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
110     }
111
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.");
116     }
117
118     alutUnloadWAV( format, data, size, freq );
119
120     alGenSources(1, &source);
121     if (alGetError() != AL_NO_ERROR) {
122         print_openal_error( error );
123     }
124
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 );
131
132     alSourcePlay( source );
133
134     sleep(10);
135
136     return 0;
137 }