LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
lasp_siggen.cpp
Go to the documentation of this file.
1/* #define DEBUGTRACE_ENABLED */
2#include "lasp_siggen.h"
3#include "debugtrace.hpp"
4#include "lasp_mathtypes.h"
5#include <cassert>
6#include <type_traits>
7using std::cerr;
8using std::endl;
9using rte = std::runtime_error;
10
11inline d level_amp(d level_dB) { return pow(10, level_dB / 20); }
12
13using mutexlock = std::scoped_lock<std::mutex>;
14
15vd Siggen::genSignal(const us nframes) {
16
17 DEBUGTRACE_ENTER;
19
20 DEBUGTRACE_PRINT(nframes);
21 vd signal(nframes, arma::fill::value(_dc_offset));
22
23 if (!_muted) {
24 vd signal_dynamic = _level_linear * genSignalUnscaled(nframes);
25 for (auto f : _filters) {
26 assert(f.second);
27 f.second->filter(signal_dynamic);
28 }
29 signal += signal_dynamic;
30 }
31
32 return signal;
33}
34void Siggen::setFilter(const std::string &name,
35 std::shared_ptr<Filter> filter) {
36 DEBUGTRACE_ENTER;
38 if (filter) {
39 _filters[name] = filter;
40 } else if (_filters.find(name) != _filters.end()) {
41 _filters.extract(name);
42 }
43}
44void Siggen::setDCOffset(const d offset) {
45 DEBUGTRACE_ENTER;
47 _dc_offset = offset;
48}
49void Siggen::setLevel(const d level, bool dB) {
50 DEBUGTRACE_ENTER;
52 _level_linear = dB ? level_amp(level) : level;
53}
54void Siggen::reset(const d newFs) {
55 DEBUGTRACE_ENTER;
57 _fs = newFs;
58 for (auto &f : _filters) {
59 assert(f.second);
60 f.second->reset();
61 }
62 resetImpl();
63}
void setFilter(const std::string &name, std::shared_ptr< Filter > f)
Set a filter on the signal. For example to EQ the signal, or otherwise to shape the spectrum....
vd genSignal(const us nframes)
Called whenever the implementation needs to create new samples.
virtual vd genSignalUnscaled(const us nframes)=0
std::mutex _mtx
Definition lasp_siggen.h:30
void setDCOffset(d offset)
Set a linear DC offset value to the signal.
void setLevel(const d level, bool dB=true)
Set the level of the signal generator.
virtual void resetImpl()=0
void reset(const d newFs)
Reset the signal generator. Should be called whenever the output is based on a different sampling fre...
std::runtime_error rte
Definition lasp_daq.cpp:16
d level_amp(d level_dB)
std::scoped_lock< std::mutex > mutexlock
arma::Col< d > vd
std::scoped_lock< std::mutex > lck
size_t us
We often use boolean values.
Definition lasp_types.h:29