]> git.mxchange.org Git - simgear.git/blob - simgear/sound/openal_test1.cxx
Add support for the MacOS variations of OpenAL.
[simgear.git] / simgear / sound / openal_test1.cxx
1 #include <stdio.h>
2
3 #if defined( __APPLE__ )
4 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
5 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
6 # include <OpenAL/al.h>
7 # include <OpenAL/alut.h>
8 #else
9 # include <AL/al.h>
10 # include <AL/alut.h>
11 #endif
12
13 #include <simgear/debug/logstream.hxx>
14
15 static void print_openal_error( ALuint error ) {
16     if ( error == AL_INVALID_NAME ) {
17         SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
18     } else if ( error == AL_ILLEGAL_ENUM ) {
19         SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
20     } else if ( error == AL_INVALID_VALUE ) {
21         SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
22     } else if ( error == AL_ILLEGAL_COMMAND ) {
23         SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
24     } else if ( error == AL_OUT_OF_MEMORY ) {
25         SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
26     } else {
27         SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
28     }
29 }
30
31
32 int main( int argc, char *argv[] ) {
33     // initialize OpenAL
34     alutInit( 0, NULL );
35     alGetError();
36     if ( alGetError() != AL_NO_ERROR) {
37         SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
38     }
39
40     // Position of the listener.
41     ALfloat listener_pos[3];
42
43     // Velocity of the listener.
44     ALfloat listener_vel[3];
45
46     // Orientation of the listener. (first 3 elements are "at", second
47     // 3 are "up")
48     ALfloat listener_ori[6];
49
50     listener_pos[0] = 0.0;
51     listener_pos[1] = 0.0;
52     listener_pos[2] = 0.0;
53
54     listener_vel[0] = 0.0;
55     listener_vel[1] = 0.0;
56     listener_vel[2] = 0.0;
57     
58     listener_ori[0] = 0.0;
59     listener_ori[1] = 0.0;
60     listener_ori[2] = -1.0;
61     listener_ori[3] = 0.0;
62     listener_ori[4] = 1.0;
63     listener_ori[5] = 0.0;
64
65     alListenerfv( AL_POSITION, listener_pos );
66     alListenerfv( AL_VELOCITY, listener_vel );
67     alListenerfv( AL_ORIENTATION, listener_ori );
68
69     // Buffers hold sound data.
70     ALuint buffer;
71
72     // Sources are points emitting sound.
73     ALuint source;
74
75     // Position of the source sound.
76     ALfloat source_pos[3];
77
78     // Velocity of the source sound.
79     ALfloat source_vel[3];
80
81     // configuration values
82     ALenum format;
83     ALsizei size;
84     ALvoid* data;
85     ALsizei freq;
86     ALboolean loop;
87
88     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
89     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
90
91     // create an OpenAL buffer handle
92     alGenBuffers(1, &buffer);
93     ALuint error = alGetError();
94     if ( error != AL_NO_ERROR ) {
95         print_openal_error( error );
96         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
97     } else {
98         SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
99     }
100
101     // Load the sample file
102     alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq, &loop );
103     if (alGetError() != AL_NO_ERROR) {
104         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
105     }
106
107     // Copy data to the internal OpenAL buffer
108     alBufferData( buffer, format, data, size, freq );
109     if (alGetError() != AL_NO_ERROR) {
110         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
111     }
112
113     alutUnloadWAV( format, data, size, freq );
114
115     alGenSources(1, &source);
116     if (alGetError() != AL_NO_ERROR) {
117         print_openal_error( error );
118     }
119
120     alSourcei( source, AL_BUFFER, buffer );
121     alSourcef( source, AL_PITCH, 1.0 );
122     alSourcef( source, AL_GAIN, 1.0 );
123     alSourcefv( source, AL_POSITION, source_pos );
124     alSourcefv( source, AL_VELOCITY, source_vel );
125     alSourcei( source, AL_LOOPING, loop );
126
127     alSourcePlay( source );
128
129     sleep(10);
130
131     return 0;
132 }