-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.cpp
94 lines (82 loc) · 2.89 KB
/
reader.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
#include "reader.h"
#include <lsl_cpp.h>
#include <string>
// eegoSports
#define WIN32_LEAN_AND_MEAN
#define EEGO_SDK_BIND_DYNAMIC
#define _UNICODE
//#include <windows.h>
#include "eemagine/sdk/factory.h"
using namespace eemagine::sdk;
// --- DECONSTRUCTOR ---
Reader::~Reader() { }
void Reader::setParams(int sampling_rate) {
this->samplingRate = sampling_rate;
}
// --- PROCESS ---
// Start processing data.
void Reader::read() {
bool ampFound = true;
try {
factory fact(dllpath);
amp = fact.getAmplifier();
eegStream = amp->OpenEegStream(samplingRate);
std::vector<channel> channelList = eegStream->getChannelList();
// create data streaminfo and append some meta-data
lsl::stream_info data_info("eegoSports " + amp->getSerialNumber(), "EEG", channelList.size() - 2, samplingRate, lsl::cf_float32, "eegoSports_" + amp->getSerialNumber());
lsl::xml_element channels = data_info.desc().append_child("channels");
for (int k = 0; k < channelList.size() - 2; k++) {
channels.append_child("channel")
.append_child_value("label", "Ch" + std::to_string(k))
.append_child_value("type", "EEG")
.append_child_value("unit", "microvolts");
}
data_info.desc().append_child("acquisition")
.append_child_value("manufacturer", "antneuro")
.append_child_value("serial_number", amp->getSerialNumber());
// make a data outlet
lsl::stream_outlet data_outlet(data_info);
// create marker streaminfo and outlet
lsl::stream_info marker_info("eegoSports-" + amp->getSerialNumber() + "_markers" + "Markers", "Markers", 1, 0, lsl::cf_string, "eegoSports_" + amp->getSerialNumber() + "_markers");
lsl::stream_outlet marker_outlet(marker_info);
std::vector<channel> eegChannelList = eegStream->getChannelList();
int timeout_count = 0;
while (!stop) {
buffer buffer = eegStream->getData();
auto channelCount = buffer.getChannelCount();
auto sampleCount = buffer.size() / channelCount;
std::vector<std::vector<float>> send_buffer(sampleCount, std::vector<float>(channelCount - 2));
for (unsigned int s = 0; s < sampleCount; s++) {
for (unsigned int c = 0; c < channelCount - 2; c++) {
send_buffer[s][c] = buffer.getSample(c, s);
}
}
double now = lsl::local_clock();
data_outlet.push_chunk(send_buffer, now);
unsigned int last_mrk = 0;
for (unsigned int s = 0; s < sampleCount; s++) {
//if (int mrk = src_buffer[channelCount + s*(channelCount + 1)]) {
unsigned int mrk = buffer.getSample(channelCount - 2, s);
if (mrk != last_mrk) {
std::string mrk_string = std::to_string(mrk);
marker_outlet.push_sample(&mrk_string, now + (s + 1 - sampleCount) / samplingRate);
last_mrk = mrk;
}
}
}
}
catch (exceptions::notFound) {
ampFound = false;
emit ampNotFound();
}
catch (exceptions::notConnected) {
emit connectionLost();
}
catch (std::exception &e) {
}
if (ampFound) {
delete eegStream;
delete amp;
}
emit finished();
}