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@1118: #include Chris@1118: using namespace std; Chris@1118: Chris@1118: #define DEBUG_SCROLLABLE_MAG_RANGE_CACHE 1 Chris@1118: Chris@1118: void Chris@1118: ScrollableMagRangeCache::scrollTo(const LayerGeometryProvider *v, Chris@1118: sv_frame_t newStartFrame) Chris@1122: { Chris@1118: int dx = (v->getXForFrame(m_startFrame) - Chris@1118: v->getXForFrame(newStartFrame)); Chris@1118: Chris@1118: #ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE Chris@1118: cerr << "ScrollableMagRangeCache::scrollTo: start frame " << m_startFrame Chris@1118: << " -> " << newStartFrame << ", dx = " << dx << endl; Chris@1118: #endif Chris@1122: Chris@1122: if (m_startFrame == newStartFrame) { Chris@1122: // haven't moved Chris@1122: return; Chris@1122: } Chris@1118: Chris@1118: m_startFrame = newStartFrame; Chris@1118: Chris@1118: if (dx == 0) { Chris@1118: // haven't moved visibly (even though start frame may have changed) Chris@1118: return; Chris@1118: } Chris@1118: Chris@1118: int w = int(m_ranges.size()); Chris@1118: Chris@1118: if (dx <= -w || dx >= w) { Chris@1118: // scrolled entirely off Chris@1118: invalidate(); Chris@1118: return; Chris@1118: } Chris@1118: Chris@1118: // dx is in range, cache is scrollable Chris@1118: Chris@1118: if (dx < 0) { Chris@1118: // The new start frame is to the left of the old start Chris@1118: // frame. We need to add some empty ranges at the left (start) Chris@1118: // end and clip the right end. Assemble -dx new values, then Chris@1118: // w+dx old values starting at index 0. Chris@1118: Chris@1118: auto newRanges = vector(-dx); Chris@1118: newRanges.insert(newRanges.end(), Chris@1122: m_ranges.begin(), m_ranges.begin() + (w + dx)); Chris@1118: m_ranges = newRanges; Chris@1118: Chris@1118: } else { Chris@1118: // The new start frame is to the right of the old start Chris@1118: // frame. We want to clip the left (start) end and add some Chris@1118: // empty ranges at the right end. Assemble w-dx old values Chris@1118: // starting at index dx, then dx new values. Chris@1118: Chris@1118: auto newRanges = vector(dx); Chris@1118: newRanges.insert(newRanges.begin(), Chris@1118: m_ranges.begin() + dx, m_ranges.end()); Chris@1118: m_ranges = newRanges; Chris@1118: } Chris@1122: Chris@1132: //!!! cerr << "maxes now: "; Chris@1132: // for (int i = 0; in_range_for(m_ranges, i); ++i) { Chris@1132: // cerr << m_ranges[i].getMax() << " "; Chris@1132: // } Chris@1132: // cerr << endl; 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@1122: 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@1120: cerr << "ERROR: ScrollableMagRangeCache::sampleColumn: column " << column Chris@1120: << " is out of range for cache of width " << m_ranges.size() Chris@1120: << " (with start frame " << m_startFrame << ")" << endl; Chris@1120: throw logic_error("column out of range"); Chris@1120: } else { Chris@1120: m_ranges[column].sample(r); Chris@1120: } Chris@1120: } Chris@1120: Chris@1122: //!!! unneeded? Chris@1120: void Chris@1118: ScrollableMagRangeCache::sampleColumn(const LayerGeometryProvider *v, Chris@1118: sv_frame_t frame, Chris@1118: const MagnitudeRange &r) Chris@1118: { Chris@1118: int x = (v->getXForFrame(frame) - Chris@1118: v->getXForFrame(m_startFrame)); Chris@1118: Chris@1118: if (!in_range_for(m_ranges, x)) { Chris@1118: cerr << "WARNING: ScrollableMagRangeCache::sampleColumn: column " << x Chris@1118: << " arising from frame " << frame << " is out of range for cache " Chris@1120: << "of width " << m_ranges.size() Chris@1120: << " (with start frame " << m_startFrame << ")" << endl; Chris@1120: throw logic_error("column out of range"); Chris@1118: } else { Chris@1118: sampleColumn(x, r); Chris@1118: } Chris@1118: } Chris@1118: