Chris@1118: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1118: Chris@1118: /* Chris@1118: Sonic Visualiser Chris@1118: An audio file viewer and annotation editor. Chris@1118: Centre for Digital Music, Queen Mary, University of London. Chris@1118: Chris@1118: This program is free software; you can redistribute it and/or Chris@1118: modify it under the terms of the GNU General Public License as Chris@1118: published by the Free Software Foundation; either version 2 of the Chris@1118: License, or (at your option) any later version. See the file Chris@1118: COPYING included with this distribution for more information. Chris@1118: */ Chris@1118: Chris@1118: #include "ScrollableMagRangeCache.h" Chris@1118: Chris@1164: #include "base/HitCount.h" Chris@1164: Chris@1118: #include Chris@1118: using namespace std; Chris@1118: Chris@1143: //#define DEBUG_SCROLLABLE_MAG_RANGE_CACHE 1 Chris@1118: Chris@1118: void Chris@1118: ScrollableMagRangeCache::scrollTo(const LayerGeometryProvider *v, Chris@1266: sv_frame_t newStartFrame) Chris@1266: { Chris@1164: static HitCount count("ScrollableMagRangeCache: scrolling"); Chris@1164: Chris@1118: int dx = (v->getXForFrame(m_startFrame) - Chris@1266: v->getXForFrame(newStartFrame)); Chris@1118: Chris@1118: #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE Chris@1118: cerr << "ScrollableMagRangeCache::scrollTo: start frame " << m_startFrame Chris@1266: << " -> " << newStartFrame << ", dx = " << dx << endl; Chris@1118: #endif Chris@1122: Chris@1122: if (m_startFrame == newStartFrame) { Chris@1266: // haven't moved Chris@1164: count.hit(); Chris@1122: return; Chris@1122: } Chris@1118: Chris@1118: m_startFrame = newStartFrame; Chris@1118: Chris@1118: if (dx == 0) { Chris@1266: // haven't moved visibly (even though start frame may have changed) Chris@1164: count.hit(); Chris@1266: return; Chris@1118: } Chris@1266: Chris@1118: int w = int(m_ranges.size()); Chris@1118: Chris@1118: if (dx <= -w || dx >= w) { Chris@1266: // scrolled entirely off Chris@1266: invalidate(); Chris@1164: count.miss(); Chris@1266: return; Chris@1118: } Chris@1164: Chris@1164: count.partial(); Chris@1266: Chris@1118: // dx is in range, cache is scrollable Chris@1118: Chris@1118: if (dx < 0) { Chris@1266: // The new start frame is to the left of the old start Chris@1266: // frame. We need to add some empty ranges at the left (start) Chris@1266: // end and clip the right end. Assemble -dx new values, then Chris@1266: // w+dx old values starting at index 0. Chris@1118: Chris@1266: auto newRanges = vector(-dx); Chris@1266: newRanges.insert(newRanges.end(), Chris@1266: m_ranges.begin(), m_ranges.begin() + (w + dx)); Chris@1266: m_ranges = newRanges; Chris@1266: Chris@1118: } else { Chris@1266: // The new start frame is to the right of the old start Chris@1266: // frame. We want to clip the left (start) end and add some Chris@1266: // empty ranges at the right end. Assemble w-dx old values Chris@1266: // starting at index dx, then dx new values. Chris@1118: Chris@1266: auto newRanges = vector(dx); Chris@1266: newRanges.insert(newRanges.begin(), Chris@1266: m_ranges.begin() + dx, m_ranges.end()); Chris@1266: m_ranges = newRanges; Chris@1118: } Chris@1122: Chris@1143: #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE Chris@1136: cerr << "maxes (" << m_ranges.size() << ") now: "; Chris@1136: for (int i = 0; in_range_for(m_ranges, i); ++i) { Chris@1266: cerr << m_ranges[i].getMax() << " "; Chris@1136: } Chris@1136: cerr << endl; Chris@1143: #endif Chris@1122: } Chris@1122: Chris@1122: MagnitudeRange Chris@1122: ScrollableMagRangeCache::getRange(int x, int count) const Chris@1122: { Chris@1122: MagnitudeRange r; Chris@1122: #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE Chris@1122: cerr << "ScrollableMagRangeCache::getRange(" << x << ", " << count << ")" << endl; Chris@1122: #endif Chris@1122: for (int i = 0; i < count; ++i) { Chris@1266: r.sample(m_ranges.at(x + i)); Chris@1122: } Chris@1122: return r; Chris@1118: } Chris@1118: Chris@1118: void Chris@1120: ScrollableMagRangeCache::sampleColumn(int column, const MagnitudeRange &r) Chris@1120: { Chris@1120: if (!in_range_for(m_ranges, column)) { Chris@1266: cerr << "ERROR: ScrollableMagRangeCache::sampleColumn: column " << column Chris@1266: << " is out of range for cache of width " << m_ranges.size() Chris@1266: << " (with start frame " << m_startFrame << ")" << endl; Chris@1266: throw logic_error("column out of range"); Chris@1120: } else { Chris@1266: m_ranges[column].sample(r); Chris@1120: } Chris@1120: } Chris@1120: