-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathlsl_inlet_c.cpp
305 lines (272 loc) · 10.7 KB
/
lsl_inlet_c.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "lsl_c_api_helpers.hpp"
#include "stream_inlet_impl.h"
#include <cstdlib>
#include <exception>
#include <loguru.hpp>
#include <stdexcept>
#include <string>
#include <vector>
extern "C" {
#include "api_types.hpp"
// include api_types before public API header
#include "../include/lsl/inlet.h"
using namespace lsl;
LIBLSL_C_API lsl_inlet lsl_create_inlet_ex(lsl_streaminfo info, int32_t max_buflen,
int32_t max_chunklen, int32_t recover, lsl_transport_options_t flags) {
try {
int32_t buf_samples = info->calc_transport_buf_samples(max_buflen, flags);
return create_object_noexcept<stream_inlet_impl>(
*info, buf_samples, max_chunklen, recover != 0);
}
LSLCATCHANDSTORE(nullptr, std::invalid_argument, lsl_argument_error);
return nullptr;
}
LIBLSL_C_API lsl_inlet lsl_create_inlet(
lsl_streaminfo info, int32_t max_buflen, int32_t max_chunklen, int32_t recover) {
return lsl_create_inlet_ex(info, max_buflen, max_chunklen, recover, transp_default);
}
LIBLSL_C_API void lsl_destroy_inlet(lsl_inlet in) {
try {
delete in;
} catch (std::exception &e) { LOG_F(ERROR, "Unexpected error in %s: %s", __func__, e.what()); }
}
LIBLSL_C_API lsl_streaminfo lsl_get_fullinfo(lsl_inlet in, double timeout, int32_t *ec) {
try {
return new stream_info_impl(in->info(timeout));
}
LSL_STORE_EXCEPTION_IN(ec)
return nullptr;
}
LIBLSL_C_API void lsl_open_stream(lsl_inlet in, double timeout, int32_t *ec) {
if (ec) *ec = lsl_no_error;
try {
in->open_stream(timeout);
} LSL_STORE_EXCEPTION_IN(ec)
}
LIBLSL_C_API void lsl_close_stream(lsl_inlet in) {
try {
in->close_stream();
} catch (std::exception &e) { LOG_F(ERROR, "Unexpected error in %s: %s", __func__, e.what()); }
}
LIBLSL_C_API double lsl_time_correction(lsl_inlet in, double timeout, int32_t *ec) {
if (ec) *ec = lsl_no_error;
try {
return in->time_correction(timeout);
} LSL_STORE_EXCEPTION_IN(ec)
return 0.0;
}
LIBLSL_C_API double lsl_time_correction_ex(
lsl_inlet in, double *remote_time, double *uncertainty, double timeout, int32_t *ec) {
if (ec) *ec = lsl_no_error;
try {
double correction = in->time_correction(remote_time, uncertainty, timeout);
return correction;
} LSL_STORE_EXCEPTION_IN(ec)
return 0.0;
}
LIBLSL_C_API int32_t lsl_set_postprocessing(lsl_inlet in, uint32_t flags) {
try {
in->set_postprocessing(flags);
return lsl_no_error;
} catch (std::invalid_argument &) { return lsl_argument_error; } catch (std::exception &) {
return lsl_internal_error;
}
}
/* === Pulling a sample from the inlet === */
LIBLSL_C_API double lsl_pull_sample_f(
lsl_inlet in, float *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API double lsl_pull_sample_d(
lsl_inlet in, double *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API double lsl_pull_sample_l(
lsl_inlet in, int64_t *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API double lsl_pull_sample_i(
lsl_inlet in, int32_t *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API double lsl_pull_sample_s(
lsl_inlet in, int16_t *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API double lsl_pull_sample_c(
lsl_inlet in, char *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
return in->pull_sample_noexcept(buffer, buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API double lsl_pull_sample_str(
lsl_inlet in, char **buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
if (ec) *ec = lsl_no_error;
try {
// capture output in a temporary string buffer
std::vector<std::string> tmp;
double result = in->pull_sample(tmp, timeout);
if (buffer_elements < (int)tmp.size())
throw std::range_error(
"The provided buffer has fewer elements than the stream's number of channels.");
// allocate memory and copy over into buffer
for (std::size_t k = 0; k < tmp.size(); k++) {
buffer[k] = (char *)malloc(tmp[k].size() + 1);
if (buffer[k] == nullptr) {
for (std::size_t k2 = 0; k2 < k; k2++) free(buffer[k2]);
if (ec) *ec = lsl_internal_error;
return 0.0;
}
memcpy(buffer[k], tmp[k].data(), tmp[k].size());
buffer[k][tmp[k].size()] = '\0';
}
return result;
} LSL_STORE_EXCEPTION_IN(ec)
return 0.0;
}
LIBLSL_C_API double lsl_pull_sample_buf(lsl_inlet in, char **buffer, uint32_t *buffer_lengths,
int32_t buffer_elements, double timeout, int32_t *ec) {
if (ec) *ec = lsl_no_error;
try {
// capture output in a temporary string buffer
std::vector<std::string> tmp;
double result = in->pull_sample(tmp, timeout);
if (buffer_elements < (int)tmp.size())
throw std::range_error(
"The provided buffer has fewer elements than the stream's number of channels.");
// allocate memory and copy over into buffer
for (std::size_t k = 0; k < tmp.size(); k++) {
buffer[k] = (char *)malloc(tmp[k].size());
if (buffer[k] == nullptr) {
for (std::size_t k2 = 0; k2 < k; k2++) free(buffer[k2]);
if (ec) *ec = lsl_internal_error;
return 0.0;
}
buffer_lengths[k] = (uint32_t)tmp[k].size();
memcpy(buffer[k], tmp[k].data(), tmp[k].size());
}
return result;
} LSL_STORE_EXCEPTION_IN(ec)
return 0.0;
}
LIBLSL_C_API double lsl_pull_sample_v(
lsl_inlet in, void *buffer, int32_t buffer_bytes, double timeout, int32_t *ec) {
if (ec) *ec = lsl_no_error;
try {
return in->pull_numeric_raw(buffer, buffer_bytes, timeout);
} LSL_STORE_EXCEPTION_IN(ec)
return 0.0;
}
LIBLSL_C_API unsigned long lsl_pull_chunk_f(lsl_inlet in, float *data_buffer,
double *timestamp_buffer, unsigned long data_buffer_elements,
unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API unsigned long lsl_pull_chunk_d(lsl_inlet in, double *data_buffer,
double *timestamp_buffer, unsigned long data_buffer_elements,
unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API unsigned long lsl_pull_chunk_l(lsl_inlet in, int64_t *data_buffer,
double *timestamp_buffer, unsigned long data_buffer_elements,
unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API unsigned long lsl_pull_chunk_i(lsl_inlet in, int32_t *data_buffer,
double *timestamp_buffer, unsigned long data_buffer_elements,
unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API unsigned long lsl_pull_chunk_s(lsl_inlet in, int16_t *data_buffer,
double *timestamp_buffer, unsigned long data_buffer_elements,
unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API unsigned long lsl_pull_chunk_c(lsl_inlet in, char *data_buffer,
double *timestamp_buffer, unsigned long data_buffer_elements,
unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
return in->pull_chunk_multiplexed_noexcept(data_buffer, timestamp_buffer, data_buffer_elements,
timestamp_buffer_elements, timeout, (lsl_error_code_t *)ec);
}
LIBLSL_C_API unsigned long lsl_pull_chunk_str(lsl_inlet in, char **data_buffer,
double *timestamp_buffer, unsigned long data_buffer_elements,
unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
if (ec) *ec = lsl_no_error;
try {
// capture output in a temporary string buffer
if (data_buffer_elements) {
std::vector<std::string> tmp(data_buffer_elements);
uint32_t result = in->pull_chunk_multiplexed(tmp.data(), timestamp_buffer,
data_buffer_elements, timestamp_buffer_elements, timeout);
// allocate memory and copy over into buffer
for (std::size_t k = 0; k < tmp.size(); k++) {
data_buffer[k] = (char *)malloc(tmp[k].size() + 1);
if (data_buffer[k] == nullptr) {
for (std::size_t k2 = 0; k2 < k; k2++) free(data_buffer[k2]);
if (ec) *ec = lsl_internal_error;
return 0;
}
memcpy(data_buffer[k], tmp[k].data(), tmp[k].size());
data_buffer[k][tmp[k].size()] = '\0';
}
return result;
}
return 0;
}
LSL_STORE_EXCEPTION_IN(ec)
return 0;
}
LIBLSL_C_API unsigned long lsl_pull_chunk_buf(lsl_inlet in, char **data_buffer,
uint32_t *lengths_buffer, double *timestamp_buffer, unsigned long data_buffer_elements,
unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
if (ec) *ec = lsl_no_error;
try {
// capture output in a temporary string buffer
if (data_buffer_elements) {
std::vector<std::string> tmp(data_buffer_elements);
uint32_t result = in->pull_chunk_multiplexed(tmp.data(), timestamp_buffer,
data_buffer_elements, timestamp_buffer_elements, timeout);
// allocate memory and copy over into buffer
for (uint32_t k = 0; k < tmp.size(); k++) {
data_buffer[k] = (char *)malloc(tmp[k].size() + 1);
if (data_buffer[k] == nullptr) {
for (uint32_t k2 = 0; k2 < k; k2++) free(data_buffer[k2]);
if (ec) *ec = lsl_internal_error;
return 0;
}
lengths_buffer[k] = (uint32_t)tmp[k].size();
memcpy(data_buffer[k], tmp[k].data(), tmp[k].size());
data_buffer[k][tmp[k].size()] = '\0';
}
return result;
}
return 0;
}
LSL_STORE_EXCEPTION_IN(ec)
return 0;
}
LIBLSL_C_API uint32_t lsl_samples_available(lsl_inlet in) {
try {
return (uint32_t)in->samples_available();
} catch (std::exception &) { return 0; }
}
LIBLSL_C_API uint32_t lsl_inlet_flush(lsl_inlet in) {
return in->flush();
}
LIBLSL_C_API uint32_t lsl_was_clock_reset(lsl_inlet in) {
try {
return (uint32_t)in->was_clock_reset();
} catch (std::exception &) { return 0; }
}
LIBLSL_C_API int32_t lsl_smoothing_halftime(lsl_inlet in, float value) {
try {
in->smoothing_halftime(value);
return lsl_no_error;
} catch (std::invalid_argument &) { return lsl_argument_error; } catch (std::exception &) {
return lsl_internal_error;
}
}
}