Dense3DModelPeakCache.cpp
Go to the documentation of this file.
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  This file copyright 2009 QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "Dense3DModelPeakCache.h"
17 
18 #include "base/Profiler.h"
19 
20 #include "base/HitCount.h"
21 
23  int columnsPerPeak) :
24  m_source(sourceId),
25  m_columnsPerPeak(columnsPerPeak),
26  m_finalColumnIncomplete(false)
27 {
28  auto source = ModelById::getAs<DenseThreeDimensionalModel>(m_source);
29  if (!source) {
30  SVCERR << "WARNING: Dense3DModelPeakCache constructed for unknown or wrong-type source model id " << m_source << endl;
31  m_source = {};
32  return;
33  }
34 
35  connect(source.get(), SIGNAL(modelChanged(ModelId)),
36  this, SLOT(sourceModelChanged(ModelId)));
37 }
38 
40 {
41 }
42 
45 {
46  if (!haveColumn(column)) fillColumn(column);
47  return m_cache.at(column);
48 }
49 
50 float
51 Dense3DModelPeakCache::getValueAt(int column, int n) const
52 {
53  if (!haveColumn(column)) fillColumn(column);
54  return m_cache.at(column).at(n);
55 }
56 
57 void
59 {
60  if (m_finalColumnIncomplete && m_coverage.size() > 0) {
61  // The last peak came from an incomplete read, which may since
62  // have been filled, so reset it
63  m_coverage[m_coverage.size()-1] = false;
65  }
66 }
67 
68 bool
70 {
71  static HitCount count("Dense3DModelPeakCache");
72  if (in_range_for(m_coverage, column) && m_coverage[column]) {
73  count.hit();
74  return true;
75  } else {
76  count.miss();
77  return false;
78  }
79 }
80 
81 void
83 {
84  Profiler profiler("Dense3DModelPeakCache::fillColumn");
85 
86  if (!in_range_for(m_coverage, column)) {
87  if (m_finalColumnIncomplete && m_coverage.size() > 0) {
88  // The last peak may have come from an incomplete read, which
89  // may since have been filled, so reset it
90  m_coverage[m_coverage.size()-1] = false;
92  }
93  m_coverage.resize(column + 1, false);
94  m_cache.resize(column + 1, {});
95  }
96 
97  auto source = ModelById::getAs<DenseThreeDimensionalModel>(m_source);
98  if (!source) {
99  return;
100  }
101 
102  int sourceWidth = source->getWidth();
103  int sourceColumn = column * m_columnsPerPeak;
104  if (sourceColumn >= sourceWidth) {
105  return;
106  }
107 
108  Column peak = source->getColumn(sourceColumn);
109  int n = int(peak.size());
110 
111  for (int i = 1; i < m_columnsPerPeak; ++i) {
112 
113  ++sourceColumn;
114  if (sourceColumn >= sourceWidth) {
116  break;
117  }
118 
119  Column here = source->getColumn(sourceColumn);
120  int m = std::min(n, int(here.size()));
121  for (int j = 0; j < m; ++j) {
122  peak[j] = std::max(here[j], peak[j]);
123  }
124  }
125 
126  m_cache[column] = peak;
127  m_coverage[column] = true;
128 }
129 
130 
float getValueAt(int col, int n) const override
Get the single data point from the n&#39;th bin of the given column.
std::vector< Column > m_cache
Dense3DModelPeakCache(ModelId source, int columnsPerPeak)
Profile class for counting cache hits and the like.
Definition: HitCount.h:26
Column getColumn(int col) const override
Retrieve the peaks column at peak-cache column number col.
bool in_range_for(const C &container, T i)
Check whether an integer index is in range for a container, avoiding overflows and signed/unsigned co...
Definition: BaseTypes.h:37
void miss()
Definition: HitCount.h:61
std::vector< bool > m_coverage
#define SVCERR
Definition: Debug.h:109
bool haveColumn(int column) const
void fillColumn(int column) const
Definition: ById.h:115
void hit()
Definition: HitCount.h:59
void modelChanged(ModelId myId)
Emitted when a model has been edited (or more data retrieved from cache, in the case of a cached mode...
Profile point instance class.
Definition: Profiler.h:93