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@545
|
18 Dense3DModelPeakCache::Dense3DModelPeakCache(DenseThreeDimensionalModel *source,
|
Chris@545
|
19 size_t columnsPerPeak) :
|
Chris@545
|
20 m_source(source),
|
Chris@545
|
21 m_resolution(columnsPerPeak)
|
Chris@545
|
22 {
|
Chris@546
|
23 m_coverage.resize(1); // otherwise it is simply invalid
|
Chris@546
|
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@545
|
45 Dense3DModelPeakCache::isColumnAvailable(size_t column) const
|
Chris@545
|
46 {
|
Chris@545
|
47 if (!m_source) return false;
|
Chris@545
|
48 if (haveColumn(column)) return true;
|
Chris@545
|
49 for (int i = 0; i < m_resolution; ++i) {
|
Chris@545
|
50 if (!m_source->isColumnAvailable(column * m_resolution + i)) {
|
Chris@545
|
51 return false;
|
Chris@545
|
52 }
|
Chris@545
|
53 }
|
Chris@545
|
54 return true;
|
Chris@545
|
55 }
|
Chris@545
|
56
|
Chris@545
|
57 Dense3DModelPeakCache::Column
|
Chris@545
|
58 Dense3DModelPeakCache::getColumn(size_t column) const
|
Chris@545
|
59 {
|
Chris@545
|
60 if (!m_source) return Column();
|
Chris@545
|
61 if (!haveColumn(column)) fillColumn(column);
|
Chris@545
|
62 return m_cache->getColumn(column);
|
Chris@545
|
63 }
|
Chris@545
|
64
|
Chris@545
|
65 float
|
Chris@545
|
66 Dense3DModelPeakCache::getValueAt(size_t column, size_t n) const
|
Chris@545
|
67 {
|
Chris@545
|
68 if (!m_source) return 0.f;
|
Chris@545
|
69 if (!haveColumn(column)) fillColumn(column);
|
Chris@545
|
70 return m_cache->getValueAt(column, n);
|
Chris@545
|
71 }
|
Chris@545
|
72
|
Chris@545
|
73 void
|
Chris@545
|
74 Dense3DModelPeakCache::sourceModelChanged()
|
Chris@545
|
75 {
|
Chris@545
|
76 if (!m_source) return;
|
Chris@546
|
77 if (m_coverage.size() > 0) {
|
Chris@546
|
78 // The last peak may have come from an incomplete read, which
|
Chris@546
|
79 // may since have been filled, so reset it
|
Chris@546
|
80 m_coverage.reset(m_coverage.size()-1);
|
Chris@546
|
81 }
|
Chris@548
|
82 m_coverage.resize(getWidth()); // retaining data
|
Chris@545
|
83 }
|
Chris@545
|
84
|
Chris@545
|
85 void
|
Chris@545
|
86 Dense3DModelPeakCache::sourceModelAboutToBeDeleted()
|
Chris@545
|
87 {
|
Chris@545
|
88 m_source = 0;
|
Chris@545
|
89 }
|
Chris@545
|
90
|
Chris@545
|
91 bool
|
Chris@545
|
92 Dense3DModelPeakCache::haveColumn(size_t column) const
|
Chris@545
|
93 {
|
Chris@546
|
94 return column < m_coverage.size() && m_coverage.get(column);
|
Chris@545
|
95 }
|
Chris@545
|
96
|
Chris@545
|
97 void
|
Chris@545
|
98 Dense3DModelPeakCache::fillColumn(size_t column) const
|
Chris@545
|
99 {
|
Chris@546
|
100 if (column >= m_coverage.size()) m_coverage.resize(column + 1);
|
Chris@546
|
101
|
Chris@545
|
102 Column peak;
|
Chris@545
|
103 for (int i = 0; i < m_resolution; ++i) {
|
Chris@545
|
104 Column here = m_source->getColumn(column * m_resolution + i);
|
Chris@545
|
105 if (i == 0) {
|
Chris@545
|
106 peak = here;
|
Chris@545
|
107 } else {
|
Chris@545
|
108 for (int j = 0; j < peak.size() && j < here.size(); ++j) {
|
Chris@545
|
109 if (here[j] > peak[j]) peak[j] = here[j];
|
Chris@545
|
110 }
|
Chris@545
|
111 }
|
Chris@545
|
112 }
|
Chris@546
|
113
|
Chris@545
|
114 m_cache->setColumn(column, peak);
|
Chris@545
|
115 m_coverage.set(column);
|
Chris@545
|
116 }
|
Chris@545
|
117
|
Chris@545
|
118
|