2019-03-17 16:31:42 -04:00
|
|
|
// This file is part of LeanSDR Copyright (C) 2016-2018 <pabr@pabr.org>.
|
|
|
|
// See the toplevel README for more information.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-02-22 16:52:49 -05:00
|
|
|
#ifndef LEANSDR_GENERIC_H
|
|
|
|
#define LEANSDR_GENERIC_H
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2018-02-22 16:52:49 -05:00
|
|
|
#include <sys/types.h>
|
2019-03-24 14:53:23 -04:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <io.h>
|
|
|
|
#else
|
2018-02-22 16:52:49 -05:00
|
|
|
#include <unistd.h>
|
2019-03-24 14:53:23 -04:00
|
|
|
#endif
|
2018-02-22 16:52:49 -05:00
|
|
|
|
|
|
|
#include "leansdr/math.h"
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
namespace leansdr
|
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Simple blocks
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// [file_reader] reads raw data from a file descriptor into a [pipebuf].
|
|
|
|
// If the file descriptor is seekable, data can be looped.
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct file_reader : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
bool loop;
|
|
|
|
|
|
|
|
file_reader(
|
|
|
|
scheduler *sch,
|
|
|
|
int _fdin,
|
|
|
|
pipebuf<T> &_out
|
|
|
|
) :
|
|
|
|
runnable(sch, _out.name),
|
|
|
|
loop(false),
|
|
|
|
filler(nullptr),
|
|
|
|
fdin(_fdin),
|
|
|
|
out(_out)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~file_reader()
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
delete[] filler;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
size_t size = out.writable() * sizeof(T);
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (!size) {
|
2018-02-25 19:02:33 -05:00
|
|
|
return;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
2018-02-25 19:02:33 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
again:
|
|
|
|
ssize_t nr = read(fdin, out.wr(), size);
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
if (nr < 0 && errno == EWOULDBLOCK)
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
if (filler)
|
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
if (sch->debug) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fprintf(stderr, "U");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
out.write(*filler);
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (nr < 0) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fatal("read(file_reader)");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
if (!nr)
|
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
if (!loop) {
|
2018-02-25 19:02:33 -05:00
|
|
|
return;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sch->debug) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fprintf(stderr, "%s looping\n", name);
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
off_t res = lseek(fdin, 0, SEEK_SET);
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (res == (off_t)-1) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fatal("lseek");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always stop at element boundary (may block)
|
|
|
|
size_t partial = nr % sizeof(T);
|
|
|
|
size_t remain = partial ? sizeof(T) - partial : 0;
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
while (remain)
|
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
if (sch->debug) {
|
2018-02-25 19:02:33 -05:00
|
|
|
fprintf(stderr, "+");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
ssize_t nr2 = read(fdin, (char *)out.wr() + nr, remain);
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (nr2 <= 0) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fatal("partial read");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
nr += nr2;
|
|
|
|
remain -= nr2;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.written(nr / sizeof(T));
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
void set_realtime(T &_filler)
|
|
|
|
{
|
|
|
|
int flags = fcntl(fdin, F_GETFL);
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (fcntl(fdin, F_SETFL, flags | O_NONBLOCK)) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fatal("fcntl");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
filler = new T(_filler);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *filler;
|
2018-02-25 19:02:33 -05:00
|
|
|
int fdin;
|
|
|
|
pipewriter<T> out;
|
2018-02-22 16:52:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// [file_writer] writes raw data from a [pipebuf] to a file descriptor.
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct file_writer : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
file_writer(
|
|
|
|
scheduler *sch,
|
|
|
|
pipebuf<T> &_in,
|
|
|
|
int _fdout
|
|
|
|
) :
|
|
|
|
runnable(sch, _in.name),
|
|
|
|
in(_in),
|
|
|
|
fdout(_fdout)
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
int size = in.readable() * sizeof(T);
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (!size) {
|
2018-02-25 19:02:33 -05:00
|
|
|
return;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
int nw = write(fdout, in.rd(), size);
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (!nw) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fatal("pipe");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nw < 0) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fatal("write");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nw % sizeof(T)) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fatal("partial write");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
in.read(nw / sizeof(T));
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
private:
|
2018-02-25 19:02:33 -05:00
|
|
|
pipereader<T> in;
|
|
|
|
int fdout;
|
2018-02-22 16:52:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// [file_printer] writes data from a [pipebuf] to a file descriptor,
|
|
|
|
// with printf-style formatting and optional scaling.
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct file_printer : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
file_printer(
|
|
|
|
scheduler *sch,
|
|
|
|
const char *_format,
|
|
|
|
pipebuf<T> &_in,
|
|
|
|
int _fdout,
|
|
|
|
int _decimation = 1
|
|
|
|
) :
|
|
|
|
runnable(sch, _in.name),
|
|
|
|
scale(1),
|
|
|
|
decimation(_decimation),
|
|
|
|
in(_in),
|
|
|
|
format(_format),
|
|
|
|
fdout(_fdout),
|
|
|
|
phase(0)
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
int n = in.readable();
|
|
|
|
T *pin = in.rd(), *pend = pin + n;
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
for (; pin < pend; ++pin)
|
|
|
|
{
|
|
|
|
if (++phase >= decimation)
|
|
|
|
{
|
|
|
|
phase -= decimation;
|
|
|
|
char buf[256];
|
|
|
|
int len = snprintf(buf, sizeof(buf), format, (*pin) * scale);
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (len < 0) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fatal("obsolete glibc");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
int nw = write(fdout, buf, len);
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (nw != len) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fatal("partial write");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
2018-02-25 19:02:33 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
in.read(n);
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
T scale;
|
|
|
|
int decimation;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
private:
|
2018-02-25 19:02:33 -05:00
|
|
|
pipereader<T> in;
|
|
|
|
const char *format;
|
|
|
|
int fdout;
|
|
|
|
int phase;
|
2018-02-22 16:52:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// [file_carrayprinter] writes all data available from a [pipebuf]
|
|
|
|
// to a file descriptor on a single line.
|
|
|
|
// Special case for complex.
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct file_carrayprinter : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
file_carrayprinter(
|
|
|
|
scheduler *sch,
|
|
|
|
const char *_head,
|
|
|
|
const char *_format,
|
|
|
|
const char *_sep,
|
|
|
|
const char *_tail,
|
|
|
|
pipebuf<complex<T>> &_in,
|
|
|
|
int _fdout
|
|
|
|
) :
|
|
|
|
runnable(sch, _in.name),
|
|
|
|
scale(1),
|
|
|
|
fixed_size(0),
|
|
|
|
in(_in),
|
|
|
|
head(_head),
|
|
|
|
format(_format),
|
|
|
|
sep(_sep),
|
|
|
|
tail(_tail),
|
|
|
|
fout(fdopen(_fdout, "w"))
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
int n, nmin = fixed_size ? fixed_size : 1;
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
while ((n = in.readable()) >= nmin)
|
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
if (fixed_size) {
|
2018-02-25 19:02:33 -05:00
|
|
|
n = fixed_size;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
if (fout)
|
|
|
|
{
|
|
|
|
fprintf(fout, head, n);
|
|
|
|
complex<T> *pin = in.rd();
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
if (i) {
|
2018-02-25 19:02:33 -05:00
|
|
|
fprintf(fout, "%s", sep);
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
fprintf(fout, format, pin[i].re * scale, pin[i].im * scale);
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
fprintf(fout, "%s", tail);
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
fflush(fout);
|
|
|
|
in.read(n);
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
T scale;
|
2019-03-17 16:31:42 -04:00
|
|
|
int fixed_size; // Number of elements per batch, or 0.
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
|
|
|
pipereader<complex<T>> in;
|
2018-02-25 19:02:33 -05:00
|
|
|
const char *head, *format, *sep, *tail;
|
|
|
|
FILE *fout;
|
2018-02-22 16:52:49 -05:00
|
|
|
};
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T, int N>
|
|
|
|
struct file_vectorprinter : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
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
|
|
|
|
) :
|
|
|
|
runnable(sch, _in.name),
|
|
|
|
scale(1),
|
|
|
|
in(_in),
|
|
|
|
head(_head),
|
|
|
|
format(_format),
|
|
|
|
sep(_sep),
|
|
|
|
tail(_tail),
|
|
|
|
n(_n)
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
|
|
|
fout = fdopen(_fdout, "w");
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (!fout) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fatal("fdopen");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
while (in.readable() >= 1)
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
fprintf(fout, head, n);
|
|
|
|
T(*pin)
|
|
|
|
[N] = in.rd();
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
for (int i = 0; i < n; ++i)
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
if (i) {
|
2018-02-25 19:02:33 -05:00
|
|
|
fprintf(fout, "%s", sep);
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
fprintf(fout, format, (*pin)[i] * scale);
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
fprintf(fout, "%s", tail);
|
|
|
|
in.read(1);
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
fflush(fout);
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
T scale;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
private:
|
2018-02-25 19:02:33 -05:00
|
|
|
pipereader<T[N]> in;
|
|
|
|
const char *head, *format, *sep, *tail;
|
|
|
|
FILE *fout;
|
2019-03-17 16:31:42 -04:00
|
|
|
int n;
|
2018-02-22 16:52:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// [itemcounter] writes the number of input items to the output [pipebuf].
|
|
|
|
// [Tout] must be a numeric type.
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename Tin, typename Tout>
|
|
|
|
struct itemcounter : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
itemcounter(
|
|
|
|
scheduler *sch,
|
|
|
|
pipebuf<Tin> &_in,
|
|
|
|
pipebuf<Tout> &_out
|
|
|
|
) :
|
|
|
|
runnable(sch, "itemcounter"),
|
|
|
|
in(_in),
|
|
|
|
out(_out)
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
void run()
|
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
if (out.writable() < 1) {
|
2018-02-25 19:02:33 -05:00
|
|
|
return;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
unsigned long count = in.readable();
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (!count) {
|
2018-02-25 19:02:33 -05:00
|
|
|
return;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
out.write(count);
|
|
|
|
in.read(count);
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
private:
|
2018-02-25 19:02:33 -05:00
|
|
|
pipereader<Tin> in;
|
|
|
|
pipewriter<Tout> out;
|
2018-02-22 16:52:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// [decimator] forwards 1 in N sample.
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct decimator : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2019-03-24 14:53:23 -04:00
|
|
|
int d;
|
2018-02-25 19:02:33 -05:00
|
|
|
|
2021-03-21 21:06:26 -04:00
|
|
|
decimator(
|
|
|
|
scheduler *sch,
|
|
|
|
int _d,
|
|
|
|
pipebuf<T> &_in,
|
|
|
|
pipebuf<T> &_out
|
|
|
|
) :
|
|
|
|
runnable(sch, "decimator"),
|
|
|
|
d(_d),
|
|
|
|
in(_in),
|
|
|
|
out(_out)
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
void run()
|
|
|
|
{
|
2019-03-24 14:53:23 -04:00
|
|
|
long count = min(in.readable() / d, out.writable());
|
2018-02-25 19:02:33 -05:00
|
|
|
T *pin = in.rd(), *pend = pin + count * d, *pout = out.wr();
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
for (; pin < pend; pin += d, ++pout) {
|
2018-02-25 19:02:33 -05:00
|
|
|
*pout = *pin;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
in.read(count * d);
|
|
|
|
out.written(count);
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
private:
|
2018-02-25 19:02:33 -05:00
|
|
|
pipereader<T> in;
|
|
|
|
pipewriter<T> out;
|
2018-02-22 16:52:49 -05:00
|
|
|
};
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
// [rate_estimator] accumulates counts of two quantities
|
|
|
|
// and periodically outputs their ratio.
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct rate_estimator : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
int sample_size;
|
|
|
|
|
2021-03-21 21:06:26 -04:00
|
|
|
rate_estimator(
|
|
|
|
scheduler *sch,
|
|
|
|
pipebuf<int> &_num,
|
|
|
|
pipebuf<int> &_den,
|
|
|
|
pipebuf<float> &_rate
|
|
|
|
) :
|
|
|
|
runnable(sch, "rate_estimator"),
|
|
|
|
sample_size(10000),
|
|
|
|
num(_num),
|
|
|
|
den(_den),
|
|
|
|
rate(_rate),
|
|
|
|
acc_num(0),
|
|
|
|
acc_den(0)
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-25 19:02:33 -05:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
if (rate.writable() < 1) {
|
2018-02-25 19:02:33 -05:00
|
|
|
return;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
int count = min(num.readable(), den.readable());
|
|
|
|
int *pnum = num.rd(), *pden = den.rd();
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
for (int n = count; n--; ++pnum, ++pden)
|
|
|
|
{
|
|
|
|
acc_num += *pnum;
|
|
|
|
acc_den += *pden;
|
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
num.read(count);
|
|
|
|
den.read(count);
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
if (acc_den >= sample_size)
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
rate.write((float)acc_num / acc_den);
|
2018-02-25 19:02:33 -05:00
|
|
|
acc_num = acc_den = 0;
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-25 19:02:33 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
2018-02-22 16:52:49 -05:00
|
|
|
pipereader<int> num, den;
|
|
|
|
pipewriter<float> rate;
|
|
|
|
T acc_num, acc_den;
|
2018-02-25 19:02:33 -05:00
|
|
|
};
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
// SERIALIZER
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename Tin, typename Tout>
|
|
|
|
struct serializer : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
serializer(
|
|
|
|
scheduler *sch,
|
|
|
|
pipebuf<Tin> &_in,
|
|
|
|
pipebuf<Tout> &_out
|
|
|
|
) :
|
|
|
|
nin(max((size_t)1,
|
|
|
|
sizeof(Tin) / sizeof(Tout))),
|
|
|
|
nout(max((size_t)1,
|
|
|
|
sizeof(Tout) / sizeof(Tin))),
|
|
|
|
in(_in),
|
|
|
|
out(_out, nout)
|
2018-02-22 16:52:49 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
if (nin * sizeof(Tin) != nout * sizeof(Tout)) {
|
2019-03-17 16:31:42 -04:00
|
|
|
fail("serializer: incompatible sizes");
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
while (in.readable() >= nin && out.writable() >= nout)
|
|
|
|
{
|
|
|
|
memcpy(out.wr(), in.rd(), nout * sizeof(Tout));
|
|
|
|
in.read(nin);
|
|
|
|
out.written(nout);
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
private:
|
2018-02-22 16:52:49 -05:00
|
|
|
int nin, nout;
|
|
|
|
pipereader<Tin> in;
|
|
|
|
pipewriter<Tout> out;
|
2019-03-17 16:31:42 -04:00
|
|
|
}; // serializer
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
// [buffer_reader] reads from a user-supplied buffer.
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct buffer_reader : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
buffer_reader(
|
|
|
|
scheduler *sch,
|
|
|
|
T *_data,
|
|
|
|
int _count,
|
|
|
|
pipebuf<T> &_out
|
|
|
|
) :
|
|
|
|
runnable(sch, "buffer_reader"),
|
|
|
|
data(_data),
|
|
|
|
count(_count),
|
|
|
|
out(_out),
|
|
|
|
pos(0)
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
void run()
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
int n = min(out.writable(), (unsigned long)(count - pos));
|
2018-02-25 19:02:33 -05:00
|
|
|
memcpy(out.wr(), &data[pos], n * sizeof(T));
|
|
|
|
pos += n;
|
|
|
|
out.written(n);
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
private:
|
2018-02-22 16:52:49 -05:00
|
|
|
T *data;
|
|
|
|
int count;
|
|
|
|
pipewriter<T> out;
|
|
|
|
int pos;
|
2019-03-17 16:31:42 -04:00
|
|
|
}; // buffer_reader
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
// [buffer_writer] writes to a user-supplied buffer.
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct buffer_writer : runnable
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2021-03-21 21:06:26 -04:00
|
|
|
buffer_writer(
|
|
|
|
scheduler *sch,
|
|
|
|
pipebuf<T> &_in,
|
|
|
|
T *_data,
|
|
|
|
int _count
|
|
|
|
) :
|
|
|
|
runnable(sch, "buffer_reader"),
|
|
|
|
in(_in),
|
|
|
|
data(_data),
|
|
|
|
count(_count),
|
|
|
|
pos(0)
|
2018-02-25 19:02:33 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2018-02-25 19:02:33 -05:00
|
|
|
void run()
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
int n = min(in.readable(), (unsigned long)(count - pos));
|
2018-02-25 19:02:33 -05:00
|
|
|
memcpy(&data[pos], in.rd(), n * sizeof(T));
|
|
|
|
in.read(n);
|
|
|
|
pos += n;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
private:
|
2018-02-22 16:52:49 -05:00
|
|
|
pipereader<T> in;
|
|
|
|
T *data;
|
|
|
|
int count;
|
|
|
|
int pos;
|
2019-03-17 16:31:42 -04:00
|
|
|
}; // buffer_writer
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
} // namespace leansdr
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
#endif // LEANSDR_GENERIC_H
|