]> git.mxchange.org Git - simgear.git/blob - simgear/sound/openal_test1.cxx
Remove ALUT usage from SimGear .
[simgear.git] / simgear / sound / openal_test1.cxx
1 #include <stdio.h>
2
3 #ifdef _WIN32
4 #include <windows.h>
5 #define sleep(x) Sleep(x*1000)
6 #else
7 #include <unistd.h>     // sleep()
8 #endif
9
10 #if defined( __APPLE__ )
11 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
12 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
13 # include <OpenAL/al.h>
14 # include <OpenAL/alc.h>
15 #elif defined(OPENALSDK)
16 # include <al.h>
17 # include <alc.h>
18 #else
19 # include <AL/al.h>
20 # include <AL/alc.h>
21 #endif
22
23 #define AUDIOFILE       SRC_DIR"/jet.wav"
24
25 #include <simgear/sound/readwav.hxx>
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/misc/sg_path.hxx>
28
29 static void print_openal_error( ALuint error ) {
30     if ( error == AL_INVALID_NAME ) {
31         SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
32     } else if ( error == AL_ILLEGAL_ENUM ) {
33         SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
34     } else if ( error == AL_INVALID_VALUE ) {
35         SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
36     } else if ( error == AL_ILLEGAL_COMMAND ) {
37         SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
38     } else if ( error == AL_OUT_OF_MEMORY ) {
39         SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
40     } else {
41         SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
42     }
43 }
44
45
46 int main( int argc, char *argv[] ) 
47 {
48     sglog().setLogLevels( SG_ALL, SG_ALERT );
49     
50     // initialize OpenAL
51     ALCdevice *dev = alcOpenDevice(NULL); 
52     if (!dev) {
53       SG_LOG( SG_GENERAL, SG_ALERT, "Audio device initialization failed!" );
54       return EXIT_FAILURE;
55     }
56     
57     ALCcontext *context = alcCreateContext(dev, NULL);
58     if (!context) {
59       SG_LOG( SG_GENERAL, SG_ALERT, "Audio context initialization failed!" );
60       return EXIT_FAILURE;
61     }
62     
63     alcMakeContextCurrent( context );
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     ALboolean loop = false;
108
109     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
110     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
111
112     // Load the sample file
113       buffer = simgear::createBufferFromFile(SGPath(AUDIOFILE));
114       if (buffer == AL_NONE) {
115         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
116       }
117
118     alGenSources(1, &source);
119     if (alGetError() != AL_NO_ERROR) {
120         ALuint error = alGetError();
121         print_openal_error( error );
122     }
123
124     alSourcei( source, AL_BUFFER, buffer );
125     alSourcef( source, AL_PITCH, 1.0 );
126     alSourcef( source, AL_GAIN, 1.0 );
127     alSourcefv( source, AL_POSITION, source_pos );
128     alSourcefv( source, AL_VELOCITY, source_vel );
129     alSourcei( source, AL_LOOPING, loop );
130
131     alSourcePlay( source );
132
133     sleep(10);
134     
135      alcMakeContextCurrent(NULL);
136      alcDestroyContext(context);
137      alcCloseDevice(dev);
138      
139     return 0;
140 }