XT-Audio
XtDevice.hpp
Go to the documentation of this file.
1 #ifndef XT_API_DEVICE_HPP
2 #define XT_API_DEVICE_HPP
3 
6 #include <xt/cpp/Core.hpp>
7 #include <xt/cpp/Error.hpp>
8 #include <xt/api/Structs.hpp>
9 #include <xt/api/XtStream.hpp>
10 
11 #include <memory>
12 #include <vector>
13 #include <optional>
16 namespace Xt {
17 
18 using namespace Detail;
19 
20 class Device final
21 {
23  friend class Service;
25  XtDevice* const _d;
26  Device(XtDevice* d): _d(d) { }
27 public:
28  ~Device();
29  void ShowControlPanel();
30  void* GetHandle() const;
31  std::optional<Mix> GetMix() const;
32  int32_t GetChannelCount(bool output) const;
33  bool SupportsAccess(bool interleaved) const;
34  bool SupportsFormat(Format const& format) const;
35  BufferSize GetBufferSize(Format const& format) const;
36  std::string GetChannelName(bool output, int32_t index) const;
37  std::unique_ptr<Stream> OpenStream(DeviceStreamParams const& params, void* user);
38 };
39 
40 inline
42 { Detail::HandleDestroy(XtDeviceDestroy, _d); }
43 inline void
45 { Detail::HandleError(XtDeviceShowControlPanel(_d)); }
46 inline void*
48 { return Detail::HandleAssert(XtDeviceGetHandle(_d)); }
49 
50 inline int32_t
51 Device::GetChannelCount(bool output) const
52 {
53  int32_t count;
54  Detail::HandleError(XtDeviceGetChannelCount(_d, output, &count));
55  return count;
56 }
57 
58 inline BufferSize
59 Device::GetBufferSize(Format const& format) const
60 {
61  BufferSize result;
62  auto coreSize = reinterpret_cast<XtBufferSize*>(&result);
63  auto coreFormat = reinterpret_cast<XtFormat const*>(&format);
64  Detail::HandleError(XtDeviceGetBufferSize(_d, coreFormat, coreSize));
65  return result;
66 }
67 
68 inline bool
69 Device::SupportsFormat(Format const& format) const
70 {
71  XtBool supports;
72  auto coreFormat = reinterpret_cast<XtFormat const*>(&format);
73  Detail::HandleError(XtDeviceSupportsFormat(_d, coreFormat, &supports));
74  return supports != XtFalse;
75 }
76 
77 inline bool
78 Device::SupportsAccess(bool interleaved) const
79 {
80  XtBool supports;
81  Detail::HandleError(XtDeviceSupportsAccess(_d, interleaved, &supports));
82  return supports != XtFalse;
83 }
84 
85 inline std::optional<Mix>
87 {
88  Mix mix;
89  XtBool valid;
90  auto coreMix = reinterpret_cast<XtMix*>(&mix);
91  Detail::HandleError(XtDeviceGetMix(_d, &valid, coreMix));
92  return valid? std::optional<Mix>(mix): std::optional<Mix>(std::nullopt);
93 }
94 
95 inline std::string
96 Device::GetChannelName(bool output, int32_t index) const
97 {
98  int32_t size = 0;
99  Detail::HandleError(XtDeviceGetChannelName(_d, output, index, nullptr, &size));
100  std::vector<char> buffer(static_cast<size_t>(size));
101  Detail::HandleError(XtDeviceGetChannelName(_d, output, index, buffer.data(), &size));
102  return std::string(buffer.data());
103 }
104 
105 inline std::unique_ptr<Stream>
106 Device::OpenStream(DeviceStreamParams const& params, void* user)
107 {
108  XtStream* stream;
109  XtDeviceStreamParams coreParams = { 0 };
110  coreParams.bufferSize = params.bufferSize;
111  coreParams.stream.onBuffer = &Detail::ForwardOnBuffer;
112  coreParams.stream.interleaved = params.stream.interleaved;
113  coreParams.format = *reinterpret_cast<XtFormat const*>(&params.format);
114  coreParams.stream.onXRun = params.stream.onXRun == nullptr? nullptr: &Detail::ForwardOnXRun;
115  coreParams.stream.onRunning = params.stream.onRunning == nullptr? nullptr: &Detail::ForwardOnRunning;
116  std::unique_ptr<Stream> result(new Stream(params.stream, user));
117  Detail::HandleError(XtDeviceOpenStream(_d, &coreParams, result.get(), &stream));
118  result->_s = stream;
119  return result;
120 }
121 
122 } // namespace Xt
123 #endif // XT_API_DEVICE_HPP
int32_t GetChannelCount(bool output) const
Definition: XtDevice.hpp:51
void ShowControlPanel()
Definition: XtDevice.hpp:44
bool SupportsFormat(Format const &format) const
Definition: XtDevice.hpp:69
Definition: Structs.hpp:85
~Device()
Definition: XtDevice.hpp:41
Definition: Structs.hpp:50
Format format
Definition: Structs.hpp:108
Definition: Structs.hpp:105
OnXRun onXRun
Definition: Structs.hpp:98
std::unique_ptr< Stream > OpenStream(DeviceStreamParams const &params, void *user)
Definition: XtDevice.hpp:106
Definition: XtService.hpp:23
BufferSize GetBufferSize(Format const &format) const
Definition: XtDevice.hpp:59
std::optional< Mix > GetMix() const
Definition: XtDevice.hpp:86
StreamParams stream
Definition: Structs.hpp:107
bool interleaved
Definition: Structs.hpp:96
void * GetHandle() const
Definition: XtDevice.hpp:47
double bufferSize
Definition: Structs.hpp:109
bool SupportsAccess(bool interleaved) const
Definition: XtDevice.hpp:78
Definition: Callbacks.hpp:10
OnRunning onRunning
Definition: Structs.hpp:99
Definition: XtStream.hpp:18
Definition: XtDevice.hpp:20
Definition: Structs.hpp:65
std::string GetChannelName(bool output, int32_t index) const
Definition: XtDevice.hpp:96