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