Catch exceptions from backend start calls
[openal-soft.git] / al / listener.cpp
blob7a14a9baf998fd23ba0657e9355c74ea1a69586f
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2000 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include "listener.h"
25 #include <cmath>
26 #include <mutex>
28 #include "AL/al.h"
29 #include "AL/alc.h"
30 #include "AL/efx.h"
32 #include "alcontext.h"
33 #include "alexcpt.h"
34 #include "almalloc.h"
35 #include "atomic.h"
36 #include "opthelpers.h"
39 #define DO_UPDATEPROPS() do { \
40 if(!context->mDeferUpdates.load(std::memory_order_acquire)) \
41 UpdateListenerProps(context.get()); \
42 else \
43 listener.PropsClean.clear(std::memory_order_release); \
44 } while(0)
47 AL_API ALvoid AL_APIENTRY alListenerf(ALenum param, ALfloat value)
48 START_API_FUNC
50 ContextRef context{GetContextRef()};
51 if UNLIKELY(!context) return;
53 ALlistener &listener = context->mListener;
54 std::lock_guard<std::mutex> _{context->mPropLock};
55 switch(param)
57 case AL_GAIN:
58 if(!(value >= 0.0f && std::isfinite(value)))
59 SETERR_RETURN(context, AL_INVALID_VALUE,, "Listener gain out of range");
60 listener.Gain = value;
61 DO_UPDATEPROPS();
62 break;
64 case AL_METERS_PER_UNIT:
65 if(!(value >= AL_MIN_METERS_PER_UNIT && value <= AL_MAX_METERS_PER_UNIT))
66 SETERR_RETURN(context, AL_INVALID_VALUE,, "Listener meters per unit out of range");
67 listener.mMetersPerUnit = value;
68 DO_UPDATEPROPS();
69 break;
71 default:
72 context->setError(AL_INVALID_ENUM, "Invalid listener float property");
75 END_API_FUNC
77 AL_API ALvoid AL_APIENTRY alListener3f(ALenum param, ALfloat value1, ALfloat value2, ALfloat value3)
78 START_API_FUNC
80 ContextRef context{GetContextRef()};
81 if UNLIKELY(!context) return;
83 ALlistener &listener = context->mListener;
84 std::lock_guard<std::mutex> _{context->mPropLock};
85 switch(param)
87 case AL_POSITION:
88 if(!(std::isfinite(value1) && std::isfinite(value2) && std::isfinite(value3)))
89 SETERR_RETURN(context, AL_INVALID_VALUE,, "Listener position out of range");
90 listener.Position[0] = value1;
91 listener.Position[1] = value2;
92 listener.Position[2] = value3;
93 DO_UPDATEPROPS();
94 break;
96 case AL_VELOCITY:
97 if(!(std::isfinite(value1) && std::isfinite(value2) && std::isfinite(value3)))
98 SETERR_RETURN(context, AL_INVALID_VALUE,, "Listener velocity out of range");
99 listener.Velocity[0] = value1;
100 listener.Velocity[1] = value2;
101 listener.Velocity[2] = value3;
102 DO_UPDATEPROPS();
103 break;
105 default:
106 context->setError(AL_INVALID_ENUM, "Invalid listener 3-float property");
109 END_API_FUNC
111 AL_API ALvoid AL_APIENTRY alListenerfv(ALenum param, const ALfloat *values)
112 START_API_FUNC
114 if(values)
116 switch(param)
118 case AL_GAIN:
119 case AL_METERS_PER_UNIT:
120 alListenerf(param, values[0]);
121 return;
123 case AL_POSITION:
124 case AL_VELOCITY:
125 alListener3f(param, values[0], values[1], values[2]);
126 return;
130 ContextRef context{GetContextRef()};
131 if UNLIKELY(!context) return;
133 ALlistener &listener = context->mListener;
134 std::lock_guard<std::mutex> _{context->mPropLock};
135 if(!values) SETERR_RETURN(context, AL_INVALID_VALUE,, "NULL pointer");
136 switch(param)
138 case AL_ORIENTATION:
139 if(!(std::isfinite(values[0]) && std::isfinite(values[1]) && std::isfinite(values[2]) &&
140 std::isfinite(values[3]) && std::isfinite(values[4]) && std::isfinite(values[5])))
141 SETERR_RETURN(context, AL_INVALID_VALUE,, "Listener orientation out of range");
142 /* AT then UP */
143 listener.OrientAt[0] = values[0];
144 listener.OrientAt[1] = values[1];
145 listener.OrientAt[2] = values[2];
146 listener.OrientUp[0] = values[3];
147 listener.OrientUp[1] = values[4];
148 listener.OrientUp[2] = values[5];
149 DO_UPDATEPROPS();
150 break;
152 default:
153 context->setError(AL_INVALID_ENUM, "Invalid listener float-vector property");
156 END_API_FUNC
159 AL_API ALvoid AL_APIENTRY alListeneri(ALenum param, ALint /*value*/)
160 START_API_FUNC
162 ContextRef context{GetContextRef()};
163 if UNLIKELY(!context) return;
165 std::lock_guard<std::mutex> _{context->mPropLock};
166 switch(param)
168 default:
169 context->setError(AL_INVALID_ENUM, "Invalid listener integer property");
172 END_API_FUNC
174 AL_API void AL_APIENTRY alListener3i(ALenum param, ALint value1, ALint value2, ALint value3)
175 START_API_FUNC
177 switch(param)
179 case AL_POSITION:
180 case AL_VELOCITY:
181 alListener3f(param, static_cast<ALfloat>(value1), static_cast<ALfloat>(value2), static_cast<ALfloat>(value3));
182 return;
185 ContextRef context{GetContextRef()};
186 if UNLIKELY(!context) return;
188 std::lock_guard<std::mutex> _{context->mPropLock};
189 switch(param)
191 default:
192 context->setError(AL_INVALID_ENUM, "Invalid listener 3-integer property");
195 END_API_FUNC
197 AL_API void AL_APIENTRY alListeneriv(ALenum param, const ALint *values)
198 START_API_FUNC
200 if(values)
202 ALfloat fvals[6];
203 switch(param)
205 case AL_POSITION:
206 case AL_VELOCITY:
207 alListener3f(param, static_cast<ALfloat>(values[0]), static_cast<ALfloat>(values[1]), static_cast<ALfloat>(values[2]));
208 return;
210 case AL_ORIENTATION:
211 fvals[0] = static_cast<ALfloat>(values[0]);
212 fvals[1] = static_cast<ALfloat>(values[1]);
213 fvals[2] = static_cast<ALfloat>(values[2]);
214 fvals[3] = static_cast<ALfloat>(values[3]);
215 fvals[4] = static_cast<ALfloat>(values[4]);
216 fvals[5] = static_cast<ALfloat>(values[5]);
217 alListenerfv(param, fvals);
218 return;
222 ContextRef context{GetContextRef()};
223 if UNLIKELY(!context) return;
225 std::lock_guard<std::mutex> _{context->mPropLock};
226 if(!values)
227 context->setError(AL_INVALID_VALUE, "NULL pointer");
228 else switch(param)
230 default:
231 context->setError(AL_INVALID_ENUM, "Invalid listener integer-vector property");
234 END_API_FUNC
237 AL_API ALvoid AL_APIENTRY alGetListenerf(ALenum param, ALfloat *value)
238 START_API_FUNC
240 ContextRef context{GetContextRef()};
241 if UNLIKELY(!context) return;
243 ALlistener &listener = context->mListener;
244 std::lock_guard<std::mutex> _{context->mPropLock};
245 if(!value)
246 context->setError(AL_INVALID_VALUE, "NULL pointer");
247 else switch(param)
249 case AL_GAIN:
250 *value = listener.Gain;
251 break;
253 case AL_METERS_PER_UNIT:
254 *value = listener.mMetersPerUnit;
255 break;
257 default:
258 context->setError(AL_INVALID_ENUM, "Invalid listener float property");
261 END_API_FUNC
263 AL_API ALvoid AL_APIENTRY alGetListener3f(ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
264 START_API_FUNC
266 ContextRef context{GetContextRef()};
267 if UNLIKELY(!context) return;
269 ALlistener &listener = context->mListener;
270 std::lock_guard<std::mutex> _{context->mPropLock};
271 if(!value1 || !value2 || !value3)
272 context->setError(AL_INVALID_VALUE, "NULL pointer");
273 else switch(param)
275 case AL_POSITION:
276 *value1 = listener.Position[0];
277 *value2 = listener.Position[1];
278 *value3 = listener.Position[2];
279 break;
281 case AL_VELOCITY:
282 *value1 = listener.Velocity[0];
283 *value2 = listener.Velocity[1];
284 *value3 = listener.Velocity[2];
285 break;
287 default:
288 context->setError(AL_INVALID_ENUM, "Invalid listener 3-float property");
291 END_API_FUNC
293 AL_API ALvoid AL_APIENTRY alGetListenerfv(ALenum param, ALfloat *values)
294 START_API_FUNC
296 switch(param)
298 case AL_GAIN:
299 case AL_METERS_PER_UNIT:
300 alGetListenerf(param, values);
301 return;
303 case AL_POSITION:
304 case AL_VELOCITY:
305 alGetListener3f(param, values+0, values+1, values+2);
306 return;
309 ContextRef context{GetContextRef()};
310 if UNLIKELY(!context) return;
312 ALlistener &listener = context->mListener;
313 std::lock_guard<std::mutex> _{context->mPropLock};
314 if(!values)
315 context->setError(AL_INVALID_VALUE, "NULL pointer");
316 else switch(param)
318 case AL_ORIENTATION:
319 // AT then UP
320 values[0] = listener.OrientAt[0];
321 values[1] = listener.OrientAt[1];
322 values[2] = listener.OrientAt[2];
323 values[3] = listener.OrientUp[0];
324 values[4] = listener.OrientUp[1];
325 values[5] = listener.OrientUp[2];
326 break;
328 default:
329 context->setError(AL_INVALID_ENUM, "Invalid listener float-vector property");
332 END_API_FUNC
335 AL_API ALvoid AL_APIENTRY alGetListeneri(ALenum param, ALint *value)
336 START_API_FUNC
338 ContextRef context{GetContextRef()};
339 if UNLIKELY(!context) return;
341 std::lock_guard<std::mutex> _{context->mPropLock};
342 if(!value)
343 context->setError(AL_INVALID_VALUE, "NULL pointer");
344 else switch(param)
346 default:
347 context->setError(AL_INVALID_ENUM, "Invalid listener integer property");
350 END_API_FUNC
352 AL_API void AL_APIENTRY alGetListener3i(ALenum param, ALint *value1, ALint *value2, ALint *value3)
353 START_API_FUNC
355 ContextRef context{GetContextRef()};
356 if UNLIKELY(!context) return;
358 ALlistener &listener = context->mListener;
359 std::lock_guard<std::mutex> _{context->mPropLock};
360 if(!value1 || !value2 || !value3)
361 context->setError(AL_INVALID_VALUE, "NULL pointer");
362 else switch(param)
364 case AL_POSITION:
365 *value1 = static_cast<ALint>(listener.Position[0]);
366 *value2 = static_cast<ALint>(listener.Position[1]);
367 *value3 = static_cast<ALint>(listener.Position[2]);
368 break;
370 case AL_VELOCITY:
371 *value1 = static_cast<ALint>(listener.Velocity[0]);
372 *value2 = static_cast<ALint>(listener.Velocity[1]);
373 *value3 = static_cast<ALint>(listener.Velocity[2]);
374 break;
376 default:
377 context->setError(AL_INVALID_ENUM, "Invalid listener 3-integer property");
380 END_API_FUNC
382 AL_API void AL_APIENTRY alGetListeneriv(ALenum param, ALint* values)
383 START_API_FUNC
385 switch(param)
387 case AL_POSITION:
388 case AL_VELOCITY:
389 alGetListener3i(param, values+0, values+1, values+2);
390 return;
393 ContextRef context{GetContextRef()};
394 if UNLIKELY(!context) return;
396 ALlistener &listener = context->mListener;
397 std::lock_guard<std::mutex> _{context->mPropLock};
398 if(!values)
399 context->setError(AL_INVALID_VALUE, "NULL pointer");
400 else switch(param)
402 case AL_ORIENTATION:
403 // AT then UP
404 values[0] = static_cast<ALint>(listener.OrientAt[0]);
405 values[1] = static_cast<ALint>(listener.OrientAt[1]);
406 values[2] = static_cast<ALint>(listener.OrientAt[2]);
407 values[3] = static_cast<ALint>(listener.OrientUp[0]);
408 values[4] = static_cast<ALint>(listener.OrientUp[1]);
409 values[5] = static_cast<ALint>(listener.OrientUp[2]);
410 break;
412 default:
413 context->setError(AL_INVALID_ENUM, "Invalid listener integer-vector property");
416 END_API_FUNC
419 void UpdateListenerProps(ALCcontext *context)
421 /* Get an unused proprty container, or allocate a new one as needed. */
422 ALlistenerProps *props{context->mFreeListenerProps.load(std::memory_order_acquire)};
423 if(!props)
424 props = new ALlistenerProps{};
425 else
427 ALlistenerProps *next;
428 do {
429 next = props->next.load(std::memory_order_relaxed);
430 } while(context->mFreeListenerProps.compare_exchange_weak(props, next,
431 std::memory_order_seq_cst, std::memory_order_acquire) == 0);
434 /* Copy in current property values. */
435 ALlistener &listener = context->mListener;
436 props->Position = listener.Position;
437 props->Velocity = listener.Velocity;
438 props->OrientAt = listener.OrientAt;
439 props->OrientUp = listener.OrientUp;
440 props->Gain = listener.Gain;
441 props->MetersPerUnit = listener.mMetersPerUnit;
443 /* Set the new container for updating internal parameters. */
444 props = listener.Params.Update.exchange(props, std::memory_order_acq_rel);
445 if(props)
447 /* If there was an unused update container, put it back in the
448 * freelist.
450 AtomicReplaceHead(context->mFreeListenerProps, props);