17 #ifndef LEANSDR_GENERIC_H 18 #define LEANSDR_GENERIC_H 22 #include <sys/types.h> 55 size_t size =
out.writable() *
sizeof(T);
60 ssize_t nr = read(
fdin,
out.wr(), size);
61 if (nr < 0 && errno == EWOULDBLOCK)
72 fatal(
"read(file_reader)");
78 fprintf(stderr,
"%s looping\n",
name);
79 off_t res = lseek(
fdin, 0, SEEK_SET);
86 size_t partial = nr %
sizeof(T);
87 size_t remain = partial ?
sizeof(T) - partial : 0;
92 ssize_t nr2 = read(
fdin, (
char *)
out.wr() + nr, remain);
94 fatal(
"partial read");
99 out.written(nr /
sizeof(T));
104 int flags = fcntl(
fdin, F_GETFL);
105 if (fcntl(
fdin, F_SETFL, flags | O_NONBLOCK))
118 template <
typename T>
122 in(_in), fdout(_fdout)
127 int size = in.readable() *
sizeof(T);
130 int nw = write(fdout, in.rd(), size);
136 fatal(
"partial write");
137 in.read(nw /
sizeof(T));
148 template <
typename T>
155 in(_in), format(_format), fdout(_fdout), phase(0)
160 int n = in.readable();
161 T *pin = in.rd(), *pend = pin + n;
162 for (; pin < pend; ++pin)
168 int len = snprintf(buf,
sizeof(buf), format, (*pin) * scale);
170 fatal(
"obsolete glibc");
171 int nw = write(fdout, buf, len);
173 fatal(
"partial write");
192 template <
typename T>
201 scale(1), fixed_size(0), in(_in),
202 head(_head), format(_format), sep(_sep), tail(_tail),
203 fout(fdopen(_fdout,
"w"))
208 int n, nmin = fixed_size ? fixed_size : 1;
209 while ((n = in.readable()) >= nmin)
215 fprintf(fout, head, n);
217 for (
int i = 0;
i < n; ++
i)
220 fprintf(fout,
"%s", sep);
221 fprintf(fout, format, pin[
i].re * scale, pin[
i].im * scale);
223 fprintf(fout,
"%s", tail);
233 const char *head, *format, *sep, *
tail;
237 template <
typename T,
int N>
246 head(_head), format(_format), sep(_sep), tail(_tail), n(_n)
248 fout = fdopen(_fdout,
"w");
254 while (in.readable() >= 1)
256 fprintf(fout, head, n);
259 for (
int i = 0;
i < n; ++
i)
262 fprintf(fout,
"%s", sep);
263 fprintf(fout, format, (*pin)[
i] * scale);
265 fprintf(fout,
"%s", tail);
274 const char *head, *format, *sep, *
tail;
282 template <
typename Tin,
typename Tout>
292 if (
out.writable() < 1)
294 unsigned long count = in.readable();
308 template <
typename T>
321 long count =
min(in.readable() / d,
out.writable());
322 T *pin = in.rd(), *pend = pin + count * d, *pout =
out.wr();
323 for (; pin < pend; pin += d, ++pout)
337 template <
typename T>
347 num(_num), den(_den), rate(_rate),
348 acc_num(0), acc_den(0)
354 if (rate.writable() < 1)
356 int count =
min(num.readable(), den.readable());
357 int *pnum = num.rd(), *pden = den.rd();
358 for (
int n = count; n--; ++pnum, ++pden)
365 if (acc_den >= sample_size)
367 rate.write((
float)acc_num / acc_den);
368 acc_num = acc_den = 0;
380 template <
typename Tin,
typename Tout>
384 : nin(
max((size_t)1, sizeof(Tin) / sizeof(Tout))),
385 nout(
max((size_t)1, sizeof(Tout) / sizeof(Tin))),
386 in(_in),
out(_out, nout)
388 if (nin *
sizeof(Tin) != nout *
sizeof(Tout))
389 fail(
"serializer: incompatible sizes");
393 while (in.readable() >= nin &&
out.writable() >= nout)
395 memcpy(
out.wr(), in.rd(), nout *
sizeof(Tout));
409 template <
typename T>
414 data(_data), count(_count),
out(_out), pos(0)
419 int n =
min(
out.writable(), (
unsigned long)(count - pos));
420 memcpy(
out.wr(), &data[pos], n *
sizeof(T));
434 template <
typename T>
439 in(_in), data(_data), count(_count), pos(0)
444 int n =
min(in.readable(), (
unsigned long)(count - pos));
445 memcpy(&data[pos], in.rd(), n *
sizeof(T));
459 #endif // LEANSDR_GENERIC_H
rate_estimator(scheduler *sch, pipebuf< int > &_num, pipebuf< int > &_den, pipebuf< float > &_rate)
int decimation(float Fin, float Fout)
file_reader(scheduler *sch, int _fdin, pipebuf< T > &_out)
pipereader< complex< T > > in
file_carrayprinter(scheduler *sch, const char *_head, const char *_format, const char *_sep, const char *_tail, pipebuf< complex< T >> &_in, int _fdout)
buffer_writer(scheduler *sch, pipebuf< T > &_in, T *_data, int _count)
serializer(scheduler *sch, pipebuf< Tin > &_in, pipebuf< Tout > &_out)
void set_realtime(T &_filler)
file_printer(scheduler *sch, const char *_format, pipebuf< T > &_in, int _fdout, int _decimation=1)
decimator(scheduler *sch, int _d, pipebuf< T > &_in, pipebuf< T > &_out)
file_writer(scheduler *sch, pipebuf< T > &_in, int _fdout)
itemcounter(scheduler *sch, pipebuf< Tin > &_in, pipebuf< Tout > &_out)
buffer_reader(scheduler *sch, T *_data, int _count, pipebuf< T > &_out)
void fatal(const char *s)
file_vectorprinter(scheduler *sch, const char *_head, const char *_format, const char *_sep, const char *_tail, pipebuf< T[N]> &_in, int _fdout, int _n=N)
T max(const T &x, const T &y)
T min(const T &x, const T &y)