LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
lasp_daqdata.h
Go to the documentation of this file.
1#pragma once
2#include "lasp_daqconfig.h"
3#include "lasp_types.h"
4#include <armadillo>
5#include <cassert>
6#include <functional>
7#include <gsl/gsl-lite.hpp>
8#include <memory>
9#include <type_traits>
10
14class Daq;
15
16using byte_t = char;
17
22class DaqData {
23protected:
28
29public:
34
39
44
49
54
62 DaqData(const us nframes, const us nchannels,
67 DaqData(const DaqData &);
68 DaqData(DaqData &&);
69 DaqData &operator=(const DaqData &) = delete;
70 ~DaqData();
71
81 byte_t *raw_ptr(const us frame = 0, const us channel = 0) {
82 assert(frame < nframes);
83 assert(channel < nchannels);
84 return &(_data[sw * (frame + channel * nframes)]);
85 }
86 const byte_t *raw_ptr(const us frame = 0, const us channel = 0) const {
87 assert(frame < nframes);
88 assert(channel < nchannels);
89 return &(_data[sw * (frame + channel * nframes)]);
90 }
91
97 us size_bytes() const { return sw * nchannels * nframes; }
98
105 void copyInFromRaw(const std::vector<byte_t *> &ptrs);
106
114 void copyInFromRaw(const us channel, const byte_t *ptr);
115
122 void copyToRaw(const us channel, byte_t *ptr);
123
131 arma::Mat<d> toFloat() const;
132
142 arma::Col<d> toFloat(const us channel_no) const;
143
154 d toFloat(const us frame_no, const us channel_no) const;
155
164 void fromFloat(const us frame_no, const us channel_no,
165 const d data);
166
174 void fromFloat(const us channel, const arma::Col<d> &data);
175
176 // Return value based on type
177 template <typename T> T &value(const us frame, const us channel) {
178#if LASP_DEBUG == 1
179 check_type<T>();
180#endif
181 return *reinterpret_cast<T *>(raw_ptr(frame, channel));
182 }
183 template <typename T> const T &value(const us frame, const us channel) const {
184#if LASP_DEBUG == 1
185 check_type<T>();
186#endif
187 return *reinterpret_cast<const T *>(raw_ptr(frame, channel));
188 }
189
193 void print() const;
194
195protected:
196 template <typename T> void check_type() const {
197 using DataType = DataTypeDescriptor::DataType;
198
199 bool correct = false;
200 static_assert(std::is_arithmetic<T>::value);
201
202 if constexpr (std::is_floating_point<T>::value) {
203 correct |= sizeof(T) == 4 && dtype == DataType::dtype_fl32;
204 correct |= sizeof(T) == 8 && dtype == DataType::dtype_fl64;
205
206 } else {
207 correct |= sizeof(T) == 1 && dtype == DataType::dtype_int8;
208 correct |= sizeof(T) == 2 && dtype == DataType::dtype_int16;
209 correct |= sizeof(T) == 4 && dtype == DataType::dtype_int32;
210 }
211 if (!correct) {
212 throw std::runtime_error("Wrong datatype for template argument");
213 }
214 }
215
216 template <typename T> arma::Mat<d> toFloat() const;
217 template <typename T> arma::Col<d> toFloat(const us channel_no) const;
218 template <typename T> d toFloat(const us frame_no, const us channel_no) const;
219
220 /* template <typename T> void fromFloat(const dmat&); */
221 template <typename T>
222 void fromFloat(const us channel_no, const arma::Col<d> &vals);
223 template <typename T>
224 void fromFloat(const us frame_no, const us channel_no, const d val);
235};
236
Data coming from / going to DAQ. Non-interleaved format, which means data in buffer is ordered by cha...
void copyInFromRaw(const std::vector< byte_t * > &ptrs)
Copy data from a set of raw pointers of uninterleaved data. Overwrites any existing available data.
arma::Mat< d > toFloat() const
Convert samples to floating point values and return a nframes x nchannels array of floats....
arma::Mat< d > toFloat() const
DataTypeDescriptor::DataType dtype
The data type corresponding to a sample.
arma::Col< d > toFloat(const us channel_no) const
byte_t * raw_ptr(const us frame=0, const us channel=0)
Return pointer to the raw data corresponding to a certain sample (frame, channel combo).
void print() const
For debugging purposes: prints some stats.
const byte_t * raw_ptr(const us frame=0, const us channel=0) const
DataTypeDescriptor dtype_descr
The data type description corresponding to a sample.
void copyToRaw(const us channel, byte_t *ptr)
Copy contents of DaqData for a certain channel to a raw pointer.
void fromFloat(const us frame_no, const us channel_no, const d data)
Convert to channel data of native type from floating point values. Useful for 'changing' raw data in ...
us nchannels
The number of channels.
us size_bytes() const
Return the total number of bytes.
us sw
The number of bytes per sample (sample width, sw)
void check_type() const
byte_t * _data
Storage for the actual data.
void fromFloat(const us channel_no, const arma::Col< d > &vals)
DaqData & operator=(const DaqData &)=delete
us nframes
The number of frames in this block of data.
T & value(const us frame, const us channel)
const T & value(const us frame, const us channel) const
Base cass for all DAQ (Data Acquisition) interfaces. A DAQ can be a custom device,...
Definition lasp_daq.h:29
Descriptor for data types containing more detailed information.
DataType
Basic data types coming from a DAQ that we can deal with. The naming will be self-explainging.
char byte_t
size_t us
We often use boolean values.
Definition lasp_types.h:29