-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.cpp
More file actions
346 lines (267 loc) · 9.81 KB
/
FileReader.cpp
File metadata and controls
346 lines (267 loc) · 9.81 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
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
/*
------------------------------------------------------------------
This file is part of the Open Ephys GUI
Copyright (C) 2016 Open Ephys
------------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FileReader.h"
#include "FileReaderEditor.h"
#include <stdio.h>
#include "../../AccessClass.h"
#include "../PluginManager/PluginManager.h"
FileReader::FileReader()
: GenericProcessor ("File Reader")
, timestamp (0)
, currentSampleRate (0)
, currentNumChannels (0)
, currentSample (0)
, currentNumSamples (0)
, startSample (0)
, stopSample (0)
, counter (0)
{
setProcessorType (PROCESSOR_TYPE_SOURCE);
setEnabledState (false);
const int numFileSources = AccessClass::getPluginManager()->getNumFileSources();
for (int i = 0; i < numFileSources; ++i)
{
Plugin::FileSourceInfo info = AccessClass::getPluginManager()->getFileSourceInfo (i);
StringArray extensions;
extensions.addTokens (info.extensions, ";", "\"");
const int numExtensions = extensions.size();
for (int j = 0; j < numExtensions; ++j)
{
supportedExtensions.set (extensions[j].toLowerCase(), i + 1);
}
}
/*
const DataChannel* in = getDataChannel(0);
EventChannel* ev = new EventChannel(EventChannel::TTL, 8, 1, (in) ? in->getSampleRate() : CoreServices::getGlobalSampleRate(), this);
moduleEventChannels.add(ev);
*/
}
FileReader::~FileReader()
{
}
AudioProcessorEditor* FileReader::createEditor()
{
editor = new FileReaderEditor (this, true);
return editor;
}
bool FileReader::isReady()
{
if (! input)
{
CoreServices::sendStatusMessage ("No file selected in File Reader.");
return false;
}
else
{
return input->isReady();
}
}
float FileReader::getDefaultSampleRate() const
{
if (input)
return currentSampleRate;
else
return 44100.0;
}
int FileReader::getDefaultNumDataOutputs(DataChannel::DataChannelTypes type, int subproc) const
{
if (subproc != 0) return 0;
if (type != DataChannel::HEADSTAGE_CHANNEL) return 0;
if (input)
return currentNumChannels;
else
return 16;
}
float FileReader::getBitVolts (const DataChannel* chan) const
{
if (input)
return chan->getBitVolts();
else
return 0.05f;
}
void FileReader::setEnabledState (bool t)
{
isEnabled = t;
}
bool FileReader::isFileSupported (const String& fileName) const
{
const File file (fileName);
String ext = file.getFileExtension().toLowerCase().substring (1);
return isFileExtensionSupported (ext);
}
bool FileReader::isFileExtensionSupported (const String& ext) const
{
const int index = supportedExtensions[ext] - 1;
const bool isExtensionSupported = index >= 0;
return isExtensionSupported;
}
bool FileReader::setFile (String fullpath)
{
File file (fullpath);
String ext = file.getFileExtension().toLowerCase().substring (1);
const int index = supportedExtensions[ext] - 1;
const bool isExtensionSupported = index >= 0;
if (isExtensionSupported)
{
const int index = supportedExtensions[ext] - 1;
Plugin::FileSourceInfo sourceInfo = AccessClass::getPluginManager()->getFileSourceInfo (index);
input = sourceInfo.creator();
}
else
{
CoreServices::sendStatusMessage ("File type not supported");
return false;
}
if (! input->OpenFile (file))
{
input = nullptr;
CoreServices::sendStatusMessage ("Invalid file");
return false;
}
const bool isEmptyFile = input->getNumRecords() <= 0;
if (isEmptyFile)
{
input = nullptr;
CoreServices::sendStatusMessage ("Empty file. Inoring open operation");
return false;
}
static_cast<FileReaderEditor*> (getEditor())->populateRecordings (input);
setActiveRecording (0);
return true;
}
void FileReader::setActiveRecording (int index)
{
input->setActiveRecord (index);
currentNumChannels = input->getActiveNumChannels();
currentNumSamples = input->getActiveNumSamples();
currentSampleRate = input->getActiveSampleRate();
currentSample = 0;
startSample = 0;
stopSample = currentNumSamples;
for (int i = 0; i < currentNumChannels; ++i)
{
channelInfo.add (input->getChannelInfo (i));
}
static_cast<FileReaderEditor*> (getEditor())->setTotalTime (samplesToMilliseconds (currentNumSamples));
readBuffer.malloc (currentNumChannels * BUFFER_SIZE);
}
String FileReader::getFile() const
{
if (input)
return input->getFileName();
else
return String::empty;
}
void FileReader::updateSettings()
{
if (!input) return;
for (int i=0; i < currentNumChannels; i++)
{
dataChannelArray[i]->setBitVolts(channelInfo[i].bitVolts);
dataChannelArray[i]->setName(channelInfo[i].name);
}
}
void FileReader::process (AudioSampleBuffer& buffer)
{
const int samplesNeeded = int (float (buffer.getNumSamples()) * (getDefaultSampleRate() / 44100.0f));
// FIXME: needs to account for the fact that the ratio might not be an exact
// integer value
int samplesRead = 0;
while (samplesRead < samplesNeeded)
{
count +=1;
int samplesToRead = samplesNeeded - samplesRead;
int iterationsPerSecond = getDefaultSampleRate()/samplesToRead;
int sample = getDefaultSampleRate()-iterationsPerSecond*samplesToRead;
//std::cout<<"sample: " <<sample<<"\n";
if ( (currentSample + samplesToRead) > stopSample)
{
samplesToRead = stopSample - currentSample;
if (samplesToRead > 0)
input->readData (readBuffer + samplesRead, samplesToRead);
input->seekTo (startSample);
currentSample = startSample;
}
else
{
input->readData (readBuffer + samplesRead, samplesToRead);
currentSample += samplesToRead;
}
//std::cout<<"samplesRead: " << samplesRead << "\n";
//std::cout<<"mod operation: " << count % iterationsPerSecond << "\n";
if (count % iterationsPerSecond == 0){
int *evntData= 0;
TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[0], getTimestamp(0),
&evntData, sizeof(int), 0);
//TTLEvent::createTTLEvent(<#const EventChannel *channelInfo#>, <#int64 timestamp#>, <#const void *eventData#>, <#int dataSize#>, <#uint16 channel#>)
addEvent(moduleEventChannels[0], event, sample);
//addEvent(<#int channelIndex#>, <#const Event *event#>, <#int sampleNum#>)
}
samplesRead += samplesToRead;
}
for (int i = 0; i < currentNumChannels; ++i)
{
input->processChannelData (readBuffer, buffer.getWritePointer (i, 0), i, samplesNeeded);
}
timestamp += samplesNeeded;
setTimestampAndSamples(timestamp, samplesNeeded);
}
void FileReader::setParameter (int parameterIndex, float newValue)
{
switch (parameterIndex)
{
//Change selected recording
case 0:
setActiveRecording (newValue);
break;
//set startTime
case 1:
startSample = millisecondsToSamples (newValue);
currentSample = startSample;
static_cast<FileReaderEditor*> (getEditor())->setCurrentTime (samplesToMilliseconds (currentSample));
break;
//set stop time
case 2:
stopSample = millisecondsToSamples(newValue);
currentSample = startSample;
static_cast<FileReaderEditor*> (getEditor())->setCurrentTime (samplesToMilliseconds (currentSample));
break;
}
}
unsigned int FileReader::samplesToMilliseconds (int64 samples) const
{
return (unsigned int) (1000.f * float (samples) / currentSampleRate);
}
int64 FileReader::millisecondsToSamples (unsigned int ms) const
{
return (int64) (currentSampleRate * float (ms) / 1000.f);
}
void FileReader::createEventChannels(){
moduleEventChannels.clear();
const DataChannel* in = getDataChannel(0);
EventChannel* ev = new EventChannel(EventChannel::TTL, 8, 1, (in) ? in->getSampleRate() : CoreServices::getGlobalSampleRate(), this);
ev->setName("regular file reader output ");
ev->setDescription("Triggers about every second");
String identifier = "secondly.reader.";
String typeDesc = "secondly";
ev->setIdentifier(identifier);
//MetaDataDescriptor md(MetaDataDescriptor::CHAR, 34, "Phase Type", "Description of the phase condition", "channelInfo.extra");
//MetaDataValue mv(md);
//mv.setValue(typeDesc);
eventChannelArray.add(ev);
moduleEventChannels.add(ev);
}