lsl-sys 0.1.1

Low-level bindings to the system liblsl library (lab streaming layer).
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include "stream_inlet_impl.h"

extern "C" {
#include "../include/lsl_c.h"
// === implementation of the stream_inlet class ===

using namespace lsl;

/**
* Construct a new stream inlet from a resolved stream info.
* @param info A resolved stream info object (as coming from one of the resolver functions).
* @param max_buflen Optionally the maximum amount of data to buffer (in seconds if there is a nominal 
*					sampling rate, otherwise x100 in samples). Recording applications want to use a fairly 
*					large buffer size here, while real-time applications would only buffer as much as 
*					they need to perform their next calculation.
* @param max_chunklen Optionally the maximum size, in samples, at which chunks are transmitted 
*					  (the default corresponds to the chunk sizes used by the sender).
*				      Recording applications can use a generous size here (leaving it to the network how 
*					  to pack things), while real-time applications may want a finer (perhaps 1-sample) granularity.
* @param recover Try to silently recover lost streams that are recoverable (=those that that have a source_id set). 
*				 In all other cases (recover is false or the stream is not recoverable) functions may throw an 
*				 lsl_lost_error if the stream's source is lost (e.g., due to an app or computer crash).
*/
LIBLSL_C_API lsl_inlet lsl_create_inlet(lsl_streaminfo info, int32_t max_buflen, int32_t max_chunklen, int32_t recover) {
	try {
		stream_info_impl *infoimpl = (stream_info_impl*)info;
		lsl_inlet result = (lsl_inlet)new stream_inlet_impl(*infoimpl, infoimpl->nominal_srate()?(int)(infoimpl->nominal_srate()*max_buflen):max_buflen*100, max_chunklen, recover!=0);
		return result;
	} 
	catch(std::invalid_argument &e) {
		std::cerr << "Error during construction of a stream_inlet: " << e.what() << std::endl;
		return NULL;
	}
	catch(std::exception &e) {
		std::cerr << "Unexpected error during construction of a stream_inlet: " << e.what() << std::endl;
		return NULL;
	}
}

/** 
* Destructor.
* The inlet will automatically disconnect if destroyed.
*/
LIBLSL_C_API void lsl_destroy_inlet(lsl_inlet in) { 
	try {
		delete (stream_inlet_impl*)in; 
	} catch(std::exception &e) {
		std::cerr << "Unexpected during destruction of a stream_inlet: " << e.what() << std::endl;
	}
}

/**
* Retrieve the complete information of the given stream, including the extended description.
* Can be invoked at any time of the stream's lifetime.
* @param timeout Timeout of the operation.
* @param ec Error code: if nonzero, can be either lsl_timeout_error (if the timeout has expired) or lsl_lost_error (if the stream source has been lost).
*/
LIBLSL_C_API lsl_streaminfo lsl_get_fullinfo(lsl_inlet in, double timeout, int32_t *ec) {
	if (ec)
		*ec = lsl_no_error;
	try {
		return (lsl_streaminfo)new stream_info_impl(((stream_inlet_impl*)in)->info(timeout));
	}
	catch(timeout_error &) { 
		if (ec)
			*ec = lsl_timeout_error; 
	}
	catch(lost_error &) { 
		if (ec)
			*ec = lsl_lost_error; 
	}
	catch(std::exception &e) {
		std::cerr << "Unexpected error in " << __func__ << ": " << e.what() << std::endl;
		if (ec)
			*ec = lsl_internal_error; 
	}
	return NULL;
}

/**
* Subscribe to the data stream.
* All samples pushed in at the other end from this moment onwards will be queued and 
* eventually be delivered in response to pull_sample() calls.
* Pulling a sample without some preceding lsl_open_stream() is permitted (the stream will then be opened implicitly).
* @param timeout Optional timeout of the operation.
* @param ec Error code: if nonzero, can be either lsl_timeout_error (if the timeout has expired) or lsl_lost_error (if the stream source has been lost).
*/
LIBLSL_C_API void lsl_open_stream(lsl_inlet in, double timeout, int32_t *ec) {
	if (ec)
		*ec = lsl_no_error;
	try {
		((stream_inlet_impl*)in)->open_stream(timeout);
	}
	catch(timeout_error &) { 
		if (ec)
			*ec = lsl_timeout_error; 
	}
	catch(lost_error &) { 
		if (ec)
			*ec = lsl_lost_error; 
	}
	catch(std::exception &e) {
		std::cerr << "Unexpected error in " << __func__ << ": " << e.what() << std::endl;
		if (ec)
			*ec = lsl_internal_error; 
	}
}

/**
* Drop the current data stream.
* All samples that are still buffered or in flight will be dropped and the source will halt its buffering of data for this inlet.
* If an application stops being interested in data from a source (temporarily or not) but keeps the outlet alive, it should 
* call lsl_close_stream() to not pressure the source outlet to buffer unnecessarily large amounts of data.
*/
LIBLSL_C_API void lsl_close_stream(lsl_inlet in) { 
	try {
		((stream_inlet_impl*)in)->close_stream();
	} catch(std::exception &e) {
		std::cerr << "Unexpected error during close_stream(): " << e.what() << std::endl;
	}
}

/**
* Retrieve an estimated time correction offset for the given stream.
* The first call to this function takes several milliseconds until a reliable first estimate is obtained.
* Subsequent calls are instantaneous (and rely on periodic background updates).
* The precision of these estimates should be below 1 ms (empirically within +/-0.2 ms).
* @timeout Timeout to acquire the first time-correction estimate.
* @return The time correction estimate. If the first estimate cannot within the alloted time, the result is NaN.
* @param ec Error code: if nonzero, can be either lsl_timeout_error (if the timeout has expired) or lsl_lost_error (if the stream source has been lost).
*/
LIBLSL_C_API double lsl_time_correction(lsl_inlet in, double timeout, int32_t *ec) {
	if (ec)
		*ec = lsl_no_error;
	try {
		return ((stream_inlet_impl*)in)->time_correction(timeout);
	}
	catch(timeout_error &) { 
		if (ec)
			*ec = lsl_timeout_error; 
	}
	catch(lost_error &) { 
		if (ec)
			*ec = lsl_lost_error; 
	}
	catch(std::exception &e) {
		std::cerr << "Unexpected error in " << __func__ << ": " << e.what() << std::endl;
		if (ec)
			*ec = lsl_internal_error; 
	}
	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 = ((stream_inlet_impl*)in)->time_correction(remote_time, uncertainty, timeout);
		return correction;
	}
	catch(timeout_error &) {
		if (ec)
			*ec = lsl_timeout_error;
	}
	catch(lost_error &) {
		if (ec)
			*ec = lsl_lost_error;
	}
	catch(std::exception &) {
		if (ec)
			*ec = lsl_internal_error;
	}
	return 0.0;
}

