annotate data/fileio/CSVFileReader.cpp @ 1056:c4898e57eea5 tonioni

Merge from default branch
author Chris Cannam
date Mon, 23 Mar 2015 10:04:48 +0000
parents 920699b6989d
children cc27f35aa75c
rev   line source
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@148 21 #include "model/SparseOneDimensionalModel.h"
Chris@148 22 #include "model/SparseTimeValueModel.h"
Chris@152 23 #include "model/EditableDenseThreeDimensionalModel.h"
Chris@628 24 #include "model/RegionModel.h"
Chris@897 25 #include "model/NoteModel.h"
Chris@308 26 #include "DataFileReaderFactory.h"
Chris@148 27
Chris@148 28 #include <QFile>
Chris@1030 29 #include <QFileInfo>
Chris@148 30 #include <QString>
Chris@148 31 #include <QRegExp>
Chris@148 32 #include <QStringList>
Chris@148 33 #include <QTextStream>
Chris@148 34
Chris@148 35 #include <iostream>
Chris@628 36 #include <map>
Chris@148 37
Chris@392 38 CSVFileReader::CSVFileReader(QString path, CSVFormat format,
Chris@929 39 int mainModelSampleRate) :
Chris@392 40 m_format(format),
Chris@1009 41 m_device(0),
Chris@1009 42 m_ownDevice(true),
Chris@631 43 m_warnings(0),
Chris@148 44 m_mainModelSampleRate(mainModelSampleRate)
Chris@148 45 {
Chris@1009 46 QFile *file = new QFile(path);
Chris@148 47 bool good = false;
Chris@148 48
Chris@1009 49 if (!file->exists()) {
Chris@148 50 m_error = QFile::tr("File \"%1\" does not exist").arg(path);
Chris@1009 51 } else if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
Chris@148 52 m_error = QFile::tr("Failed to open file \"%1\"").arg(path);
Chris@148 53 } else {
Chris@148 54 good = true;
Chris@148 55 }
Chris@148 56
Chris@1009 57 if (good) {
Chris@1009 58 m_device = file;
Chris@1030 59 m_filename = QFileInfo(path).fileName();
Chris@1009 60 } else {
Chris@1009 61 delete file;
Chris@148 62 }
Chris@148 63 }
Chris@148 64
Chris@1009 65 CSVFileReader::CSVFileReader(QIODevice *device, CSVFormat format,
Chris@1009 66 int mainModelSampleRate) :
Chris@1009 67 m_format(format),
Chris@1009 68 m_device(device),
Chris@1009 69 m_ownDevice(false),
Chris@1009 70 m_warnings(0),
Chris@1009 71 m_mainModelSampleRate(mainModelSampleRate)
Chris@1009 72 {
Chris@1009 73 }
Chris@1009 74
Chris@148 75 CSVFileReader::~CSVFileReader()
Chris@148 76 {
Chris@1009 77 SVDEBUG << "CSVFileReader::~CSVFileReader: device is " << m_device << endl;
Chris@148 78
Chris@1009 79 if (m_device && m_ownDevice) {
Chris@1009 80 SVDEBUG << "CSVFileReader::CSVFileReader: Closing device" << endl;
Chris@1009 81 m_device->close();
Chris@1009 82 delete m_device;
Chris@148 83 }
Chris@148 84 }
Chris@148 85
Chris@148 86 bool
Chris@148 87 CSVFileReader::isOK() const
Chris@148 88 {
Chris@1009 89 return (m_device != 0);
Chris@148 90 }
Chris@148 91
Chris@148 92 QString
Chris@148 93 CSVFileReader::getError() const
Chris@148 94 {
Chris@148 95 return m_error;
Chris@148 96 }
Chris@148 97
Chris@929 98 int
Chris@929 99 CSVFileReader::convertTimeValue(QString s, int lineno, int sampleRate,
Chris@929 100 int windowSize) const
Chris@631 101 {
Chris@631 102 QRegExp nonNumericRx("[^0-9eE.,+-]");
Chris@897 103 int warnLimit = 10;
Chris@631 104
Chris@631 105 CSVFormat::TimeUnits timeUnits = m_format.getTimeUnits();
Chris@631 106
Chris@929 107 int calculatedFrame = 0;
Chris@631 108
Chris@631 109 bool ok = false;
Chris@631 110 QString numeric = s;
Chris@631 111 numeric.remove(nonNumericRx);
Chris@631 112
Chris@631 113 if (timeUnits == CSVFormat::TimeSeconds) {
Chris@631 114
Chris@631 115 double time = numeric.toDouble(&ok);
Chris@631 116 if (!ok) time = StringBits::stringToDoubleLocaleFree(numeric, &ok);
Chris@631 117 calculatedFrame = int(time * sampleRate + 0.5);
Chris@990 118
Chris@990 119 } else if (timeUnits == CSVFormat::TimeMilliseconds) {
Chris@990 120
Chris@990 121 double time = numeric.toDouble(&ok);
Chris@990 122 if (!ok) time = StringBits::stringToDoubleLocaleFree(numeric, &ok);
Chris@990 123 calculatedFrame = int((time / 1000.0) * sampleRate + 0.5);
Chris@631 124
Chris@631 125 } else {
Chris@631 126
Chris@631 127 long n = numeric.toLong(&ok);
Chris@631 128 if (n >= 0) calculatedFrame = n;
Chris@631 129
Chris@631 130 if (timeUnits == CSVFormat::TimeWindows) {
Chris@631 131 calculatedFrame *= windowSize;
Chris@631 132 }
Chris@631 133 }
Chris@631 134
Chris@631 135 if (!ok) {
Chris@631 136 if (m_warnings < warnLimit) {
Chris@843 137 cerr << "WARNING: CSVFileReader::load: "
Chris@844 138 << "Bad time format (\"" << s
Chris@631 139 << "\") in data line "
Chris@843 140 << lineno+1 << endl;
Chris@631 141 } else if (m_warnings == warnLimit) {
Chris@843 142 cerr << "WARNING: Too many warnings" << endl;
Chris@631 143 }
Chris@631 144 ++m_warnings;
Chris@631 145 }
Chris@631 146
Chris@631 147 return calculatedFrame;
Chris@631 148 }
Chris@631 149
Chris@148 150 Model *
Chris@148 151 CSVFileReader::load() const
Chris@148 152 {
Chris@1009 153 if (!m_device) return 0;
Chris@148 154
Chris@628 155 CSVFormat::ModelType modelType = m_format.getModelType();
Chris@392 156 CSVFormat::TimingType timingType = m_format.getTimingType();
Chris@628 157 CSVFormat::TimeUnits timeUnits = m_format.getTimeUnits();
Chris@929 158 int sampleRate = m_format.getSampleRate();
Chris@929 159 int windowSize = m_format.getWindowSize();
Chris@631 160 QChar separator = m_format.getSeparator();
Chris@631 161 bool allowQuoting = m_format.getAllowQuoting();
Chris@148 162
Chris@392 163 if (timingType == CSVFormat::ExplicitTiming) {
Chris@611 164 if (modelType == CSVFormat::ThreeDimensionalModel) {
Chris@611 165 // This will be overridden later if more than one line
Chris@611 166 // appears in our file, but we want to choose a default
Chris@611 167 // that's likely to be visible
Chris@611 168 windowSize = 1024;
Chris@611 169 } else {
Chris@611 170 windowSize = 1;
Chris@611 171 }
Chris@990 172 if (timeUnits == CSVFormat::TimeSeconds ||
Chris@990 173 timeUnits == CSVFormat::TimeMilliseconds) {
Chris@148 174 sampleRate = m_mainModelSampleRate;
Chris@148 175 }
Chris@148 176 }
Chris@148 177
Chris@148 178 SparseOneDimensionalModel *model1 = 0;
Chris@148 179 SparseTimeValueModel *model2 = 0;
Chris@628 180 RegionModel *model2a = 0;
Chris@897 181 NoteModel *model2b = 0;
Chris@152 182 EditableDenseThreeDimensionalModel *model3 = 0;
Chris@148 183 Model *model = 0;
Chris@148 184
Chris@1009 185 QTextStream in(m_device);
Chris@148 186
Chris@148 187 unsigned int warnings = 0, warnLimit = 10;
Chris@148 188 unsigned int lineno = 0;
Chris@148 189
Chris@148 190 float min = 0.0, max = 0.0;
Chris@148 191
Chris@929 192 int frameNo = 0;
Chris@929 193 int duration = 0;
Chris@929 194 int endFrame = 0;
Chris@631 195
Chris@631 196 bool haveAnyValue = false;
Chris@631 197 bool haveEndTime = false;
Chris@897 198 bool pitchLooksLikeMIDI = true;
Chris@631 199
Chris@929 200 int startFrame = 0; // for calculation of dense model resolution
Chris@631 201 bool firstEverValue = true;
Chris@148 202
Chris@631 203 std::map<QString, int> labelCountMap;
Chris@631 204
Chris@676 205 int valueColumns = 0;
Chris@676 206 for (int i = 0; i < m_format.getColumnCount(); ++i) {
Chris@676 207 if (m_format.getColumnPurpose(i) == CSVFormat::ColumnValue) {
Chris@676 208 ++valueColumns;
Chris@676 209 }
Chris@676 210 }
Chris@676 211
Chris@148 212 while (!in.atEnd()) {
Chris@148 213
Chris@283 214 // QTextStream's readLine doesn't cope with old-style Mac
Chris@283 215 // CR-only line endings. Why did they bother making the class
Chris@283 216 // cope with more than one sort of line ending, if it still
Chris@283 217 // can't be configured to cope with all the common sorts?
Chris@148 218
Chris@283 219 // For the time being we'll deal with this case (which is
Chris@283 220 // relatively uncommon for us, but still necessary to handle)
Chris@283 221 // by reading the entire file using a single readLine, and
Chris@283 222 // splitting it. For CR and CR/LF line endings this will just
Chris@283 223 // read a line at a time, and that's obviously OK.
Chris@148 224
Chris@283 225 QString chunk = in.readLine();
Chris@283 226 QStringList lines = chunk.split('\r', QString::SkipEmptyParts);
Chris@283 227
Chris@897 228 for (int li = 0; li < lines.size(); ++li) {
Chris@148 229
Chris@283 230 QString line = lines[li];
Chris@1009 231
Chris@283 232 if (line.startsWith("#")) continue;
Chris@283 233
Chris@631 234 QStringList list = StringBits::split(line, separator, allowQuoting);
Chris@283 235 if (!model) {
Chris@283 236
Chris@283 237 switch (modelType) {
Chris@283 238
Chris@392 239 case CSVFormat::OneDimensionalModel:
Chris@283 240 model1 = new SparseOneDimensionalModel(sampleRate, windowSize);
Chris@283 241 model = model1;
Chris@283 242 break;
Chris@148 243
Chris@392 244 case CSVFormat::TwoDimensionalModel:
Chris@283 245 model2 = new SparseTimeValueModel(sampleRate, windowSize, false);
Chris@283 246 model = model2;
Chris@283 247 break;
Chris@148 248
Chris@628 249 case CSVFormat::TwoDimensionalModelWithDuration:
Chris@628 250 model2a = new RegionModel(sampleRate, windowSize, false);
Chris@628 251 model = model2a;
Chris@628 252 break;
Chris@628 253
Chris@897 254 case CSVFormat::TwoDimensionalModelWithDurationAndPitch:
Chris@897 255 model2b = new NoteModel(sampleRate, windowSize, false);
Chris@897 256 model = model2b;
Chris@897 257 break;
Chris@897 258
Chris@392 259 case CSVFormat::ThreeDimensionalModel:
Chris@535 260 model3 = new EditableDenseThreeDimensionalModel
Chris@535 261 (sampleRate,
Chris@535 262 windowSize,
Chris@676 263 valueColumns,
Chris@535 264 EditableDenseThreeDimensionalModel::NoCompression);
Chris@283 265 model = model3;
Chris@283 266 break;
Chris@283 267 }
Chris@1030 268
Chris@1030 269 if (model) {
Chris@1030 270 if (m_filename != "") {
Chris@1030 271 model->setObjectName(m_filename);
Chris@1030 272 }
Chris@1030 273 }
Chris@283 274 }
Chris@148 275
Chris@631 276 float value = 0.f;
Chris@897 277 float pitch = 0.f;
Chris@631 278 QString label = "";
Chris@148 279
Chris@631 280 duration = 0.f;
Chris@631 281 haveEndTime = false;
Chris@628 282
Chris@283 283 for (int i = 0; i < list.size(); ++i) {
Chris@148 284
Chris@631 285 QString s = list[i];
Chris@631 286
Chris@631 287 CSVFormat::ColumnPurpose purpose = m_format.getColumnPurpose(i);
Chris@631 288
Chris@631 289 switch (purpose) {
Chris@631 290
Chris@631 291 case CSVFormat::ColumnUnknown:
Chris@631 292 break;
Chris@631 293
Chris@631 294 case CSVFormat::ColumnStartTime:
Chris@631 295 frameNo = convertTimeValue(s, lineno, sampleRate, windowSize);
Chris@631 296 break;
Chris@631 297
Chris@631 298 case CSVFormat::ColumnEndTime:
Chris@631 299 endFrame = convertTimeValue(s, lineno, sampleRate, windowSize);
Chris@631 300 haveEndTime = true;
Chris@631 301 break;
Chris@631 302
Chris@631 303 case CSVFormat::ColumnDuration:
Chris@631 304 duration = convertTimeValue(s, lineno, sampleRate, windowSize);
Chris@631 305 break;
Chris@631 306
Chris@631 307 case CSVFormat::ColumnValue:
Chris@631 308 value = s.toFloat();
Chris@631 309 haveAnyValue = true;
Chris@631 310 break;
Chris@631 311
Chris@897 312 case CSVFormat::ColumnPitch:
Chris@897 313 pitch = s.toFloat();
Chris@897 314 if (pitch < 0.f || pitch > 127.f) {
Chris@897 315 pitchLooksLikeMIDI = false;
Chris@897 316 }
Chris@897 317 break;
Chris@897 318
Chris@631 319 case CSVFormat::ColumnLabel:
Chris@631 320 label = s;
Chris@631 321 ++labelCountMap[label];
Chris@631 322 break;
Chris@283 323 }
Chris@631 324 }
Chris@148 325
Chris@631 326 if (haveEndTime) { // ... calculate duration now all cols read
Chris@631 327 if (endFrame > frameNo) {
Chris@631 328 duration = endFrame - frameNo;
Chris@628 329 }
Chris@283 330 }
Chris@148 331
Chris@392 332 if (modelType == CSVFormat::OneDimensionalModel) {
Chris@148 333
Chris@631 334 SparseOneDimensionalModel::Point point(frameNo, label);
Chris@283 335 model1->addPoint(point);
Chris@148 336
Chris@392 337 } else if (modelType == CSVFormat::TwoDimensionalModel) {
Chris@148 338
Chris@631 339 SparseTimeValueModel::Point point(frameNo, value, label);
Chris@283 340 model2->addPoint(point);
Chris@148 341
Chris@628 342 } else if (modelType == CSVFormat::TwoDimensionalModelWithDuration) {
Chris@628 343
Chris@631 344 RegionModel::Point point(frameNo, value, duration, label);
Chris@628 345 model2a->addPoint(point);
Chris@628 346
Chris@897 347 } else if (modelType == CSVFormat::TwoDimensionalModelWithDurationAndPitch) {
Chris@897 348
Chris@897 349 float level = ((value >= 0.f && value <= 1.f) ? value : 1.f);
Chris@897 350 NoteModel::Point point(frameNo, pitch, duration, level, label);
Chris@897 351 model2b->addPoint(point);
Chris@897 352
Chris@392 353 } else if (modelType == CSVFormat::ThreeDimensionalModel) {
Chris@148 354
Chris@283 355 DenseThreeDimensionalModel::Column values;
Chris@148 356
Chris@631 357 for (int i = 0; i < list.size(); ++i) {
Chris@148 358
Chris@676 359 if (m_format.getColumnPurpose(i) != CSVFormat::ColumnValue) {
Chris@676 360 continue;
Chris@676 361 }
Chris@676 362
Chris@283 363 bool ok = false;
Chris@283 364 float value = list[i].toFloat(&ok);
Chris@611 365
Chris@676 366 values.push_back(value);
Chris@148 367
Chris@631 368 if (firstEverValue || value < min) min = value;
Chris@631 369 if (firstEverValue || value > max) max = value;
Chris@676 370
Chris@631 371 if (firstEverValue) {
Chris@611 372 startFrame = frameNo;
Chris@611 373 model3->setStartFrame(startFrame);
Chris@611 374 } else if (lineno == 1 &&
Chris@611 375 timingType == CSVFormat::ExplicitTiming) {
Chris@611 376 model3->setResolution(frameNo - startFrame);
Chris@611 377 }
Chris@631 378
Chris@631 379 firstEverValue = false;
Chris@148 380
Chris@283 381 if (!ok) {
Chris@283 382 if (warnings < warnLimit) {
Chris@843 383 cerr << "WARNING: CSVFileReader::load: "
Chris@390 384 << "Non-numeric value \""
Chris@844 385 << list[i]
Chris@491 386 << "\" in data line " << lineno+1
Chris@843 387 << ":" << endl;
Chris@843 388 cerr << line << endl;
Chris@283 389 ++warnings;
Chris@283 390 } else if (warnings == warnLimit) {
Chris@843 391 // cerr << "WARNING: Too many warnings" << endl;
Chris@283 392 }
Chris@283 393 }
Chris@283 394 }
Chris@148 395
Chris@690 396 // SVDEBUG << "Setting bin values for count " << lineno << ", frame "
Chris@687 397 // << frameNo << ", time " << RealTime::frame2RealTime(frameNo, sampleRate) << endl;
Chris@148 398
Chris@611 399 model3->setColumn(lineno, values);
Chris@283 400 }
Chris@148 401
Chris@283 402 ++lineno;
Chris@392 403 if (timingType == CSVFormat::ImplicitTiming ||
Chris@283 404 list.size() == 0) {
Chris@283 405 frameNo += windowSize;
Chris@283 406 }
Chris@283 407 }
Chris@148 408 }
Chris@148 409
Chris@631 410 if (!haveAnyValue) {
Chris@631 411 if (model2a) {
Chris@631 412 // assign values for regions based on label frequency; we
Chris@631 413 // have this in our labelCountMap, sort of
Chris@631 414
Chris@631 415 std::map<int, std::map<QString, float> > countLabelValueMap;
Chris@631 416 for (std::map<QString, int>::iterator i = labelCountMap.begin();
Chris@631 417 i != labelCountMap.end(); ++i) {
Chris@631 418 countLabelValueMap[i->second][i->first] = 0.f;
Chris@631 419 }
Chris@631 420
Chris@631 421 float v = 0.f;
Chris@631 422 for (std::map<int, std::map<QString, float> >::iterator i =
Chris@631 423 countLabelValueMap.end(); i != countLabelValueMap.begin(); ) {
Chris@631 424 --i;
Chris@631 425 for (std::map<QString, float>::iterator j = i->second.begin();
Chris@631 426 j != i->second.end(); ++j) {
Chris@631 427 j->second = v;
Chris@631 428 v = v + 1.f;
Chris@631 429 }
Chris@631 430 }
Chris@631 431
Chris@631 432 std::map<RegionModel::Point, RegionModel::Point,
Chris@631 433 RegionModel::Point::Comparator> pointMap;
Chris@631 434 for (RegionModel::PointList::const_iterator i =
Chris@631 435 model2a->getPoints().begin();
Chris@631 436 i != model2a->getPoints().end(); ++i) {
Chris@631 437 RegionModel::Point p(*i);
Chris@631 438 v = countLabelValueMap[labelCountMap[p.label]][p.label];
Chris@631 439 RegionModel::Point pp(p.frame, v, p.duration, p.label);
Chris@631 440 pointMap[p] = pp;
Chris@631 441 }
Chris@631 442
Chris@631 443 for (std::map<RegionModel::Point, RegionModel::Point>::iterator i =
Chris@631 444 pointMap.begin(); i != pointMap.end(); ++i) {
Chris@631 445 model2a->deletePoint(i->first);
Chris@631 446 model2a->addPoint(i->second);
Chris@631 447 }
Chris@631 448 }
Chris@631 449 }
Chris@631 450
Chris@897 451 if (model2b) {
Chris@897 452 if (pitchLooksLikeMIDI) {
Chris@897 453 model2b->setScaleUnits("MIDI Pitch");
Chris@897 454 } else {
Chris@897 455 model2b->setScaleUnits("Hz");
Chris@897 456 }
Chris@897 457 }
Chris@897 458
Chris@961 459 if (model3) {
Chris@148 460 model3->setMinimumLevel(min);
Chris@148 461 model3->setMaximumLevel(max);
Chris@148 462 }
Chris@148 463
Chris@148 464 return model;
Chris@148 465 }
Chris@148 466