SparseOneDimensionalModel.h

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #ifndef _SPARSE_ONE_DIMENSIONAL_MODEL_H_
00017 #define _SPARSE_ONE_DIMENSIONAL_MODEL_H_
00018 
00019 #include "SparseModel.h"
00020 #include "base/PlayParameterRepository.h"
00021 #include "base/RealTime.h"
00022 
00023 struct OneDimensionalPoint
00024 {
00025 public:
00026     OneDimensionalPoint(long _frame) : frame(_frame) { }
00027     OneDimensionalPoint(long _frame, QString _label) : frame(_frame), label(_label) { }
00028 
00029     int getDimensions() const { return 1; }
00030     
00031     long frame;
00032     QString label;
00033 
00034     QString getLabel() const { return label; }
00035 
00036     void toXml(QTextStream &stream,
00037                QString indent = "",
00038                QString extraAttributes = "") const
00039     {
00040         stream << QString("%1<point frame=\"%2\" label=\"%3\" %4/>\n")
00041             .arg(indent).arg(frame).arg(label).arg(extraAttributes);
00042     }
00043 
00044     QString toDelimitedDataString(QString delimiter, size_t sampleRate) const
00045     {
00046         QStringList list;
00047         list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
00048         if (label != "") list << label;
00049         return list.join(delimiter);
00050     }
00051 
00052     struct Comparator {
00053         bool operator()(const OneDimensionalPoint &p1,
00054                         const OneDimensionalPoint &p2) const {
00055             if (p1.frame != p2.frame) return p1.frame < p2.frame;
00056             return p1.label < p2.label;
00057         }
00058     };
00059     
00060     struct OrderComparator {
00061         bool operator()(const OneDimensionalPoint &p1,
00062                         const OneDimensionalPoint &p2) const {
00063             return p1.frame < p2.frame;
00064         }
00065     };
00066 };
00067 
00068 
00069 class SparseOneDimensionalModel : public SparseModel<OneDimensionalPoint>
00070 {
00071 public:
00072     SparseOneDimensionalModel(size_t sampleRate, size_t resolution,
00073                               bool notifyOnAdd = true) :
00074         SparseModel<OneDimensionalPoint>(sampleRate, resolution, notifyOnAdd)
00075     {
00076         PlayParameterRepository::getInstance()->addModel(this);
00077     }
00078 
00079     int getIndexOf(const Point &point) {
00080         // slow
00081         int i = 0;
00082         Point::Comparator comparator;
00083         for (PointList::const_iterator j = m_points.begin();
00084              j != m_points.end(); ++j, ++i) {
00085             if (!comparator(*j, point) && !comparator(point, *j)) return i;
00086         }
00087         return -1;
00088     }
00089 
00090     QString getTypeName() const { return tr("Sparse 1-D"); }
00091 };
00092 
00093 #endif
00094 
00095 
00096     

Generated on Wed Feb 20 15:45:28 2008 for SonicVisualiser by  doxygen 1.5.1