/**
* Set post-processing flags to use. 
*/
LIBLSL_C_API int32_t lsl_set_postprocessing(lsl_inlet in, uint32_t flags) {
	try {
		((stream_inlet_impl*)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 === */

/**
* Pull a sample from the inlet and read it into a pointer to values.
* Handles type checking & conversion.
* @param buffer A pointer to hold the resulting values. 
* @param buffer_elements The number of samples allocated in the buffer. Note: it is the responsibility of the user to allocate enough memory.
* @param timeout The timeout for this operation, if any.
* @return The capture time of the sample on the remote machine, or 0.0 if no new sample was available. 
*		  To remap this time stamp to the local clock, add the value returned by lsl_time_correction() to it. 
* @param ec Error code: if nonzero, can be either lsl_timeout_error (if the timeout has expired) or lsl_lost_error (if the stream source has been lost).
*/
LIBLSL_C_API double lsl_pull_sample_f(lsl_inlet in, float *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
		return ((stream_inlet_impl*)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 ((stream_inlet_impl*)in)->pull_sample_noexcept(buffer,buffer_elements,timeout,(lsl_error_code_t*) ec);
}

LIBLSL_C_API double lsl_pull_sample_l(lsl_inlet in, long *buffer, int32_t buffer_elements, double timeout, int32_t *ec) {
		return ((stream_inlet_impl*)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 ((stream_inlet_impl*)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 ((stream_inlet_impl*)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 ((stream_inlet_impl*)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 = ((stream_inlet_impl*)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] == NULL) {
				for (std::size_t k2=0;k2<k;k2++)
					free(buffer[k2]);
				if (ec) *ec = lsl_internal_error;
				return 0.0;
			}
			strcpy(buffer[k],tmp[k].c_str());
		}
		return result;
	}
	catch(timeout_error &) { 
		if (ec)
			*ec = lsl_timeout_error; 
	}
	catch(lost_error &) { 
		if (ec)
			*ec = lsl_lost_error; 
	}
	catch(std::invalid_argument &) { 
		if (ec)
			*ec = lsl_argument_error; 
	}
	catch(std::range_error &) {
		if (ec)
			*ec = lsl_argument_error; 
	}
	catch(std::exception &e) {
		std::cerr << "Unexpected error in " << __func__ << ": " << e.what() << std::endl;
		if (ec)
			*ec = lsl_internal_error; 
	}
	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 = ((stream_inlet_impl*)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] == NULL) {
				for (std::size_t k2=0;k2<k;k++)
					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][0],tmp[k].size());
		}
		return result;
	}
	catch(timeout_error &) { 
		if (ec)
			*ec = lsl_timeout_error; 
	}
	catch(lost_error &) { 
		if (ec)
			*ec = lsl_lost_error; 
	}
	catch(std::invalid_argument &) { 
		if (ec)
			*ec = lsl_argument_error; 
	}
	catch(std::range_error &) {
		if (ec)
			*ec = lsl_argument_error; 
	}
	catch(std::exception &e) {
		std::cerr << "Unexpected error in " << __func__ << ": " << e.what() << std::endl;
		if (ec)
			*ec = lsl_internal_error; 
	}
	return 0.0;
}


/**
* Pull a sample from the inlet and read it into a custom struct or buffer. 
* Overall size checking but no type checking or conversion are done. Do not use for variable-size/string-formatted streams.
* @param sample Pointer to hold the sample data. Search for #pragma pack for information on how to pack structs appropriately.
* @param timeout The timeout for this operation, if any.
* @return The capture time of the sample on the remote machine, or 0.0 if no new sample was available. 
*		   To remap this time stamp to the local clock, add the value returned by .time_correction() to it. 
* @param ec Error code: if nonzero, can be either lsl_timeout_error (if the timeout has expired) or lsl_lost_error (if the stream source has been lost).
*/
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 ((stream_inlet_impl*)in)->pull_numeric_raw(buffer,buffer_bytes,timeout);
	}
	catch(timeout_error &) { 
		if (ec)
			*ec = lsl_timeout_error; 
	}
	catch(lost_error &) { 
		if (ec)
			*ec = lsl_lost_error; 
	}
	catch(std::invalid_argument &) { 
		if (ec)
			*ec = lsl_argument_error; 
	}
	catch(std::range_error &) {
		if (ec)
			*ec = lsl_argument_error; 
	}
	catch(std::exception &e) {
		std::cerr << "Unexpected error in " << __func__ << ": " << e.what() << std::endl;
		if (ec)
			*ec = lsl_internal_error; 
	}
	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 ((stream_inlet_impl*)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 ((stream_inlet_impl*)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, long *data_buffer, double *timestamp_buffer, unsigned long data_buffer_elements, unsigned long timestamp_buffer_elements, double timeout, int32_t *ec) {
	return ((stream_inlet_impl*)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 ((stream_inlet_impl*)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 ((stream_inlet_impl*)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 ((stream_inlet_impl*)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);
			unsigned long result = ((stream_inlet_impl*)in)->pull_chunk_multiplexed(&tmp[0],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] == NULL) {
					for (std::size_t k2=0;k2<k;k2++)
						free(data_buffer[k2]);
					if (ec) *ec = lsl_internal_error;
					return 0;
				}
				strcpy(data_buffer[k],tmp[k].c_str());
			}
			return result;
		} else
			return 0;
	}
	catch(timeout_error &) { 
		if (ec)
			*ec = lsl_timeout_error; 
	}
	catch(lost_error &) { 
		if (ec)
			*ec = lsl_lost_error; 
	}
	catch(std::invalid_argument &) { 
		if (ec)
			*ec = lsl_argument_error; 
	}
	catch(std::range_error &) {
		if (ec)
			*ec = lsl_argument_error; 
	}
	catch(std::exception &e) {
		std::cerr << "Unexpected error in " << __func__ << ": " << e.what() << std::endl;
		if (ec)
			*ec = lsl_internal_error; 
	}
	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);
			unsigned long result = ((stream_inlet_impl*)in)->pull_chunk_multiplexed(&tmp[0],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] == NULL) {
					for (uint32_t k2=0;k2<k;k++)
						free(data_buffer[k2]);
					if (ec) *ec = lsl_internal_error;
					return 0;
				}
				lengths_buffer[k] = (uint32_t)tmp[k].size();
				strcpy(data_buffer[k],tmp[k].c_str());
			}
			return result;
		} else
			return 0;
	}
	catch(timeout_error &) { 
		if (ec)
			*ec = lsl_timeout_error; 
	}
	catch(lost_error &) { 
		if (ec)
			*ec = lsl_lost_error; 
	}
	catch(std::invalid_argument &) { 
		if (ec)
			*ec = lsl_argument_error; 
	}
	catch(std::range_error &) {
		if (ec)
			*ec = lsl_argument_error; 
	}
	catch(std::exception &e) {
		std::cerr << "Unexpected error in " << __func__ << ": " << e.what() << std::endl;
		if (ec)
			*ec = lsl_internal_error; 
	}
	return 0;
}

/**
* Query the number of samples that are currently available for immediate pickup.
*/
LIBLSL_C_API uint32_t lsl_samples_available(lsl_inlet in) {
	try {
		return (uint32_t)((stream_inlet_impl*)in)->samples_available();
	}
	catch(std::exception &) {
		return 0;
	}
}

/**
* Query whether the clock was potentially reset since the last call to was_clock_reset().
*/
LIBLSL_C_API uint32_t lsl_was_clock_reset(lsl_inlet in) {
	try {
		return (uint32_t)((stream_inlet_impl*)in)->was_clock_reset();
	}
	catch(std::exception &) {
		return 0;
	}
}

/**
* Override the half-time (forget factor) of the time-stamp smoothing.
*/
LIBLSL_C_API int32_t lsl_smoothing_halftime(lsl_inlet in, float value) {
	try {
		((stream_inlet_impl*)in)->smoothing_halftime(value);
		return lsl_no_error;
	}
	catch(std::invalid_argument &) { 
		return lsl_argument_error; 
	}
	catch(std::exception &) {
		return lsl_internal_error;
	}
}
}