Torque3D/Engine/lib/openal-soft/alc/backends/base.h

126 lines
3.3 KiB
C
Raw Normal View History

#ifndef ALC_BACKENDS_BASE_H
#define ALC_BACKENDS_BASE_H
2016-10-22 09:22:33 +10:00
#include <chrono>
#include <cstdarg>
2024-06-30 14:35:57 -05:00
#include <cstddef>
#include <memory>
#include <string>
2024-06-30 14:35:57 -05:00
#include <string_view>
#include <vector>
2016-10-22 09:22:33 +10:00
#include "alc/events.h"
#include "core/device.h"
#include "core/except.h"
#include "fmt/core.h"
2016-10-22 09:22:33 +10:00
using uint = unsigned int;
2016-10-22 09:22:33 +10:00
struct ClockLatency {
std::chrono::nanoseconds ClockTime;
std::chrono::nanoseconds Latency;
};
2016-10-22 09:22:33 +10:00
struct BackendBase {
2024-06-30 14:35:57 -05:00
virtual void open(std::string_view name) = 0;
2016-10-22 09:22:33 +10:00
virtual bool reset();
virtual void start() = 0;
virtual void stop() = 0;
2016-10-22 09:22:33 +10:00
2024-06-30 14:35:57 -05:00
virtual void captureSamples(std::byte *buffer, uint samples);
virtual uint availableSamples();
2016-10-22 09:22:33 +10:00
virtual ClockLatency getClockLatency();
2016-10-22 09:22:33 +10:00
DeviceBase *const mDevice;
std::string mDeviceName;
2016-10-22 09:22:33 +10:00
2024-06-30 14:35:57 -05:00
BackendBase() = delete;
BackendBase(const BackendBase&) = delete;
BackendBase(BackendBase&&) = delete;
explicit BackendBase(DeviceBase *device) noexcept : mDevice{device} { }
virtual ~BackendBase() = default;
2016-10-22 09:22:33 +10:00
2024-06-30 14:35:57 -05:00
void operator=(const BackendBase&) = delete;
void operator=(BackendBase&&) = delete;
protected:
/** Sets the default channel order used by most non-WaveFormatEx-based APIs. */
2024-06-30 14:35:57 -05:00
void setDefaultChannelOrder() const;
/** Sets the default channel order used by WaveFormatEx. */
2024-06-30 14:35:57 -05:00
void setDefaultWFXChannelOrder() const;
};
using BackendPtr = std::unique_ptr<BackendBase>;
2016-10-22 09:22:33 +10:00
enum class BackendType {
Playback,
Capture
};
2016-10-22 09:22:33 +10:00
/* Helper to get the device latency from the backend, including any fixed
* latency from post-processing.
*/
inline ClockLatency GetClockLatency(DeviceBase *device, BackendBase *backend)
{
ClockLatency ret{backend->getClockLatency()};
ret.Latency += device->FixedLatency;
return ret;
}
2016-10-22 09:22:33 +10:00
struct BackendFactory {
2024-06-30 14:35:57 -05:00
BackendFactory() = default;
BackendFactory(const BackendFactory&) = delete;
BackendFactory(BackendFactory&&) = delete;
virtual ~BackendFactory() = default;
2016-10-22 09:22:33 +10:00
2024-06-30 14:35:57 -05:00
void operator=(const BackendFactory&) = delete;
void operator=(BackendFactory&&) = delete;
2016-10-22 09:22:33 +10:00
2024-06-30 14:35:57 -05:00
virtual auto init() -> bool = 0;
2016-10-22 09:22:33 +10:00
2024-06-30 14:35:57 -05:00
virtual auto querySupport(BackendType type) -> bool = 0;
2016-10-22 09:22:33 +10:00
2024-06-30 14:35:57 -05:00
virtual auto queryEventSupport(alc::EventType, BackendType) -> alc::EventSupport
{ return alc::EventSupport::NoSupport; }
virtual auto enumerate(BackendType type) -> std::vector<std::string> = 0;
virtual auto createBackend(DeviceBase *device, BackendType type) -> BackendPtr = 0;
};
2016-10-22 09:22:33 +10:00
namespace al {
2016-10-22 09:22:33 +10:00
enum class backend_error {
NoDevice,
DeviceError,
OutOfMemory
2016-10-22 09:22:33 +10:00
};
class backend_exception final : public base_exception {
backend_error mErrorCode;
static auto make_string(fmt::string_view fmt, fmt::format_args args) -> std::string;
public:
template<typename ...Args>
backend_exception(backend_error code, fmt::format_string<Args...> fmt, Args&& ...args)
: base_exception{make_string(fmt, fmt::make_format_args(args...))}, mErrorCode{code}
{ }
backend_exception(const backend_exception&) = default;
backend_exception(backend_exception&&) = default;
~backend_exception() override;
backend_exception& operator=(const backend_exception&) = default;
backend_exception& operator=(backend_exception&&) = default;
2024-06-30 14:35:57 -05:00
[[nodiscard]] auto errorCode() const noexcept -> backend_error { return mErrorCode; }
};
2016-10-22 09:22:33 +10:00
} // namespace al
2016-10-22 09:22:33 +10:00
#endif /* ALC_BACKENDS_BASE_H */