]> git.mxchange.org Git - simgear.git/blob - simgear/sound/openal_test1.cxx
fix a memory leak and add some temporary debugging statements.
[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     alutInit(&argc, argv);
54     sglog().setLogLevels( SG_ALL, SG_ALERT );
55
56     // initialize OpenAL
57     if ( (dev = alcOpenDevice( NULL )) != NULL
58             && ( context = alcCreateContext( dev, NULL )) != NULL ) {
59         alcMakeContextCurrent( context );
60     } else {
61         context = 0;
62         SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
63     }
64
65     // Position of the listener.
66     ALfloat listener_pos[3];
67
68     // Velocity of the listener.
69     ALfloat listener_vel[3];
70
71     // Orientation of the listener. (first 3 elements are "at", second
72     // 3 are "up")
73     ALfloat listener_ori[6];
74
75     listener_pos[0] = 0.0;
76     listener_pos[1] = 0.0;
77     listener_pos[2] = 0.0;
78
79     listener_vel[0] = 0.0;
80     listener_vel[1] = 0.0;
81     listener_vel[2] = 0.0;
82     
83     listener_ori[0] = 0.0;
84     listener_ori[1] = 0.0;
85     listener_ori[2] = -1.0;
86     listener_ori[3] = 0.0;
87     listener_ori[4] = 1.0;
88     listener_ori[5] = 0.0;
89
90     alListenerfv( AL_POSITION, listener_pos );
91     alListenerfv( AL_VELOCITY, listener_vel );
92     alListenerfv( AL_ORIENTATION, listener_ori );
93
94     // Buffers hold sound data.
95     ALuint buffer;
96
97     // Sources are points emitting sound.
98     ALuint source;
99
100     // Position of the source sound.
101     ALfloat source_pos[3];
102
103     // Velocity of the source sound.
104     ALfloat source_vel[3];
105
106     // configuration values
107 //    ALenum format;
108 //    ALsizei size;
109 //    ALvoid* data;
110 //    ALsizei freq;
111     ALboolean loop = false;
112
113     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
114     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
115
116     // create an OpenAL buffer handle
117     alGenBuffers(1, &buffer);
118     ALuint error = alGetError();
119     if ( error != AL_NO_ERROR ) {
120         print_openal_error( error );
121         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
122     } else {
123         SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
124     }
125
126     // Load the sample file
127 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
128
129   buffer = alutCreateBufferFromFile(AUDIOFILE);
130   if (buffer == AL_NONE) {
131     SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
132   }
133
134 #else
135 # if defined (__APPLE__)
136     alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq );
137 # else
138     alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq, &loop );
139 # endif
140     if (alGetError() != AL_NO_ERROR) {
141         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
142     }
143
144     // Copy data to the internal OpenAL buffer
145     alBufferData( buffer, format, data, size, freq );
146     if (alGetError() != AL_NO_ERROR) {
147         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
148     }
149
150     alutUnloadWAV( format, data, size, freq );
151 #endif
152
153     alGenSources(1, &source);
154     if (alGetError() != AL_NO_ERROR) {
155         print_openal_error( error );
156     }
157
158     alSourcei( source, AL_BUFFER, buffer );
159     alSourcef( source, AL_PITCH, 1.0 );
160     alSourcef( source, AL_GAIN, 1.0 );
161     alSourcefv( source, AL_POSITION, source_pos );
162     alSourcefv( source, AL_VELOCITY, source_vel );
163     alSourcei( source, AL_LOOPING, loop );
164
165     alSourcePlay( source );
166
167     sleep(10);
168     alutExit();
169
170     return 0;
171 }