1 #include <simgear_config.h>
2
3 #include <stdio.h>
4 #include <cstdlib> // EXIT_FAILURE
5 #include <cassert>
6
7 #ifdef _WIN32
8 #include <windows.h>
9 #define sleep(x) Sleep(x*1000)
10 #else
11 #include <unistd.h> // sleep()
12 #endif
13
14 #if defined( __APPLE__ )
15 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
16 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
17 # include <OpenAL/al.h>
18 # include <OpenAL/alc.h>
19 #elif defined(OPENALSDK)
20 # include <al.h>
21 # include <alc.h>
22 #else
23 # include <AL/al.h>
24 # include <AL/alc.h>
25 #endif
26
27 #define AUDIOFILE SRC_DIR"/jet.wav"
28
29 #include <simgear/sound/readwav.hxx>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/structure/exception.hxx>
33
print_openal_error(ALuint error)34 static void print_openal_error( ALuint error ) {
35 if ( error == AL_INVALID_NAME ) {
36 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
37 } else if ( error == AL_ILLEGAL_ENUM ) {
38 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
39 } else if ( error == AL_INVALID_VALUE ) {
40 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
41 } else if ( error == AL_ILLEGAL_COMMAND ) {
42 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
43 } else if ( error == AL_OUT_OF_MEMORY ) {
44 SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
45 } else {
46 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
47 }
48 }
49
createBufferFromFile(const SGPath & path)50 ALuint createBufferFromFile(const SGPath& path)
51 {
52 ALuint buffer = -1;
53 #ifdef ENABLE_SOUND
54 unsigned int format;
55 unsigned int block_align;
56 ALsizei size;
57 ALfloat sampleFrequency;
58 ALvoid* data = simgear::loadWAVFromFile(path, format, size, sampleFrequency, block_align);
59 assert(data);
60
61 alGenBuffers(1, &buffer);
62 if (alGetError() != AL_NO_ERROR) {
63 free(data);
64 throw sg_io_exception("OpenAL buffer allocation failed", sg_location(path.str()));
65 }
66
67 alBufferData (buffer, format, data, size, (ALsizei) sampleFrequency);
68 if (alGetError() != AL_NO_ERROR) {
69 alDeleteBuffers(1, &buffer);
70 free(data);
71 throw sg_io_exception("OpenAL setting buffer data failed", sg_location(path.str()));
72 }
73 #endif
74 return buffer;
75 }
76
77
main(int argc,char * argv[])78 int main( int argc, char *argv[] )
79 {
80 sglog().setLogLevels( SG_ALL, SG_ALERT );
81
82 // initialize OpenAL
83 ALCdevice *dev = alcOpenDevice(nullptr);
84 if (!dev) {
85 SG_LOG( SG_GENERAL, SG_ALERT, "Audio device initialization failed!" );
86 return EXIT_FAILURE;
87 }
88
89 ALCcontext *context = alcCreateContext(dev, nullptr);
90 if (!context) {
91 SG_LOG( SG_GENERAL, SG_ALERT, "Audio context initialization failed!" );
92 return EXIT_FAILURE;
93 }
94
95 alcMakeContextCurrent( context );
96
97 // Position of the listener.
98 ALfloat listener_pos[3];
99
100 // Velocity of the listener.
101 ALfloat listener_vel[3];
102
103 // Orientation of the listener. (first 3 elements are "at", second
104 // 3 are "up")
105 ALfloat listener_ori[6];
106
107 listener_pos[0] = 0.0;
108 listener_pos[1] = 0.0;
109 listener_pos[2] = 0.0;
110
111 listener_vel[0] = 0.0;
112 listener_vel[1] = 0.0;
113 listener_vel[2] = 0.0;
114
115 listener_ori[0] = 0.0;
116 listener_ori[1] = 0.0;
117 listener_ori[2] = -1.0;
118 listener_ori[3] = 0.0;
119 listener_ori[4] = 1.0;
120 listener_ori[5] = 0.0;
121
122 alListenerfv( AL_POSITION, listener_pos );
123 alListenerfv( AL_VELOCITY, listener_vel );
124 alListenerfv( AL_ORIENTATION, listener_ori );
125
126 // Buffers hold sound data.
127 ALuint buffer;
128
129 // Sources are points emitting sound.
130 ALuint source;
131
132 // Position of the source sound.
133 ALfloat source_pos[3];
134
135 // Velocity of the source sound.
136 ALfloat source_vel[3];
137
138 // configuration values
139 ALboolean loop = false;
140
141 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
142 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
143
144 // Load the sample file
145 buffer = createBufferFromFile(SGPath(AUDIOFILE));
146 if (buffer == AL_NONE) {
147 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
148 }
149
150 alGenSources(1, &source);
151 if (alGetError() != AL_NO_ERROR) {
152 ALuint error = alGetError();
153 print_openal_error( error );
154 }
155
156 alSourcei( source, AL_BUFFER, buffer );
157 alSourcef( source, AL_PITCH, 1.0 );
158 alSourcef( source, AL_GAIN, 1.0 );
159 alSourcefv( source, AL_POSITION, source_pos );
160 alSourcefv( source, AL_VELOCITY, source_vel );
161 alSourcei( source, AL_LOOPING, loop );
162
163 alSourcePlay( source );
164
165 sleep(10);
166
167 alcMakeContextCurrent(nullptr);
168 alcDestroyContext(context);
169 alcCloseDevice(dev);
170
171 return 0;
172 }
173