File: ReceiveDataInChunks.cpp

package info (click to toggle)
liblsl 1.16.2b1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,724 kB
  • sloc: cpp: 12,515; ansic: 666; python: 28; sh: 25; makefile: 18
file content (75 lines) | stat: -rw-r--r-- 2,559 bytes parent folder | download
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
#include <chrono>
#include <iostream>
#include <lsl_cpp.h>
#include <stdint.h>
#include <thread>


int main(int argc, char **argv) {
	std::cout << "ReceiveDataInChunks" << std::endl;
	std::cout << "ReceiveDataInChunks StreamName max_buflen flush" << std::endl;
	std::cout << "- max_buffered -- duration in sec (or x100 samples if samplerate is 0) to buffer "
				 "in the receiver"
			  << std::endl;
	std::cout
		<< "- flush -- set non-zero to flush data instead of pulling; useful for testing throughput"
		<< std::endl;

	try {

		std::string name{argc > 1 ? argv[1] : "MyAudioStream"};
		double max_buffered = argc > 2 ? std::stod(argv[2]) : 360.;
		bool flush = argc > 3;
		// resolve the stream of interest & make an inlet
		int32_t buf_samples = (int32_t)(max_buffered * 1000);
		lsl::stream_info inlet_info = lsl::resolve_stream("name", name).at(0);
		lsl::stream_inlet inlet(inlet_info, buf_samples, transp_bufsize_thousandths);

		// Use set_postprocessing to get the timestamps in a common base clock.
		// Do not use if this application will record timestamps to disk -- it is better to 
		//  do posthoc synchronization.
		inlet.set_postprocessing(lsl::post_ALL);

		// Inlet opening is implicit when doing pull_sample or pull_chunk.
		// Here we open the stream explicitly because we might be doing
		//  `flush` only.
		inlet.open_stream();

		double starttime = lsl::local_clock(), next_display = starttime + 1,
			   next_reset = starttime + 10;

		// and retrieve the chunks
		uint64_t k = 0, num_samples = 0;
		std::vector<std::vector<int16_t>> result;
		auto fetch_interval = std::chrono::milliseconds(20);
		auto next_fetch = std::chrono::steady_clock::now() + fetch_interval;


		while (true) {
			std::this_thread::sleep_until(next_fetch);
			if (flush) {
				// You almost certainly don't want to use flush. This is here so we
				//  can test maximum outlet throughput.
				num_samples += inlet.flush();
			} else {
				if (double timestamp = inlet.pull_chunk(result)) num_samples += result.size();
			}
			k++;
			next_fetch += fetch_interval;
			if (k % 50 == 0) {
				double now = lsl::local_clock();
				std::cout << num_samples / (now - starttime) << " samples/sec" << std::endl;
				if (now > next_reset) {
					std::cout << "Resetting counters..." << std::endl;
					starttime = now;
					next_reset = now + 10;
					num_samples = 0;
				}
			}
		}

	} catch (std::exception &e) { std::cerr << "Got an exception: " << e.what() << std::endl; }
	std::cout << "Press any key to exit. " << std::endl;
	std::cin.get();
	return 0;
}