1+ /*
2+ ------------------------------------------------------------------
3+
4+ This file is part of the Open Ephys GUI
5+ Copyright (C) 2018 Open Ephys
6+
7+ ------------------------------------------------------------------
8+
9+ This program is free software: you can redistribute it and/or modify
10+ it under the terms of the GNU General Public License as published by
11+ the Free Software Foundation, either version 3 of the License, or
12+ (at your option) any later version.
13+
14+ This program is distributed in the hope that it will be useful,
15+ but WITHOUT ANY WARRANTY; without even the implied warranty of
16+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+ GNU General Public License for more details.
18+
19+ You should have received a copy of the GNU General Public License
20+ along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+ */
23+
24+ #include " BinaryFileSource.h"
25+
26+ using namespace BinarySource ;
27+
28+ BinaryFileSource::BinaryFileSource () : m_samplePos(0 )
29+ {}
30+
31+ BinaryFileSource::~BinaryFileSource ()
32+ {}
33+
34+ bool BinaryFileSource::Open (File file)
35+ {
36+ m_jsonData = JSON::parse (file);
37+ if (m_jsonData.isVoid ())
38+ return false ;
39+
40+ if (m_jsonData[" GUI version" ].isVoid ())
41+ return false ;
42+
43+ var cont = m_jsonData[" continuous" ];
44+ if (cont.isVoid () || cont.size () <= 0 )
45+ return false ;
46+
47+ m_rootPath = file.getParentDirectory ();
48+
49+ return true ;
50+ }
51+
52+ void BinaryFileSource::fillRecordInfo ()
53+ {
54+ var continuousData = m_jsonData[" continuous" ];
55+
56+ // create identifiers to speed up stuff
57+ Identifier idFolder (" folder_name" );
58+ Identifier idSampleRate (" sample_rate" );
59+ Identifier idNumChannels (" num_channels" );
60+ Identifier idChannels (" channels" );
61+ Identifier idChannelName (" channel_name" );
62+ Identifier idBitVolts (" bit_volts" );
63+
64+ int numProcessors = continuousData.size ();
65+
66+ for (int i = 0 ; i < numProcessors; i++)
67+ {
68+ var record = continuousData[i];
69+ if (record.isVoid ()) continue ;
70+
71+ var channels = record[idChannels];
72+ if (channels.isVoid () || channels.size () <= 0 ) continue ;
73+
74+ RecordInfo info;
75+
76+ String folderName = record[idFolder];
77+ folderName = folderName.trimCharactersAtEnd (" /" );
78+
79+ File dataFile = m_rootPath.getChildFile (" continuous" ).getChildFile (folderName).getChildFile (" continuous.dat" );
80+ if (!dataFile.existsAsFile ()) continue ;
81+
82+ int numChannels = record[idNumChannels];
83+ int64 numSamples = (dataFile.getSize () / numChannels) / sizeof (int16);
84+
85+ info.name = folderName;
86+ info.sampleRate = record[idSampleRate];
87+ info.numSamples = numSamples;
88+
89+ for (int c = 0 ; c < numChannels; c++)
90+ {
91+ var chan = channels[c];
92+ RecordedChannelInfo cInfo;
93+
94+ cInfo.name = chan[idChannelName];
95+ cInfo.bitVolts = chan[idBitVolts];
96+
97+ info.channels .add (cInfo);
98+ }
99+
100+ infoArray.add (info);
101+ numRecords++;
102+
103+ m_dataFileArray.add (dataFile);
104+
105+ }
106+
107+ }
108+
109+ void BinaryFileSource::updateActiveRecord ()
110+ {
111+ m_dataFile = new MemoryMappedFile (m_dataFileArray[activeRecord.get ()], MemoryMappedFile::readOnly);
112+ m_samplePos = 0 ;
113+ }
114+
115+ void BinaryFileSource::seekTo (int64 sample)
116+ {
117+ m_samplePos = sample % getActiveNumSamples ();
118+ }
119+
120+ int BinaryFileSource::readData (int16* buffer, int nSamples)
121+ {
122+ int nChans = getActiveNumChannels ();
123+ int64 samplesToRead;
124+
125+ if (m_samplePos + nSamples > getActiveNumSamples ())
126+ {
127+ samplesToRead = getActiveNumSamples () - m_samplePos;
128+ }
129+ else
130+ {
131+ samplesToRead = nSamples;
132+ }
133+
134+ int16* data = static_cast <int16*>(m_dataFile->getData ()) + (m_samplePos * nChans);
135+
136+ memcpy (buffer, data, samplesToRead*nChans*sizeof (int16));
137+ return samplesToRead;
138+ }
139+
140+ void BinaryFileSource::processChannelData (int16* inBuffer, float * outBuffer, int channel, int64 numSamples)
141+ {
142+ int n = getActiveNumChannels ();
143+ float bitVolts = getChannelInfo (channel).bitVolts ;
144+
145+ for (int i = 0 ; i < numSamples; i++)
146+ {
147+ *(outBuffer + i) = *(inBuffer + (n*i) + channel) * bitVolts;
148+ }
149+ }
150+
151+ bool BinaryFileSource::isReady ()
152+ {
153+ return true ;
154+ }
0 commit comments