Chris@147: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@147: 
Chris@147: /*
Chris@147:     Sonic Visualiser
Chris@147:     An audio file viewer and annotation editor.
Chris@147:     Centre for Digital Music, Queen Mary, University of London.
Chris@297:     This file copyright 2006-2007 Chris Cannam and QMUL.
Chris@147:     
Chris@147:     This program is free software; you can redistribute it and/or
Chris@147:     modify it under the terms of the GNU General Public License as
Chris@147:     published by the Free Software Foundation; either version 2 of the
Chris@147:     License, or (at your option) any later version.  See the file
Chris@147:     COPYING included with this distribution for more information.
Chris@147: */
Chris@147: 
Chris@147: #ifndef _RANGE_SUMMARISABLE_TIME_VALUE_MODEL_H_
Chris@147: #define _RANGE_SUMMARISABLE_TIME_VALUE_MODEL_H_
Chris@147: 
Chris@147: #include <QObject>
Chris@147: 
Chris@147: #include "DenseTimeValueModel.h"
Chris@147: #include "base/ZoomConstraint.h"
Chris@147: 
chris@769: #include <stdint.h>
chris@769: 
Chris@147: /**
Chris@147:  * Base class for models containing dense two-dimensional data (value
Chris@147:  * against time) that may be meaningfully represented in a zoomed view
Chris@147:  * using min/max range summaries.  Audio waveform data is an obvious
Chris@147:  * example: think "peaks and minima" for "ranges".
Chris@147:  */
Chris@147: 
Chris@179: class RangeSummarisableTimeValueModel : public DenseTimeValueModel
Chris@147: {
Chris@147:     Q_OBJECT
Chris@147: 
Chris@147: public:
Chris@319:     RangeSummarisableTimeValueModel() { }
Chris@297: 
Chris@410:     class Range
Chris@410:     {
Chris@410:     public:
Chris@410:         Range() : 
Chris@1053:             m_new(true), m_min(0.f), m_max(0.f), m_absmean(0.f) { }
Chris@410:         Range(const Range &r) : 
Chris@1053:             m_new(true), m_min(r.m_min), m_max(r.m_max), m_absmean(r.m_absmean) { }
Chris@410:         Range(float min, float max, float absmean) :
Chris@1053:             m_new(true), m_min(min), m_max(max), m_absmean(absmean) { }
Chris@410: 
Chris@410:         float min() const { return m_min; }
Chris@410:         float max() const { return m_max; }
Chris@410:         float absmean() const { return m_absmean; }
Chris@410: 
Chris@1053:         void setMin(float min) { m_min = min; m_new = false; }
Chris@1053:         void setMax(float max) { m_max = max; m_new = false; }
Chris@410:         void setAbsmean(float absmean) { m_absmean = absmean; }
Chris@410: 
Chris@1053:         void sample(float s) {
Chris@1053:             if (m_new) {
Chris@1053:                 m_min = s;
Chris@1053:                 m_max = s;
Chris@1053:                 m_new = false;
Chris@1053:             } else {
Chris@1053:                 if (s < m_min) m_min = s;
Chris@1053:                 if (s > m_max) m_max = s;
Chris@1053:             }
Chris@1053:         }
Chris@1053:         
Chris@410:     private:
Chris@1053:         bool m_new;
Chris@410:         float m_min;
Chris@410:         float m_max;
Chris@410:         float m_absmean;
Chris@410:     };
Chris@147: 
Chris@147:     typedef std::vector<Range> RangeBlock;
Chris@147: 
Chris@147:     /**
Chris@300:      * Return ranges from the given start frame, corresponding to the
Chris@300:      * given number of underlying sample frames, summarised at the
Chris@300:      * given block size.  duration / blockSize ranges should ideally
Chris@300:      * be returned.
Chris@147:      *
Chris@147:      * If the given block size is not supported by this model
Chris@147:      * (according to its zoom constraint), also modify the blockSize
Chris@147:      * parameter so as to return the block size that was actually
Chris@147:      * obtained.
Chris@147:      */
Chris@1038:     virtual void getSummaries(int channel, sv_frame_t start, sv_frame_t count,
Chris@300:                               RangeBlock &ranges,
Chris@929:                               int &blockSize) const = 0;
Chris@147: 
Chris@147:     /**
Chris@300:      * Return the range from the given start frame, corresponding to
Chris@300:      * the given number of underlying sample frames, summarised at a
Chris@300:      * block size equal to the distance between start and end frames.
Chris@147:      */
Chris@1038:     virtual Range getSummary(int channel, sv_frame_t start, sv_frame_t count) const = 0;
Chris@345: 
Chris@929:     virtual int getSummaryBlockSize(int desired) const = 0;
Chris@377: 
Chris@345:     QString getTypeName() const { return tr("Range-Summarisable Time-Value"); }
Chris@147: };
Chris@147: 
Chris@147: #endif
Chris@147: