LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
lasp_daq.cpp
Go to the documentation of this file.
1/* #define DEBUGTRACE_ENABLED */
2#include "debugtrace.hpp"
3#include "lasp_daqconfig.h"
4
5#include "lasp_config.h"
6#include "lasp_daq.h"
7#if LASP_HAS_ULDAQ == 1
8#include "lasp_uldaq.h"
9#endif
10#if LASP_HAS_RTAUDIO == 1
11#include "lasp_rtaudiodaq.h"
12#endif
13#if LASP_HAS_PORTAUDIO == 1
14#include "lasp_portaudiodaq.h"
15#endif
16using rte = std::runtime_error;
17
18Daq::~Daq() { DEBUGTRACE_ENTER; }
19
20std::unique_ptr<Daq> Daq::createDaq(const DeviceInfo &devinfo,
21 const DaqConfiguration &config) {
22 DEBUGTRACE_ENTER;
23
24#if LASP_HAS_ULDAQ == 1
25 if (devinfo.api.apicode == LASP_ULDAQ_APICODE) {
26 return createUlDaqDevice(devinfo, config);
27 }
28#endif
29#if LASP_HAS_RTAUDIO == 1
30 // See lasp_daqconfig.h:114 ALSA, up to
31 if (devinfo.api.apicode == LASP_RTAUDIO_APICODE) {
32 return createRtAudioDevice(devinfo, config);
33 }
34#endif
35#if LASP_HAS_PORTAUDIO == 1
36 if (devinfo.api.apicode == LASP_PORTAUDIO_APICODE) {
37 return createPortAudioDevice(devinfo, config);
38 }
39#endif
40 throw rte(string("Unable to match Device API: ") + devinfo.api.apiname);
41}
42
43Daq::Daq(const DeviceInfo &devinfo, const DaqConfiguration &config)
44 : DaqConfiguration(config), DeviceInfo(devinfo) {
45 DEBUGTRACE_ENTER;
46
47 if (duplexMode()) {
48 if (neninchannels() == 0) {
49 throw rte("Duplex mode enabled, but no input channels enabled");
50 }
51
52 if (nenoutchannels() == 0) {
53 throw rte("Duplex mode enabled, but no output channels enabled");
54 }
55 }
56 if(!duplexMode() && monitorOutput) {
57 throw rte("Output monitoring only allowed when running in duplex mode");
58 }
59
61 throw rte(
62 "Output monitor flag set, but device does not have output monitor");
63 }
64
65 if (!config.match(devinfo)) {
66 throw rte("DaqConfiguration does not match device info");
67 }
68 if (neninchannels(false) > devinfo.ninchannels) {
69 throw rte(
70 "Number of enabled input channels is higher than device capability");
71 }
72 if (nenoutchannels() > devinfo.noutchannels) {
73 throw rte(
74 "Number of enabled output channels is higher than device capability");
75 }
76}
77
78double Daq::samplerate() const {
79 DEBUGTRACE_ENTER;
81}
82
87 return dtype_map.at(dataType());
88}
89
90dvec Daq::inputRangeForEnabledChannels(const bool include_monitor) const {
91 dvec res;
92 auto chs = enabledInChannels(include_monitor);
93 for(auto& ch : chs) {
94 res.push_back(availableInputRanges.at(ch.rangeIndex));
95 }
96 return res;
97}
98
99us Daq::neninchannels(const bool include_monitorchannel) const {
100 boolvec eninchannels = this->eninchannels(include_monitorchannel);
101 return std::count(eninchannels.cbegin(), eninchannels.cend(), true);
102}
103
105 boolvec enchannels = this->enoutchannels();
106 return std::count(enchannels.cbegin(), enchannels.cend(), true);
107}
108
109boolvec Daq::eninchannels(bool include_monitor) const {
110 boolvec res;
111 if (hasInternalOutputMonitor && include_monitor) {
112 res.push_back(monitorOutput);
113 }
114
115 for (auto &ch : inchannel_config) {
116 res.push_back(ch.enabled);
117 }
118 return res;
119}
121 boolvec res;
122 for (auto &ch : outchannel_config) {
123 res.push_back(ch.enabled);
124 }
125 return res;
126}
string apiname
Configuration of a DAQ device.
std::vector< DaqChannel > enabledInChannels(const bool include_monitor=true) const
Return list of enabled input channels.
int dataTypeIndex
Required datatype for output, should be present in the list.
std::vector< DaqChannel > inchannel_config
Channel configuration for input channels.
bool monitorOutput
If set to true and if the device has this capability, the output channels are added as input channels...
int sampleRateIndex
Index in list of sample rates that are available for the device.
std::vector< DaqChannel > outchannel_config
Channel configuration for output channels.
bool match(const DeviceInfo &devinfo) const
Check to see whether the DAQ configuration matches with the device. This means, some basic checks are...
const DataTypeDescriptor & dtypeDescr() const
More elaborate description of the datatypes.
Definition lasp_daq.cpp:86
std::runtime_error rte
Definition lasp_daq.h:86
us neninchannels(bool include_monitorchannels=true) const
Returns the number of enabled input channels.
Definition lasp_daq.cpp:99
boolvec eninchannels(const bool include_monitor=true) const
Create a vector of boolean values of the enabled input channels.
Definition lasp_daq.cpp:109
static std::unique_ptr< Daq > createDaq(const DeviceInfo &devinfo, const DaqConfiguration &config)
Create a Daq based on given device info and configuration.
Definition lasp_daq.cpp:20
bool duplexMode() const
Whether the device runs in duplex mode (both input and output), or false if only input / only output.
Definition lasp_daq.h:224
virtual ~Daq()=0
Definition lasp_daq.cpp:18
double samplerate() const
Returns current sample rate.
Definition lasp_daq.cpp:78
DataTypeDescriptor::DataType dataType() const
Returns datatype (enum) corresponding to the datatype of the samples.
Definition lasp_daq.cpp:83
boolvec enoutchannels() const
Create an array of booleans for each enabled output channel.
Definition lasp_daq.cpp:120
dvec inputRangeForEnabledChannels(const bool include_monitor=true) const
Returns the input range for each channel. Maximum allowed absolute value of the signal that can pass ...
Definition lasp_daq.cpp:90
Daq(const DeviceInfo &devinfo, const DaqConfiguration &config)
Definition lasp_daq.cpp:43
us nenoutchannels() const
Returns the number of enabled output channels.
Definition lasp_daq.cpp:104
Descriptor for data types containing more detailed information.
DataType
Basic data types coming from a DAQ that we can deal with. The naming will be self-explainging.
Structure containing device info parameters.
unsigned ninchannels
The number of input channels available for the device.
unsigned noutchannels
The number of output channels available for the device.
dvec availableSampleRates
Available sample rates the device can run on.
std::vector< DataTypeDescriptor::DataType > availableDataTypes
The available data types the device can output.
DaqApi api
Backend API corresponding to device.
bool hasInternalOutputMonitor
Whether the device has an internal monitor of the output signal. If true, the device is able to monit...
dvec availableInputRanges
Available ranges for the input, i.e. +/- 1V and/or +/- 10 V etc.
std::runtime_error rte
Definition lasp_daq.cpp:16
std::vector< double > dvec
const std::map< DataTypeDescriptor::DataType, const DataTypeDescriptor > dtype_map
std::vector< bool > boolvec
std::unique_ptr< Daq > createPortAudioDevice(const DeviceInfo &devinfo, const DaqConfiguration &config)
Method called from Daq::createDaq.
std::unique_ptr< Daq > createRtAudioDevice(const DeviceInfo &devinfo, const DaqConfiguration &config)
Method called from Daq::createDaq.
std::unique_ptr< Daq > createUlDaqDevice(const DeviceInfo &devinfo, const DaqConfiguration &config)
size_t us
We often use boolean values.
Definition lasp_types.h:29