annotate base/MagnitudeRange.h @ 1195:c118d2022ffa spectrogram-minor-refactor

Scale range matching
author Chris Cannam
date Mon, 01 Aug 2016 15:06:16 +0100
parents 238780e92f86
children 4d0d94ba2ea7
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@1194 19 #include <vector>
Chris@1194 20
Chris@1186 21 /**
Chris@1186 22 * Maintain a min and max value, and update them when supplied a new
Chris@1186 23 * data point.
Chris@1186 24 */
Chris@1186 25 class MagnitudeRange
Chris@1186 26 {
Chris@1186 27 public:
Chris@1186 28 MagnitudeRange() : m_min(0), m_max(0) { }
Chris@1186 29 bool operator==(const MagnitudeRange &r) {
Chris@1186 30 return r.m_min == m_min && r.m_max == m_max;
Chris@1186 31 }
Chris@1186 32 bool isSet() const { return (m_min != 0.f || m_max != 0.f); }
Chris@1186 33 void set(float min, float max) {
Chris@1186 34 m_min = min;
Chris@1186 35 m_max = max;
Chris@1186 36 if (m_max < m_min) m_max = m_min;
Chris@1186 37 }
Chris@1186 38 bool sample(float f) {
Chris@1186 39 bool changed = false;
Chris@1186 40 if (isSet()) {
Chris@1186 41 if (f < m_min) { m_min = f; changed = true; }
Chris@1186 42 if (f > m_max) { m_max = f; changed = true; }
Chris@1186 43 } else {
Chris@1186 44 m_max = m_min = f;
Chris@1186 45 changed = true;
Chris@1186 46 }
Chris@1186 47 return changed;
Chris@1194 48 }
Chris@1194 49 bool sample(const std::vector<float> &ff) {
Chris@1194 50 bool changed = false;
Chris@1194 51 for (auto f: ff) {
Chris@1194 52 if (sample(f)) {
Chris@1194 53 changed = true;
Chris@1194 54 }
Chris@1194 55 }
Chris@1194 56 return changed;
Chris@1194 57 }
Chris@1186 58 bool sample(const MagnitudeRange &r) {
Chris@1186 59 bool changed = false;
Chris@1186 60 if (isSet()) {
Chris@1186 61 if (r.m_min < m_min) { m_min = r.m_min; changed = true; }
Chris@1186 62 if (r.m_max > m_max) { m_max = r.m_max; changed = true; }
Chris@1186 63 } else {
Chris@1186 64 m_min = r.m_min;
Chris@1186 65 m_max = r.m_max;
Chris@1186 66 changed = true;
Chris@1186 67 }
Chris@1186 68 return changed;
Chris@1186 69 }
Chris@1186 70 float getMin() const { return m_min; }
Chris@1186 71 float getMax() const { return m_max; }
Chris@1186 72 private:
Chris@1186 73 float m_min;
Chris@1186 74 float m_max;
Chris@1186 75 };
Chris@1186 76
Chris@1186 77 #endif