comparison base/MagnitudeRange.h @ 1186:12a8daa89970 spectrogram-minor-refactor

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