Chris@545
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@545
|
2
|
Chris@545
|
3 /*
|
Chris@545
|
4 Sonic Visualiser
|
Chris@545
|
5 An audio file viewer and annotation editor.
|
Chris@545
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@545
|
7 This file copyright 2009 QMUL.
|
Chris@545
|
8
|
Chris@545
|
9 This program is free software; you can redistribute it and/or
|
Chris@545
|
10 modify it under the terms of the GNU General Public License as
|
Chris@545
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@545
|
12 License, or (at your option) any later version. See the file
|
Chris@545
|
13 COPYING included with this distribution for more information.
|
Chris@545
|
14 */
|
Chris@545
|
15
|
Chris@545
|
16 #include "Dense3DModelPeakCache.h"
|
Chris@545
|
17
|
Chris@551
|
18 #include "base/Profiler.h"
|
Chris@551
|
19
|
Chris@545
|
20 Dense3DModelPeakCache::Dense3DModelPeakCache(DenseThreeDimensionalModel *source,
|
Chris@929
|
21 int columnsPerPeak) :
|
Chris@545
|
22 m_source(source),
|
Chris@545
|
23 m_resolution(columnsPerPeak)
|
Chris@545
|
24 {
|
Chris@545
|
25 m_cache = new EditableDenseThreeDimensionalModel
|
Chris@545
|
26 (source->getSampleRate(),
|
Chris@545
|
27 getResolution(),
|
Chris@545
|
28 source->getHeight(),
|
Chris@545
|
29 EditableDenseThreeDimensionalModel::NoCompression,
|
Chris@545
|
30 false);
|
Chris@545
|
31
|
Chris@545
|
32 connect(source, SIGNAL(modelChanged()),
|
Chris@545
|
33 this, SLOT(sourceModelChanged()));
|
Chris@546
|
34 connect(source, SIGNAL(aboutToBeDeleted()),
|
Chris@545
|
35 this, SLOT(sourceModelAboutToBeDeleted()));
|
Chris@545
|
36
|
Chris@545
|
37 }
|
Chris@545
|
38
|
Chris@545
|
39 Dense3DModelPeakCache::~Dense3DModelPeakCache()
|
Chris@545
|
40 {
|
Chris@545
|
41 delete m_cache;
|
Chris@545
|
42 }
|
Chris@545
|
43
|
Chris@545
|
44 bool
|
Chris@929
|
45 Dense3DModelPeakCache::isColumnAvailable(int column) const
|
Chris@545
|
46 {
|
Chris@545
|
47 if (!m_source) return false;
|
Chris@545
|
48 if (haveColumn(column)) return true;
|
Chris@552
|
49 for (int i = m_resolution; i > 0; ) {
|
Chris@552
|
50 --i;
|
Chris@545
|
51 if (!m_source->isColumnAvailable(column * m_resolution + i)) {
|
Chris@545
|
52 return false;
|
Chris@545
|
53 }
|
Chris@545
|
54 }
|
Chris@545
|
55 return true;
|
Chris@545
|
56 }
|
Chris@545
|
57
|
Chris@545
|
58 Dense3DModelPeakCache::Column
|
Chris@929
|
59 Dense3DModelPeakCache::getColumn(int column) const
|
Chris@545
|
60 {
|
Chris@551
|
61 Profiler profiler("Dense3DModelPeakCache::getColumn");
|
Chris@545
|
62 if (!m_source) return Column();
|
Chris@545
|
63 if (!haveColumn(column)) fillColumn(column);
|
Chris@545
|
64 return m_cache->getColumn(column);
|
Chris@545
|
65 }
|
Chris@545
|
66
|
Chris@545
|
67 float
|
Chris@929
|
68 Dense3DModelPeakCache::getValueAt(int column, int n) const
|
Chris@545
|
69 {
|
Chris@545
|
70 if (!m_source) return 0.f;
|
Chris@545
|
71 if (!haveColumn(column)) fillColumn(column);
|
Chris@545
|
72 return m_cache->getValueAt(column, n);
|
Chris@545
|
73 }
|
Chris@545
|
74
|
Chris@545
|
75 void
|
Chris@545
|
76 Dense3DModelPeakCache::sourceModelChanged()
|
Chris@545
|
77 {
|
Chris@545
|
78 if (!m_source) return;
|
Chris@546
|
79 if (m_coverage.size() > 0) {
|
Chris@546
|
80 // The last peak may have come from an incomplete read, which
|
Chris@546
|
81 // may since have been filled, so reset it
|
Chris@1153
|
82 m_coverage[m_coverage.size()-1] = false;
|
Chris@546
|
83 }
|
Chris@1153
|
84 m_coverage.resize(getWidth(), false); // retaining data
|
Chris@545
|
85 }
|
Chris@545
|
86
|
Chris@545
|
87 void
|
Chris@545
|
88 Dense3DModelPeakCache::sourceModelAboutToBeDeleted()
|
Chris@545
|
89 {
|
Chris@545
|
90 m_source = 0;
|
Chris@545
|
91 }
|
Chris@545
|
92
|
Chris@545
|
93 bool
|
Chris@929
|
94 Dense3DModelPeakCache::haveColumn(int column) const
|
Chris@545
|
95 {
|
Chris@1153
|
96 return in_range_for(m_coverage, column) && m_coverage[column];
|
Chris@545
|
97 }
|
Chris@545
|
98
|
Chris@545
|
99 void
|
Chris@929
|
100 Dense3DModelPeakCache::fillColumn(int column) const
|
Chris@545
|
101 {
|
Chris@551
|
102 Profiler profiler("Dense3DModelPeakCache::fillColumn");
|
Chris@551
|
103
|
Chris@1153
|
104 if (!in_range_for(m_coverage, column)) {
|
Chris@551
|
105 // see note in sourceModelChanged
|
Chris@1153
|
106 if (m_coverage.size() > 0) m_coverage[m_coverage.size()-1] = false;
|
Chris@1153
|
107 m_coverage.resize(column + 1, false);
|
Chris@551
|
108 }
|
Chris@546
|
109
|
Chris@545
|
110 Column peak;
|
Chris@1153
|
111 for (int i = 0; i < m_resolution; ++i) {
|
Chris@545
|
112 Column here = m_source->getColumn(column * m_resolution + i);
|
Chris@545
|
113 if (i == 0) {
|
Chris@545
|
114 peak = here;
|
Chris@545
|
115 } else {
|
Chris@1153
|
116 for (int j = 0; in_range_for(peak, j) && in_range_for(here, j); ++j) {
|
Chris@545
|
117 if (here[j] > peak[j]) peak[j] = here[j];
|
Chris@545
|
118 }
|
Chris@545
|
119 }
|
Chris@545
|
120 }
|
Chris@546
|
121
|
Chris@545
|
122 m_cache->setColumn(column, peak);
|
Chris@1153
|
123 m_coverage[column] = true;
|
Chris@545
|
124 }
|
Chris@545
|
125
|
Chris@545
|
126
|