forked from open-ephys/plugin-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceNodeTests.cpp
More file actions
137 lines (110 loc) · 3.77 KB
/
SourceNodeTests.cpp
File metadata and controls
137 lines (110 loc) · 3.77 KB
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
#include "gtest/gtest.h"
#include <ProcessorHeaders.h>
#include <TestFixtures.h>
class FakeDataThread : public DataThread
{
public:
FakeDataThread(SourceNode* sn) :
DataThread(sn)
{}
~FakeDataThread() noexcept override
{}
bool updateBuffer() override
{
return false;
}
bool foundInputSource() override
{
return false;
}
bool startAcquisition() override
{
return true;
}
bool stopAcquisition() override
{
return true;
}
void updateSettings(
OwnedArray<ContinuousChannel>* continuousChannels,
OwnedArray<EventChannel>* eventChannels,
OwnedArray<SpikeChannel>* spikeChannels,
OwnedArray<DataStream>* sourceStreams,
OwnedArray<DeviceInfo>* devices,
OwnedArray<ConfigurationObject>* configurationObjects) override
{
}
private:
};
class SourceNodeTests : public testing::Test
{
protected:
void SetUp() override
{
currentSampleIndex = 0;
tester = std::make_unique<DataThreadTester>(TestSourceNodeBuilder(FakeSourceNodeParams{}));
dataThread.reset(tester->createDataThread<FakeDataThread>());
}
AudioBuffer<float> createBuffer(float startingValue, float step, int numChannels, int numSamples)
{
AudioBuffer<float> inputBuffer(numChannels, numSamples);
// in microvolts
float curValue = startingValue;
for (int chidx = 0; chidx < numChannels; chidx++)
{
for (int sample_idx = 0; sample_idx < numSamples; sample_idx++)
{
inputBuffer.setSample(chidx, sample_idx, curValue);
curValue += step;
}
}
return inputBuffer;
}
void writeBlock(AudioBuffer<float>& buffer)
{
auto audioProcessor = (AudioProcessor*)tester->getSourceNode();
auto dataStreams = tester->getSourceNode()->getDataStreams();
ASSERT_EQ(dataStreams.size(), 1);
auto streamId = dataStreams[0]->getStreamId();
HeapBlock<char> data;
size_t dataSize = SystemEvent::fillTimestampAndSamplesData(
data,
tester->getSourceNode(),
streamId,
currentSampleIndex,
0,
buffer.getNumSamples(),
0);
MidiBuffer eventBuffer;
eventBuffer.addEvent(data, dataSize, 0);
auto originalBuffer = buffer;
audioProcessor->processBlock(buffer, eventBuffer);
// Assert the buffer hasn't changed after process()
ASSERT_EQ(buffer.getNumSamples(), originalBuffer.getNumSamples());
ASSERT_EQ(buffer.getNumChannels(), originalBuffer.getNumChannels());
for (int chidx = 0; chidx < buffer.getNumChannels(); chidx++)
{
for (int sampleIdx = 0; sampleIdx < buffer.getNumSamples(); ++sampleIdx)
ASSERT_EQ(buffer.getSample(chidx, sampleIdx), originalBuffer.getSample(chidx, sampleIdx));
}
currentSampleIndex += buffer.getNumSamples();
}
protected:
std::unique_ptr<FakeDataThread> dataThread;
std::unique_ptr<DataThreadTester> tester;
int64_t currentSampleIndex;
};
/*
Source Nodes create an instance of a Data Thread that is responsible for acquiring data.
This data is stored in an internal buffer within the Source Node.
The Source Node regularly polls this buffer and writes any new samples to an output Audio Buffer.
This test verifies that given a Data Thread, the Source Node will perform this write and output an Audio Buffer with data samples.
*/
TEST_F(SourceNodeTests, DataAcquisition)
{
tester->getSourceNode()->startAcquisition();
int numSamples = 100;
auto inputBuffer = createBuffer(1000.0, 20.0, 5, numSamples);
writeBlock(inputBuffer);
tester->getSourceNode()->stopAcquisition();
}