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@545
|
23 m_cache = new EditableDenseThreeDimensionalModel
|
Chris@545
|
24 (source->getSampleRate(),
|
Chris@545
|
25 getResolution(),
|
Chris@545
|
26 source->getHeight(),
|
Chris@545
|
27 EditableDenseThreeDimensionalModel::NoCompression,
|
Chris@545
|
28 false);
|
Chris@545
|
29
|
Chris@545
|
30 connect(source, SIGNAL(modelChanged()),
|
Chris@545
|
31 this, SLOT(sourceModelChanged()));
|
Chris@545
|
32 connect(source, SIGNAL(modelAboutToBeDeleted()),
|
Chris@545
|
33 this, SLOT(sourceModelAboutToBeDeleted()));
|
Chris@545
|
34
|
Chris@545
|
35 }
|
Chris@545
|
36
|
Chris@545
|
37 Dense3DModelPeakCache::~Dense3DModelPeakCache()
|
Chris@545
|
38 {
|
Chris@545
|
39 delete m_cache;
|
Chris@545
|
40 }
|
Chris@545
|
41
|
Chris@545
|
42 bool
|
Chris@545
|
43 Dense3DModelPeakCache::isColumnAvailable(size_t column) const
|
Chris@545
|
44 {
|
Chris@545
|
45 if (!m_source) return false;
|
Chris@545
|
46 if (haveColumn(column)) return true;
|
Chris@545
|
47 for (int i = 0; i < m_resolution; ++i) {
|
Chris@545
|
48 if (!m_source->isColumnAvailable(column * m_resolution + i)) {
|
Chris@545
|
49 return false;
|
Chris@545
|
50 }
|
Chris@545
|
51 }
|
Chris@545
|
52 return true;
|
Chris@545
|
53 }
|
Chris@545
|
54
|
Chris@545
|
55 Dense3DModelPeakCache::Column
|
Chris@545
|
56 Dense3DModelPeakCache::getColumn(size_t column) const
|
Chris@545
|
57 {
|
Chris@545
|
58 if (!m_source) return Column();
|
Chris@545
|
59 if (!haveColumn(column)) fillColumn(column);
|
Chris@545
|
60 return m_cache->getColumn(column);
|
Chris@545
|
61 }
|
Chris@545
|
62
|
Chris@545
|
63 float
|
Chris@545
|
64 Dense3DModelPeakCache::getValueAt(size_t column, size_t n) const
|
Chris@545
|
65 {
|
Chris@545
|
66 if (!m_source) return 0.f;
|
Chris@545
|
67 if (!haveColumn(column)) fillColumn(column);
|
Chris@545
|
68 return m_cache->getValueAt(column, n);
|
Chris@545
|
69 }
|
Chris@545
|
70
|
Chris@545
|
71 void
|
Chris@545
|
72 Dense3DModelPeakCache::sourceModelChanged()
|
Chris@545
|
73 {
|
Chris@545
|
74 if (!m_source) return;
|
Chris@545
|
75 m_coverage.resize(getWidth());
|
Chris@545
|
76 }
|
Chris@545
|
77
|
Chris@545
|
78 void
|
Chris@545
|
79 Dense3DModelPeakCache::sourceModelAboutToBeDeleted()
|
Chris@545
|
80 {
|
Chris@545
|
81 m_source = 0;
|
Chris@545
|
82 }
|
Chris@545
|
83
|
Chris@545
|
84 bool
|
Chris@545
|
85 Dense3DModelPeakCache::haveColumn(size_t column) const
|
Chris@545
|
86 {
|
Chris@545
|
87 return m_coverage.get(column);
|
Chris@545
|
88 }
|
Chris@545
|
89
|
Chris@545
|
90 void
|
Chris@545
|
91 Dense3DModelPeakCache::fillColumn(size_t column) const
|
Chris@545
|
92 {
|
Chris@545
|
93 Column peak;
|
Chris@545
|
94 for (int i = 0; i < m_resolution; ++i) {
|
Chris@545
|
95 Column here = m_source->getColumn(column * m_resolution + i);
|
Chris@545
|
96 if (i == 0) {
|
Chris@545
|
97 peak = here;
|
Chris@545
|
98 } else {
|
Chris@545
|
99 for (int j = 0; j < peak.size() && j < here.size(); ++j) {
|
Chris@545
|
100 if (here[j] > peak[j]) peak[j] = here[j];
|
Chris@545
|
101 }
|
Chris@545
|
102 }
|
Chris@545
|
103 }
|
Chris@545
|
104 m_cache->setColumn(column, peak);
|
Chris@545
|
105 m_coverage.set(column);
|
Chris@545
|
106 }
|
Chris@545
|
107
|
Chris@545
|
108
|