1 #define USE_OPENAL
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <math.h>
6 #ifdef USE_OPENAL
7 #ifdef __APPLE__
8 #include <OpenAL/al.h>
9 #include <OpenAL/alc.h>
10 #else
11 #include <AL/al.h>
12 #include <AL/alc.h>
13 #endif
14 #endif
15 #include "ibm.h"
16 #include "sound.h"
17
18 FILE *allog;
19 #ifdef USE_OPENAL
20 ALuint buffers[4]; // front and back buffers
21 ALuint buffers_cd[4]; // front and back buffers
22 static ALuint source[2]; // audio source
23 #endif
24 #define FREQ 48000
25
26 int SOUNDBUFLEN = 48000/20;
27
28 #define BUFLEN SOUNDBUFLEN
29
30 void closeal();
alutInit(ALint * argc,ALbyte ** argv)31 ALvoid alutInit(ALint *argc,ALbyte **argv)
32 {
33 ALCcontext *Context;
34 ALCdevice *Device;
35
36 //Open device
37 // Device=alcOpenDevice((ALubyte*)"");
38 Device=alcOpenDevice((void *)"");
39 //Create context(s)
40 Context=alcCreateContext(Device,NULL);
41 //Set active context
42 alcMakeContextCurrent(Context);
43 //Register extensions
44 }
45
alutExit(ALvoid)46 ALvoid alutExit(ALvoid)
47 {
48 ALCcontext *Context;
49 ALCdevice *Device;
50
51 //Unregister extensions
52
53 //Get active context
54 Context=alcGetCurrentContext();
55 //Get device for active context
56 Device=alcGetContextsDevice(Context);
57 //Disable context
58 alcMakeContextCurrent(NULL);
59 //Release context(s)
60 alcDestroyContext(Context);
61 //Close device
62 alcCloseDevice(Device);
63 }
initalmain(int argc,char * argv[])64 void initalmain(int argc, char *argv[])
65 {
66 #ifdef USE_OPENAL
67 alutInit(0,0);
68 // printf("AlutInit\n");
69 atexit(closeal);
70 // printf("AlutInit\n");
71 #endif
72 }
73
closeal()74 void closeal()
75 {
76 #ifdef USE_OPENAL
77 alutExit();
78 #endif
79 }
80
check()81 void check()
82 {
83 #ifdef USE_OPENAL
84 ALenum error;
85 if ((error = alGetError()) != AL_NO_ERROR)
86 {
87 // printf("Error : %08X\n", error);
88 // exit(-1);
89 }
90 #endif
91 }
92
inital()93 void inital()
94 {
95 #ifdef USE_OPENAL
96 int c;
97 int16_t buf[BUFLEN*2];
98 int16_t cd_buf[CD_BUFLEN*2];
99
100 // printf("1\n");
101 check();
102
103 // printf("2\n");
104 alGenBuffers(4, buffers);
105 check();
106 alGenBuffers(4, buffers_cd);
107 check();
108
109 // printf("3\n");
110 alGenSources(2, source);
111 check();
112
113 // printf("4\n");
114 alSource3f(source[0], AL_POSITION, 0.0, 0.0, 0.0);
115 alSource3f(source[0], AL_VELOCITY, 0.0, 0.0, 0.0);
116 alSource3f(source[0], AL_DIRECTION, 0.0, 0.0, 0.0);
117 alSourcef (source[0], AL_ROLLOFF_FACTOR, 0.0 );
118 alSourcei (source[0], AL_SOURCE_RELATIVE, AL_TRUE );
119 check();
120 alSource3f(source[1], AL_POSITION, 0.0, 0.0, 0.0);
121 alSource3f(source[1], AL_VELOCITY, 0.0, 0.0, 0.0);
122 alSource3f(source[1], AL_DIRECTION, 0.0, 0.0, 0.0);
123 alSourcef (source[1], AL_ROLLOFF_FACTOR, 0.0 );
124 alSourcei (source[1], AL_SOURCE_RELATIVE, AL_TRUE );
125 check();
126
127 memset(buf,0,BUFLEN*4);
128 memset(cd_buf, 0, CD_BUFLEN*4);
129
130 // printf("5\n");
131 for (c = 0; c < 4; c++)
132 {
133 alBufferData(buffers[c], AL_FORMAT_STEREO16, buf, BUFLEN*2*2, FREQ);
134 alBufferData(buffers_cd[c], AL_FORMAT_STEREO16, cd_buf, CD_BUFLEN*2*2, CD_FREQ);
135 }
136
137 alSourceQueueBuffers(source[0], 4, buffers);
138 check();
139 alSourceQueueBuffers(source[1], 4, buffers_cd);
140 check();
141 // printf("6 %08X\n",source);
142 alSourcePlay(source[0]);
143 check();
144 alSourcePlay(source[1]);
145 check();
146 // printf("InitAL!!! %08X\n",source);
147 #endif
148 }
149
givealbuffer(int32_t * buf)150 void givealbuffer(int32_t *buf)
151 {
152 #ifdef USE_OPENAL
153 int16_t buf16[BUFLEN*2];
154 int processed;
155 int state;
156
157 //return;
158
159 // printf("Start\n");
160 check();
161
162 // printf("GiveALBuffer %08X\n",source);
163
164 alGetSourcei(source[0], AL_SOURCE_STATE, &state);
165
166 check();
167
168 if (state==0x1014)
169 {
170 alSourcePlay(source[0]);
171 // printf("Resetting sound\n");
172 }
173 // printf("State - %i %08X\n",state,state);
174 alGetSourcei(source[0], AL_BUFFERS_PROCESSED, &processed);
175
176 // printf("P ");
177 check();
178 // printf("Processed - %i\n",processed);
179
180 if (processed>=1)
181 {
182 int c;
183 ALuint buffer;
184 double gain = pow(10.0, (double)sound_gain / 20.0);
185
186 alListenerf(AL_GAIN, gain);
187
188 alSourceUnqueueBuffers(source[0], 1, &buffer);
189 // printf("U ");
190 check();
191
192 for (c=0;c<BUFLEN*2;c++)
193 {
194 if (buf[c] < -32768)
195 buf16[c] = -32768;
196 else if (buf[c] > 32767)
197 buf16[c] = 32767;
198 else
199 buf16[c] = buf[c];
200 }
201 // for (c=0;c<BUFLEN*2;c++) buf[c]^=0x8000;
202 alBufferData(buffer, AL_FORMAT_STEREO16, buf16, BUFLEN*2*2, FREQ);
203 // printf("B ");
204 check();
205
206 alSourceQueueBuffers(source[0], 1, &buffer);
207 // printf("Q ");
208 check();
209
210 // printf("\n");
211
212 // if (!allog) allog=fopen("al.pcm","wb");
213 // fwrite(buf,BUFLEN*2,1,allog);
214 }
215 // printf("\n");
216 #endif
217 }
218
givealbuffer_cd(int16_t * buf)219 void givealbuffer_cd(int16_t *buf)
220 {
221 #ifdef USE_OPENAL
222 int processed;
223 int state;
224
225 //return;
226
227 // printf("Start\n");
228 check();
229
230 // printf("GiveALBuffer %08X\n",source);
231
232 alGetSourcei(source[1], AL_SOURCE_STATE, &state);
233
234 check();
235
236 if (state==0x1014)
237 {
238 alSourcePlay(source[1]);
239 // printf("Resetting sound\n");
240 }
241 // printf("State - %i %08X\n",state,state);
242 alGetSourcei(source[1], AL_BUFFERS_PROCESSED, &processed);
243
244 // printf("P ");
245 check();
246 // printf("Processed - %i\n",processed);
247
248 if (processed>=1)
249 {
250 ALuint buffer;
251 double gain = pow(10.0, (double)sound_gain / 20.0);
252
253 alListenerf(AL_GAIN, gain);
254
255 alSourceUnqueueBuffers(source[1], 1, &buffer);
256 // printf("U ");
257 check();
258
259 // for (c=0;c<BUFLEN*2;c++) buf[c]^=0x8000;
260 alBufferData(buffer, AL_FORMAT_STEREO16, buf, CD_BUFLEN*2*2, CD_FREQ);
261 // printf("B ");
262 check();
263
264 alSourceQueueBuffers(source[1], 1, &buffer);
265 // printf("Q ");
266 check();
267
268 // printf("\n");
269
270 // if (!allog) allog=fopen("al.pcm","wb");
271 // fwrite(buf,BUFLEN*2,1,allog);
272 }
273 // printf("\n");
274 #endif
275 }
276