]> git.mxchange.org Git - simgear.git/blob - simgear/sound/openal_test1.cxx
cppbind.Ghost: clean up a bit
[simgear.git] / simgear / sound / openal_test1.cxx
1 #include <stdio.h>
2 #include <cstdlib> // EXIT_FAILURE
3
4 #ifdef _WIN32
5 #include <windows.h>
6 #define sleep(x) Sleep(x*1000)
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 #elif defined(OPENALSDK)
17 # include <al.h>
18 # include <alc.h>
19 #else
20 # include <AL/al.h>
21 # include <AL/alc.h>
22 #endif
23
24 #define AUDIOFILE       SRC_DIR"/jet.wav"
25
26 #include <simgear/sound/readwav.hxx>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sg_path.hxx>
29
30 static void print_openal_error( ALuint error ) {
31     if ( error == AL_INVALID_NAME ) {
32         SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
33     } else if ( error == AL_ILLEGAL_ENUM ) {
34         SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
35     } else if ( error == AL_INVALID_VALUE ) {
36         SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
37     } else if ( error == AL_ILLEGAL_COMMAND ) {
38         SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
39     } else if ( error == AL_OUT_OF_MEMORY ) {
40         SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
41     } else {
42         SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
43     }
44 }
45
46
47 int main( int argc, char *argv[] ) 
48 {
49     sglog().setLogLevels( SG_ALL, SG_ALERT );
50     
51     // initialize OpenAL
52     ALCdevice *dev = alcOpenDevice(NULL); 
53     if (!dev) {
54       SG_LOG( SG_GENERAL, SG_ALERT, "Audio device initialization failed!" );
55       return EXIT_FAILURE;
56     }
57     
58     ALCcontext *context = alcCreateContext(dev, NULL);
59     if (!context) {
60       SG_LOG( SG_GENERAL, SG_ALERT, "Audio context initialization failed!" );
61       return EXIT_FAILURE;
62     }
63     
64     alcMakeContextCurrent( context );
65
66     // Position of the listener.
67     ALfloat listener_pos[3];
68
69     // Velocity of the listener.
70     ALfloat listener_vel[3];
71
72     // Orientation of the listener. (first 3 elements are "at", second
73     // 3 are "up")
74     ALfloat listener_ori[6];
75
76     listener_pos[0] = 0.0;
77     listener_pos[1] = 0.0;
78     listener_pos[2] = 0.0;
79
80     listener_vel[0] = 0.0;
81     listener_vel[1] = 0.0;
82     listener_vel[2] = 0.0;
83     
84     listener_ori[0] = 0.0;
85     listener_ori[1] = 0.0;
86     listener_ori[2] = -1.0;
87     listener_ori[3] = 0.0;
88     listener_ori[4] = 1.0;
89     listener_ori[5] = 0.0;
90
91     alListenerfv( AL_POSITION, listener_pos );
92     alListenerfv( AL_VELOCITY, listener_vel );
93     alListenerfv( AL_ORIENTATION, listener_ori );
94
95     // Buffers hold sound data.
96     ALuint buffer;
97
98     // Sources are points emitting sound.
99     ALuint source;
100
101     // Position of the source sound.
102     ALfloat source_pos[3];
103
104     // Velocity of the source sound.
105     ALfloat source_vel[3];
106
107     // configuration values
108     ALboolean loop = false;
109
110     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
111     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
112
113     // Load the sample file
114       buffer = simgear::createBufferFromFile(SGPath(AUDIOFILE));
115       if (buffer == AL_NONE) {
116         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
117       }
118
119     alGenSources(1, &source);
120     if (alGetError() != AL_NO_ERROR) {
121         ALuint error = alGetError();
122         print_openal_error( error );
123     }
124
125     alSourcei( source, AL_BUFFER, buffer );
126     alSourcef( source, AL_PITCH, 1.0 );
127     alSourcef( source, AL_GAIN, 1.0 );
128     alSourcefv( source, AL_POSITION, source_pos );
129     alSourcefv( source, AL_VELOCITY, source_vel );
130     alSourcei( source, AL_LOOPING, loop );
131
132     alSourcePlay( source );
133
134     sleep(10);
135     
136      alcMakeContextCurrent(NULL);
137      alcDestroyContext(context);
138      alcCloseDevice(dev);
139      
140     return 0;
141 }