31 #include <QStringList> 32 #include <QTextStream> 38 int mainModelSampleRate) :
42 m_mainModelSampleRate(mainModelSampleRate)
48 m_error = QFile::tr(
"File \"%1\" does not exist").arg(path);
49 }
else if (!
m_file->open(QIODevice::ReadOnly | QIODevice::Text)) {
50 m_error = QFile::tr(
"Failed to open file \"%1\"").arg(path);
63 SVDEBUG <<
"CSVFileReader::~CSVFileReader: file is " <<
m_file << endl;
66 SVDEBUG <<
"CSVFileReader::CSVFileReader: Closing file" << endl;
88 QRegExp nonNumericRx(
"[^0-9eE.,+-]");
93 int calculatedFrame = 0;
97 numeric.remove(nonNumericRx);
101 double time = numeric.toDouble(&ok);
103 calculatedFrame = int(time * sampleRate + 0.5);
107 long n = numeric.toLong(&ok);
108 if (n >= 0) calculatedFrame = n;
111 calculatedFrame *= windowSize;
117 cerr <<
"WARNING: CSVFileReader::load: " 118 <<
"Bad time format (\"" << s
119 <<
"\") in data line " 122 cerr <<
"WARNING: Too many warnings" << endl;
127 return calculatedFrame;
167 unsigned int warnings = 0, warnLimit = 10;
168 unsigned int lineno = 0;
170 float min = 0.0, max = 0.0;
176 bool haveAnyValue =
false;
177 bool haveEndTime =
false;
178 bool pitchLooksLikeMIDI =
true;
181 bool firstEverValue =
true;
183 std::map<QString, int> labelCountMap;
185 int valueColumns = 0;
192 while (!in.atEnd()) {
205 QString chunk = in.readLine();
206 QStringList lines = chunk.split(
'\r', QString::SkipEmptyParts);
208 for (
int li = 0; li < lines.size(); ++li) {
210 QString line = lines[li];
212 if (line.startsWith(
"#"))
continue;
230 model2a =
new RegionModel(sampleRate, windowSize,
false);
235 model2b =
new NoteModel(sampleRate, windowSize,
false);
257 for (
int i = 0; i < list.size(); ++i) {
288 if (pitch < 0.f || pitch > 127.f) {
289 pitchLooksLikeMIDI =
false;
295 ++labelCountMap[label];
301 if (endFrame > frameNo) {
302 duration = endFrame - frameNo;
323 float level = ((value >= 0.f && value <= 1.f) ? value : 1.f);
331 for (
int i = 0; i < list.size(); ++i) {
338 float value = list[i].toFloat(&ok);
340 values.push_back(value);
342 if (firstEverValue || value < min) min = value;
343 if (firstEverValue || value > max) max = value;
345 if (firstEverValue) {
346 startFrame = frameNo;
348 }
else if (lineno == 1 &&
353 firstEverValue =
false;
356 if (warnings < warnLimit) {
357 cerr <<
"WARNING: CSVFileReader::load: " 358 <<
"Non-numeric value \"" 360 <<
"\" in data line " << lineno+1
362 cerr << line << endl;
364 }
else if (warnings == warnLimit) {
379 frameNo += windowSize;
389 std::map<int, std::map<QString, float> > countLabelValueMap;
390 for (std::map<QString, int>::iterator i = labelCountMap.begin();
391 i != labelCountMap.end(); ++i) {
392 countLabelValueMap[i->second][i->first] = 0.f;
396 for (std::map<
int, std::map<QString, float> >::iterator i =
397 countLabelValueMap.end(); i != countLabelValueMap.begin(); ) {
399 for (std::map<QString, float>::iterator j = i->second.begin();
400 j != i->second.end(); ++j) {
408 for (RegionModel::PointList::const_iterator i =
412 v = countLabelValueMap[labelCountMap[p.
label]][p.
label];
417 for (std::map<RegionModel::Point, RegionModel::Point>::iterator i =
418 pointMap.begin(); i != pointMap.end(); ++i) {
426 if (pitchLooksLikeMIDI) {
virtual void setMinimumLevel(float sz)
Set the minimum value of the value in a bin.
virtual void deletePoint(const PointType &point)
Remove a point.
static QStringList split(QString s, QChar separator, bool quoted)
Split a string at the given separator character.
int m_mainModelSampleRate
NoteModel – a concrete IntervalModel for notes.
RegionModel – a concrete IntervalModel for intervals associated with a value, which we call regions f...
virtual bool isOK() const
Return true if the file appears to be of the correct type.
int convertTimeValue(QString, int lineno, int sampleRate, int windowSize) const
virtual void addPoint(const PointType &point)
Add a point.
virtual SparseValueModel< PointType >::PointList getPoints(long start, long end) const
PointTypes have a duration, so this returns all points that span any of the given range (as well as t...
CSVFileReader(QString path, CSVFormat format, int mainModelSampleRate)
Time/value point type for use in a SparseModel or SparseValueModel.
virtual QString getError() const
virtual void setScaleUnits(QString units)
virtual void addPoint(const Point &point)
Add a point.
virtual void setStartFrame(int)
Set the frame offset of the first column.
virtual void addPoint(const PointType &point)
Add a point.
Model is the base class for all data models that represent any sort of data on a time scale based on ...
virtual void setMaximumLevel(float sz)
Set the maximum value of the value in a bin.
virtual Model * load() const
Read the file and return the corresponding data model.
virtual void setResolution(int sz)
Set the number of sample frames covered by each set of bins.
virtual void setColumn(int x, const Column &values)
Set the entire set of bin values at the given column.
static double stringToDoubleLocaleFree(QString s, bool *ok=0)
Convert a string to a double using basic "C"-locale syntax, i.e.