comparison layer/ScrollableMagRangeCache.h @ 1118:175d4e15884d spectrogram-minor-refactor

Introduce ScrollableMagRangeCache, plus some tidying etc
author Chris Cannam
date Wed, 20 Jul 2016 08:30:20 +0100
parents
children be5b91ec81a0
comparison
equal deleted inserted replaced
1117:64709d4d09ef 1118:175d4e15884d
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
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #ifndef SCROLLABLE_MAG_RANGE_CACHE_H
16 #define SCROLLABLE_MAG_RANGE_CACHE_H
17
18 #include "base/BaseTypes.h"
19 #include "base/MagnitudeRange.h"
20
21 #include "LayerGeometryProvider.h"
22
23 /**
24 * A cached set of magnitude range records for a view that scrolls
25 * horizontally, such as a spectrogram. The cache object holds a
26 * magnitude range per column of the view, can report width (likely
27 * the same as the underlying view, but it's the caller's
28 * responsibility to set the size appropriately), can scroll the set
29 * of ranges, and can report and update which columns have had a range
30 * specified.
31 *
32 * The only way to *update* the valid area in a cache is to update the
33 * magnitude range for a column using the sampleColumn call.
34 */
35 class ScrollableMagRangeCache
36 {
37 public:
38 ScrollableMagRangeCache() :
39 m_startFrame(0),
40 m_zoomLevel(0)
41 {}
42
43 void invalidate() {
44 m_ranges = std::vector<MagnitudeRange>(m_ranges.size());
45 }
46
47 int getWidth() const {
48 return int(m_ranges.size());
49 }
50
51 /**
52 * Set the width of the cache in columns. If the new size differs
53 * from the current size, the cache is invalidated.
54 */
55 void resize(int newWidth) {
56 if (getWidth() != newWidth) {
57 m_ranges = std::vector<MagnitudeRange>(newWidth);
58 }
59 }
60
61 int getZoomLevel() const {
62 return m_zoomLevel;
63 }
64
65 /**
66 * Set the zoom level. If the new zoom level differs from the
67 * current one, the cache is invalidated. (Determining whether to
68 * invalidate the cache here is the only thing the zoom level is
69 * used for.)
70 */
71 void setZoomLevel(int zoom) {
72 if (m_zoomLevel != zoom) {
73 m_zoomLevel = zoom;
74 invalidate();
75 }
76 }
77
78 sv_frame_t getStartFrame() const {
79 return m_startFrame;
80 }
81
82 /**
83 * Set the start frame. If the new start frame differs from the
84 * current one, the cache is invalidated. To scroll, i.e. to set
85 * the start frame while retaining cache validity where possible,
86 * use scrollTo() instead.
87 */
88 void setStartFrame(sv_frame_t frame) {
89 if (m_startFrame != frame) {
90 m_startFrame = frame;
91 invalidate();
92 }
93 }
94
95 bool isColumnSet(int column) const {
96 return in_range_for(m_ranges, column) && m_ranges.at(column).isSet();
97 }
98
99 const MagnitudeRange &getRange(int column) const {
100 return m_ranges.at(column);
101 }
102
103 /**
104 * Set the new start frame for the cache, according to the
105 * geometry of the supplied LayerGeometryProvider, if possible
106 * also moving along any existing valid data within the cache so
107 * that it continues to be valid for the new start frame.
108 */
109 void scrollTo(const LayerGeometryProvider *v, sv_frame_t newStartFrame);
110
111 /**
112 * Update a column in the cache, by column number.
113 */
114 void sampleColumn(int column, const MagnitudeRange &r) {
115 m_ranges[column].sample(r);
116 }
117
118 /**
119 * Update a column in the cache, by frame.
120 */
121 void sampleColumn(const LayerGeometryProvider *v, sv_frame_t frame,
122 const MagnitudeRange &r);
123
124 private:
125 std::vector<MagnitudeRange> m_ranges;
126 sv_frame_t m_startFrame;
127 int m_zoomLevel;
128 };
129
130 #endif