]> git.mxchange.org Git - simgear.git/blob - simgear/sound/openal_test1.cxx
dd2a77f01620e813c6ba6b1026bf4518719557e9
[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/alc.h>
16 # include <OpenAL/alut.h>
17 #elif defined(OPENALSDK)
18 # include <al.h>
19 # include <alc.h>
20 # include <AL/alut.h> 
21 #else
22 # include <AL/al.h>
23 # include <AL/alc.h>
24 # include <AL/alut.h>
25 #endif
26
27 #define AUDIOFILE       SRC_DIR"/jet.wav"
28
29 #include <simgear/debug/logstream.hxx>
30
31 static void print_openal_error( ALuint error ) {
32     if ( error == AL_INVALID_NAME ) {
33         SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
34     } else if ( error == AL_ILLEGAL_ENUM ) {
35         SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
36     } else if ( error == AL_INVALID_VALUE ) {
37         SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
38     } else if ( error == AL_ILLEGAL_COMMAND ) {
39         SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
40     } else if ( error == AL_OUT_OF_MEMORY ) {
41         SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
42     } else {
43         SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
44     }
45 }
46
47
48 int main( int argc, char *argv[] ) {
49     // initialize OpenAL
50     ALCdevice *dev;
51     ALCcontext *context;
52
53     sglog().setLogLevels( SG_ALL, SG_ALERT );
54
55     // initialize OpenAL
56     if ( (dev = alcOpenDevice( NULL )) != NULL
57             && ( context = alcCreateContext( dev, NULL )) != NULL ) {
58         alcMakeContextCurrent( context );
59     } else {
60         context = 0;
61         SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
62     }
63
64     // Position of the listener.
65     ALfloat listener_pos[3];
66
67     // Velocity of the listener.
68     ALfloat listener_vel[3];
69
70     // Orientation of the listener. (first 3 elements are "at", second
71     // 3 are "up")
72     ALfloat listener_ori[6];
73
74     listener_pos[0] = 0.0;
75     listener_pos[1] = 0.0;
76     listener_pos[2] = 0.0;
77
78     listener_vel[0] = 0.0;
79     listener_vel[1] = 0.0;
80     listener_vel[2] = 0.0;
81     
82     listener_ori[0] = 0.0;
83     listener_ori[1] = 0.0;
84     listener_ori[2] = -1.0;
85     listener_ori[3] = 0.0;
86     listener_ori[4] = 1.0;
87     listener_ori[5] = 0.0;
88
89     alListenerfv( AL_POSITION, listener_pos );
90     alListenerfv( AL_VELOCITY, listener_vel );
91     alListenerfv( AL_ORIENTATION, listener_ori );
92
93     // Buffers hold sound data.
94     ALuint buffer;
95
96     // Sources are points emitting sound.
97     ALuint source;
98
99     // Position of the source sound.
100     ALfloat source_pos[3];
101
102     // Velocity of the source sound.
103     ALfloat source_vel[3];
104
105     // configuration values
106 //    ALenum format;
107 //    ALsizei size;
108 //    ALvoid* data;
109 //    ALsizei freq;
110     ALboolean loop = false;
111
112     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
113     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
114
115     // create an OpenAL buffer handle
116     alGenBuffers(1, &buffer);
117     ALuint error = alGetError();
118     if ( error != AL_NO_ERROR ) {
119         print_openal_error( error );
120         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
121     } else {
122         SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
123     }
124
125     // Load the sample file
126 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
127
128   buffer = alutCreateBufferFromFile(AUDIOFILE);
129   if (buffer == AL_NONE) {
130     SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
131   }
132
133 #else
134 # if defined (__APPLE__)
135     alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq );
136 # else
137     alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq, &loop );
138 # endif
139     if (alGetError() != AL_NO_ERROR) {
140         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
141     }
142
143     // Copy data to the internal OpenAL buffer
144     alBufferData( buffer, format, data, size, freq );
145     if (alGetError() != AL_NO_ERROR) {
146         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
147     }
148
149     alutUnloadWAV( format, data, size, freq );
150 #endif
151
152     alGenSources(1, &source);
153     if (alGetError() != AL_NO_ERROR) {
154         print_openal_error( error );
155     }
156
157     alSourcei( source, AL_BUFFER, buffer );
158     alSourcef( source, AL_PITCH, 1.0 );
159     alSourcef( source, AL_GAIN, 1.0 );
160     alSourcefv( source, AL_POSITION, source_pos );
161     alSourcefv( source, AL_VELOCITY, source_vel );
162     alSourcei( source, AL_LOOPING, loop );
163
164     alSourcePlay( source );
165
166     sleep(10);
167
168     return 0;
169 }