annotate data/fileio/DataFileReaderFactory.cpp @ 1126:39019ce29178 tony-2.0-integration

Merge through to branch for Tony 2.0
author Chris Cannam
date Thu, 20 Aug 2015 14:54:21 +0100
parents 26cf6d5251ec
children 1dc64d3d323c
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 "DataFileReaderFactory.h"
Chris@148 17 #include "MIDIFileReader.h"
Chris@148 18 #include "CSVFileReader.h"
Chris@148 19
Chris@150 20 #include "model/Model.h"
Chris@148 21
Chris@148 22 #include <QString>
Chris@148 23
Chris@148 24 QString
Chris@148 25 DataFileReaderFactory::getKnownExtensions()
Chris@148 26 {
Chris@148 27 return "*.svl *.csv *.lab *.mid *.txt";
Chris@148 28 }
Chris@148 29
Chris@148 30 DataFileReader *
Chris@392 31 DataFileReaderFactory::createReader(QString path,
Chris@392 32 bool csv,
Chris@392 33 MIDIFileImportPreferenceAcquirer *acquirer,
Chris@392 34 CSVFormat format,
Chris@1047 35 sv_samplerate_t mainModelSampleRate)
Chris@148 36 {
Chris@148 37 QString err;
Chris@148 38
Chris@148 39 DataFileReader *reader = 0;
Chris@148 40
Chris@392 41 if (!csv) {
Chris@392 42 reader = new MIDIFileReader(path, acquirer, mainModelSampleRate);
Chris@392 43 if (reader->isOK()) return reader;
Chris@392 44 if (reader->getError() != "") err = reader->getError();
Chris@392 45 delete reader;
Chris@392 46 }
Chris@148 47
Chris@392 48 if (csv) {
Chris@392 49 reader = new CSVFileReader(path, format, mainModelSampleRate);
Chris@392 50 if (reader->isOK()) return reader;
Chris@392 51 if (reader->getError() != "") err = reader->getError();
Chris@392 52 delete reader;
Chris@392 53 }
Chris@392 54
Chris@392 55 return 0;
Chris@392 56 }
Chris@392 57
Chris@392 58 DataFileReader *
Chris@392 59 DataFileReaderFactory::createReader(QString path,
Chris@392 60 MIDIFileImportPreferenceAcquirer *acquirer,
Chris@1047 61 sv_samplerate_t mainModelSampleRate)
Chris@392 62 {
Chris@392 63 DataFileReader *reader = createReader
Chris@392 64 (path, false, acquirer, CSVFormat(), mainModelSampleRate);
Chris@392 65 if (reader) return reader;
Chris@392 66
Chris@392 67 reader = createReader
Chris@392 68 (path, true, acquirer, CSVFormat(path), mainModelSampleRate);
Chris@392 69 if (reader) return reader;
Chris@148 70
Chris@148 71 return 0;
Chris@148 72 }
Chris@148 73
Chris@148 74 Model *
Chris@392 75 DataFileReaderFactory::load(QString path,
Chris@392 76 MIDIFileImportPreferenceAcquirer *acquirer,
Chris@1047 77 sv_samplerate_t mainModelSampleRate)
Chris@148 78 {
Chris@392 79 DataFileReader *reader = createReader(path,
Chris@392 80 acquirer,
Chris@392 81 mainModelSampleRate);
Chris@148 82 if (!reader) return NULL;
Chris@148 83
Chris@308 84 try {
Chris@308 85 Model *model = reader->load();
Chris@308 86 delete reader;
Chris@308 87 return model;
Chris@308 88 } catch (Exception) {
Chris@308 89 delete reader;
Chris@308 90 throw;
Chris@308 91 }
Chris@148 92 }
Chris@148 93
Chris@392 94 Model *
Chris@392 95 DataFileReaderFactory::loadNonCSV(QString path,
Chris@392 96 MIDIFileImportPreferenceAcquirer *acquirer,
Chris@1047 97 sv_samplerate_t mainModelSampleRate)
Chris@392 98 {
Chris@392 99 DataFileReader *reader = createReader(path, false,
Chris@392 100 acquirer,
Chris@392 101 CSVFormat(),
Chris@392 102 mainModelSampleRate);
Chris@392 103 if (!reader) return NULL;
Chris@392 104
Chris@392 105 try {
Chris@392 106 Model *model = reader->load();
Chris@392 107 delete reader;
Chris@392 108 return model;
Chris@392 109 } catch (Exception) {
Chris@392 110 delete reader;
Chris@392 111 throw;
Chris@392 112 }
Chris@392 113 }
Chris@392 114
Chris@392 115 Model *
Chris@392 116 DataFileReaderFactory::loadCSV(QString path, CSVFormat format,
Chris@1047 117 sv_samplerate_t mainModelSampleRate)
Chris@392 118 {
Chris@392 119 DataFileReader *reader = createReader(path, true, 0, format,
Chris@392 120 mainModelSampleRate);
Chris@392 121 if (!reader) return NULL;
Chris@392 122
Chris@392 123 try {
Chris@392 124 Model *model = reader->load();
Chris@392 125 delete reader;
Chris@392 126 return model;
Chris@392 127 } catch (Exception) {
Chris@392 128 delete reader;
Chris@392 129 throw;
Chris@392 130 }
Chris@392 131 }
Chris@392 132