comparison layer/ScrollableMagRangeCache.cpp @ 1146:74f2706995b7 3.0-integration

Merge work on unified spectrogram and colour 3d plot caching renderer
author Chris Cannam
date Fri, 05 Aug 2016 15:05:02 +0100
parents c53ed1a6fcbd
children f2f43802718b
comparison
equal deleted inserted replaced
1056:b4fd6c67fce5 1146:74f2706995b7
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 #include "ScrollableMagRangeCache.h"
16
17 #include <iostream>
18 using namespace std;
19
20 //#define DEBUG_SCROLLABLE_MAG_RANGE_CACHE 1
21
22 void
23 ScrollableMagRangeCache::scrollTo(const LayerGeometryProvider *v,
24 sv_frame_t newStartFrame)
25 {
26 int dx = (v->getXForFrame(m_startFrame) -
27 v->getXForFrame(newStartFrame));
28
29 #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE
30 cerr << "ScrollableMagRangeCache::scrollTo: start frame " << m_startFrame
31 << " -> " << newStartFrame << ", dx = " << dx << endl;
32 #endif
33
34 if (m_startFrame == newStartFrame) {
35 // haven't moved
36 return;
37 }
38
39 m_startFrame = newStartFrame;
40
41 if (dx == 0) {
42 // haven't moved visibly (even though start frame may have changed)
43 return;
44 }
45
46 int w = int(m_ranges.size());
47
48 if (dx <= -w || dx >= w) {
49 // scrolled entirely off
50 invalidate();
51 return;
52 }
53
54 // dx is in range, cache is scrollable
55
56 if (dx < 0) {
57 // The new start frame is to the left of the old start
58 // frame. We need to add some empty ranges at the left (start)
59 // end and clip the right end. Assemble -dx new values, then
60 // w+dx old values starting at index 0.
61
62 auto newRanges = vector<MagnitudeRange>(-dx);
63 newRanges.insert(newRanges.end(),
64 m_ranges.begin(), m_ranges.begin() + (w + dx));
65 m_ranges = newRanges;
66
67 } else {
68 // The new start frame is to the right of the old start
69 // frame. We want to clip the left (start) end and add some
70 // empty ranges at the right end. Assemble w-dx old values
71 // starting at index dx, then dx new values.
72
73 auto newRanges = vector<MagnitudeRange>(dx);
74 newRanges.insert(newRanges.begin(),
75 m_ranges.begin() + dx, m_ranges.end());
76 m_ranges = newRanges;
77 }
78
79 #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE
80 cerr << "maxes (" << m_ranges.size() << ") now: ";
81 for (int i = 0; in_range_for(m_ranges, i); ++i) {
82 cerr << m_ranges[i].getMax() << " ";
83 }
84 cerr << endl;
85 #endif
86 }
87
88 MagnitudeRange
89 ScrollableMagRangeCache::getRange(int x, int count) const
90 {
91 MagnitudeRange r;
92 #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE
93 cerr << "ScrollableMagRangeCache::getRange(" << x << ", " << count << ")" << endl;
94 #endif
95 for (int i = 0; i < count; ++i) {
96 r.sample(m_ranges.at(x + i));
97 }
98 return r;
99 }
100
101 void
102 ScrollableMagRangeCache::sampleColumn(int column, const MagnitudeRange &r)
103 {
104 if (!in_range_for(m_ranges, column)) {
105 cerr << "ERROR: ScrollableMagRangeCache::sampleColumn: column " << column
106 << " is out of range for cache of width " << m_ranges.size()
107 << " (with start frame " << m_startFrame << ")" << endl;
108 throw logic_error("column out of range");
109 } else {
110 m_ranges[column].sample(r);
111 }
112 }
113