Add a method to fade out sources
[alure.git] / include / AL / alure2.h
blob696e5f9de21ea5d20bfb6669effbf0c6b02a5f22
1 #ifndef AL_ALURE2_H
2 #define AL_ALURE2_H
4 #include <vector>
5 #include <string>
6 #include <memory>
7 #include <cstring>
8 #include <utility>
9 #include <future>
10 #include <chrono>
11 #include <array>
12 #include <cmath>
14 #include "alc.h"
15 #include "al.h"
17 #ifndef ALURE_API
18 #if defined(ALURE_STATIC_LIB)
19 #define ALURE_API
20 #elif defined(_WIN32)
21 #define ALURE_API __declspec(dllimport)
22 #elif defined(__GNUC__) || (defined(__has_attribute) && __has_attribute(visibility))
23 #define ALURE_API __attribute__((visibility("default")))
24 #else
25 #define ALURE_API
26 #endif
27 #endif /* ALURE_API */
29 #ifndef EFXEAXREVERBPROPERTIES_DEFINED
30 #define EFXEAXREVERBPROPERTIES_DEFINED
31 typedef struct {
32 float flDensity;
33 float flDiffusion;
34 float flGain;
35 float flGainHF;
36 float flGainLF;
37 float flDecayTime;
38 float flDecayHFRatio;
39 float flDecayLFRatio;
40 float flReflectionsGain;
41 float flReflectionsDelay;
42 float flReflectionsPan[3];
43 float flLateReverbGain;
44 float flLateReverbDelay;
45 float flLateReverbPan[3];
46 float flEchoTime;
47 float flEchoDepth;
48 float flModulationTime;
49 float flModulationDepth;
50 float flAirAbsorptionGainHF;
51 float flHFReference;
52 float flLFReference;
53 float flRoomRolloffFactor;
54 int iDecayHFLimit;
55 } EFXEAXREVERBPROPERTIES, *LPEFXEAXREVERBPROPERTIES;
56 #endif
58 namespace alure {
60 class DeviceManager;
61 class DeviceManagerImpl;
62 class Device;
63 class DeviceImpl;
64 class Context;
65 class ContextImpl;
66 class Listener;
67 class ListenerImpl;
68 class Buffer;
69 class BufferImpl;
70 class Source;
71 class SourceImpl;
72 class SourceGroup;
73 class SourceGroupImpl;
74 class AuxiliaryEffectSlot;
75 class AuxiliaryEffectSlotImpl;
76 class Effect;
77 class EffectImpl;
78 class Decoder;
79 class DecoderFactory;
80 class MessageHandler;
82 // Convenience aliases
83 template<typename T> using RemoveRefT = typename std::remove_reference<T>::type;
84 template<bool B> using EnableIfT = typename std::enable_if<B>::type;
87 // Duration in seconds, using double precision
88 using Seconds = std::chrono::duration<double>;
90 // A SharedPtr implementation, defaults to C++11's std::shared_ptr. If this is
91 // changed, you must recompile the library.
92 template<typename... Args> using SharedPtr = std::shared_ptr<Args...>;
93 template<typename T, typename... Args>
94 constexpr inline SharedPtr<T> MakeShared(Args&&... args)
96 return std::make_shared<T>(std::forward<Args>(args)...);
99 // A UniquePtr implementation, defaults to C++11's std::unique_ptr. If this is
100 // changed, you must recompile the library.
101 template<typename... Args> using UniquePtr = std::unique_ptr<Args...>;
102 template<typename T, typename... Args>
103 constexpr inline UniquePtr<T> MakeUnique(Args&&... args)
105 #if __cplusplus >= 201402L
106 return std::make_unique<T>(std::forward<Args>(args)...);
107 #else
108 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
109 #endif
112 // A Promise/Future (+SharedFuture) implementation, defaults to C++11's
113 // std::promise, std::future, and std::shared_future. If this is changed, you
114 // must recompile the library.
115 template<typename... Args> using Promise = std::promise<Args...>;
116 template<typename... Args> using Future = std::future<Args...>;
117 template<typename... Args> using SharedFuture = std::shared_future<Args...>;
119 // A Vector implementation, defaults to C++'s std::vector. If this is changed,
120 // you must recompile the library.
121 template<typename... Args> using Vector = std::vector<Args...>;
123 // A static-sized Array implementation, defaults to C++11's std::array. If this
124 // is changed, you must recompile the library.
125 template<typename T, std::size_t N> using Array = std::array<T, N>;
127 // A String implementation, default's to C++'s std::string. If this is changed,
128 // you must recompile the library.
129 template<typename... Args> using BasicString = std::basic_string<Args...>;
130 using String = BasicString<std::string::value_type>;
132 // Tag specific containers that guarantee contiguous storage. The standard
133 // provides no such mechanism, so we have to manually specify which are
134 // acceptable.
135 template<typename T> struct IsContiguousTag : std::false_type {};
136 template<typename T, size_t N> struct IsContiguousTag<Array<T,N>> : std::true_type {};
137 template<typename T> struct IsContiguousTag<Vector<T>> : std::true_type {};
138 template<typename T> struct IsContiguousTag<BasicString<T>> : std::true_type {};
140 // A rather simple ArrayView container. This allows accepting various array
141 // types (Array, Vector, a static-sized array, a dynamic array + size) without
142 // copying its elements.
143 template<typename T>
144 class ArrayView {
145 public:
146 using value_type = T;
148 using iterator = const value_type*;
149 using const_iterator = const value_type*;
151 using size_type = size_t;
153 static constexpr size_type npos = static_cast<size_type>(-1);
155 private:
156 const value_type *mElems;
157 size_t mNumElems;
159 public:
160 ArrayView() noexcept : mElems(nullptr), mNumElems(0) { }
161 ArrayView(const ArrayView&) noexcept = default;
162 ArrayView(ArrayView&&) noexcept = default;
163 ArrayView(const value_type *elems, size_type num_elems) noexcept
164 : mElems(elems), mNumElems(num_elems) { }
165 template<typename OtherT> ArrayView(RemoveRefT<OtherT>&&) = delete;
166 template<typename OtherT,
167 typename = EnableIfT<IsContiguousTag<RemoveRefT<OtherT>>::value>>
168 ArrayView(const OtherT &rhs) noexcept : mElems(rhs.data()), mNumElems(rhs.size()) { }
169 template<size_t N>
170 ArrayView(const value_type (&elems)[N]) noexcept : mElems(elems), mNumElems(N) { }
172 ArrayView& operator=(const ArrayView&) noexcept = default;
174 const value_type *data() const noexcept { return mElems; }
176 size_type size() const noexcept { return mNumElems; }
177 bool empty() const noexcept { return mNumElems == 0; }
179 const value_type& operator[](size_t i) const { return mElems[i]; }
181 const value_type& front() const { return mElems[0]; }
182 const value_type& back() const { return mElems[mNumElems-1]; }
184 const value_type& at(size_t i) const
186 if(i >= mNumElems)
187 throw std::out_of_range("alure::ArrayView::at: element out of range");
188 return mElems[i];
191 const_iterator begin() const noexcept { return mElems; }
192 const_iterator cbegin() const noexcept { return mElems; }
194 const_iterator end() const noexcept { return mElems + mNumElems; }
195 const_iterator cend() const noexcept { return mElems + mNumElems; }
197 ArrayView slice(size_type pos, size_type len = npos) const noexcept
199 if(pos >= size())
200 return ArrayView(data()+size(), 0);
201 if(len == npos || size()-pos >= len)
202 return ArrayView(data()+pos, size()-pos);
203 return ArrayView(data()+pos, len);
207 template<typename T, typename Tr=std::char_traits<T>>
208 class BasicStringView : public ArrayView<T> {
209 using BaseT = ArrayView<T>;
210 using StringT = BasicString<T,Tr>;
212 public:
213 using typename BaseT::value_type;
214 using typename BaseT::size_type;
215 using BaseT::npos;
216 using traits_type = Tr;
218 BasicStringView() noexcept = default;
219 BasicStringView(const BasicStringView&) noexcept = default;
220 BasicStringView(const value_type *elems, size_type num_elems) noexcept
221 : ArrayView<T>(elems, num_elems) { }
222 BasicStringView(const value_type *elems) : ArrayView<T>(elems, std::strlen(elems)) { }
223 BasicStringView(StringT&&) = delete;
224 BasicStringView(const StringT &rhs) noexcept : ArrayView<T>(rhs) { }
225 #if __cplusplus >= 201703L
226 BasicStringView(const std::basic_string_view<T> &rhs) noexcept
227 : ArrayView<T>(rhs.data(), rhs.length()) { }
228 #endif
230 BasicStringView& operator=(const BasicStringView&) noexcept = default;
232 size_type length() const { return BaseT::size(); }
234 explicit operator StringT() const { return StringT(BaseT::data(), length()); }
235 #if __cplusplus >= 201703L
236 operator std::basic_string_view<T,Tr>() const noexcept
237 { return std::basic_string_view<T,Tr>(BaseT::data(), length()); }
238 #endif
240 StringT operator+(const StringT &rhs) const
242 StringT ret = StringT(*this);
243 ret += rhs;
244 return ret;
246 StringT operator+(const typename StringT::value_type *rhs) const
248 StringT ret = StringT(*this);
249 ret += rhs;
250 return ret;
253 int compare(BasicStringView other) const noexcept
255 int ret = traits_type::compare(
256 BaseT::data(), other.data(), std::min<size_t>(length(), other.length())
258 if(ret == 0)
260 if(length() > other.length()) return 1;
261 if(length() < other.length()) return -1;
262 return 0;
264 return ret;
266 bool operator==(BasicStringView rhs) const noexcept { return compare(rhs) == 0; }
267 bool operator!=(BasicStringView rhs) const noexcept { return compare(rhs) != 0; }
268 bool operator<=(BasicStringView rhs) const noexcept { return compare(rhs) <= 0; }
269 bool operator>=(BasicStringView rhs) const noexcept { return compare(rhs) >= 0; }
270 bool operator<(BasicStringView rhs) const noexcept { return compare(rhs) < 0; }
271 bool operator>(BasicStringView rhs) const noexcept { return compare(rhs) > 0; }
273 BasicStringView substr(size_type pos, size_type len = npos) const noexcept
274 { return BaseT::slice(pos, len); }
276 using StringView = BasicStringView<String::value_type>;
278 // Inline operators to concat String and C-style strings with StringViews.
279 template<typename T, typename Tr>
280 inline BasicString<T,Tr> operator+(const BasicString<T,Tr> &lhs, BasicStringView<T,Tr> rhs)
281 { return BasicString<T,Tr>(lhs).append(rhs.data(), rhs.size()); }
282 template<typename T, typename Tr>
283 inline BasicString<T,Tr> operator+(BasicString<T,Tr> lhs, BasicStringView<T,Tr> rhs)
284 { return std::move(lhs.append(rhs.data(), rhs.size())); }
285 template<typename T, typename Tr>
286 inline BasicString<T,Tr> operator+(const typename BasicString<T,Tr>::value_type *lhs, BasicStringView<T,Tr> rhs)
287 { return lhs + BasicString<T,Tr>(rhs); }
288 template<typename T, typename Tr>
289 inline BasicString<T,Tr>& operator+=(BasicString<T,Tr> &lhs, BasicStringView<T,Tr> rhs)
290 { return lhs.append(rhs.data(), rhs.size()); }
292 // Inline operators to compare String and C-style strings with StringViews.
293 #define ALURE_DECL_STROP(op) \
294 template<typename T, typename Tr> \
295 inline bool operator op(const BasicString<T,Tr> &lhs, BasicStringView<T,Tr> rhs) \
296 { return BasicStringView<T,Tr>(lhs) op rhs; } \
297 template<typename T, typename Tr> \
298 inline bool operator op(const typename BasicString<T,Tr>::value_type *lhs, \
299 BasicStringView<T,Tr> rhs) \
300 { return BasicStringView<T,Tr>(lhs) op rhs; }
301 ALURE_DECL_STROP(==)
302 ALURE_DECL_STROP(!=)
303 ALURE_DECL_STROP(<=)
304 ALURE_DECL_STROP(>=)
305 ALURE_DECL_STROP(<)
306 ALURE_DECL_STROP(>)
307 #undef ALURE_DECL_STROP
309 // Inline operator to write out a StringView to an ostream
310 template<typename T, typename Tr>
311 inline std::basic_ostream<T>& operator<<(std::basic_ostream<T,Tr> &lhs, BasicStringView<T,Tr> rhs)
313 for(auto ch : rhs)
314 lhs << ch;
315 return lhs;
320 * An attribute pair, for passing attributes to Device::createContext and
321 * Device::reset.
323 using AttributePair = std::pair<ALCint,ALCint>;
324 static_assert(sizeof(AttributePair) == sizeof(ALCint[2]), "Bad AttributePair size");
325 constexpr AttributePair AttributesEnd{0, 0};
328 struct FilterParams {
329 ALfloat mGain;
330 ALfloat mGainHF; // For low-pass and band-pass filters
331 ALfloat mGainLF; // For high-pass and band-pass filters
335 class Vector3 {
336 Array<ALfloat,3> mValue;
338 public:
339 constexpr Vector3() noexcept
340 : mValue{{0.0f, 0.0f, 0.0f}}
342 constexpr Vector3(const Vector3 &rhs) noexcept
343 : mValue{{rhs.mValue[0], rhs.mValue[1], rhs.mValue[2]}}
345 constexpr Vector3(ALfloat val) noexcept
346 : mValue{{val, val, val}}
348 constexpr Vector3(ALfloat x, ALfloat y, ALfloat z) noexcept
349 : mValue{{x, y, z}}
351 Vector3(const ALfloat *vec) noexcept
352 : mValue{{vec[0], vec[1], vec[2]}}
355 const ALfloat *getPtr() const noexcept
356 { return mValue.data(); }
358 ALfloat& operator[](size_t i) noexcept
359 { return mValue[i]; }
360 constexpr const ALfloat& operator[](size_t i) const noexcept
361 { return mValue[i]; }
363 #define ALURE_DECL_OP(op) \
364 constexpr Vector3 operator op(const Vector3 &rhs) const noexcept \
366 return Vector3(mValue[0] op rhs.mValue[0], \
367 mValue[1] op rhs.mValue[1], \
368 mValue[2] op rhs.mValue[2]); \
370 ALURE_DECL_OP(+)
371 ALURE_DECL_OP(-)
372 ALURE_DECL_OP(*)
373 ALURE_DECL_OP(/)
374 #undef ALURE_DECL_OP
375 #define ALURE_DECL_OP(op) \
376 Vector3& operator op(const Vector3 &rhs) noexcept \
378 mValue[0] op rhs.mValue[0]; \
379 mValue[1] op rhs.mValue[1]; \
380 mValue[2] op rhs.mValue[2]; \
381 return *this; \
383 ALURE_DECL_OP(+=)
384 ALURE_DECL_OP(-=)
385 ALURE_DECL_OP(*=)
386 ALURE_DECL_OP(/=)
388 #undef ALURE_DECL_OP
389 #define ALURE_DECL_OP(op) \
390 constexpr Vector3 operator op(ALfloat scale) const noexcept \
392 return Vector3(mValue[0] op scale, \
393 mValue[1] op scale, \
394 mValue[2] op scale); \
396 ALURE_DECL_OP(*)
397 ALURE_DECL_OP(/)
398 #undef ALURE_DECL_OP
399 #define ALURE_DECL_OP(op) \
400 Vector3& operator op(ALfloat scale) noexcept \
402 mValue[0] op scale; \
403 mValue[1] op scale; \
404 mValue[2] op scale; \
405 return *this; \
407 ALURE_DECL_OP(*=)
408 ALURE_DECL_OP(/=)
409 #undef ALURE_DECL_OP
411 constexpr ALfloat getLengthSquared() const noexcept
412 { return mValue[0]*mValue[0] + mValue[1]*mValue[1] + mValue[2]*mValue[2]; }
413 ALfloat getLength() const noexcept
414 { return std::sqrt(getLengthSquared()); }
416 constexpr ALfloat getDistanceSquared(const Vector3 &pos) const noexcept
417 { return (pos - *this).getLengthSquared(); }
418 ALfloat getDistance(const Vector3 &pos) const noexcept
419 { return (pos - *this).getLength(); }
421 static_assert(sizeof(Vector3) == sizeof(ALfloat[3]), "Bad Vector3 size");
424 enum class SampleType {
425 UInt8,
426 Int16,
427 Float32,
428 Mulaw
430 ALURE_API const char *GetSampleTypeName(SampleType type);
432 enum class ChannelConfig {
433 /** 1-channel mono sound. */
434 Mono,
435 /** 2-channel stereo sound. */
436 Stereo,
437 /** 2-channel rear sound (back-left and back-right). */
438 Rear,
439 /** 4-channel surround sound. */
440 Quad,
441 /** 5.1 surround sound. */
442 X51,
443 /** 6.1 surround sound. */
444 X61,
445 /** 7.1 surround sound. */
446 X71,
447 /** 3-channel B-Format, using FuMa channel ordering and scaling. */
448 BFormat2D,
449 /** 4-channel B-Format, using FuMa channel ordering and scaling. */
450 BFormat3D
452 ALURE_API const char *GetChannelConfigName(ChannelConfig cfg);
454 ALURE_API ALuint FramesToBytes(ALuint frames, ChannelConfig chans, SampleType type);
455 ALURE_API ALuint BytesToFrames(ALuint bytes, ChannelConfig chans, SampleType type);
458 /** Class for storing a major.minor version number. */
459 class Version {
460 ALuint mMajor : 16;
461 ALuint mMinor : 16;
463 public:
464 constexpr Version(ALuint _maj, ALuint _min) : mMajor(_maj), mMinor(_min) { }
466 constexpr ALuint getMajor() const noexcept { return mMajor; }
467 constexpr ALuint getMinor() const noexcept { return mMinor; }
468 constexpr bool isZero() const noexcept { return mMajor == 0 && mMinor == 0; }
471 #define MAKE_PIMPL(BaseT, ImplT) \
472 private: \
473 ImplT *pImpl; \
475 public: \
476 using handle_type = ImplT*; \
478 BaseT() : pImpl(nullptr) { } \
479 BaseT(ImplT *impl) : pImpl(impl) { } \
480 BaseT(const BaseT&) = default; \
481 BaseT(BaseT&& rhs) : pImpl(rhs.pImpl) { rhs.pImpl = nullptr; } \
483 BaseT& operator=(const BaseT&) = default; \
484 BaseT& operator=(BaseT&& rhs) \
486 pImpl = rhs.pImpl; rhs.pImpl = nullptr; \
487 return *this; \
490 bool operator==(const BaseT &rhs) const { return pImpl == rhs.pImpl; } \
491 bool operator==(BaseT&& rhs) const { return pImpl == rhs.pImpl; } \
493 operator bool() const { return !!pImpl; } \
495 handle_type getHandle() const { return pImpl; }
497 enum class DeviceEnumeration {
498 Basic = ALC_DEVICE_SPECIFIER,
499 Full = ALC_ALL_DEVICES_SPECIFIER,
500 Capture = ALC_CAPTURE_DEVICE_SPECIFIER
503 enum class DefaultDeviceType {
504 Basic = ALC_DEFAULT_DEVICE_SPECIFIER,
505 Full = ALC_DEFAULT_ALL_DEVICES_SPECIFIER,
506 Capture = ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER
510 * A class managing Device objects and other related functionality. This class
511 * is a singleton, only one instance will exist in a process.
513 class ALURE_API DeviceManager {
514 DeviceManagerImpl *pImpl;
516 DeviceManager(DeviceManagerImpl *impl) : pImpl(impl) { }
517 friend class ALDeviceManager;
519 public:
520 DeviceManager(const DeviceManager&) = default;
521 DeviceManager(DeviceManager&& rhs) : pImpl(rhs.pImpl) { }
523 /** Retrieves the DeviceManager instance. */
524 static DeviceManager get();
526 /** Queries the existence of a non-device-specific ALC extension. */
527 bool queryExtension(const String &name) const;
528 bool queryExtension(const char *name) const;
530 /** Enumerates available device names of the given type. */
531 Vector<String> enumerate(DeviceEnumeration type) const;
532 /** Retrieves the default device of the given type. */
533 String defaultDeviceName(DefaultDeviceType type) const;
536 * Opens the playback device given by name, or the default if blank. Throws
537 * an exception on error.
539 Device openPlayback(const String &name=String());
540 Device openPlayback(const char *name);
543 * Opens the playback device given by name, or the default if blank.
544 * Returns an empty Device on error.
546 Device openPlayback(const String &name, const std::nothrow_t&);
547 Device openPlayback(const char *name, const std::nothrow_t&);
549 /** Opens the default playback device. Returns an empty Device on error. */
550 Device openPlayback(const std::nothrow_t&);
554 enum class PlaybackName {
555 Basic = ALC_DEVICE_SPECIFIER,
556 Full = ALC_ALL_DEVICES_SPECIFIER
559 class ALURE_API Device {
560 MAKE_PIMPL(Device, DeviceImpl)
562 public:
563 /** Retrieves the device name as given by type. */
564 String getName(PlaybackName type=PlaybackName::Full) const;
565 /** Queries the existence of an ALC extension on this device. */
566 bool queryExtension(const String &name) const;
567 bool queryExtension(const char *name) const;
569 /** Retrieves the ALC version supported by this device. */
570 Version getALCVersion() const;
573 * Retrieves the EFX version supported by this device. If the ALC_EXT_EFX
574 * extension is unsupported, this will be 0.0.
576 Version getEFXVersion() const;
578 /** Retrieves the device's playback frequency, in hz. */
579 ALCuint getFrequency() const;
582 * Retrieves the maximum number of auxiliary source sends. If ALC_EXT_EFX
583 * is unsupported, this will be 0.
585 ALCuint getMaxAuxiliarySends() const;
588 * Enumerates available HRTF names. The names are sorted as OpenAL gives
589 * them, such that the index of a given name is the ID to use with
590 * ALC_HRTF_ID_SOFT.
592 * Requires the ALC_SOFT_HRTF extension.
594 Vector<String> enumerateHRTFNames() const;
597 * Retrieves whether HRTF is enabled on the device or not.
599 * Requires the ALC_SOFT_HRTF extension.
601 bool isHRTFEnabled() const;
604 * Retrieves the name of the HRTF currently being used by this device.
606 * Requires the ALC_SOFT_HRTF extension.
608 String getCurrentHRTF() const;
611 * Resets the device, using the specified attributes.
613 * Requires the ALC_SOFT_HRTF extension.
615 void reset(ArrayView<AttributePair> attributes);
618 * Creates a new Context on this device, using the specified attributes.
619 * Throws an exception if context creation fails.
621 Context createContext(ArrayView<AttributePair> attributes=ArrayView<AttributePair>());
623 * Creates a new Context on this device, using the specified attributes.
624 * Returns an empty Context if context creation fails.
626 Context createContext(ArrayView<AttributePair> attributes, const std::nothrow_t&);
627 Context createContext(const std::nothrow_t&);
630 * Pauses device processing, stopping updates for its contexts. Multiple
631 * calls are allowed but it is not reference counted, so the device will
632 * resume after one resumeDSP call.
634 * Requires the ALC_SOFT_pause_device extension.
636 void pauseDSP();
639 * Resumes device processing, restarting updates for its contexts. Multiple
640 * calls are allowed and will no-op.
642 void resumeDSP();
645 * Closes and frees the device. All previously-created contexts must first
646 * be destroyed.
648 void close();
652 enum class DistanceModel {
653 InverseClamped = AL_INVERSE_DISTANCE_CLAMPED,
654 LinearClamped = AL_LINEAR_DISTANCE_CLAMPED,
655 ExponentClamped = AL_EXPONENT_DISTANCE_CLAMPED,
656 Inverse = AL_INVERSE_DISTANCE,
657 Linear = AL_LINEAR_DISTANCE,
658 Exponent = AL_EXPONENT_DISTANCE,
659 None = AL_NONE,
662 class ALURE_API Context {
663 MAKE_PIMPL(Context, ContextImpl)
665 public:
666 /** Makes the specified context current for OpenAL operations. */
667 static void MakeCurrent(Context context);
668 /** Retrieves the current context used for OpenAL operations. */
669 static Context GetCurrent();
672 * Makes the specified context current for OpenAL operations on the calling
673 * thread only. Requires the ALC_EXT_thread_local_context extension on both
674 * the context's device and the DeviceManager.
676 static void MakeThreadCurrent(Context context);
677 /** Retrieves the thread-specific context used for OpenAL operations. */
678 static Context GetThreadCurrent();
681 * Destroys the context. The context must not be current when this is
682 * called.
684 void destroy();
686 /** Retrieves the Device this context was created from. */
687 Device getDevice();
689 void startBatch();
690 void endBatch();
693 * Retrieves a Listener instance for this context. Each context will only
694 * have one listener, which is automatically destroyed with the context.
696 Listener getListener();
699 * Sets a MessageHandler instance which will be used to provide certain
700 * messages back to the application. Only one handler may be set for a
701 * context at a time. The previously set handler will be returned.
703 SharedPtr<MessageHandler> setMessageHandler(SharedPtr<MessageHandler> handler);
705 /** Gets the currently-set message handler. */
706 SharedPtr<MessageHandler> getMessageHandler() const;
709 * Specifies the desired interval that the background thread will be woken
710 * up to process tasks, e.g. keeping streaming sources filled. An interval
711 * of 0 means the background thread will only be woken up manually with
712 * calls to update. The default is 0.
714 void setAsyncWakeInterval(std::chrono::milliseconds interval);
717 * Retrieves the current interval used for waking up the background thread.
719 std::chrono::milliseconds getAsyncWakeInterval() const;
721 // Functions below require the context to be current
724 * Creates a Decoder instance for the given audio file or resource name.
726 SharedPtr<Decoder> createDecoder(StringView name);
729 * Queries if the channel configuration and sample type are supported by
730 * the context.
732 bool isSupported(ChannelConfig channels, SampleType type) const;
735 * Queries the list of resamplers supported by the context. If the
736 * AL_SOFT_source_resampler extension is unsupported this will be an empty
737 * array, otherwise there will be at least one entry.
739 ArrayView<String> getAvailableResamplers();
741 * Queries the context's default resampler index. Be aware, if the
742 * AL_SOFT_source_resampler extension is unsupported the resampler list
743 * will be empty and this will resturn 0. If you try to access the
744 * resampler list with this index without the extension, undefined behavior
745 * will occur (accessing an out of bounds array index).
747 ALsizei getDefaultResamplerIndex() const;
750 * Creates and caches a Buffer for the given audio file or resource name.
751 * Multiple calls with the same name will return the same Buffer object.
752 * Cached buffers must be freed using removeBuffer before destroying the
753 * context. If the buffer can't be loaded it will throw an exception.
755 Buffer getBuffer(StringView name);
758 * Asynchronously prepares a cached Buffer for the given audio file or
759 * resource name. Multiple calls with the same name will return multiple
760 * SharedFutures for the same Buffer object. Once called, the buffer must
761 * be freed using removeBuffer before destroying the context, even if you
762 * never get the Buffer from the SharedFuture.
764 * The Buffer will be scheduled to load asynchronously, and the caller gets
765 * back a SharedFuture that can be checked later (or waited on) to get the
766 * actual Buffer when it's ready. The application must take care to handle
767 * exceptions from the SharedFuture in case an unrecoverable error ocurred
768 * during the load.
770 SharedFuture<Buffer> getBufferAsync(StringView name);
773 * Asynchronously prepares cached Buffers for the given audio file or
774 * resource names. Duplicate names and buffers already cached are ignored.
775 * Cached buffers must be freed using removeBuffer before destroying the
776 * context.
778 * The Buffer objects will be scheduled for loading asynchronously, and
779 * should be retrieved later when needed using getBufferAsync or getBuffer.
780 * Buffers that cannot be loaded, for example due to an unsupported format,
781 * will be ignored and a later call to getBuffer or getBufferAsync will
782 * throw an exception.
784 * Note that you should avoid trying to asynchronously cache more than 16
785 * buffers at a time. The internal ringbuffer used to communicate with the
786 * background thread can only hold 16 async load requests, and trying to
787 * add more will cause the call to stall until the background thread
788 * completes some loads for more to be filled in.
790 void precacheBuffersAsync(ArrayView<StringView> names);
793 * Creates and caches a Buffer using the given name. The name may alias an
794 * audio file, but it must not currently exist in the buffer cache.
796 Buffer createBufferFrom(StringView name, SharedPtr<Decoder> decoder);
799 * Asynchronously prepares a cached Buffer using the given name. The name
800 * may alias an audio file, but it must not currently exist in the buffer
801 * cache. Once called, the buffer must be freed using removeBuffer before
802 * destroying the context, even if you never get the Buffer from the
803 * SharedFuture.
805 * The Buffer will be scheduled to load asynchronously, and the caller gets
806 * back a SharedFuture that can be checked later (or waited on) to get the
807 * actual Buffer when it's ready. The application must take care to handle
808 * exceptions from the SharedFuture in case an unrecoverable error ocurred
809 * during the load. The decoder must not have its read or seek methods
810 * called while the buffer is not ready.
812 SharedFuture<Buffer> createBufferAsyncFrom(StringView name, SharedPtr<Decoder> decoder);
815 * Deletes the cached Buffer object for the given audio file or resource
816 * name. The buffer must not be in use by a Source.
818 void removeBuffer(StringView name);
820 * Deletes the given cached buffer. The buffer must not be in use by a
821 * Source.
823 void removeBuffer(Buffer buffer);
826 * Creates a new Source. There is no practical limit to the number of
827 * sources you may create. You must call Source::release when the source is
828 * no longer needed.
830 Source createSource();
832 AuxiliaryEffectSlot createAuxiliaryEffectSlot();
834 Effect createEffect();
836 SourceGroup createSourceGroup(StringView name);
837 SourceGroup getSourceGroup(StringView name);
839 /** Sets the doppler factor to apply to all source calculations. */
840 void setDopplerFactor(ALfloat factor);
843 * Sets the speed of sound propagation, in units per second, to calculate
844 * the doppler effect along with other distance-related time effects. The
845 * default is 343.3 units per second (a realistic speed assuming 1 meter
846 * per unit). If this is adjusted for a different unit scale,
847 * Listener::setMetersPerUnit should also be adjusted.
849 void setSpeedOfSound(ALfloat speed);
851 void setDistanceModel(DistanceModel model);
853 /** Updates the context and all sources belonging to this context. */
854 void update();
857 class ALURE_API Listener {
858 MAKE_PIMPL(Listener, ListenerImpl)
860 public:
861 /** Sets the "master" gain for all context output. */
862 void setGain(ALfloat gain);
865 * Specifies the listener's 3D position, velocity, and orientation
866 * together.
868 void set3DParameters(const Vector3 &position, const Vector3 &velocity, std::pair<Vector3,Vector3> orientation);
870 /** Specifies the listener's 3D position. */
871 void setPosition(ALfloat x, ALfloat y, ALfloat z);
872 void setPosition(const ALfloat *pos);
875 * Specifies the listener's 3D velocity, in units per second. As with
876 * OpenAL, this does not actually alter the listener's position, and
877 * instead just alters the pitch as determined by the doppler effect.
879 void setVelocity(ALfloat x, ALfloat y, ALfloat z);
880 void setVelocity(const ALfloat *vel);
883 * Specifies the listener's 3D orientation, using position-relative 'at'
884 * and 'up' direction vectors.
886 void setOrientation(ALfloat x1, ALfloat y1, ALfloat z1, ALfloat x2, ALfloat y2, ALfloat z2);
887 void setOrientation(const ALfloat *at, const ALfloat *up);
888 void setOrientation(const ALfloat *ori);
891 * Sets the number of meters per unit, used for various effects that rely
892 * on the distance in meters (including air absorption and initial reverb
893 * decay). If this is changed, it's strongly recommended to also set the
894 * speed of sound (e.g. context.setSpeedOfSound(343.3 / m_u) to maintain a
895 * realistic 343.3m/s for sound propagation).
897 void setMetersPerUnit(ALfloat m_u);
901 class ALURE_API Buffer {
902 MAKE_PIMPL(Buffer, BufferImpl)
904 public:
905 /** Retrieves the length of the buffer in sample frames. */
906 ALuint getLength() const;
908 /** Retrieves the buffer's frequency in hz. */
909 ALuint getFrequency() const;
911 /** Retrieves the buffer's sample configuration. */
912 ChannelConfig getChannelConfig() const;
914 /** Retrieves the buffer's sample type. */
915 SampleType getSampleType() const;
917 /** Retrieves the storage size used by the buffer, in bytes. */
918 ALuint getSize() const;
921 * Sets the buffer's loop points, used for looping sources. If the current
922 * context does not support the AL_SOFT_loop_points extension, start and
923 * end must be 0 and getLength() respectively. Otherwise, start must be
924 * less than end, and end must be less than or equal to getLength().
926 * The buffer must not be in use when this method is called.
928 * \param start The starting point, in sample frames (inclusive).
929 * \param end The ending point, in sample frames (exclusive).
931 void setLoopPoints(ALuint start, ALuint end);
933 /** Retrieves the current loop points as a [start,end) pair. */
934 std::pair<ALuint,ALuint> getLoopPoints() const;
937 * Retrieves the Source objects currently playing the buffer. Stopping the
938 * returned sources will allow the buffer to be removed from the context.
940 Vector<Source> getSources() const;
942 /** Retrieves the name the buffer was created with. */
943 const String &getName() const;
945 /** Queries if the buffer is in use and can't be removed. */
946 bool isInUse() const;
950 enum class Spatialize {
951 Off = AL_FALSE,
952 On = AL_TRUE,
953 Auto = 0x0002 /* AL_AUTO_SOFT */
956 class ALURE_API Source {
957 MAKE_PIMPL(Source, SourceImpl)
959 public:
961 * Plays the source using buffer. The same buffer may be played from
962 * multiple sources simultaneously.
964 void play(Buffer buffer);
966 * Plays the source by streaming audio from decoder. This will use
967 * queuesize buffers, each with updatelen sample frames. The given decoder
968 * must *NOT* have its read or seek methods called from elsewhere while in
969 * use.
971 void play(SharedPtr<Decoder> decoder, ALuint updatelen, ALuint queuesize);
972 /** Stops playback, releasing the buffer or decoder reference. */
973 void stop();
976 * Fades the source to the specified gain over the given duration, at which
977 * point playback will stop. This gain is in addition to the base gain, and
978 * must be greater than 0 and less than 1. The duration must also be
979 * greater than 0.
981 * Fading is updated during calls to \c Context::update, which should be
982 * called regularly (30 to 50 times per second) for the fading to be
983 * smooth.
985 void fadeOutToStop(ALfloat gain, std::chrono::milliseconds duration);
987 /** Pauses the source if it is playing. */
988 void pause();
990 /** Resumes the source if it is paused. */
991 void resume();
993 /** Specifies if the source is currently playing. */
994 bool isPlaying() const;
996 /** Specifies if the source is currently paused. */
997 bool isPaused() const;
1000 * Specifies the source's playback priority. Lowest priority sources will
1001 * be evicted first when higher priority sources are played.
1003 void setPriority(ALuint priority);
1004 /** Retrieves the source's priority. */
1005 ALuint getPriority() const;
1008 * Sets the source's offset, in sample frames. If the source is playing or
1009 * paused, it will go to that offset immediately, otherwise the source will
1010 * start at the specified offset the next time it's played.
1012 void setOffset(uint64_t offset);
1014 * Retrieves the source offset in sample frames and its latency in nano-
1015 * seconds. For streaming sources this will be the offset based on the
1016 * decoder's read position.
1018 * If the AL_SOFT_source_latency extension is unsupported, the latency will
1019 * be 0.
1021 std::pair<uint64_t,std::chrono::nanoseconds> getSampleOffsetLatency() const;
1022 uint64_t getSampleOffset() const { return std::get<0>(getSampleOffsetLatency()); }
1024 * Retrieves the source offset and latency in seconds. For streaming
1025 * sources this will be the offset based on the decoder's read position.
1027 * If the AL_SOFT_source_latency extension is unsupported, the latency will
1028 * be 0.
1030 std::pair<Seconds,Seconds> getSecOffsetLatency() const;
1031 Seconds getSecOffset() const { return std::get<0>(getSecOffsetLatency()); }
1034 * Specifies if the source should loop on the Buffer or Decoder object's
1035 * loop points.
1037 void setLooping(bool looping);
1038 bool getLooping() const;
1041 * Specifies a linear pitch shift base. A value of 1.0 is the default
1042 * normal speed.
1044 void setPitch(ALfloat pitch);
1045 ALfloat getPitch() const;
1048 * Specifies the base linear gain. A value of 1.0 is the default normal
1049 * volume.
1051 void setGain(ALfloat gain);
1052 ALfloat getGain() const;
1055 * Specifies the minimum and maximum gain. The source's gain is clamped to
1056 * this range after distance attenuation and cone attenuation are applied
1057 * to the gain base, although before the filter gain adjustements.
1059 void setGainRange(ALfloat mingain, ALfloat maxgain);
1060 std::pair<ALfloat,ALfloat> getGainRange() const;
1061 ALfloat getMinGain() const { return std::get<0>(getGainRange()); }
1062 ALfloat getMaxGain() const { return std::get<1>(getGainRange()); }
1065 * Specifies the reference distance and maximum distance the source will
1066 * use for the current distance model. For Clamped distance models, the
1067 * source's calculated distance is clamped to the specified range before
1068 * applying distance-related attenuation.
1070 * For all distance models, the reference distance is the distance at which
1071 * the source's volume will not have any extra attenuation (an effective
1072 * gain multiplier of 1).
1074 void setDistanceRange(ALfloat refdist, ALfloat maxdist);
1075 std::pair<ALfloat,ALfloat> getDistanceRange() const;
1076 ALfloat getReferenceDistance() const { return std::get<0>(getDistanceRange()); }
1077 ALfloat getMaxDistance() const { return std::get<1>(getDistanceRange()); }
1079 /** Specifies the source's 3D position, velocity, and direction together. */
1080 void set3DParameters(const Vector3 &position, const Vector3 &velocity, const Vector3 &direction);
1082 /** Specifies the source's 3D position, velocity, and orientation together. */
1083 void set3DParameters(const Vector3 &position, const Vector3 &velocity, std::pair<Vector3,Vector3> orientation);
1085 /** Specifies the source's 3D position. */
1086 void setPosition(ALfloat x, ALfloat y, ALfloat z);
1087 void setPosition(const ALfloat *pos);
1088 Vector3 getPosition() const;
1091 * Specifies the source's 3D velocity, in units per second. As with OpenAL,
1092 * this does not actually alter the source's position, and instead just
1093 * alters the pitch as determined by the doppler effect.
1095 void setVelocity(ALfloat x, ALfloat y, ALfloat z);
1096 void setVelocity(const ALfloat *vel);
1097 Vector3 getVelocity() const;
1100 * Specifies the source's 3D facing direction. Deprecated in favor of
1101 * setOrientation.
1103 void setDirection(ALfloat x, ALfloat y, ALfloat z);
1104 void setDirection(const ALfloat *dir);
1105 Vector3 getDirection() const;
1108 * Specifies the source's 3D orientation. Note: unlike the AL_EXT_BFORMAT
1109 * extension this property comes from, this also affects the facing
1110 * direction, superceding setDirection.
1112 void setOrientation(ALfloat x1, ALfloat y1, ALfloat z1, ALfloat x2, ALfloat y2, ALfloat z2);
1113 void setOrientation(const ALfloat *at, const ALfloat *up);
1114 void setOrientation(const ALfloat *ori);
1115 std::pair<Vector3,Vector3> getOrientation() const;
1118 * Specifies the source's cone angles, in degrees. The inner angle is the
1119 * area within which the listener will hear the source with no extra
1120 * attenuation, while the listener being outside of the outer angle will
1121 * hear the source attenuated according to the outer cone gains.
1123 void setConeAngles(ALfloat inner, ALfloat outer);
1124 std::pair<ALfloat,ALfloat> getConeAngles() const;
1125 ALfloat getInnerConeAngle() const { return std::get<0>(getConeAngles()); }
1126 ALfloat getOuterConeAngle() const { return std::get<1>(getConeAngles()); }
1129 * Specifies the linear gain multiplier when the listener is outside of the
1130 * source's outer cone area. The specified gain applies to all frequencies,
1131 * while gainhf applies extra attenuation to high frequencies.
1133 * \param gainhf has no effect without the ALC_EXT_EFX extension.
1135 void setOuterConeGains(ALfloat gain, ALfloat gainhf=1.0f);
1136 std::pair<ALfloat,ALfloat> getOuterConeGains() const;
1137 ALfloat getOuterConeGain() const { return std::get<0>(getOuterConeGains()); }
1138 ALfloat getOuterConeGainHF() const { return std::get<1>(getOuterConeGains()); }
1141 * Specifies the rolloff factors for the direct and send paths. This is
1142 * effectively a distance scaling relative to the reference distance. Note:
1143 * the room rolloff factor is 0 by default, disabling distance attenuation
1144 * for send paths. This is because the reverb engine will, by default,
1145 * apply a more realistic room decay based on the reverb decay time and
1146 * distance.
1148 void setRolloffFactors(ALfloat factor, ALfloat roomfactor=0.0f);
1149 std::pair<ALfloat,ALfloat> getRolloffFactors() const;
1150 ALfloat getRolloffFactor() const { return std::get<0>(getRolloffFactors()); }
1151 ALfloat getRoomRolloffFactor() const { return std::get<1>(getRolloffFactors()); }
1154 * Specifies the doppler factor for the doppler effect's pitch shift. This
1155 * effectively scales the source and listener velocities for the doppler
1156 * calculation.
1158 void setDopplerFactor(ALfloat factor);
1159 ALfloat getDopplerFactor() const;
1162 * Specifies if the source's position, velocity, and direction/orientation
1163 * are relative to the listener.
1165 void setRelative(bool relative);
1166 bool getRelative() const;
1169 * Specifies the source's radius. This causes the source to behave as if
1170 * every point within the spherical area emits sound.
1172 * Has no effect without the AL_EXT_SOURCE_RADIUS extension.
1174 void setRadius(ALfloat radius);
1175 ALfloat getRadius() const;
1178 * Specifies the left and right channel angles, in radians, when playing a
1179 * stereo buffer or stream. The angles go counter-clockwise, with 0 being
1180 * in front and positive values going left.
1182 * Has no effect without the AL_EXT_STEREO_ANGLES extension.
1184 void setStereoAngles(ALfloat leftAngle, ALfloat rightAngle);
1185 std::pair<ALfloat,ALfloat> getStereoAngles() const;
1188 * Specifies if the source always has 3D spatialization features (On),
1189 * never has 3D spatialization features (Off), or if spatialization is
1190 * enabled based on playing a mono sound or not (Auto, default).
1192 * Has no effect without the AL_SOFT_source_spatialize extension.
1194 void set3DSpatialize(Spatialize spatialize);
1195 Spatialize get3DSpatialize() const;
1197 void setResamplerIndex(ALsizei index);
1198 ALsizei getResamplerIndex() const;
1201 * Specifies a multiplier for the amount of atmospheric high-frequency
1202 * absorption, ranging from 0 to 10. A factor of 1 results in a nominal
1203 * -0.05dB per meter, with higher values simulating foggy air and lower
1204 * values simulating dryer air. The default is 0.
1206 void setAirAbsorptionFactor(ALfloat factor);
1207 ALfloat getAirAbsorptionFactor() const;
1209 void setGainAuto(bool directhf, bool send, bool sendhf);
1210 std::tuple<bool,bool,bool> getGainAuto() const;
1211 bool getDirectGainHFAuto() const { return std::get<0>(getGainAuto()); }
1212 bool getSendGainAuto() const { return std::get<1>(getGainAuto()); }
1213 bool getSendGainHFAuto() const { return std::get<2>(getGainAuto()); }
1215 /** Sets the filter properties on the direct path signal. */
1216 void setDirectFilter(const FilterParams &filter);
1218 * Sets the filter properties on the given send path signal. Any auxiliary
1219 * effect slot on the send path remains in place.
1221 void setSendFilter(ALuint send, const FilterParams &filter);
1223 * Connects the effect slot to the given send path. Any filter properties
1224 * on the send path remain as they were.
1226 void setAuxiliarySend(AuxiliaryEffectSlot slot, ALuint send);
1228 * Connects the effect slot to the given send path, using the filter
1229 * properties.
1231 void setAuxiliarySendFilter(AuxiliaryEffectSlot slot, ALuint send, const FilterParams &filter);
1234 * Releases the source, stopping playback, releasing resources, and
1235 * returning it to the system.
1237 void release();
1241 class ALURE_API SourceGroup {
1242 MAKE_PIMPL(SourceGroup, SourceGroupImpl)
1244 public:
1245 /** Retrieves the associated name of the source group. */
1246 const String &getName() const;
1249 * Adds source to the source group. A source may only be part of one group
1250 * at a time, and will automatically be removed from its current group as
1251 * needed.
1253 void addSource(Source source);
1254 /** Removes source from the source group. */
1255 void removeSource(Source source);
1257 /** Adds a list of sources to the group at once. */
1258 void addSources(ArrayView<Source> sources);
1259 /** Removes a list of sources from the source group. */
1260 void removeSources(ArrayView<Source> sources);
1263 * Adds group as a subgroup of the source group. This method will throw an
1264 * exception if group is being added to a group it has as a sub-group (i.e.
1265 * it would create a circular sub-group chain).
1267 void addSubGroup(SourceGroup group);
1268 /** Removes group from the source group. */
1269 void removeSubGroup(SourceGroup group);
1271 /** Returns the list of sources currently in the group. */
1272 Vector<Source> getSources() const;
1274 /** Returns the list of subgroups currently in the group. */
1275 Vector<SourceGroup> getSubGroups() const;
1277 /** Sets the source group gain, which accumulates with its sources. */
1278 void setGain(ALfloat gain);
1279 /** Gets the source group gain. */
1280 ALfloat getGain() const;
1282 /** Sets the source group pitch, which accumulates with its sources. */
1283 void setPitch(ALfloat pitch);
1284 /** Gets the source group pitch. */
1285 ALfloat getPitch() const;
1288 * Pauses all currently-playing sources that are under this group,
1289 * including sub-groups.
1291 void pauseAll() const;
1293 * Resumes all paused sources that are under this group, including
1294 * sub-groups.
1296 void resumeAll() const;
1298 /** Stops all sources that are under this group, including sub-groups. */
1299 void stopAll() const;
1302 * Releases the source group, removing all sources from it before being
1303 * freed.
1305 void release();
1309 struct SourceSend {
1310 Source mSource;
1311 ALuint mSend;
1314 class ALURE_API AuxiliaryEffectSlot {
1315 MAKE_PIMPL(AuxiliaryEffectSlot, AuxiliaryEffectSlotImpl)
1317 public:
1318 void setGain(ALfloat gain);
1320 * If set to true, the reverb effect will automatically apply adjustments
1321 * to the source's send slot gains based on the effect properties.
1323 * Has no effect when using non-reverb effects. Default is true.
1325 void setSendAuto(bool sendauto);
1328 * Updates the effect slot with a new effect. The given effect object may
1329 * be altered or destroyed without affecting the effect slot.
1331 void applyEffect(Effect effect);
1334 * Releases the effect slot, returning it to the system. It must not be in
1335 * use by a source.
1337 void release();
1340 * Retrieves each Source object and its pairing send this effect slot is
1341 * set on. Setting a different (or null) effect slot on each source's given
1342 * send will allow the effect slot to be released.
1344 Vector<SourceSend> getSourceSends() const;
1346 /** Determines if the effect slot is in use by a source. */
1347 bool isInUse() const;
1351 class ALURE_API Effect {
1352 MAKE_PIMPL(Effect, EffectImpl)
1354 public:
1356 * Updates the effect with the specified reverb properties. If the
1357 * EAXReverb effect is not supported, it will automatically attempt to
1358 * downgrade to the Standard Reverb effect.
1360 void setReverbProperties(const EFXEAXREVERBPROPERTIES &props);
1362 void destroy();
1367 * Audio decoder interface. Applications may derive from this, implementing the
1368 * necessary methods, and use it in places the API wants a Decoder object.
1370 class ALURE_API Decoder {
1371 public:
1372 virtual ~Decoder();
1374 /** Retrieves the sample frequency, in hz, of the audio being decoded. */
1375 virtual ALuint getFrequency() const = 0;
1376 /** Retrieves the channel configuration of the audio being decoded. */
1377 virtual ChannelConfig getChannelConfig() const = 0;
1378 /** Retrieves the sample type of the audio being decoded. */
1379 virtual SampleType getSampleType() const = 0;
1382 * Retrieves the total length of the audio, in sample frames. If unknown,
1383 * returns 0. Note that if the returned length is 0, the decoder may not be
1384 * used to load a Buffer.
1386 virtual uint64_t getLength() const = 0;
1388 * Seek to pos, specified in sample frames. Returns true if the seek was
1389 * successful.
1391 virtual bool seek(uint64_t pos) = 0;
1394 * Retrieves the loop points, in sample frames, as a [start,end) pair. If
1395 * start >= end, use all available data.
1397 virtual std::pair<uint64_t,uint64_t> getLoopPoints() const = 0;
1400 * Decodes count sample frames, writing them to ptr, and returns the number
1401 * of sample frames written. Returning less than the requested count
1402 * indicates the end of the audio.
1404 virtual ALuint read(ALvoid *ptr, ALuint count) = 0;
1408 * Audio decoder factory interface. Applications may derive from this,
1409 * implementing the necessary methods, and use it in places the API wants a
1410 * DecoderFactory object.
1412 class ALURE_API DecoderFactory {
1413 public:
1414 virtual ~DecoderFactory();
1417 * Creates and returns a Decoder instance for the given resource file. If
1418 * the decoder needs to retain the file handle for reading as-needed, it
1419 * should move the UniquePtr to internal storage.
1421 * \return nullptr if a decoder can't be created from the file.
1423 virtual SharedPtr<Decoder> createDecoder(UniquePtr<std::istream> &file) = 0;
1427 * Registers a decoder factory for decoding audio. Registered factories are
1428 * used in lexicographical order, e.g. if Factory1 is registered with name1 and
1429 * Factory2 is registered with name2, Factory1 will be used before Factory2 if
1430 * name1 < name2. Internal decoder factories are always used after registered
1431 * ones.
1433 * Alure retains a reference to the DecoderFactory instance and will release it
1434 * (destructing the object) when the library unloads.
1436 * \param name A unique name identifying this decoder factory.
1437 * \param factory A DecoderFactory instance used to create Decoder instances.
1439 ALURE_API void RegisterDecoder(StringView name, UniquePtr<DecoderFactory> factory);
1442 * Unregisters a decoder factory by name. Alure returns the instance back to
1443 * the application.
1445 * \param name The unique name identifying a previously-registered decoder
1446 * factory.
1448 * \return The unregistered decoder factory instance, or 0 (nullptr) if a
1449 * decoder factory with the given name doesn't exist.
1451 ALURE_API UniquePtr<DecoderFactory> UnregisterDecoder(StringView name);
1455 * A file I/O factory interface. Applications may derive from this and set an
1456 * instance to be used by the audio decoders. By default, the library uses
1457 * standard I/O.
1459 class ALURE_API FileIOFactory {
1460 public:
1462 * Sets the factory instance to be used by the audio decoders. If a
1463 * previous factory was set, it's returned to the application. Passing in a
1464 * nullptr reverts to the default.
1466 static UniquePtr<FileIOFactory> set(UniquePtr<FileIOFactory> factory);
1469 * Gets the current FileIOFactory instance being used by the audio
1470 * decoders.
1472 static FileIOFactory &get();
1474 virtual ~FileIOFactory();
1476 /** Opens a read-only binary file for the given name. */
1477 virtual UniquePtr<std::istream> openFile(const String &name) = 0;
1482 * A message handler interface. Applications may derive from this and set an
1483 * instance on a context to receive messages. The base methods are no-ops, so
1484 * derived classes only need to implement methods for relevant messages.
1486 * It's recommended that applications mark their handler methods using the
1487 * override keyword, to ensure they're properly overriding the base methods in
1488 * case they change.
1490 class ALURE_API MessageHandler {
1491 public:
1492 virtual ~MessageHandler();
1495 * Called when the given device has been disconnected and is no longer
1496 * usable for output. As per the ALC_EXT_disconnect specification,
1497 * disconnected devices remain valid, however all playing sources are
1498 * automatically stopped, any sources that are attempted to play will
1499 * immediately stop, and new contexts may not be created on the device.
1501 * Note that connection status is checked during Context::update calls, so
1502 * that method must be called regularly to be notified when a device is
1503 * disconnected. This method may not be called if the device lacks support
1504 * for the ALC_EXT_disconnect extension.
1506 virtual void deviceDisconnected(Device device);
1509 * Called when the given source reaches the end of the buffer or stream.
1511 * Sources that stopped automatically will be detected upon a call to
1512 * Context::update.
1514 virtual void sourceStopped(Source source);
1517 * Called when the given source was forced to stop. This can be because
1518 * either there were no more system sources and a higher-priority source
1519 * preempted it, or it's part of a SourceGroup (or sub-group thereof) that
1520 * had its SourceGroup::stopAll method called.
1522 virtual void sourceForceStopped(Source source);
1525 * Called when a new buffer is about to be created and loaded. May be
1526 * called asynchronously for buffers being loaded asynchronously.
1528 * \param name The resource name, as passed to Context::getBuffer.
1529 * \param channels Channel configuration of the given audio data.
1530 * \param type Sample type of the given audio data.
1531 * \param samplerate Sample rate of the given audio data.
1532 * \param data The audio data that is about to be fed to the OpenAL buffer.
1534 virtual void bufferLoading(StringView name, ChannelConfig channels, SampleType type, ALuint samplerate, ArrayView<ALbyte> data);
1537 * Called when a resource isn't found, allowing the app to substitute in a
1538 * different resource. For buffers created with Context::getBuffer or
1539 * Context::getBufferAsync, the original name will still be used for the
1540 * cache map so the app doesn't have to keep track of substituted resource
1541 * names.
1543 * This will be called again if the new name isn't found.
1545 * \param name The resource name that was not found.
1546 * \return The replacement resource name to use instead. Returning an empty
1547 * string means to stop trying.
1549 virtual String resourceNotFound(StringView name);
1552 #undef MAKE_PIMPL
1554 } // namespace alure
1556 #endif /* AL_ALURE2_H */