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

124 lines
2.8 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>
#include <memory>
#include <ratio>
#include <string>
2016-10-22 09:22:33 +10:00
#include "albyte.h"
#include "core/device.h"
#include "core/except.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 {
virtual void open(const char *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
virtual void captureSamples(al::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;
2016-10-22 09:22:33 +10:00
BackendBase(DeviceBase *device) noexcept : mDevice{device} { }
virtual ~BackendBase() = default;
2016-10-22 09:22:33 +10:00
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();
2016-10-22 09:22:33 +10:00
#ifdef _WIN32
/** Sets the channel order given the WaveFormatEx mask. */
void setChannelOrderFromWFXMask(uint chanmask);
#endif
};
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 current clock time from the device's ClockBase, and
* SamplesDone converted from the sample rate.
*/
inline std::chrono::nanoseconds GetDeviceClockTime(DeviceBase *device)
{
using std::chrono::seconds;
using std::chrono::nanoseconds;
2016-10-22 09:22:33 +10:00
auto ns = nanoseconds{seconds{device->SamplesDone}} / device->Frequency;
return device->ClockBase + ns;
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 {
virtual bool init() = 0;
2016-10-22 09:22:33 +10:00
virtual bool querySupport(BackendType type) = 0;
2016-10-22 09:22:33 +10:00
virtual std::string probe(BackendType type) = 0;
2016-10-22 09:22:33 +10:00
virtual BackendPtr createBackend(DeviceBase *device, BackendType type) = 0;
2016-10-22 09:22:33 +10:00
protected:
virtual ~BackendFactory() = default;
};
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;
public:
#ifdef __USE_MINGW_ANSI_STDIO
[[gnu::format(gnu_printf, 3, 4)]]
#else
[[gnu::format(printf, 3, 4)]]
#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; }
};
2016-10-22 09:22:33 +10:00
} // namespace al
2016-10-22 09:22:33 +10:00
#endif /* ALC_BACKENDS_BASE_H */