Handle 3D7.1 as a separate channel configuration
[openal-soft.git] / alc / backends / base.h
blob65bc636b3f4a4cdf8afe9ed8f69a06e1bc67bacb
1 #ifndef ALC_BACKENDS_BASE_H
2 #define ALC_BACKENDS_BASE_H
4 #include <chrono>
5 #include <cstdarg>
6 #include <memory>
7 #include <ratio>
8 #include <string>
10 #include "albyte.h"
11 #include "core/device.h"
12 #include "core/except.h"
15 using uint = unsigned int;
17 struct ClockLatency {
18 std::chrono::nanoseconds ClockTime;
19 std::chrono::nanoseconds Latency;
22 struct BackendBase {
23 virtual void open(const char *name) = 0;
25 virtual bool reset();
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;
39 protected:
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 {
48 Playback,
49 Capture
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;
72 return ret;
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;
85 protected:
86 virtual ~BackendFactory() = default;
89 namespace al {
91 enum class backend_error {
92 NoDevice,
93 DeviceError,
94 OutOfMemory
97 class backend_exception final : public base_exception {
98 backend_error mErrorCode;
100 public:
101 #ifdef __USE_MINGW_ANSI_STDIO
102 [[gnu::format(gnu_printf, 3, 4)]]
103 #else
104 [[gnu::format(printf, 3, 4)]]
105 #endif
106 backend_exception(backend_error code, const char *msg, ...) : mErrorCode{code}
108 std::va_list args;
109 va_start(args, msg);
110 setMessage(msg, args);
111 va_end(args);
113 backend_error errorCode() const noexcept { return mErrorCode; }
116 } // namespace al
118 #endif /* ALC_BACKENDS_BASE_H */