annotate data/fileio/CSVFormat.h @ 1008:d9e0e59a1581

When using an aggregate model to pass data to a transform, zero-pad the shorter input to the duration of the longer rather than truncating the longer. (This is better behaviour for e.g. MATCH, and in any case the code was previously truncating incorrectly and ending up with garbage data at the end.)
author Chris Cannam
date Fri, 14 Nov 2014 13:51:33 +0000
parents dc1695b90a58
children e369dd281cf2
rev   line source
Chris@392 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@392 2
Chris@392 3 /*
Chris@392 4 Sonic Visualiser
Chris@392 5 An audio file viewer and annotation editor.
Chris@392 6 Centre for Digital Music, Queen Mary, University of London.
Chris@392 7 This file copyright 2006 Chris Cannam.
Chris@392 8
Chris@392 9 This program is free software; you can redistribute it and/or
Chris@392 10 modify it under the terms of the GNU General Public License as
Chris@392 11 published by the Free Software Foundation; either version 2 of the
Chris@392 12 License, or (at your option) any later version. See the file
Chris@392 13 COPYING included with this distribution for more information.
Chris@392 14 */
Chris@392 15
Chris@392 16 #ifndef _CSV_FORMAT_H_
Chris@392 17 #define _CSV_FORMAT_H_
Chris@392 18
Chris@392 19 #include <QString>
Chris@392 20 #include <QStringList>
Chris@392 21
Chris@392 22 class CSVFormat
Chris@392 23 {
Chris@392 24 public:
Chris@392 25 enum ModelType {
Chris@392 26 OneDimensionalModel,
Chris@392 27 TwoDimensionalModel,
Chris@628 28 TwoDimensionalModelWithDuration,
Chris@897 29 TwoDimensionalModelWithDurationAndPitch,
Chris@392 30 ThreeDimensionalModel
Chris@392 31 };
Chris@392 32
Chris@392 33 enum TimingType {
Chris@392 34 ExplicitTiming,
Chris@392 35 ImplicitTiming
Chris@392 36 };
Chris@628 37
Chris@392 38 enum TimeUnits {
Chris@392 39 TimeSeconds,
Chris@990 40 TimeMilliseconds,
Chris@392 41 TimeAudioFrames,
Chris@990 42 TimeWindows,
Chris@392 43 };
Chris@392 44
Chris@629 45 enum ColumnPurpose {
Chris@629 46 ColumnUnknown,
Chris@629 47 ColumnStartTime,
Chris@629 48 ColumnEndTime,
Chris@629 49 ColumnDuration,
Chris@629 50 ColumnValue,
Chris@897 51 ColumnPitch,
Chris@629 52 ColumnLabel
Chris@629 53 };
Chris@629 54
Chris@629 55 enum ColumnQuality {
Chris@629 56 ColumnNumeric = 0x1,
Chris@629 57 ColumnIntegral = 0x2,
Chris@629 58 ColumnIncreasing = 0x4,
Chris@629 59 ColumnLarge = 0x8
Chris@629 60 };
Chris@629 61 typedef unsigned int ColumnQualities;
Chris@392 62
Chris@392 63 CSVFormat() : // arbitrary defaults
Chris@392 64 m_modelType(TwoDimensionalModel),
Chris@392 65 m_timingType(ExplicitTiming),
Chris@392 66 m_timeUnits(TimeSeconds),
Chris@392 67 m_separator(","),
Chris@392 68 m_sampleRate(44100),
Chris@392 69 m_windowSize(1024),
Chris@629 70 m_columnCount(0),
Chris@629 71 m_variableColumnCount(false),
Chris@629 72 m_allowQuoting(true),
Chris@629 73 m_maxExampleCols(0)
Chris@392 74 { }
Chris@629 75
Chris@629 76 CSVFormat(QString path); // guess format
Chris@629 77
Chris@629 78 /**
Chris@629 79 * Guess the format of the given CSV file, setting the fields in
Chris@629 80 * this object accordingly. If the current separator is the empty
Chris@629 81 * string, the separator character will also be guessed; otherwise
Chris@629 82 * the current separator will be used. The other properties of
Chris@629 83 * this object will be set according to guesses from the file.
Chris@629 84 */
Chris@629 85 void guessFormatFor(QString path);
Chris@628 86
Chris@628 87 ModelType getModelType() const { return m_modelType; }
Chris@628 88 TimingType getTimingType() const { return m_timingType; }
Chris@628 89 TimeUnits getTimeUnits() const { return m_timeUnits; }
Chris@929 90 int getSampleRate() const { return m_sampleRate; }
Chris@929 91 int getWindowSize() const { return m_windowSize; }
Chris@630 92 int getColumnCount() const { return m_columnCount; }
Chris@631 93 bool getAllowQuoting() const { return m_allowQuoting; }
Chris@631 94 QChar getSeparator() const {
Chris@631 95 if (m_separator == "") return ' ';
Chris@631 96 else return m_separator[0];
Chris@631 97 }
Chris@630 98
Chris@628 99 void setModelType(ModelType t) { m_modelType = t; }
Chris@628 100 void setTimingType(TimingType t) { m_timingType = t; }
Chris@628 101 void setTimeUnits(TimeUnits t) { m_timeUnits = t; }
Chris@631 102 void setSeparator(QChar s) { m_separator = s; }
Chris@929 103 void setSampleRate(int r) { m_sampleRate = r; }
Chris@929 104 void setWindowSize(int s) { m_windowSize = s; }
Chris@630 105 void setColumnCount(int c) { m_columnCount = c; }
Chris@631 106 void setAllowQuoting(bool q) { m_allowQuoting = q; }
Chris@392 107
Chris@631 108 QList<ColumnPurpose> getColumnPurposes() const { return m_columnPurposes; }
Chris@629 109 void setColumnPurposes(QList<ColumnPurpose> cl) { m_columnPurposes = cl; }
Chris@631 110
Chris@631 111 ColumnPurpose getColumnPurpose(int i);
Chris@631 112 ColumnPurpose getColumnPurpose(int i) const;
Chris@631 113 void setColumnPurpose(int i, ColumnPurpose p);
Chris@392 114
Chris@629 115 // read-only; only valid if format has been guessed:
Chris@629 116 QList<ColumnQualities> getColumnQualities() const { return m_columnQualities; }
Chris@629 117
Chris@629 118 // read-only; only valid if format has been guessed:
Chris@392 119 QList<QStringList> getExample() const { return m_example; }
Chris@392 120 int getMaxExampleCols() const { return m_maxExampleCols; }
Chris@631 121
Chris@392 122 protected:
Chris@628 123 ModelType m_modelType;
Chris@628 124 TimingType m_timingType;
Chris@628 125 TimeUnits m_timeUnits;
Chris@628 126 QString m_separator;
Chris@929 127 int m_sampleRate;
Chris@929 128 int m_windowSize;
Chris@392 129
Chris@629 130 int m_columnCount;
Chris@629 131 bool m_variableColumnCount;
Chris@629 132
Chris@629 133 QList<ColumnQualities> m_columnQualities;
Chris@629 134 QList<ColumnPurpose> m_columnPurposes;
Chris@629 135
Chris@629 136 QList<float> m_prevValues;
Chris@629 137
Chris@629 138 bool m_allowQuoting;
Chris@392 139
Chris@392 140 QList<QStringList> m_example;
Chris@392 141 int m_maxExampleCols;
Chris@629 142
Chris@629 143 void guessSeparator(QString line);
Chris@629 144 void guessQualities(QString line, int lineno);
Chris@629 145 void guessPurposes();
Chris@629 146
Chris@629 147 void guessFormatFor_Old(QString path);
Chris@629 148
Chris@392 149 };
Chris@392 150
Chris@392 151 #endif