LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
lasp_timebuffer.cpp
Go to the documentation of this file.
1/* #define DEBUGTRACE_ENABLED */
2#include "lasp_timebuffer.h"
3#include "debugtrace.hpp"
4#include <algorithm>
5#include <cassert>
6#include <deque>
7#include <memory>
8#include <optional>
9#include <stdexcept>
10
11using rte = std::runtime_error;
12
17 std::deque<arma::rowvec> _storage;
18
19public:
20 void reset() {
21 DEBUGTRACE_ENTER;
22 _storage.clear();
23 }
24 void push(const dmat &mat) {
25 DEBUGTRACE_ENTER;
26#if LASP_DEBUG == 1
27 if (!_storage.empty()) {
28 if (mat.n_cols != _storage.front().n_cols) {
29 throw rte("Invalid number of channels in mat");
30 }
31 }
32#endif
33 for (us i = 0; i < mat.n_rows; i++) {
34 _storage.push_back(mat.row(i));
35 }
36 }
37
38 dmat pop(const us nframes, const us keep) {
39
40 DEBUGTRACE_ENTER;
41
42 if (keep >= nframes) {
43 throw rte("keep should be < nframes");
44 }
45 if (nframes > n_frames()) {
46 throw rte("Requested more than currently in storage");
47 }
48
49 assert(!_storage.empty());
50
51 dmat res(nframes, _storage.front().n_cols);
52
53 us j = 0;
54 for (us i = 0; i < nframes; i++) {
55 if (i + keep < nframes) {
56 // Just pop elements and copy over
57 res.row(i) = _storage.front();
58 _storage.pop_front();
59
60 } else {
61
62 // Suppose keep == 0, then we never arrive here
63 // Suppose keep == 1, then storage[0] is copyied over.
64 // Suppose keep == 2, then storage[0[ and storage[1] is copyied over.
65 // Etc.
66 res.row(i) = _storage[j];
67 j++;
68 }
69 }
70
71 return res;
72 }
73
79 us n_frames() const { return _storage.size(); }
80};
81
82TimeBuffer::TimeBuffer() : _imp(std::make_unique<TimeBufferImp>()) {}
83dmat TimeBuffer::pop(const us n_rows, const us keep) {
84 return _imp->pop(n_rows, keep);
85}
87void TimeBuffer::reset() { _imp->reset(); }
88void TimeBuffer::push(const dmat &dat) { _imp->push(dat); }
89us TimeBuffer::size() const { return _imp->n_frames(); }
dmat pop(const us nframes, const us keep=0)
Pop frames from the buffer. Throws a runtime error if more frames are requested than actually stored.
us size() const
Returns current size of stored amount of frames.
void reset()
Reset (empties) the time buffer.
void push(const dmat &mat)
Put samples in the buffer. Number of channels should match other frames, otherwise things go wrong.
void push(const dmat &mat)
us n_frames() const
Counts the number of available frames in the queue.
dmat pop(const us nframes, const us keep)
std::runtime_error rte
Definition lasp_daq.cpp:16
arma::Mat< d > dmat
std::runtime_error rte
size_t us
We often use boolean values.
Definition lasp_types.h:29