annotate data/fileio/CSVFormat.h @ 1853:f36fef97ac81

Add isSupported
author Chris Cannam
date Wed, 06 May 2020 09:08:06 +0100
parents f0ffc88a36b3
children 1b8c4ee06f6d
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@1362 16 #ifndef SV_CSV_FORMAT_H
Chris@1362 17 #define SV_CSV_FORMAT_H
Chris@392 18
Chris@392 19 #include <QString>
Chris@392 20 #include <QStringList>
Chris@392 21
Chris@1585 22 #include <set>
Chris@1585 23
Chris@1047 24 #include "base/BaseTypes.h"
Chris@1047 25
Chris@392 26 class CSVFormat
Chris@392 27 {
Chris@392 28 public:
Chris@392 29 enum ModelType {
Chris@1429 30 OneDimensionalModel,
Chris@1429 31 TwoDimensionalModel,
Chris@628 32 TwoDimensionalModelWithDuration,
Chris@897 33 TwoDimensionalModelWithDurationAndPitch,
Chris@1793 34 TwoDimensionalModelWithDurationAndExtent,
Chris@1488 35 ThreeDimensionalModel,
Chris@1488 36 WaveFileModel
Chris@392 37 };
Chris@392 38
Chris@392 39 enum TimingType {
Chris@1429 40 ExplicitTiming,
Chris@1429 41 ImplicitTiming
Chris@392 42 };
Chris@628 43
Chris@392 44 enum TimeUnits {
Chris@1429 45 TimeSeconds,
Chris@990 46 TimeMilliseconds,
Chris@1429 47 TimeAudioFrames,
Chris@1429 48 TimeWindows,
Chris@392 49 };
Chris@392 50
Chris@629 51 enum ColumnPurpose {
Chris@629 52 ColumnUnknown,
Chris@629 53 ColumnStartTime,
Chris@629 54 ColumnEndTime,
Chris@629 55 ColumnDuration,
Chris@629 56 ColumnValue,
Chris@897 57 ColumnPitch,
Chris@629 58 ColumnLabel
Chris@629 59 };
Chris@629 60
Chris@629 61 enum ColumnQuality {
Chris@1512 62 ColumnNumeric = 1, // No non-numeric values were seen in sample
Chris@1512 63 ColumnIntegral = 2, // All sampled values were integers
Chris@1512 64 ColumnIncreasing = 4, // Sampled values were monotonically increasing
Chris@1512 65 ColumnSmall = 8, // All sampled values had magnitude < 1
Chris@1512 66 ColumnLarge = 16, // Values "quickly" grew to over 1000
Chris@1512 67 ColumnSigned = 32, // Some negative values were seen
Chris@1512 68 ColumnNearEmpty = 64, // Nothing in this column beyond first row
Chris@629 69 };
Chris@629 70 typedef unsigned int ColumnQualities;
Chris@392 71
Chris@1515 72 enum AudioSampleRange {
Chris@1515 73 SampleRangeSigned1 = 0, // -1 .. 1
Chris@1515 74 SampleRangeUnsigned255, // 0 .. 255
Chris@1515 75 SampleRangeSigned32767, // -32768 .. 32767
Chris@1515 76 SampleRangeOther // Other/unknown: Normalise on load
Chris@1515 77 };
Chris@1515 78
Chris@392 79 CSVFormat() : // arbitrary defaults
Chris@392 80 m_modelType(TwoDimensionalModel),
Chris@392 81 m_timingType(ExplicitTiming),
Chris@392 82 m_timeUnits(TimeSeconds),
Chris@1585 83 m_separator(""),
Chris@392 84 m_sampleRate(44100),
Chris@392 85 m_windowSize(1024),
Chris@629 86 m_columnCount(0),
Chris@629 87 m_variableColumnCount(false),
Chris@1516 88 m_audioSampleRange(SampleRangeOther),
Chris@629 89 m_allowQuoting(true),
Chris@629 90 m_maxExampleCols(0)
Chris@392 91 { }
Chris@629 92
Chris@629 93 CSVFormat(QString path); // guess format
Chris@629 94
Chris@629 95 /**
Chris@629 96 * Guess the format of the given CSV file, setting the fields in
Chris@629 97 * this object accordingly. If the current separator is the empty
Chris@629 98 * string, the separator character will also be guessed; otherwise
Chris@629 99 * the current separator will be used. The other properties of
Chris@629 100 * this object will be set according to guesses from the file.
Chris@1524 101 *
Chris@1524 102 * The properties that are guessed from the file contents are:
Chris@1524 103 * separator, column count, variable-column-count flag, audio
Chris@1524 104 * sample range, timing type, time units, column qualities, column
Chris@1524 105 * purposes, and model type. The sample rate and window size
Chris@1524 106 * cannot be guessed and will not be changed by this function.
Chris@1524 107 * Note also that this function will never guess WaveFileModel for
Chris@1524 108 * the model type.
Chris@1524 109 *
Chris@1524 110 * Return false if there is some fundamental error, e.g. the file
Chris@1524 111 * could not be opened at all. Return true otherwise. Note that
Chris@1524 112 * this function returns true even if the file doesn't appear to
Chris@1524 113 * make much sense as a data format.
Chris@629 114 */
Chris@1524 115 bool guessFormatFor(QString path);
Chris@628 116
Chris@628 117 ModelType getModelType() const { return m_modelType; }
Chris@628 118 TimingType getTimingType() const { return m_timingType; }
Chris@628 119 TimeUnits getTimeUnits() const { return m_timeUnits; }
Chris@1047 120 sv_samplerate_t getSampleRate() const { return m_sampleRate; }
Chris@929 121 int getWindowSize() const { return m_windowSize; }
Chris@630 122 int getColumnCount() const { return m_columnCount; }
Chris@1516 123 AudioSampleRange getAudioSampleRange() const { return m_audioSampleRange; }
Chris@631 124 bool getAllowQuoting() const { return m_allowQuoting; }
Chris@631 125 QChar getSeparator() const {
Chris@1585 126 if (m_separator == "") return ',';
Chris@631 127 else return m_separator[0];
Chris@631 128 }
Chris@1585 129 // set rather than QSet to ensure a fixed order
Chris@1585 130 std::set<QChar> getPlausibleSeparators() const {
Chris@1585 131 return m_plausibleSeparators;
Chris@1585 132 }
Chris@630 133
Chris@628 134 void setModelType(ModelType t) { m_modelType = t; }
Chris@628 135 void setTimingType(TimingType t) { m_timingType = t; }
Chris@628 136 void setTimeUnits(TimeUnits t) { m_timeUnits = t; }
Chris@631 137 void setSeparator(QChar s) { m_separator = s; }
Chris@1047 138 void setSampleRate(sv_samplerate_t r) { m_sampleRate = r; }
Chris@1009 139 void setWindowSize(int s) { m_windowSize = s; }
Chris@630 140 void setColumnCount(int c) { m_columnCount = c; }
Chris@1516 141 void setAudioSampleRange(AudioSampleRange r) { m_audioSampleRange = r; }
Chris@631 142 void setAllowQuoting(bool q) { m_allowQuoting = q; }
Chris@392 143
Chris@631 144 QList<ColumnPurpose> getColumnPurposes() const { return m_columnPurposes; }
Chris@629 145 void setColumnPurposes(QList<ColumnPurpose> cl) { m_columnPurposes = cl; }
Chris@631 146
Chris@631 147 ColumnPurpose getColumnPurpose(int i);
Chris@631 148 ColumnPurpose getColumnPurpose(int i) const;
Chris@631 149 void setColumnPurpose(int i, ColumnPurpose p);
Chris@392 150
Chris@629 151 // read-only; only valid if format has been guessed:
Chris@1510 152 const QList<ColumnQualities> &getColumnQualities() const {
Chris@1510 153 return m_columnQualities;
Chris@1510 154 }
Chris@629 155
Chris@629 156 // read-only; only valid if format has been guessed:
Chris@1510 157 const QList<QStringList> &getExample() const {
Chris@1510 158 return m_example;
Chris@1510 159 }
Chris@1510 160
Chris@392 161 int getMaxExampleCols() const { return m_maxExampleCols; }
Chris@1429 162
Chris@392 163 protected:
Chris@628 164 ModelType m_modelType;
Chris@628 165 TimingType m_timingType;
Chris@628 166 TimeUnits m_timeUnits;
Chris@1585 167 QString m_separator; // "" or a single char - basically QChar option
Chris@1585 168 std::set<QChar> m_plausibleSeparators;
Chris@1047 169 sv_samplerate_t m_sampleRate;
Chris@929 170 int m_windowSize;
Chris@392 171
Chris@629 172 int m_columnCount;
Chris@629 173 bool m_variableColumnCount;
Chris@629 174
Chris@629 175 QList<ColumnQualities> m_columnQualities;
Chris@629 176 QList<ColumnPurpose> m_columnPurposes;
Chris@629 177
Chris@1515 178 AudioSampleRange m_audioSampleRange;
Chris@1515 179
Chris@629 180 QList<float> m_prevValues;
Chris@629 181
Chris@629 182 bool m_allowQuoting;
Chris@392 183
Chris@392 184 QList<QStringList> m_example;
Chris@392 185 int m_maxExampleCols;
Chris@629 186
Chris@629 187 void guessSeparator(QString line);
Chris@629 188 void guessQualities(QString line, int lineno);
Chris@629 189 void guessPurposes();
Chris@1515 190 void guessAudioSampleRange();
Chris@392 191 };
Chris@392 192
Chris@392 193 #endif