libsdr  0.1.0
A simple SDR library
autocast.hh
1 #ifndef __SDR_AUTOCAST_HH__
2 #define __SDR_AUTOCAST_HH__
3 
4 #include "node.hh"
5 #include "traits.hh"
6 #include "logger.hh"
7 
8 namespace sdr {
9 
13 template <class Scalar>
14 class AutoCast: public SinkBase, public Source
15 {
16 public:
19  : SinkBase(), Source(), _buffer(), _cast(0)
20  {
21  // pass...
22  }
23 
25  virtual void config(const Config &src_cfg) {
26  // Requires buffer size, sample rate and type:
27  if ((Config::Type_UNDEFINED==src_cfg.type()) || (0==src_cfg.sampleRate()) || (0==src_cfg.bufferSize())) { return; }
28 
29  // Check type cast combination
31  switch (src_cfg.type()) {
32  case Config::Type_u8: _cast = _uint8_int8; break;
33  case Config::Type_s8: _cast = _identity; break;
34  case Config::Type_u16: _cast = _uint16_int8; break;
35  case Config::Type_s16: _cast = _int16_int8; break;
36  default: break;
37  }
39  switch (src_cfg.type()) {
40  case Config::Type_u8: _cast = _uint8_cint8; break;
41  case Config::Type_s8: _cast = _int8_cint8; break;
42  case Config::Type_cu8: _cast = _uint8_int8; break;
43  case Config::Type_cs8: _cast = _identity; break;
44  case Config::Type_u16: _cast = _uint16_cint8; break;
45  case Config::Type_s16: _cast = _int16_cint8; break;
46  case Config::Type_cu16: _cast = _uint16_int8; break;
47  case Config::Type_cs16: _cast = _int16_int8; break;
48  default: break;
49  }
51  switch (src_cfg.type()) {
52  case Config::Type_u8: _cast = _uint8_int16; break;
53  case Config::Type_s8: _cast = _int8_int16; break;
54  case Config::Type_u16: _cast = _uint16_int16; break;
55  case Config::Type_s16: _cast = _identity; break;
56  default: break;
57  }
59  switch (src_cfg.type()) {
60  case Config::Type_u8: _cast = _uint8_cint16; break;
61  case Config::Type_s8: _cast = _int8_cint16; break;
62  case Config::Type_cu8: _cast = _uint8_int16; break;
63  case Config::Type_cs8: _cast = _int8_int16; break;
64  case Config::Type_u16: _cast = _uint16_cint16; break;
65  case Config::Type_s16: _cast = _int16_cint16; break;
66  case Config::Type_cu16: _cast = _uint16_int16; break;
67  case Config::Type_cs16: _cast = _identity; break;
68  default: break;
69  }
70  }
71 
72  // Check if there exists a cast to the required type
73  if (0 == _cast) {
74  ConfigError err;
75  err << "AutoCast: Can not cast from type " << src_cfg.type() << " to " << Traits<Scalar>::scalarId;
76  throw err;
77  }
78 
79  // Allocate buffer
80  _buffer = Buffer<Scalar>(src_cfg.bufferSize());
81 
82  LogMessage msg(LOG_DEBUG);
83  msg << "Configure AutoCast node:" << std::endl
84  << " input type: " << src_cfg.type() << std::endl
85  << " output type: " << Traits<Scalar>::scalarId;
86  Logger::get().log(msg);
87 
88  // Propergate config
89  this->setConfig(Config(Config::typeId<Scalar>(), src_cfg.sampleRate(), src_cfg.bufferSize(), 1));
90  }
91 
92  virtual void handleBuffer(const RawBuffer &buffer, bool allow_overwrite) {
93  // If no conversion is selected
94  if (0 == _cast) { return; }
95  // If the identity conversion is selected -> forward buffer
96  if (_identity == _cast) { this->send(buffer, allow_overwrite); return; }
97  // Otherwise cast
98  size_t bytes = _cast(buffer, _buffer);
99  this->send(RawBuffer(_buffer, 0, bytes), false);
100  }
101 
102 
103 protected:
107  size_t (*_cast)(const RawBuffer &in, const RawBuffer &out);
108 
109 protected:
111  static size_t _identity(const RawBuffer &in, const RawBuffer &out) {
112  memcpy(out.ptr(), in.data(), in.bytesLen());
113  return in.bytesLen();
114  }
115 
117  static size_t _uint8_int8(const RawBuffer &in, const RawBuffer &out) {
118  size_t N = in.bytesLen();
119  for (size_t i=0; i<N; i++) {
120  reinterpret_cast<int8_t *>(out.data())[i] =
121  int16_t(reinterpret_cast<uint8_t *>(in.data())[i]) - 127;
122  }
123  return N;
124  }
125 
127  static size_t _uint16_int8(const RawBuffer &in, const RawBuffer &out) {
128  size_t N = in.bytesLen()/2;
129  for (size_t i=0; i<N; i++) {
130  reinterpret_cast<int8_t *>(out.data())[i] =
131  int16_t(reinterpret_cast<uint16_t *>(in.data())[i]>>8) - 127;
132  }
133  return N;
134  }
135 
137  static size_t _int16_int8(const RawBuffer &in, const RawBuffer &out) {
138  size_t N = in.bytesLen()/2;
139  for (size_t i=0; i<N; i++) {
140  reinterpret_cast<int8_t *>(out.data())[i] =
141  reinterpret_cast<int16_t *>(in.data())[i]>>8;
142  }
143  return N;
144  }
145 
147  static size_t _uint8_cint8(const RawBuffer &in, const RawBuffer &out) {
148  size_t N = in.bytesLen();
149  uint8_t *values = reinterpret_cast<uint8_t *>(in.data());
150  for (size_t i=0; i<N; i++) {
151  reinterpret_cast<std::complex<int8_t> *>(out.data())[i] =
152  (int16_t(values[i])-127);
153  }
154  return 2*N;
155  }
156 
158  static size_t _int8_cint8(const RawBuffer &in, const RawBuffer &out) {
159  size_t N = in.bytesLen();
160  for (size_t i=0; i<N; i++) {
161  reinterpret_cast<std::complex<int8_t> *>(out.data())[i] =
162  reinterpret_cast<int8_t *>(in.data())[i];
163  }
164  return 2*N;
165  }
166 
168  static size_t _uint16_cint8(const RawBuffer &in, const RawBuffer &out) {
169  size_t N = in.bytesLen()/2;
170  for (size_t i=0; i<N; i++) {
171  reinterpret_cast<std::complex<int8_t> *>(out.data())[i]
172  = int32_t(reinterpret_cast<int16_t *>(in.data())[i]>>8)-((2<<15)-1);
173  }
174  return 2*N;
175  }
176 
178  static size_t _int16_cint8(const RawBuffer &in, const RawBuffer &out) {
179  size_t N = in.bytesLen()/2;
180  for (size_t i=0; i<N; i++) {
181  reinterpret_cast<std::complex<int8_t> *>(out.data())[i] = reinterpret_cast<int16_t *>(in.data())[i]>>8;
182  }
183  return 2*N;
184  }
185 
187  static size_t _uint8_int16(const RawBuffer &in, const RawBuffer &out) {
188  size_t N = in.bytesLen();
189  int8_t *values = reinterpret_cast<int8_t *>(in.data());
190  for (size_t i=0; i<N; i++) {
191  reinterpret_cast<int16_t *>(out.data())[i] = (int16_t(values[i])-127)<<8;
192  }
193  return 2*N;
194  }
195 
197  static size_t _int8_int16(const RawBuffer &in, const RawBuffer &out) {
198  size_t N = in.bytesLen();
199  int8_t *values = reinterpret_cast<int8_t *>(in.data());
200  for (size_t i=0; i<N; i++) {
201  reinterpret_cast<int16_t *>(out.data())[i] = int16_t(values[i])<<8;
202  }
203  return 2*N;
204  }
205 
207  static size_t _uint16_int16(const RawBuffer &in, const RawBuffer &out) {
208  size_t N = in.bytesLen()/2;
209  uint16_t *values = reinterpret_cast<uint16_t *>(in.data());
210  for (size_t i=0; i<N; i++) {
211  reinterpret_cast<int16_t *>(out.data())[i] = int32_t(values[i])-((2<<15)-1);
212  }
213  return 2*N;
214  }
215 
217  static size_t _uint8_cint16(const RawBuffer &in, const RawBuffer &out) {
218  size_t N = in.bytesLen();
219  uint8_t *values = reinterpret_cast<uint8_t *>(in.data());
220  for (size_t i=0; i<N; i++) {
221  reinterpret_cast<std::complex<int16_t> *>(out.data())[i]
222  = std::complex<int16_t>((int16_t(values[i])-127)<<8);
223  }
224  return 4*N;
225  }
226 
228  static size_t _int8_cint16(const RawBuffer &in, const RawBuffer &out) {
229  size_t N = in.bytesLen();
230  int8_t *values = reinterpret_cast<int8_t *>(in.data());
231  for (size_t i=0; i<N; i++) {
232  reinterpret_cast<std::complex<int16_t> *>(out.data())[i]
233  = std::complex<int16_t>(int16_t(values[i])*(1<<8));
234  }
235  return 4*N;
236  }
237 
239  static size_t _uint16_cint16(const RawBuffer &in, const RawBuffer &out) {
240  size_t N = in.bytesLen()/2;
241  uint16_t *values = reinterpret_cast<uint16_t *>(in.data());
242  for (size_t i=0; i<N; i++) {
243  reinterpret_cast<std::complex<int16_t> *>(out.data())[i]
244  = std::complex<int16_t>(int32_t(values[i])-(1<<15));
245  }
246  return 4*N;
247  }
248 
250  static size_t _int16_cint16(const RawBuffer &in, const RawBuffer &out) {
251  size_t N = in.bytesLen()/2;
252  int16_t *values = reinterpret_cast<int16_t *>(in.data());
253  for (size_t i=0; i<N; i++) {
254  reinterpret_cast<std::complex<int16_t> *>(out.data())[i] = std::complex<int16_t>(values[i]);
255  }
256  return 4*N;
257  }
258 };
259 
260 
261 }
262 #endif // __SDR_AUTOCAST_HH__
static size_t _int16_int8(const RawBuffer &in, const RawBuffer &out)
int16 -> int8
Definition: autocast.hh:137
A collection of configuration information that is send by a source to all connected sinks to properga...
Definition: node.hh:35
Base class of all buffers, represents an untyped array of bytes.
Definition: buffer.hh:32
Buffer< Scalar > _buffer
Output buffer.
Definition: autocast.hh:105
static size_t _uint16_cint8(const RawBuffer &in, const RawBuffer &out)
uint16 -> complex int 8.
Definition: autocast.hh:168
Real signed 16b ints.
Definition: node.hh:44
virtual void send(const RawBuffer &buffer, bool allow_overwrite=false)
Sends the given buffer to all connected sinks.
Definition: node.cc:67
Complex (aka I/Q) type of unsigned 16b ints.
Definition: node.hh:49
Definition: autocast.hh:8
Real signed 8b ints.
Definition: node.hh:42
Generic source class.
Definition: node.hh:213
Real unsigned 8b ints.
Definition: node.hh:41
size_t bytesLen() const
Returns the size of the buffer by the view.
Definition: buffer.hh:73
static size_t _uint8_int16(const RawBuffer &in, const RawBuffer &out)
uint8 -> int16.
Definition: autocast.hh:187
char * data() const
Returns the pointer to the data of the buffer view.
Definition: buffer.hh:69
Real unsigned 16b ints.
Definition: node.hh:43
static size_t _int8_cint8(const RawBuffer &in, const RawBuffer &out)
int8 -> complex int8.
Definition: autocast.hh:158
virtual void setConfig(const Config &config)
Stores the configuration and propergates it if the configuration has been changed.
Definition: node.cc:98
virtual void handleBuffer(const RawBuffer &buffer, bool allow_overwrite)
Needs to be implemented by any sub-type to process the received data.
Definition: autocast.hh:92
void log(const LogMessage &message)
Logs a message.
Definition: logger.cc:100
Type type() const
Returns the type.
Definition: node.hh:71
size_t(* _cast)(const RawBuffer &in, const RawBuffer &out)
Cast function.
Definition: autocast.hh:107
static size_t _uint8_int8(const RawBuffer &in, const RawBuffer &out)
uint8_t -> int8_t
Definition: autocast.hh:117
static Logger & get()
Returns the singleton instance of the logger.
Definition: logger.cc:89
The configuration error class.
Definition: exception.hh:24
A log message.
Definition: logger.hh:22
size_t bufferSize() const
Returns the max.
Definition: node.hh:83
static size_t _int8_int16(const RawBuffer &in, const RawBuffer &out)
int8 -> int16.
Definition: autocast.hh:197
static size_t _int16_cint8(const RawBuffer &in, const RawBuffer &out)
int16 -> complex int 8.
Definition: autocast.hh:178
AutoCast()
Constructor.
Definition: autocast.hh:18
static size_t _int16_cint16(const RawBuffer &in, const RawBuffer &out)
int16 -> complex int16.
Definition: autocast.hh:250
This class performs some automatic casts to a certain buffer type (if possible) specified by the temp...
Definition: autocast.hh:14
Basic interface of all Sinks.
Definition: node.hh:174
char * ptr() const
Returns the pointer to the data (w/o view).
Definition: buffer.hh:67
Complex (aka I/Q) type of signed 8b ints.
Definition: node.hh:48
virtual void config(const Config &src_cfg)
Configures the auto cast node.
Definition: autocast.hh:25
static size_t _uint8_cint16(const RawBuffer &in, const RawBuffer &out)
unsinged int8 -> complex int16.
Definition: autocast.hh:217
static size_t _int8_cint16(const RawBuffer &in, const RawBuffer &out)
int8 -> complex int16.
Definition: autocast.hh:228
Complex (aka I/Q) type of signed 16b ints.
Definition: node.hh:50
static size_t _uint16_int16(const RawBuffer &in, const RawBuffer &out)
uint16 -> int16.
Definition: autocast.hh:207
Complex (aka I/Q) type of unsigned 8b ints.
Definition: node.hh:47
static size_t _uint16_int8(const RawBuffer &in, const RawBuffer &out)
uint16 -> int8
Definition: autocast.hh:127
Forward declaration of type tratis template.
Definition: traits.hh:20
static size_t _uint16_cint16(const RawBuffer &in, const RawBuffer &out)
uint16 -> complex int16.
Definition: autocast.hh:239
static size_t _uint8_cint8(const RawBuffer &in, const RawBuffer &out)
uint8 -> complex int8.
Definition: autocast.hh:147
static size_t _identity(const RawBuffer &in, const RawBuffer &out)
Performs no cast at all.
Definition: autocast.hh:111
double sampleRate() const
Returns the sample rate.
Definition: node.hh:77