3#include "debugtrace.hpp"
15#include <pybind11/pybind11.h>
17using namespace std::literals::chrono_literals;
21namespace py = pybind11;
32template <
typename T,
bool copy = false>
41 py::str dummyDataOwner;
50 return py::array_t<T>(
51 py::array::ShapeContainer({d.nframes, d.nchannels}),
53 py::array::StridesContainer(
55 sizeof(T) * d.nframes}),
57 reinterpret_cast<T *
>(
58 const_cast<DaqData &
>(d).raw_ptr()),
65template <
typename T,
bool copy = false>
74 py::str dummyDataOwner;
83 return py::array_t<T>(
84 py::array::ShapeContainer({d.nframes, d.nchannels}),
86 py::array::StridesContainer(
88 sizeof(T) * d.nframes}),
90 reinterpret_cast<T *
>(
91 const_cast<DaqData &
>(d).raw_ptr()),
107 py::function cb, reset_callback;
125 py::gil_scoped_release release;
132 py::gil_scoped_release release;
143 py::gil_scoped_acquire acquire;
147 reset_callback(py::none());
149 }
catch (py::error_already_set &e) {
150 cerr <<
"*************** Error calling reset callback!\n";
151 cerr << e.what() << endl;
152 cerr <<
"*************** \n";
157 }
catch (std::exception &e) {
158 cerr <<
"Caught unknown exception in reset callback:" << e.what() << endl;
174 py::gil_scoped_acquire acquire;
177 case (DataType::dtype_int8): {
178 bool_val = cb(getPyArrayNoCpy<int8_t>(d));
180 case (DataType::dtype_int16): {
181 bool_val = cb(getPyArrayNoCpy<int16_t>(d));
183 case (DataType::dtype_int32): {
184 bool_val = cb(getPyArrayNoCpy<int32_t>(d));
186 case (DataType::dtype_fl32): {
187 bool_val = cb(getPyArrayNoCpy<float>(d));
189 case (DataType::dtype_fl64): {
190 bool_val = cb(getPyArrayNoCpy<double>(d));
193 throw std::runtime_error(
"BUG");
196 bool res = bool_val.cast<
bool>();
197 }
catch (py::error_already_set &e) {
198 cerr <<
"ERROR: Python raised exception from callback function: ";
199 cerr << e.what() << endl;
201 }
catch (py::cast_error &e) {
202 cerr << e.what() << endl;
203 cerr <<
"ERROR: Python callback does not return boolean value." << endl;
205 }
catch (std::exception &e) {
206 cerr <<
"Caught unknown exception in Python callback:" << e.what()
217 py::class_<PyIndataHandler> pyidh(m,
"InDataHandler");
218 pyidh.def(py::init<SmgrHandle, py::function, py::function>());
221 py::class_<PPMHandler> ppm(m,
"PPMHandler");
222 ppm.def(py::init<SmgrHandle, const d>());
223 ppm.def(py::init<SmgrHandle>());
225 ppm.def(
"getCurrentValue", [](
const PPMHandler &ppm) {
226 std::tuple<vd, arma::uvec> tp;
228 py::gil_scoped_release release;
229 tp = ppm.getCurrentValue();
232 return py::make_tuple(ColToNpy<d>(std::get<0>(tp)),
233 ColToNpy<arma::uword>(std::get<1>(tp)));
237 py::class_<ClipHandler> clip(m,
"ClipHandler");
238 clip.def(py::init<SmgrHandle>());
240 clip.def(
"getCurrentValue", [](
const ClipHandler &clip) {
243 py::gil_scoped_release release;
244 cval = clip.getCurrentValue();
247 return ColToNpy<arma::uword>(cval);
252 py::class_<RtAps> rtaps(m,
"RtAps");
261 py::arg(
"streammgr"),
262 py::arg(
"preFilter").none(
true),
266 py::arg(
"nfft") = 2048,
268 py::arg(
"overlap_percentage") = 50.0,
269 py::arg(
"time_constant") = -1
272 rtaps.def(
"getCurrentValue", [](
RtAps &rt) {
275 py::gil_scoped_release release;
278 return CubeToNpy<c>(val);
283 py::class_<RtSignalViewer> rtsv(m,
"RtSignalViewer");
293 py::gil_scoped_release release;
296 return MatToNpy<d>(val);
Clipping detector (Clip). Detects when a signal overdrives the input.
Data coming from / going to DAQ. Non-interleaved format, which means data in buffer is ordered by cha...
Base cass for all DAQ (Data Acquisition) interfaces. A DAQ can be a custom device,...
DataType
Basic data types coming from a DAQ that we can deal with. The naming will be self-explainging.
Filter used to pre-filter a double-precision floating point data stream.
Digital Peak Programme Meter (PPM). Let the latest maximum flow away with a certain amount of dB/s....
Wraps the ThreadedInDataHandler such that it calls a Python callback with a buffer of sample data....
void inCallback(const DaqData &d)
Calls the Python callback method / function with a Numpy array of stream data.
void reset(const Daq *daq)
Calls the reset callback in Python.
PyIndataHandler(SmgrHandle mgr, py::function cb, py::function reset_callback)
Initialize PyIndataHandler.
Real time spectral estimator using Welch method of spectral estimation.
ccube getCurrentValue() const
Get the latest estimate of the power spectra.
Real time signal viewer. Shows envelope of the signal based on amount of history shown.
void startThread()
This method should be called from the derived class' constructor, to start the thread and data is inc...
void stopThread()
This method SHOULD be called from all classes that derive on ThreadedInDataHandler....
A bit of curiously recurring template pattern, to connect the specific handlers and connect the prope...
void init_datahandler(py::module &m)
std::shared_ptr< StreamMgr > SmgrHandle
py::array_t< T > getPyArrayNoCpy(const DaqData &d)
Generate a Numpy array from daqdata, does NOT create a copy of the data!. Instead,...
py::array_t< d > dmat_to_ndarray(const DaqData &d)
size_t us
We often use boolean values.