axmol/thirdparty/openal/alc/backends/base.h

119 lines
2.7 KiB
C
Raw Normal View History

#ifndef ALC_BACKENDS_BASE_H
#define ALC_BACKENDS_BASE_H
#include <chrono>
2021-05-14 10:15:42 +08:00
#include <cstdarg>
#include <memory>
2021-05-14 10:15:42 +08:00
#include <ratio>
#include <string>
#include "albyte.h"
2021-05-14 10:15:42 +08:00
#include "core/device.h"
#include "core/except.h"
using uint = unsigned int;
struct ClockLatency {
std::chrono::nanoseconds ClockTime;
std::chrono::nanoseconds Latency;
};
struct BackendBase {
virtual void open(const char *name) = 0;
virtual bool reset();
virtual void start() = 0;
virtual void stop() = 0;
virtual void captureSamples(al::byte *buffer, uint samples);
virtual uint availableSamples();
virtual ClockLatency getClockLatency();
2021-05-14 10:15:42 +08:00
DeviceBase *const mDevice;
2021-05-14 10:15:42 +08:00
BackendBase(DeviceBase *device) noexcept : mDevice{device} { }
virtual ~BackendBase() = default;
protected:
/** Sets the default channel order used by most non-WaveFormatEx-based APIs. */
void setDefaultChannelOrder();
/** Sets the default channel order used by WaveFormatEx. */
void setDefaultWFXChannelOrder();
};
using BackendPtr = std::unique_ptr<BackendBase>;
enum class BackendType {
Playback,
Capture
};
/* Helper to get the current clock time from the device's ClockBase, and
* SamplesDone converted from the sample rate.
*/
2021-05-14 10:15:42 +08:00
inline std::chrono::nanoseconds GetDeviceClockTime(DeviceBase *device)
{
using std::chrono::seconds;
using std::chrono::nanoseconds;
auto ns = nanoseconds{seconds{device->SamplesDone}} / device->Frequency;
return device->ClockBase + ns;
}
/* Helper to get the device latency from the backend, including any fixed
* latency from post-processing.
*/
2022-04-25 12:02:45 +08:00
inline ClockLatency GetClockLatency(DeviceBase *device, BackendBase *backend)
{
ClockLatency ret{backend->getClockLatency()};
ret.Latency += device->FixedLatency;
return ret;
}
struct BackendFactory {
virtual bool init() = 0;
virtual bool querySupport(BackendType type) = 0;
virtual std::string probe(BackendType type) = 0;
2021-05-14 10:15:42 +08:00
virtual BackendPtr createBackend(DeviceBase *device, BackendType type) = 0;
protected:
virtual ~BackendFactory() = default;
};
namespace al {
enum class backend_error {
NoDevice,
DeviceError,
OutOfMemory
};
class backend_exception final : public base_exception {
backend_error mErrorCode;
public:
2021-05-14 10:15:42 +08:00
#ifdef __USE_MINGW_ANSI_STDIO
[[gnu::format(gnu_printf, 3, 4)]]
#else
[[gnu::format(printf, 3, 4)]]
2021-05-14 10:15:42 +08:00
#endif
backend_exception(backend_error code, const char *msg, ...) : mErrorCode{code}
{
std::va_list args;
va_start(args, msg);
setMessage(msg, args);
va_end(args);
}
backend_error errorCode() const noexcept { return mErrorCode; }
};
} // namespace al
#endif /* ALC_BACKENDS_BASE_H */