Debian Codegraph @

3rdparty/iaxclient/lib/audio_openal.c

raw symbols SCIP
1 #include "iaxclient_lib.h"
2
3 // changed by James Turner to use non-prefixed OpenAL include paths,
4 // which is what CMake official FindOpenAL.cmake and the OpenAL-soft Config.cmake
5 // expect to provide. This avoids any pre-processor logic to decide which
6 // prefix is correct.
7 #include <al.h>
8 #include <alc.h>
9
10 struct openal_priv_data
11 {
12 int sample_rate;
13 int num_buffers;
14 int buffers_head;
15 int buffers_tail;
16 int buffers_free;
17 ALuint* buffers;
18 ALCcontext* out_ctx;
19 ALuint source;
20 ALCdevice* in_dev;
21 float input_level;
22 float output_level;
23 };
24
25 static struct iaxc_audio_device device = {
26 "default",
27 IAXC_AD_INPUT | IAXC_AD_OUTPUT | IAXC_AD_RING | IAXC_AD_INPUT_DEFAULT | IAXC_AD_OUTPUT_DEFAULT | IAXC_AD_RING_DEFAULT,
28 0
29 };
30
31 static int openal_error(const char* function, int err)
32 {
33 fprintf(stderr, "OpenAl function %s failed with code %d\n", function, err);
34 return -1;
35 }
36
37 int openal_input(struct iaxc_audio_driver *d, void *samples, int *nSamples)
38 {
39 int err;
40 struct openal_priv_data* priv = (struct openal_priv_data*)(d->priv);
41
42 ALCint available;
43 ALCsizei request;
44
45 alcGetIntegerv(priv->in_dev, ALC_CAPTURE_SAMPLES, sizeof(available), &available);
46 /* do not return less data than caller wanted, iaxclient does not like it */
47 request = (available < *nSamples) ? 0 : *nSamples;
48 if (request > 0)
49 {
50 err = alcGetError(priv->in_dev);
51 alcCaptureSamples(priv->in_dev, samples, request);
52 err = alcGetError(priv->in_dev);
53 if (err)
54 {
55 openal_error("alcCaptureSamples", err);
56 *nSamples = 0;
57 return 1;
58 }
59 // software mute, but keep data flowing for sync purposes
60 if (priv->input_level == 0)
61 {
62 memset(samples, 0, 2 * request);
63 }
64 }
65 *nSamples = request;
66
67 return 0;
68 }
69
70 static void openal_unqueue(struct openal_priv_data* priv)
71 {
72 int i;
73 ALint err;
74 ALint processed;
75
76 alGetSourcei(priv->source, AL_BUFFERS_PROCESSED, &processed);
77
78 #ifdef OPENAL_DEBUG
79 {
80 ALint queued;
81 ALint state;
82
83 alGetSourcei(priv->source, AL_BUFFERS_QUEUED, &queued);
84 alGetSourcei(priv->source, AL_SOURCE_STATE, &state);
85
86 fprintf(stderr, "free: %d processed: %d queued: %d head: %d tail: %d state: %d\n",
87 priv->buffers_free, processed, queued, priv->buffers_head, priv->buffers_tail, state);
88 }
89 #endif
90
91 alGetError();
92 for(i = 0; i < processed; i++)
93 {
94 alSourceUnqueueBuffers(priv->source, 1, priv->buffers + priv->buffers_tail);
95 err = alGetError();
96 if (err)
97 {
98 openal_error("alSourceUnqueueBuffers", err);
99 break;
100 }
101 if (++priv->buffers_tail >= priv->num_buffers)
102 {
103 priv->buffers_tail = 0;
104 }
105 ++priv->buffers_free;
106 }
107 }
108
109 int openal_output(struct iaxc_audio_driver *d, void *samples, int nSamples)
110 {
111 struct openal_priv_data* priv = (struct openal_priv_data*)(d->priv);
112
113 openal_unqueue(priv);
114 /* If we run out of buffers, wait for an arbitrary number to become free */
115 if (priv->buffers_free == 0)
116 {
117 while(priv->buffers_free < 4)
118 {
119 iaxc_millisleep(100);
120 openal_unqueue(priv);
121 }
122 }
123
124 if (priv->buffers_free > 0)
125 {
126 ALuint buffer = priv->buffers[priv->buffers_head++];
127 if (priv->buffers_head >= priv->num_buffers)
128 {
129 priv->buffers_head = 0;
130 }
131
132 alBufferData(buffer, AL_FORMAT_MONO16, samples, nSamples * 2, priv->sample_rate);
133 alSourceQueueBuffers(priv->source, 1, &buffer);
134 --priv->buffers_free;
135
136 /* delay start of output until we have 2 buffers */
137 if (priv->buffers_free == priv->num_buffers - 2)
138 {
139 ALint state;
140
141 alGetSourcei(priv->source, AL_SOURCE_STATE, &state);
142 if (state != AL_PLAYING)
143 {
144 #ifdef OPENAL_DEBUG
145 fprintf(stderr, "calling alSourcePlay\n");
146 #endif
147 alSourcePlay(priv->source);
148 }
149 }
150 } else {
151 fprintf(stderr, "openal_output buffer overflow\n");
152 return 1;
153 }
154
155 return 0;
156 }
157
158 int openal_select_devices(struct iaxc_audio_driver *d, int input, int output, int ring)
159 {
160 return (input != 0 || output !=0 || ring != 0) ? -1 : 0;
161 }
162
163 int openal_selected_devices(struct iaxc_audio_driver *d, int *input, int *output, int *ring)
164 {
165 *input = 0;
166 *output = 0;
167 *ring = 0;
168
169 return 0;
170 }
171
172 /*
173 Apparently iaxclient calls openal_start a gazillion times and doesn't call openal_stop.
174 So let's just make them no-ops.
175 */
176 int openal_start(struct iaxc_audio_driver *d)
177 {
178 int iret = 0;
179 struct openal_priv_data* priv = (struct openal_priv_data*)(d->priv);
180 if (priv) /* just to stop compiler noise */
181 iret = 0;
182 return iret;
183 }
184
185 int openal_stop(struct iaxc_audio_driver *d)
186 {
187 int iret = 0;
188 struct openal_priv_data* priv = (struct openal_priv_data*)(d->priv);
189 if (priv) /* just to stop compiler noise */
190 iret = 0;
191 return iret;
192 }
193
194 float openal_input_level_get(struct iaxc_audio_driver *d)
195 {
196 struct openal_priv_data* priv = (struct openal_priv_data*)(d->priv);
197
198 return priv->input_level;
199 }
200
201 float openal_output_level_get(struct iaxc_audio_driver *d)
202 {
203 struct openal_priv_data* priv = (struct openal_priv_data*)(d->priv);
204
205 return priv->output_level;
206 }
207
208 int openal_input_level_set(struct iaxc_audio_driver *d, float level)
209 {
210 struct openal_priv_data* priv = (struct openal_priv_data*)(d->priv);
211 priv->input_level = (level < 0.5) ? 0 : 1;
212
213 return 0;
214 }
215
216 int openal_output_level_set(struct iaxc_audio_driver *d, float level)
217 {
218 struct openal_priv_data* priv = (struct openal_priv_data*)(d->priv);
219 priv->output_level = level;
220 alSourcef(priv->source, AL_GAIN, level);
221
222 return 0;
223 }
224
225 int openal_play_sound(struct iaxc_sound *s, int ring)
226 {
227 return 0;
228 }
229
230 int openal_stop_sound(int id)
231 {
232 return 0;
233 }
234
235 int openal_mic_boost_get(struct iaxc_audio_driver *d)
236 {
237 return 0;
238 }
239
240 int openal_mic_boost_set(struct iaxc_audio_driver *d, int enable)
241 {
242 return 0;
243 }
244
245 int openal_destroy(struct iaxc_audio_driver *d)
246 {
247 struct openal_priv_data* priv = (struct openal_priv_data*)(d->priv);
248
249
250 alcCaptureStop(priv->in_dev);
251 alcCaptureCloseDevice(priv->in_dev);
252 alDeleteSources(1, &priv->source);
253
254 // FlightGear: added by James to avoid a warning from OpenAL-soft
255 // when we destroy the context in SGSoundMgr_OpenAL: since we
256 // genBuffers below, we should balance that out.
257 alDeleteBuffers(priv->num_buffers, priv->buffers);
258
259 return 0;
260 }
261
262 int openal_initialize(struct iaxc_audio_driver *d, int sample_rate)
263 {
264 struct openal_priv_data* priv = malloc(sizeof(struct openal_priv_data));
265 int err = alGetError();
266 d->priv = priv;
267
268 // First we are looking for output device
269 priv->out_ctx = alcGetCurrentContext();
270
271 if( priv->out_ctx == NULL ) { // FGCom standalone only
272 ALCdevice* out_dev = alcOpenDevice(NULL);
273 if (out_dev == 0) return openal_error("alcOpenDevice", alGetError());
274
275 priv->out_ctx = alcCreateContext(out_dev, 0);
276 if (priv->out_ctx == 0) return openal_error("alcCreateContext", alGetError());
277 }
278
279 alcMakeContextCurrent(priv->out_ctx);
280 if ((err = alGetError())) return openal_error("alcMakeContextCurrent", err);
281
282 // Then we look for input device
283 priv->in_dev = alcCaptureOpenDevice(NULL, 8000, AL_FORMAT_MONO16, 800);
284 if (!priv->in_dev) return openal_error("alcCaptureOpenDevice", alGetError());
285 if ((err = alGetError())) return openal_error("alcCaptureOpenDevice1", alGetError());
286
287 alcCaptureStart(priv->in_dev);
288 if ((err = alGetError())) return openal_error("alcCaptureStart", err);
289
290 priv->sample_rate = sample_rate;
291 priv->num_buffers = 20;
292 priv->input_level = 0;
293 priv->output_level = 1;
294 priv->buffers_head = 0;
295 priv->buffers_tail = 0;
296 priv->buffers_free = priv->num_buffers;
297 priv->buffers = (ALuint*)malloc(sizeof(ALuint) * priv->num_buffers);
298
299 alGenBuffers(priv->num_buffers, priv->buffers);
300 if ((err = alGetError())) return openal_error("alGenBuffers", err);
301
302 alGenSources(1, &priv->source);
303 if ((err = alGetError())) return openal_error("alGenSources", err);
304
305 d->initialize = openal_initialize;
306 d->destroy = openal_destroy;
307 d->select_devices = openal_select_devices;
308 d->selected_devices = openal_selected_devices;
309 d->start = openal_start;
310 d->stop = openal_stop;
311 d->output = openal_output;
312 d->input = openal_input;
313 d->input_level_get = openal_input_level_get;
314 d->input_level_set = openal_input_level_set;
315 d->output_level_get = openal_output_level_get;
316 d->output_level_set = openal_output_level_set;
317 d->mic_boost_get = openal_mic_boost_get;
318 d->mic_boost_set = openal_mic_boost_set;
319 d->play_sound = openal_play_sound;
320 d->stop_sound = openal_stop_sound;
321 d->nDevices = 1;
322 d->devices = &device;
323
324 return 0;
325 }