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