annotate base/MagnitudeRange.h @ 1187:fd40a5335968 spectrogram-minor-refactor

Pull out column ops into ColumnOp
author Chris Cannam
date Mon, 20 Jun 2016 11:30:15 +0100
parents 12a8daa89970
children 238780e92f86
rev   line source
Chris@1186 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1186 2
Chris@1186 3 /*
Chris@1186 4 Sonic Visualiser
Chris@1186 5 An audio file viewer and annotation editor.
Chris@1186 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1186 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@1186 8
Chris@1186 9 This program is free software; you can redistribute it and/or
Chris@1186 10 modify it under the terms of the GNU General Public License as
Chris@1186 11 published by the Free Software Foundation; either version 2 of the
Chris@1186 12 License, or (at your option) any later version. See the file
Chris@1186 13 COPYING included with this distribution for more information.
Chris@1186 14 */
Chris@1186 15
Chris@1186 16 #ifndef MAGNITUDE_RANGE_H
Chris@1186 17 #define MAGNITUDE_RANGE_H
Chris@1186 18
Chris@1186 19 /**
Chris@1186 20 * Maintain a min and max value, and update them when supplied a new
Chris@1186 21 * data point.
Chris@1186 22 */
Chris@1186 23 class MagnitudeRange
Chris@1186 24 {
Chris@1186 25 public:
Chris@1186 26 MagnitudeRange() : m_min(0), m_max(0) { }
Chris@1186 27 bool operator==(const MagnitudeRange &r) {
Chris@1186 28 return r.m_min == m_min && r.m_max == m_max;
Chris@1186 29 }
Chris@1186 30 bool isSet() const { return (m_min != 0.f || m_max != 0.f); }
Chris@1186 31 void set(float min, float max) {
Chris@1186 32 m_min = min;
Chris@1186 33 m_max = max;
Chris@1186 34 if (m_max < m_min) m_max = m_min;
Chris@1186 35 }
Chris@1186 36 bool sample(float f) {
Chris@1186 37 bool changed = false;
Chris@1186 38 if (isSet()) {
Chris@1186 39 if (f < m_min) { m_min = f; changed = true; }
Chris@1186 40 if (f > m_max) { m_max = f; changed = true; }
Chris@1186 41 } else {
Chris@1186 42 m_max = m_min = f;
Chris@1186 43 changed = true;
Chris@1186 44 }
Chris@1186 45 return changed;
Chris@1186 46 }
Chris@1186 47 bool sample(const MagnitudeRange &r) {
Chris@1186 48 bool changed = false;
Chris@1186 49 if (isSet()) {
Chris@1186 50 if (r.m_min < m_min) { m_min = r.m_min; changed = true; }
Chris@1186 51 if (r.m_max > m_max) { m_max = r.m_max; changed = true; }
Chris@1186 52 } else {
Chris@1186 53 m_min = r.m_min;
Chris@1186 54 m_max = r.m_max;
Chris@1186 55 changed = true;
Chris@1186 56 }
Chris@1186 57 return changed;
Chris@1186 58 }
Chris@1186 59 float getMin() const { return m_min; }
Chris@1186 60 float getMax() const { return m_max; }
Chris@1186 61 private:
Chris@1186 62 float m_min;
Chris@1186 63 float m_max;
Chris@1186 64 };
Chris@1186 65
Chris@1186 66 #endif