comparison layer/ScrollableMagRangeCache.cpp @ 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 65cdaf8d6b50
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 #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 if (m_startFrame == newStartFrame) {
27 // haven't moved
28 return;
29 }
30
31 int dx = (v->getXForFrame(m_startFrame) -
32 v->getXForFrame(newStartFrame));
33
34 #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE
35 cerr << "ScrollableMagRangeCache::scrollTo: start frame " << m_startFrame
36 << " -> " << newStartFrame << ", dx = " << dx << endl;
37 #endif
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
80 void
81 ScrollableMagRangeCache::sampleColumn(const LayerGeometryProvider *v,
82 sv_frame_t frame,
83 const MagnitudeRange &r)
84 {
85 int x = (v->getXForFrame(frame) -
86 v->getXForFrame(m_startFrame));
87
88 if (!in_range_for(m_ranges, x)) {
89 cerr << "WARNING: ScrollableMagRangeCache::sampleColumn: column " << x
90 << " arising from frame " << frame << " is out of range for cache "
91 << "of width " << m_ranges.size() << endl;
92 } else {
93 sampleColumn(x, r);
94 }
95 }
96