svcore  1.9
AggregateWaveModel.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 2007 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 "AggregateWaveModel.h"
17 
18 #include <iostream>
19 
20 #include <QTextStream>
21 
24 
26  m_components(channelSpecs)
27 {
28  for (ChannelSpecList::const_iterator i = channelSpecs.begin();
29  i != channelSpecs.end(); ++i) {
30  if (i->model->getSampleRate() !=
31  channelSpecs.begin()->model->getSampleRate()) {
32  SVDEBUG << "AggregateWaveModel::AggregateWaveModel: WARNING: Component models do not all have the same sample rate" << endl;
33  break;
34  }
35  }
36 }
37 
39 {
40 }
41 
42 bool
44 {
45  for (ChannelSpecList::const_iterator i = m_components.begin();
46  i != m_components.end(); ++i) {
47  if (!i->model->isOK()) return false;
48  }
49  return true;
50 }
51 
52 bool
53 AggregateWaveModel::isReady(int *completion) const
54 {
55  if (completion) *completion = 100;
56  bool ready = true;
57  for (ChannelSpecList::const_iterator i = m_components.begin();
58  i != m_components.end(); ++i) {
59  int completionHere = 100;
60  if (!i->model->isReady(&completionHere)) ready = false;
61  if (completion && completionHere < *completion) {
62  *completion = completionHere;
63  }
64  }
65  return ready;
66 }
67 
68 int
70 {
71  int count = 0;
72 
73  for (ChannelSpecList::const_iterator i = m_components.begin();
74  i != m_components.end(); ++i) {
75  int thisCount = i->model->getEndFrame() - i->model->getStartFrame();
76  if (thisCount > count) count = thisCount;
77  }
78 
79  return count;
80 }
81 
82 int
84 {
85  return m_components.size();
86 }
87 
88 int
90 {
91  if (m_components.empty()) return 0;
92  return m_components.begin()->model->getSampleRate();
93 }
94 
95 Model *
97 {
98  return new AggregateWaveModel(m_components);
99 }
100 
101 int
102 AggregateWaveModel::getData(int channel, int start, int count,
103  float *buffer) const
104 {
105  int ch0 = channel, ch1 = channel;
106  bool mixing = false;
107  if (channel == -1) {
108  ch0 = 0;
109  ch1 = getChannelCount()-1;
110  mixing = true;
111  }
112 
113  float *readbuf = buffer;
114  if (mixing) {
115  readbuf = new float[count];
116  for (int i = 0; i < count; ++i) {
117  buffer[i] = 0.f;
118  }
119  }
120 
121  int sz = count;
122 
123  for (int c = ch0; c <= ch1; ++c) {
124  int szHere =
125  m_components[c].model->getData(m_components[c].channel,
126  start, count,
127  readbuf);
128  if (szHere < sz) sz = szHere;
129  if (mixing) {
130  for (int i = 0; i < count; ++i) {
131  buffer[i] += readbuf[i];
132  }
133  }
134  }
135 
136  if (mixing) delete[] readbuf;
137  return sz;
138 }
139 
140 int
141 AggregateWaveModel::getData(int channel, int start, int count,
142  double *buffer) const
143 {
144  int ch0 = channel, ch1 = channel;
145  bool mixing = false;
146  if (channel == -1) {
147  ch0 = 0;
148  ch1 = getChannelCount()-1;
149  mixing = true;
150  }
151 
152  double *readbuf = buffer;
153  if (mixing) {
154  readbuf = new double[count];
155  for (int i = 0; i < count; ++i) {
156  buffer[i] = 0.0;
157  }
158  }
159 
160  int sz = count;
161 
162  for (int c = ch0; c <= ch1; ++c) {
163  int szHere =
164  m_components[c].model->getData(m_components[c].channel,
165  start, count,
166  readbuf);
167  if (szHere < sz) sz = szHere;
168  if (mixing) {
169  for (int i = 0; i < count; ++i) {
170  buffer[i] += readbuf[i];
171  }
172  }
173  }
174 
175  if (mixing) delete[] readbuf;
176  return sz;
177 }
178 
179 int
180 AggregateWaveModel::getData(int fromchannel, int tochannel,
181  int start, int count,
182  float **buffer) const
183 {
184  int min = count;
185 
186  for (int c = fromchannel; c <= tochannel; ++c) {
187  int here = getData(c, start, count, buffer[c - fromchannel]);
188  if (here < min) min = here;
189  }
190 
191  return min;
192 }
193 
194 int
196 {
198  return desired;
199 }
200 
201 void
203  RangeBlock &, int &) const
204 {
206 }
207 
209 AggregateWaveModel::getSummary(int, int, int) const
210 {
212  return Range();
213 }
214 
215 int
217 {
218  return m_components.size();
219 }
220 
223 {
224  return m_components[c];
225 }
226 
227 void
229 {
230  emit modelChanged();
231 }
232 
233 void
235 {
236  emit modelChangedWithin(start, end);
237 }
238 
239 void
241 {
242  emit completionChanged();
243 }
244 
245 void
247  QString ,
248  QString ) const
249 {
251 }
252 
virtual Model * clone() const
Return a copy of this model.
void componentModelChangedWithin(int, int)
int getSampleRate() const
Return the frame rate in frames per second.
static PowerOfSqrtTwoZoomConstraint m_zoomConstraint
void ready()
Emitted when internal processing is complete (i.e.
AggregateWaveModel(ChannelSpecList channelSpecs)
bool isOK() const
Return true if the model was constructed successfully.
int getChannelCount() const
Return the number of distinct channels for this model.
ModelChannelSpec getComponent(int c) const
ChannelSpecList m_components
virtual Range getSummary(int channel, int start, int count) const
Return the range from the given start frame, corresponding to the given number of underlying sample f...
virtual void getSummaries(int channel, int start, int count, RangeBlock &ranges, int &blockSize) const
Return ranges from the given start frame, corresponding to the given number of underlying sample fram...
Model is the base class for all data models that represent any sort of data on a time scale based on ...
Definition: Model.h:35
std::vector< ModelChannelSpec > ChannelSpecList
virtual int getSummaryBlockSize(int desired) const
bool isReady(int *) const
Return true if the model has finished loading or calculating all its data, for a model that is capabl...
#define SVDEBUG
Definition: Debug.h:42
void modelChangedWithin(int, int)
virtual void toXml(QTextStream &out, QString indent="", QString extraAttributes="") const
Stream this exportable object out to XML on a text stream.
virtual int getData(int channel, int start, int count, float *buffer) const
Get the specified set of samples from the given channel of the model in single-precision floating-poi...