1 #ifndef ALC_BACKENDS_BASE_H
2 #define ALC_BACKENDS_BASE_H
11 #include "core/device.h"
12 #include "core/except.h"
15 using uint = unsigned int;
18 std::chrono::nanoseconds ClockTime;
19 std::chrono::nanoseconds Latency;
23 virtual void open(const char *name) = 0;
26 virtual void start() = 0;
27 virtual void stop() = 0;
29 virtual void captureSamples(al::byte *buffer, uint samples);
30 virtual uint availableSamples();
32 virtual ClockLatency getClockLatency();
34 DeviceBase *const mDevice;
36 BackendBase(DeviceBase *device) noexcept : mDevice{device} { }
37 virtual ~BackendBase() = default;
40 /** Sets the default channel order used by most non-WaveFormatEx-based APIs. */
41 void setDefaultChannelOrder();
42 /** Sets the default channel order used by WaveFormatEx. */
43 void setDefaultWFXChannelOrder();
45 using BackendPtr = std::unique_ptr<BackendBase>;
47 enum class BackendType {
53 /* Helper to get the current clock time from the device's ClockBase, and
54 * SamplesDone converted from the sample rate.
56 inline std::chrono::nanoseconds GetDeviceClockTime(DeviceBase *device)
58 using std::chrono::seconds;
59 using std::chrono::nanoseconds;
61 auto ns = nanoseconds{seconds{device->SamplesDone}} / device->Frequency;
62 return device->ClockBase + ns;
65 /* Helper to get the device latency from the backend, including any fixed
66 * latency from post-processing.
68 inline ClockLatency GetClockLatency(DeviceBase *device, BackendBase *backend)
70 ClockLatency ret{backend->getClockLatency()};
71 ret.Latency += device->FixedLatency;
76 struct BackendFactory {
77 virtual bool init() = 0;
79 virtual bool querySupport(BackendType type) = 0;
81 virtual std::string probe(BackendType type) = 0;
83 virtual BackendPtr createBackend(DeviceBase *device, BackendType type) = 0;
86 virtual ~BackendFactory() = default;
91 enum class backend_error {
97 class backend_exception final : public base_exception {
98 backend_error mErrorCode;
101 #ifdef __USE_MINGW_ANSI_STDIO
102 [[gnu::format(gnu_printf, 3, 4)]]
104 [[gnu::format(printf, 3, 4)]]
106 backend_exception(backend_error code, const char *msg, ...);
107 ~backend_exception() override;
109 backend_error errorCode() const noexcept { return mErrorCode; }
114 #endif /* ALC_BACKENDS_BASE_H */