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