Chris@148
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@148
|
2
|
Chris@148
|
3 /*
|
Chris@148
|
4 Sonic Visualiser
|
Chris@148
|
5 An audio file viewer and annotation editor.
|
Chris@148
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@148
|
7 This file copyright 2006 Chris Cannam.
|
Chris@148
|
8
|
Chris@148
|
9 This program is free software; you can redistribute it and/or
|
Chris@148
|
10 modify it under the terms of the GNU General Public License as
|
Chris@148
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@148
|
12 License, or (at your option) any later version. See the file
|
Chris@148
|
13 COPYING included with this distribution for more information.
|
Chris@148
|
14 */
|
Chris@148
|
15
|
Chris@148
|
16 #include "CSVFileReader.h"
|
Chris@148
|
17
|
Chris@150
|
18 #include "model/Model.h"
|
Chris@148
|
19 #include "base/RealTime.h"
|
Chris@631
|
20 #include "base/StringBits.h"
|
Chris@1491
|
21 #include "base/ProgressReporter.h"
|
Chris@148
|
22 #include "model/SparseOneDimensionalModel.h"
|
Chris@148
|
23 #include "model/SparseTimeValueModel.h"
|
Chris@152
|
24 #include "model/EditableDenseThreeDimensionalModel.h"
|
Chris@628
|
25 #include "model/RegionModel.h"
|
Chris@897
|
26 #include "model/NoteModel.h"
|
Chris@1488
|
27 #include "model/WritableWaveFileModel.h"
|
Chris@308
|
28 #include "DataFileReaderFactory.h"
|
Chris@148
|
29
|
Chris@148
|
30 #include <QFile>
|
Chris@1030
|
31 #include <QFileInfo>
|
Chris@148
|
32 #include <QString>
|
Chris@148
|
33 #include <QRegExp>
|
Chris@148
|
34 #include <QStringList>
|
Chris@148
|
35 #include <QTextStream>
|
Chris@148
|
36
|
Chris@148
|
37 #include <iostream>
|
Chris@628
|
38 #include <map>
|
Chris@1428
|
39 #include <string>
|
Chris@148
|
40
|
Chris@1113
|
41 using namespace std;
|
Chris@1113
|
42
|
Chris@392
|
43 CSVFileReader::CSVFileReader(QString path, CSVFormat format,
|
Chris@1491
|
44 sv_samplerate_t mainModelSampleRate,
|
Chris@1491
|
45 ProgressReporter *reporter) :
|
Chris@392
|
46 m_format(format),
|
Chris@1009
|
47 m_device(0),
|
Chris@1009
|
48 m_ownDevice(true),
|
Chris@631
|
49 m_warnings(0),
|
Chris@1491
|
50 m_mainModelSampleRate(mainModelSampleRate),
|
Chris@1491
|
51 m_fileSize(0),
|
Chris@1491
|
52 m_readCount(0),
|
Chris@1492
|
53 m_progress(-1),
|
Chris@1491
|
54 m_reporter(reporter)
|
Chris@148
|
55 {
|
Chris@1009
|
56 QFile *file = new QFile(path);
|
Chris@148
|
57 bool good = false;
|
Chris@148
|
58
|
Chris@1009
|
59 if (!file->exists()) {
|
Chris@1429
|
60 m_error = QFile::tr("File \"%1\" does not exist").arg(path);
|
Chris@1009
|
61 } else if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
|
Chris@1429
|
62 m_error = QFile::tr("Failed to open file \"%1\"").arg(path);
|
Chris@148
|
63 } else {
|
Chris@1429
|
64 good = true;
|
Chris@148
|
65 }
|
Chris@148
|
66
|
Chris@1009
|
67 if (good) {
|
Chris@1009
|
68 m_device = file;
|
Chris@1030
|
69 m_filename = QFileInfo(path).fileName();
|
Chris@1491
|
70 m_fileSize = file->size();
|
Chris@1491
|
71 if (m_reporter) m_reporter->setDefinite(true);
|
Chris@1009
|
72 } else {
|
Chris@1429
|
73 delete file;
|
Chris@148
|
74 }
|
Chris@148
|
75 }
|
Chris@148
|
76
|
Chris@1009
|
77 CSVFileReader::CSVFileReader(QIODevice *device, CSVFormat format,
|
Chris@1491
|
78 sv_samplerate_t mainModelSampleRate,
|
Chris@1491
|
79 ProgressReporter *reporter) :
|
Chris@1009
|
80 m_format(format),
|
Chris@1009
|
81 m_device(device),
|
Chris@1009
|
82 m_ownDevice(false),
|
Chris@1009
|
83 m_warnings(0),
|
Chris@1491
|
84 m_mainModelSampleRate(mainModelSampleRate),
|
Chris@1491
|
85 m_fileSize(0),
|
Chris@1491
|
86 m_readCount(0),
|
Chris@1492
|
87 m_progress(-1),
|
Chris@1491
|
88 m_reporter(reporter)
|
Chris@1009
|
89 {
|
Chris@1491
|
90 if (m_reporter) m_reporter->setDefinite(false);
|
Chris@1009
|
91 }
|
Chris@1009
|
92
|
Chris@148
|
93 CSVFileReader::~CSVFileReader()
|
Chris@148
|
94 {
|
Chris@1009
|
95 SVDEBUG << "CSVFileReader::~CSVFileReader: device is " << m_device << endl;
|
Chris@148
|
96
|
Chris@1009
|
97 if (m_device && m_ownDevice) {
|
Chris@1009
|
98 SVDEBUG << "CSVFileReader::CSVFileReader: Closing device" << endl;
|
Chris@1009
|
99 m_device->close();
|
Chris@1009
|
100 delete m_device;
|
Chris@148
|
101 }
|
Chris@148
|
102 }
|
Chris@148
|
103
|
Chris@148
|
104 bool
|
Chris@148
|
105 CSVFileReader::isOK() const
|
Chris@148
|
106 {
|
Chris@1009
|
107 return (m_device != 0);
|
Chris@148
|
108 }
|
Chris@148
|
109
|
Chris@148
|
110 QString
|
Chris@148
|
111 CSVFileReader::getError() const
|
Chris@148
|
112 {
|
Chris@148
|
113 return m_error;
|
Chris@148
|
114 }
|
Chris@148
|
115
|
Chris@1038
|
116 sv_frame_t
|
Chris@1047
|
117 CSVFileReader::convertTimeValue(QString s, int lineno,
|
Chris@1047
|
118 sv_samplerate_t sampleRate,
|
Chris@929
|
119 int windowSize) const
|
Chris@631
|
120 {
|
Chris@631
|
121 QRegExp nonNumericRx("[^0-9eE.,+-]");
|
Chris@897
|
122 int warnLimit = 10;
|
Chris@631
|
123
|
Chris@631
|
124 CSVFormat::TimeUnits timeUnits = m_format.getTimeUnits();
|
Chris@631
|
125
|
Chris@1038
|
126 sv_frame_t calculatedFrame = 0;
|
Chris@631
|
127
|
Chris@631
|
128 bool ok = false;
|
Chris@631
|
129 QString numeric = s;
|
Chris@631
|
130 numeric.remove(nonNumericRx);
|
Chris@631
|
131
|
Chris@631
|
132 if (timeUnits == CSVFormat::TimeSeconds) {
|
Chris@631
|
133
|
Chris@631
|
134 double time = numeric.toDouble(&ok);
|
Chris@631
|
135 if (!ok) time = StringBits::stringToDoubleLocaleFree(numeric, &ok);
|
Chris@1038
|
136 calculatedFrame = sv_frame_t(time * sampleRate + 0.5);
|
Chris@990
|
137
|
Chris@990
|
138 } else if (timeUnits == CSVFormat::TimeMilliseconds) {
|
Chris@990
|
139
|
Chris@990
|
140 double time = numeric.toDouble(&ok);
|
Chris@990
|
141 if (!ok) time = StringBits::stringToDoubleLocaleFree(numeric, &ok);
|
Chris@1038
|
142 calculatedFrame = sv_frame_t((time / 1000.0) * sampleRate + 0.5);
|
Chris@631
|
143
|
Chris@631
|
144 } else {
|
Chris@631
|
145
|
Chris@631
|
146 long n = numeric.toLong(&ok);
|
Chris@631
|
147 if (n >= 0) calculatedFrame = n;
|
Chris@631
|
148
|
Chris@631
|
149 if (timeUnits == CSVFormat::TimeWindows) {
|
Chris@631
|
150 calculatedFrame *= windowSize;
|
Chris@631
|
151 }
|
Chris@631
|
152 }
|
Chris@631
|
153
|
Chris@631
|
154 if (!ok) {
|
Chris@631
|
155 if (m_warnings < warnLimit) {
|
Chris@1428
|
156 SVCERR << "WARNING: CSVFileReader::load: "
|
Chris@844
|
157 << "Bad time format (\"" << s
|
Chris@631
|
158 << "\") in data line "
|
Chris@843
|
159 << lineno+1 << endl;
|
Chris@631
|
160 } else if (m_warnings == warnLimit) {
|
Chris@1428
|
161 SVCERR << "WARNING: Too many warnings" << endl;
|
Chris@631
|
162 }
|
Chris@631
|
163 ++m_warnings;
|
Chris@631
|
164 }
|
Chris@631
|
165
|
Chris@631
|
166 return calculatedFrame;
|
Chris@631
|
167 }
|
Chris@631
|
168
|
Chris@148
|
169 Model *
|
Chris@148
|
170 CSVFileReader::load() const
|
Chris@148
|
171 {
|
Chris@1009
|
172 if (!m_device) return 0;
|
Chris@148
|
173
|
Chris@628
|
174 CSVFormat::ModelType modelType = m_format.getModelType();
|
Chris@392
|
175 CSVFormat::TimingType timingType = m_format.getTimingType();
|
Chris@628
|
176 CSVFormat::TimeUnits timeUnits = m_format.getTimeUnits();
|
Chris@1047
|
177 sv_samplerate_t sampleRate = m_format.getSampleRate();
|
Chris@929
|
178 int windowSize = m_format.getWindowSize();
|
Chris@631
|
179 QChar separator = m_format.getSeparator();
|
Chris@631
|
180 bool allowQuoting = m_format.getAllowQuoting();
|
Chris@148
|
181
|
Chris@392
|
182 if (timingType == CSVFormat::ExplicitTiming) {
|
Chris@611
|
183 if (modelType == CSVFormat::ThreeDimensionalModel) {
|
Chris@611
|
184 // This will be overridden later if more than one line
|
Chris@611
|
185 // appears in our file, but we want to choose a default
|
Chris@611
|
186 // that's likely to be visible
|
Chris@611
|
187 windowSize = 1024;
|
Chris@611
|
188 } else {
|
Chris@611
|
189 windowSize = 1;
|
Chris@611
|
190 }
|
Chris@1429
|
191 if (timeUnits == CSVFormat::TimeSeconds ||
|
Chris@990
|
192 timeUnits == CSVFormat::TimeMilliseconds) {
|
Chris@1429
|
193 sampleRate = m_mainModelSampleRate;
|
Chris@1429
|
194 }
|
Chris@148
|
195 }
|
Chris@148
|
196
|
Chris@148
|
197 SparseOneDimensionalModel *model1 = 0;
|
Chris@148
|
198 SparseTimeValueModel *model2 = 0;
|
Chris@628
|
199 RegionModel *model2a = 0;
|
Chris@897
|
200 NoteModel *model2b = 0;
|
Chris@152
|
201 EditableDenseThreeDimensionalModel *model3 = 0;
|
Chris@1488
|
202 WritableWaveFileModel *modelW = 0;
|
Chris@148
|
203 Model *model = 0;
|
Chris@148
|
204
|
Chris@1009
|
205 QTextStream in(m_device);
|
Chris@148
|
206
|
Chris@148
|
207 unsigned int warnings = 0, warnLimit = 10;
|
Chris@148
|
208 unsigned int lineno = 0;
|
Chris@148
|
209
|
Chris@148
|
210 float min = 0.0, max = 0.0;
|
Chris@148
|
211
|
Chris@1038
|
212 sv_frame_t frameNo = 0;
|
Chris@1038
|
213 sv_frame_t duration = 0;
|
Chris@1038
|
214 sv_frame_t endFrame = 0;
|
Chris@631
|
215
|
Chris@631
|
216 bool haveAnyValue = false;
|
Chris@631
|
217 bool haveEndTime = false;
|
Chris@897
|
218 bool pitchLooksLikeMIDI = true;
|
Chris@631
|
219
|
Chris@1038
|
220 sv_frame_t startFrame = 0; // for calculation of dense model resolution
|
Chris@631
|
221 bool firstEverValue = true;
|
Chris@148
|
222
|
Chris@1113
|
223 map<QString, int> labelCountMap;
|
Chris@631
|
224
|
Chris@676
|
225 int valueColumns = 0;
|
Chris@676
|
226 for (int i = 0; i < m_format.getColumnCount(); ++i) {
|
Chris@676
|
227 if (m_format.getColumnPurpose(i) == CSVFormat::ColumnValue) {
|
Chris@676
|
228 ++valueColumns;
|
Chris@676
|
229 }
|
Chris@676
|
230 }
|
Chris@676
|
231
|
Chris@1509
|
232 bool abandoned = false;
|
Chris@1509
|
233
|
Chris@1509
|
234 while (!in.atEnd() && !abandoned) {
|
Chris@148
|
235
|
Chris@283
|
236 // QTextStream's readLine doesn't cope with old-style Mac
|
Chris@283
|
237 // CR-only line endings. Why did they bother making the class
|
Chris@283
|
238 // cope with more than one sort of line ending, if it still
|
Chris@283
|
239 // can't be configured to cope with all the common sorts?
|
Chris@148
|
240
|
Chris@283
|
241 // For the time being we'll deal with this case (which is
|
Chris@283
|
242 // relatively uncommon for us, but still necessary to handle)
|
Chris@283
|
243 // by reading the entire file using a single readLine, and
|
Chris@283
|
244 // splitting it. For CR and CR/LF line endings this will just
|
Chris@283
|
245 // read a line at a time, and that's obviously OK.
|
Chris@148
|
246
|
Chris@283
|
247 QString chunk = in.readLine();
|
Chris@283
|
248 QStringList lines = chunk.split('\r', QString::SkipEmptyParts);
|
Chris@1491
|
249
|
Chris@1491
|
250 m_readCount += chunk.size() + 1;
|
Chris@1491
|
251
|
Chris@1491
|
252 if (m_reporter) {
|
Chris@1491
|
253 if (m_reporter->wasCancelled()) {
|
Chris@1509
|
254 abandoned = true;
|
Chris@1491
|
255 break;
|
Chris@1491
|
256 }
|
Chris@1491
|
257 int progress;
|
Chris@1491
|
258 if (m_fileSize > 0) {
|
Chris@1491
|
259 progress = int((double(m_readCount) / double(m_fileSize))
|
Chris@1491
|
260 * 100.0);
|
Chris@1491
|
261 } else {
|
Chris@1491
|
262 progress = int(m_readCount / 10000);
|
Chris@1491
|
263 }
|
Chris@1491
|
264 if (progress != m_progress) {
|
Chris@1491
|
265 m_reporter->setProgress(progress);
|
Chris@1491
|
266 m_progress = progress;
|
Chris@1491
|
267 }
|
Chris@1491
|
268 }
|
Chris@283
|
269
|
Chris@897
|
270 for (int li = 0; li < lines.size(); ++li) {
|
Chris@148
|
271
|
Chris@283
|
272 QString line = lines[li];
|
Chris@1009
|
273
|
Chris@283
|
274 if (line.startsWith("#")) continue;
|
Chris@283
|
275
|
Chris@631
|
276 QStringList list = StringBits::split(line, separator, allowQuoting);
|
Chris@283
|
277 if (!model) {
|
Chris@283
|
278
|
Chris@283
|
279 switch (modelType) {
|
Chris@283
|
280
|
Chris@392
|
281 case CSVFormat::OneDimensionalModel:
|
Chris@283
|
282 model1 = new SparseOneDimensionalModel(sampleRate, windowSize);
|
Chris@283
|
283 model = model1;
|
Chris@283
|
284 break;
|
Chris@1429
|
285
|
Chris@392
|
286 case CSVFormat::TwoDimensionalModel:
|
Chris@283
|
287 model2 = new SparseTimeValueModel(sampleRate, windowSize, false);
|
Chris@283
|
288 model = model2;
|
Chris@283
|
289 break;
|
Chris@1429
|
290
|
Chris@628
|
291 case CSVFormat::TwoDimensionalModelWithDuration:
|
Chris@628
|
292 model2a = new RegionModel(sampleRate, windowSize, false);
|
Chris@628
|
293 model = model2a;
|
Chris@628
|
294 break;
|
Chris@1429
|
295
|
Chris@897
|
296 case CSVFormat::TwoDimensionalModelWithDurationAndPitch:
|
Chris@897
|
297 model2b = new NoteModel(sampleRate, windowSize, false);
|
Chris@897
|
298 model = model2b;
|
Chris@897
|
299 break;
|
Chris@1429
|
300
|
Chris@392
|
301 case CSVFormat::ThreeDimensionalModel:
|
Chris@535
|
302 model3 = new EditableDenseThreeDimensionalModel
|
Chris@535
|
303 (sampleRate,
|
Chris@535
|
304 windowSize,
|
Chris@676
|
305 valueColumns,
|
Chris@535
|
306 EditableDenseThreeDimensionalModel::NoCompression);
|
Chris@283
|
307 model = model3;
|
Chris@283
|
308 break;
|
Chris@1488
|
309
|
Chris@1488
|
310 case CSVFormat::WaveFileModel:
|
Chris@1517
|
311 {
|
Chris@1517
|
312 bool normalise = (m_format.getAudioSampleRange()
|
Chris@1517
|
313 == CSVFormat::SampleRangeOther);
|
Chris@1488
|
314 modelW = new WritableWaveFileModel
|
Chris@1517
|
315 (sampleRate, valueColumns, QString(), normalise);
|
Chris@1488
|
316 model = modelW;
|
Chris@1488
|
317 break;
|
Chris@283
|
318 }
|
Chris@1517
|
319 }
|
Chris@1030
|
320
|
Chris@1508
|
321 if (model && model->isOK()) {
|
Chris@1030
|
322 if (m_filename != "") {
|
Chris@1030
|
323 model->setObjectName(m_filename);
|
Chris@1030
|
324 }
|
Chris@1030
|
325 }
|
Chris@283
|
326 }
|
Chris@148
|
327
|
Chris@1508
|
328 if (!model || !model->isOK()) {
|
Chris@1508
|
329 SVCERR << "Failed to create model to load CSV file into"
|
Chris@1508
|
330 << endl;
|
Chris@1508
|
331 if (model) {
|
Chris@1508
|
332 delete model;
|
Chris@1508
|
333 model = 0;
|
Chris@1508
|
334 model1 = 0; model2 = 0; model2a = 0; model2b = 0;
|
Chris@1508
|
335 model3 = 0; modelW = 0;
|
Chris@1508
|
336 }
|
Chris@1509
|
337 abandoned = true;
|
Chris@1508
|
338 break;
|
Chris@1508
|
339 }
|
Chris@1508
|
340
|
Chris@631
|
341 float value = 0.f;
|
Chris@897
|
342 float pitch = 0.f;
|
Chris@631
|
343 QString label = "";
|
Chris@148
|
344
|
Chris@631
|
345 duration = 0.f;
|
Chris@631
|
346 haveEndTime = false;
|
Chris@628
|
347
|
Chris@283
|
348 for (int i = 0; i < list.size(); ++i) {
|
Chris@148
|
349
|
Chris@631
|
350 QString s = list[i];
|
Chris@631
|
351
|
Chris@631
|
352 CSVFormat::ColumnPurpose purpose = m_format.getColumnPurpose(i);
|
Chris@631
|
353
|
Chris@631
|
354 switch (purpose) {
|
Chris@631
|
355
|
Chris@631
|
356 case CSVFormat::ColumnUnknown:
|
Chris@631
|
357 break;
|
Chris@631
|
358
|
Chris@631
|
359 case CSVFormat::ColumnStartTime:
|
Chris@631
|
360 frameNo = convertTimeValue(s, lineno, sampleRate, windowSize);
|
Chris@631
|
361 break;
|
Chris@631
|
362
|
Chris@631
|
363 case CSVFormat::ColumnEndTime:
|
Chris@631
|
364 endFrame = convertTimeValue(s, lineno, sampleRate, windowSize);
|
Chris@631
|
365 haveEndTime = true;
|
Chris@631
|
366 break;
|
Chris@631
|
367
|
Chris@631
|
368 case CSVFormat::ColumnDuration:
|
Chris@631
|
369 duration = convertTimeValue(s, lineno, sampleRate, windowSize);
|
Chris@631
|
370 break;
|
Chris@631
|
371
|
Chris@631
|
372 case CSVFormat::ColumnValue:
|
Chris@631
|
373 value = s.toFloat();
|
Chris@631
|
374 haveAnyValue = true;
|
Chris@631
|
375 break;
|
Chris@631
|
376
|
Chris@897
|
377 case CSVFormat::ColumnPitch:
|
Chris@897
|
378 pitch = s.toFloat();
|
Chris@897
|
379 if (pitch < 0.f || pitch > 127.f) {
|
Chris@897
|
380 pitchLooksLikeMIDI = false;
|
Chris@897
|
381 }
|
Chris@897
|
382 break;
|
Chris@897
|
383
|
Chris@631
|
384 case CSVFormat::ColumnLabel:
|
Chris@631
|
385 label = s;
|
Chris@631
|
386 break;
|
Chris@283
|
387 }
|
Chris@631
|
388 }
|
Chris@148
|
389
|
Chris@1113
|
390 ++labelCountMap[label];
|
Chris@1113
|
391
|
Chris@631
|
392 if (haveEndTime) { // ... calculate duration now all cols read
|
Chris@631
|
393 if (endFrame > frameNo) {
|
Chris@631
|
394 duration = endFrame - frameNo;
|
Chris@628
|
395 }
|
Chris@283
|
396 }
|
Chris@148
|
397
|
Chris@392
|
398 if (modelType == CSVFormat::OneDimensionalModel) {
|
Chris@1429
|
399
|
Chris@631
|
400 SparseOneDimensionalModel::Point point(frameNo, label);
|
Chris@283
|
401 model1->addPoint(point);
|
Chris@148
|
402
|
Chris@392
|
403 } else if (modelType == CSVFormat::TwoDimensionalModel) {
|
Chris@148
|
404
|
Chris@631
|
405 SparseTimeValueModel::Point point(frameNo, value, label);
|
Chris@283
|
406 model2->addPoint(point);
|
Chris@148
|
407
|
Chris@628
|
408 } else if (modelType == CSVFormat::TwoDimensionalModelWithDuration) {
|
Chris@628
|
409
|
Chris@631
|
410 RegionModel::Point point(frameNo, value, duration, label);
|
Chris@628
|
411 model2a->addPoint(point);
|
Chris@628
|
412
|
Chris@897
|
413 } else if (modelType == CSVFormat::TwoDimensionalModelWithDurationAndPitch) {
|
Chris@897
|
414
|
Chris@897
|
415 float level = ((value >= 0.f && value <= 1.f) ? value : 1.f);
|
Chris@897
|
416 NoteModel::Point point(frameNo, pitch, duration, level, label);
|
Chris@897
|
417 model2b->addPoint(point);
|
Chris@897
|
418
|
Chris@392
|
419 } else if (modelType == CSVFormat::ThreeDimensionalModel) {
|
Chris@148
|
420
|
Chris@283
|
421 DenseThreeDimensionalModel::Column values;
|
Chris@148
|
422
|
Chris@631
|
423 for (int i = 0; i < list.size(); ++i) {
|
Chris@148
|
424
|
Chris@676
|
425 if (m_format.getColumnPurpose(i) != CSVFormat::ColumnValue) {
|
Chris@676
|
426 continue;
|
Chris@676
|
427 }
|
Chris@676
|
428
|
Chris@283
|
429 bool ok = false;
|
Chris@283
|
430 float value = list[i].toFloat(&ok);
|
Chris@611
|
431
|
Chris@676
|
432 values.push_back(value);
|
Chris@1429
|
433
|
Chris@631
|
434 if (firstEverValue || value < min) min = value;
|
Chris@631
|
435 if (firstEverValue || value > max) max = value;
|
Chris@676
|
436
|
Chris@631
|
437 if (firstEverValue) {
|
Chris@611
|
438 startFrame = frameNo;
|
Chris@611
|
439 model3->setStartFrame(startFrame);
|
Chris@611
|
440 } else if (lineno == 1 &&
|
Chris@611
|
441 timingType == CSVFormat::ExplicitTiming) {
|
Chris@1038
|
442 model3->setResolution(int(frameNo - startFrame));
|
Chris@611
|
443 }
|
Chris@631
|
444
|
Chris@631
|
445 firstEverValue = false;
|
Chris@148
|
446
|
Chris@283
|
447 if (!ok) {
|
Chris@283
|
448 if (warnings < warnLimit) {
|
Chris@1428
|
449 SVCERR << "WARNING: CSVFileReader::load: "
|
Chris@390
|
450 << "Non-numeric value \""
|
Chris@844
|
451 << list[i]
|
Chris@491
|
452 << "\" in data line " << lineno+1
|
Chris@843
|
453 << ":" << endl;
|
Chris@1428
|
454 SVCERR << line << endl;
|
Chris@283
|
455 ++warnings;
|
Chris@283
|
456 } else if (warnings == warnLimit) {
|
Chris@1428
|
457 // SVCERR << "WARNING: Too many warnings" << endl;
|
Chris@283
|
458 }
|
Chris@283
|
459 }
|
Chris@283
|
460 }
|
Chris@1429
|
461
|
Chris@690
|
462 // SVDEBUG << "Setting bin values for count " << lineno << ", frame "
|
Chris@687
|
463 // << frameNo << ", time " << RealTime::frame2RealTime(frameNo, sampleRate) << endl;
|
Chris@148
|
464
|
Chris@611
|
465 model3->setColumn(lineno, values);
|
Chris@1488
|
466
|
Chris@1488
|
467 } else if (modelType == CSVFormat::WaveFileModel) {
|
Chris@1488
|
468
|
Chris@1488
|
469 int channels = modelW->getChannelCount();
|
Chris@1488
|
470
|
Chris@1488
|
471 float **samples =
|
Chris@1488
|
472 breakfastquay::allocate_and_zero_channels<float>
|
Chris@1488
|
473 (channels, 1);
|
Chris@1490
|
474
|
Chris@1490
|
475 int channel = 0;
|
Chris@1517
|
476 float shift = 0.f;
|
Chris@1517
|
477 float scale = 1.f;
|
Chris@1517
|
478
|
Chris@1517
|
479 switch (m_format.getAudioSampleRange()) {
|
Chris@1517
|
480 case CSVFormat::SampleRangeSigned1:
|
Chris@1517
|
481 case CSVFormat::SampleRangeOther:
|
Chris@1517
|
482 shift = 0.f;
|
Chris@1517
|
483 scale = 1.f;
|
Chris@1517
|
484 break;
|
Chris@1517
|
485 case CSVFormat::SampleRangeUnsigned255:
|
Chris@1517
|
486 shift = -128.f;
|
Chris@1517
|
487 scale = 1.f / 128.f;
|
Chris@1517
|
488 break;
|
Chris@1517
|
489 case CSVFormat::SampleRangeSigned32767:
|
Chris@1517
|
490 shift = 0.f;
|
Chris@1517
|
491 scale = 1.f / 32768.f;
|
Chris@1517
|
492 break;
|
Chris@1517
|
493 }
|
Chris@1488
|
494
|
Chris@1490
|
495 for (int i = 0; i < list.size() && channel < channels; ++i) {
|
Chris@1488
|
496
|
Chris@1490
|
497 if (m_format.getColumnPurpose(i) !=
|
Chris@1490
|
498 CSVFormat::ColumnValue) {
|
Chris@1488
|
499 continue;
|
Chris@1488
|
500 }
|
Chris@1488
|
501
|
Chris@1488
|
502 bool ok = false;
|
Chris@1488
|
503 float value = list[i].toFloat(&ok);
|
Chris@1517
|
504
|
Chris@1517
|
505 value += shift;
|
Chris@1517
|
506 value *= scale;
|
Chris@1488
|
507
|
Chris@1490
|
508 samples[channel][0] = value;
|
Chris@1490
|
509
|
Chris@1490
|
510 ++channel;
|
Chris@1488
|
511 }
|
Chris@1488
|
512
|
Chris@1488
|
513 bool ok = modelW->addSamples(samples, 1);
|
Chris@1488
|
514
|
Chris@1488
|
515 breakfastquay::deallocate_channels(samples, channels);
|
Chris@1488
|
516
|
Chris@1488
|
517 if (!ok) {
|
Chris@1488
|
518 if (warnings < warnLimit) {
|
Chris@1488
|
519 SVCERR << "WARNING: CSVFileReader::load: "
|
Chris@1488
|
520 << "Unable to add sample to wave-file model"
|
Chris@1488
|
521 << endl;
|
Chris@1488
|
522 SVCERR << line << endl;
|
Chris@1488
|
523 ++warnings;
|
Chris@1488
|
524 }
|
Chris@1488
|
525 }
|
Chris@283
|
526 }
|
Chris@1488
|
527
|
Chris@283
|
528 ++lineno;
|
Chris@392
|
529 if (timingType == CSVFormat::ImplicitTiming ||
|
Chris@283
|
530 list.size() == 0) {
|
Chris@283
|
531 frameNo += windowSize;
|
Chris@283
|
532 }
|
Chris@283
|
533 }
|
Chris@148
|
534 }
|
Chris@148
|
535
|
Chris@631
|
536 if (!haveAnyValue) {
|
Chris@631
|
537 if (model2a) {
|
Chris@631
|
538 // assign values for regions based on label frequency; we
|
Chris@631
|
539 // have this in our labelCountMap, sort of
|
Chris@631
|
540
|
Chris@1113
|
541 map<int, map<QString, float> > countLabelValueMap;
|
Chris@1113
|
542 for (map<QString, int>::iterator i = labelCountMap.begin();
|
Chris@631
|
543 i != labelCountMap.end(); ++i) {
|
Chris@1113
|
544 countLabelValueMap[i->second][i->first] = -1.f;
|
Chris@631
|
545 }
|
Chris@631
|
546
|
Chris@631
|
547 float v = 0.f;
|
Chris@1113
|
548 for (map<int, map<QString, float> >::iterator i =
|
Chris@631
|
549 countLabelValueMap.end(); i != countLabelValueMap.begin(); ) {
|
Chris@631
|
550 --i;
|
Chris@1428
|
551 SVCERR << "count -> " << i->first << endl;
|
Chris@1113
|
552 for (map<QString, float>::iterator j = i->second.begin();
|
Chris@631
|
553 j != i->second.end(); ++j) {
|
Chris@631
|
554 j->second = v;
|
Chris@1428
|
555 SVCERR << "label -> " << j->first << ", value " << v << endl;
|
Chris@631
|
556 v = v + 1.f;
|
Chris@631
|
557 }
|
Chris@631
|
558 }
|
Chris@631
|
559
|
Chris@1113
|
560 map<RegionModel::Point, RegionModel::Point,
|
Chris@631
|
561 RegionModel::Point::Comparator> pointMap;
|
Chris@631
|
562 for (RegionModel::PointList::const_iterator i =
|
Chris@631
|
563 model2a->getPoints().begin();
|
Chris@631
|
564 i != model2a->getPoints().end(); ++i) {
|
Chris@631
|
565 RegionModel::Point p(*i);
|
Chris@1113
|
566 int count = labelCountMap[p.label];
|
Chris@1113
|
567 v = countLabelValueMap[count][p.label];
|
Chris@1428
|
568 // SVCERR << "mapping from label \"" << p.label << "\" (count " << count << ") to value " << v << endl;
|
Chris@631
|
569 RegionModel::Point pp(p.frame, v, p.duration, p.label);
|
Chris@631
|
570 pointMap[p] = pp;
|
Chris@631
|
571 }
|
Chris@631
|
572
|
Chris@1113
|
573 for (map<RegionModel::Point, RegionModel::Point>::iterator i =
|
Chris@631
|
574 pointMap.begin(); i != pointMap.end(); ++i) {
|
Chris@1113
|
575 // There could be duplicate regions; if so replace
|
Chris@1113
|
576 // them all -- but we need to check we're not
|
Chris@1113
|
577 // replacing a region by itself (or else this will
|
Chris@1113
|
578 // never terminate)
|
Chris@1113
|
579 if (i->first.value == i->second.value) {
|
Chris@1113
|
580 continue;
|
Chris@1113
|
581 }
|
Chris@1113
|
582 while (model2a->containsPoint(i->first)) {
|
Chris@1113
|
583 model2a->deletePoint(i->first);
|
Chris@1113
|
584 model2a->addPoint(i->second);
|
Chris@1113
|
585 }
|
Chris@631
|
586 }
|
Chris@631
|
587 }
|
Chris@631
|
588 }
|
Chris@631
|
589
|
Chris@897
|
590 if (model2b) {
|
Chris@897
|
591 if (pitchLooksLikeMIDI) {
|
Chris@897
|
592 model2b->setScaleUnits("MIDI Pitch");
|
Chris@897
|
593 } else {
|
Chris@897
|
594 model2b->setScaleUnits("Hz");
|
Chris@897
|
595 }
|
Chris@897
|
596 }
|
Chris@897
|
597
|
Chris@961
|
598 if (model3) {
|
Chris@1429
|
599 model3->setMinimumLevel(min);
|
Chris@1429
|
600 model3->setMaximumLevel(max);
|
Chris@148
|
601 }
|
Chris@148
|
602
|
Chris@1489
|
603 if (modelW) {
|
Chris@1493
|
604 modelW->updateModel();
|
Chris@1489
|
605 modelW->writeComplete();
|
Chris@1489
|
606 }
|
Chris@1489
|
607
|
Chris@148
|
608 return model;
|
Chris@148
|
609 }
|
Chris@148
|
610
|