forked from open-ephys/plugin-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestFixtures.h
More file actions
281 lines (236 loc) · 8.84 KB
/
TestFixtures.h
File metadata and controls
281 lines (236 loc) · 8.84 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
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
#ifndef TESTFIXTURES_H
#define TESTFIXTURES_H
#include "../Processors/FakeSourceNode.h"
#include <Audio/AudioComponent.h>
#include <Processors/ProcessorGraph/ProcessorGraph.h>
#include <Processors/SourceNode/SourceNode.h>
#include <UI/ControlPanel.h>
#include <juce_audio_processors/juce_audio_processors.h>
#include <Processors/GenericProcessor/GenericProcessor.h>
enum class TestSourceNodeType
{
Fake,
Base
};
class TestSourceNodeBuilder
{
public:
TestSourceNodeBuilder (FakeSourceNodeParams params) :
mockSourceNodeParams (params),
sourceNodeType (TestSourceNodeType::Fake),
dataThreadCreator (nullptr)
{}
TestSourceNodeBuilder (DataThreadCreator creator) :
dataThreadCreator (creator),
sourceNodeType (TestSourceNodeType::Base)
{}
SourceNode* buildSourceNode()
{
switch (sourceNodeType)
{
case TestSourceNodeType::Fake:
{
return (SourceNode*) (new FakeSourceNode (mockSourceNodeParams));
}
case TestSourceNodeType::Base:
{
return new SourceNode ("BaseSourceNode", dataThreadCreator);
}
default:
break;
}
return nullptr;
}
TestSourceNodeType getTestSourceNodeType() const
{
return sourceNodeType;
}
private:
FakeSourceNodeParams mockSourceNodeParams;
DataThreadCreator dataThreadCreator;
TestSourceNodeType sourceNodeType;
};
class ProcessorTester
{
public:
ProcessorTester (TestSourceNodeBuilder sourceNodeBuilder)
{
// Singletons...
MessageManager::deleteInstance();
// initializes the singleton instance
MessageManager::getInstance();
// Reset all state so no interactions between tests.
AccessClass::clearAccessClassStateForTesting();
//Create LookAndFeel object
customLookAndFeel = std::make_unique<CustomLookAndFeel>();
LookAndFeel::setDefaultLookAndFeel (customLookAndFeel.get());
// All of these sets the global state in AccessClass in their constructors
//audioComponent = std::make_unique<AudioComponent>();
processorGraph = std::make_unique<ProcessorGraph> (true);
//controlPanel = std::make_unique<ControlPanel> (processorGraph.get(), audioComponent.get(), true);
SourceNode* snTemp = sourceNodeBuilder.buildSourceNode();
sourceNodeId = nextProcessorId++;
snTemp->setNodeId (sourceNodeId);
snTemp->registerParameters();
juce::AudioProcessorGraph::Node* n = processorGraph->addNode (
std::move (std::unique_ptr<AudioProcessor> (snTemp)),
juce::AudioProcessorGraph::NodeID (sourceNodeId));
// Create the source node, and place it in the graph
auto sn = (GenericProcessor*) n->getProcessor();
sn->setHeadlessMode (true);
sn->initialize (false);
sn->setDestNode (nullptr);
//controlPanel->updateRecordEngineList();
// Refresh everything
processorGraph->updateSettings (sn);
//controlPanel->colourChanged();
}
virtual ~ProcessorTester()
{
controlPanel = nullptr;
processorGraph = nullptr;
audioComponent = nullptr;
AccessClass::clearAccessClassStateForTesting();
DeletedAtShutdown::deleteAll();
MessageManager::deleteInstance();
}
/**
* Create a new GenericProcessor instance for testing. In addition to creating the processor, it attaches it into
* the processor graph as needed, and refreshes necessary state. Note that currently, it always attaches this to
* the FakeSourceNode created in SetUp(), and thus you cannot call this multiple times at this point (though it
* would not be hard to extend this to create a longer processor graph).
* @tparam T processor class, derived from GenericProcessor
* @param args parameters to pass to constructor of the processor, i.e. new FooProcessor(<args>)
*/
template <
typename T,
class... Args,
typename std::enable_if<std::is_base_of<GenericProcessor, T>::value>::type* = nullptr>
T* createProcessor (Plugin::Processor::Type processorType, Args&&... args)
{
T* ptr = new T (std::forward<Args> (args)...);
ptr->setProcessorType (processorType);
ptr->setHeadlessMode (true);
int nodeId = nextProcessorId++;
ptr->setNodeId (nodeId);
ptr->registerParameters();
processorGraph->addNode (
std::move (std::unique_ptr<AudioProcessor> (ptr)),
juce::AudioProcessorGraph::NodeID (nodeId));
ptr = (T*) processorGraph->getProcessorWithNodeId (nodeId);
ptr->initialize (false);
ptr->setDestNode (nullptr);
// Place the newly created node into the graph
auto sourceNode = processorGraph->getProcessorWithNodeId (sourceNodeId);
ptr->setSourceNode (sourceNode);
sourceNode->setDestNode (ptr);
// Refresh everything
processorGraph->updateSettings (ptr);
return (T*) processorGraph->getProcessorWithNodeId (nodeId);
}
void startAcquisition (bool startRecording, bool forceRecording = false)
{
if (startRecording)
{
// Do it this way to ensure the GUI elements (which apparently control logic) are set properly
controlPanel->setRecordingState (true, forceRecording);
}
else
{
controlPanel->startAcquisition (false);
}
}
void stopAcquisition()
{
controlPanel->stopAcquisition();
}
const DataStream* getProcessorDataStream (uint16 nodeId, uint16 streamId)
{
auto streams = processorGraph->getProcessorWithNodeId (nodeId)->getDataStreams();
for (auto stream : streams)
{
if (stream->getStreamId() == streamId)
{
return stream;
}
}
return nullptr;
}
const DataStream* getSourceNodeDataStream (uint16 streamId)
{
return getProcessorDataStream (sourceNodeId, streamId);
}
static void setRecordingParentDirectory (const std::string& parent_directory)
{
CoreServices::setRecordingParentDirectory (String (parent_directory));
}
GenericProcessor* getSourceNode()
{
return processorGraph->getProcessorWithNodeId (sourceNodeId);
}
AudioBuffer<float> processBlock (
GenericProcessor* processor,
const AudioBuffer<float>& buffer,
TTLEvent* maybeTtlEvent = nullptr)
{
auto audioProcessor = (AudioProcessor*) processor;
auto dataStreams = processor->getDataStreams();
MidiBuffer eventBuffer;
for (const auto* datastream : dataStreams)
{
HeapBlock<char> data;
auto streamId = datastream->getStreamId();
size_t dataSize = SystemEvent::fillTimestampAndSamplesData (
data,
processor,
streamId,
currentSampleIndex,
// NOTE: this timestamp is actually ignored in the current implementation?
0,
buffer.getNumSamples(),
0);
eventBuffer.addEvent (data, dataSize, 0);
if (maybeTtlEvent != nullptr)
{
size_t ttlSize = maybeTtlEvent->getChannelInfo()->getDataSize() + maybeTtlEvent->getChannelInfo()->getTotalEventMetadataSize() + EVENT_BASE_SIZE;
HeapBlock<char> ttlBuffer (ttlSize);
maybeTtlEvent->serialize (ttlBuffer, ttlSize);
eventBuffer.addEvent (ttlBuffer, ttlSize, 0);
}
}
// Copies the input buffer so that remains unmodified
AudioBuffer<float> outputBuffer = buffer;
audioProcessor->processBlock (outputBuffer, eventBuffer);
currentSampleIndex += buffer.getNumSamples();
return outputBuffer;
}
void updateSourceNodeSettings()
{
processorGraph->updateSettings (getSourceNode());
}
public:
int sourceNodeId; // should dynamically retrieve the processor if needed, since it apparently gets re-allocated
std::unique_ptr<AudioComponent> audioComponent;
std::unique_ptr<ProcessorGraph> processorGraph;
std::unique_ptr<ControlPanel> controlPanel;
int nextProcessorId = 1;
int currentSampleIndex = 0;
std::unique_ptr<CustomLookAndFeel> customLookAndFeel;
};
class DataThreadTester : public ProcessorTester
{
public:
DataThreadTester (TestSourceNodeBuilder sourceNodeBuilder) :
ProcessorTester (sourceNodeBuilder)
{}
template <
typename T,
class... Args,
typename std::enable_if<std::is_base_of<DataThread, T>::value>::type* = nullptr>
T* createDataThread (Args&&... args)
{
T* ptr = new T ((SourceNode*) getSourceNode(), std::forward<Args> (args)...);
return ptr;
}
};
#endif