]> git.mxchange.org Git - simgear.git/blob - simgear/sound/openal_test1.cxx
Changes to get SimGear to configure and compile out-of-the-box on
[simgear.git] / simgear / sound / openal_test1.cxx
1 #include <stdio.h>
2
3 #ifdef __MINGW32__
4 // This is broken, but allows the file to compile without a POSIX
5 // environment.
6 static unsigned int sleep(unsigned int secs) { return 0; }
7 #else
8 #include <unistd.h>     // sleep()
9 #endif
10
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>
16 #else
17 # include <AL/al.h>
18 # include <AL/alut.h>
19 #endif
20
21 #include <simgear/debug/logstream.hxx>
22
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" );
34     } else {
35         SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
36     }
37 }
38
39
40 int main( int argc, char *argv[] ) {
41     // initialize OpenAL
42     alutInit( 0, NULL );
43     alGetError();
44     if ( alGetError() != AL_NO_ERROR) {
45         SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
46     }
47
48     // Position of the listener.
49     ALfloat listener_pos[3];
50
51     // Velocity of the listener.
52     ALfloat listener_vel[3];
53
54     // Orientation of the listener. (first 3 elements are "at", second
55     // 3 are "up")
56     ALfloat listener_ori[6];
57
58     listener_pos[0] = 0.0;
59     listener_pos[1] = 0.0;
60     listener_pos[2] = 0.0;
61
62     listener_vel[0] = 0.0;
63     listener_vel[1] = 0.0;
64     listener_vel[2] = 0.0;
65     
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;
72
73     alListenerfv( AL_POSITION, listener_pos );
74     alListenerfv( AL_VELOCITY, listener_vel );
75     alListenerfv( AL_ORIENTATION, listener_ori );
76
77     // Buffers hold sound data.
78     ALuint buffer;
79
80     // Sources are points emitting sound.
81     ALuint source;
82
83     // Position of the source sound.
84     ALfloat source_pos[3];
85
86     // Velocity of the source sound.
87     ALfloat source_vel[3];
88
89     // configuration values
90     ALenum format;
91     ALsizei size;
92     ALvoid* data;
93     ALsizei freq;
94     ALboolean loop;
95
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;
98
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." );
105     } else {
106         SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
107     }
108
109     // Load the sample file
110 #if defined (__APPLE__)
111     alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq );
112 #else
113     alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq, &loop );
114 #endif
115     if (alGetError() != AL_NO_ERROR) {
116         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
117     }
118
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.");
123     }
124
125     alutUnloadWAV( format, data, size, freq );
126
127     alGenSources(1, &source);
128     if (alGetError() != AL_NO_ERROR) {
129         print_openal_error( error );
130     }
131
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 );
138
139     alSourcePlay( source );
140
141     sleep(10);
142
143     return 0;
144 }