Chris@147
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@147
|
2
|
Chris@147
|
3 /*
|
Chris@147
|
4 Sonic Visualiser
|
Chris@147
|
5 An audio file viewer and annotation editor.
|
Chris@147
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@147
|
7 This file copyright 2006 Chris Cannam.
|
Chris@147
|
8
|
Chris@147
|
9 This program is free software; you can redistribute it and/or
|
Chris@147
|
10 modify it under the terms of the GNU General Public License as
|
Chris@147
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@147
|
12 License, or (at your option) any later version. See the file
|
Chris@147
|
13 COPYING included with this distribution for more information.
|
Chris@147
|
14 */
|
Chris@147
|
15
|
Chris@150
|
16 #include "WaveFileModel.h"
|
Chris@147
|
17
|
Chris@147
|
18 #include "fileio/AudioFileReader.h"
|
Chris@147
|
19 #include "fileio/AudioFileReaderFactory.h"
|
Chris@147
|
20
|
Chris@150
|
21 #include "system/System.h"
|
Chris@147
|
22
|
Chris@147
|
23 #include <QMessageBox>
|
Chris@147
|
24 #include <QFileInfo>
|
Chris@147
|
25
|
Chris@147
|
26 #include <iostream>
|
Chris@147
|
27 #include <unistd.h>
|
Chris@147
|
28 #include <math.h>
|
Chris@147
|
29 #include <sndfile.h>
|
Chris@147
|
30
|
Chris@147
|
31 #include <cassert>
|
Chris@147
|
32
|
Chris@147
|
33 using std::cerr;
|
Chris@147
|
34 using std::endl;
|
Chris@147
|
35
|
Chris@179
|
36 PowerOfSqrtTwoZoomConstraint
|
Chris@179
|
37 WaveFileModel::m_zoomConstraint;
|
Chris@179
|
38
|
Chris@147
|
39 WaveFileModel::WaveFileModel(QString path) :
|
Chris@147
|
40 m_path(path),
|
Chris@175
|
41 m_myReader(true),
|
Chris@147
|
42 m_fillThread(0),
|
Chris@147
|
43 m_updateTimer(0),
|
Chris@147
|
44 m_lastFillExtent(0),
|
Chris@147
|
45 m_exiting(false)
|
Chris@147
|
46 {
|
Chris@147
|
47 m_reader = AudioFileReaderFactory::createReader(path);
|
Chris@175
|
48 setObjectName(QFileInfo(path).fileName());
|
Chris@175
|
49 if (isOK()) fillCache();
|
Chris@175
|
50 }
|
Chris@175
|
51
|
Chris@175
|
52 WaveFileModel::WaveFileModel(QString path, AudioFileReader *reader) :
|
Chris@175
|
53 m_path(path),
|
Chris@175
|
54 m_myReader(false),
|
Chris@175
|
55 m_fillThread(0),
|
Chris@175
|
56 m_updateTimer(0),
|
Chris@175
|
57 m_lastFillExtent(0),
|
Chris@175
|
58 m_exiting(false)
|
Chris@175
|
59 {
|
Chris@175
|
60 m_reader = reader;
|
Chris@147
|
61 setObjectName(QFileInfo(path).fileName());
|
Chris@187
|
62 fillCache();
|
Chris@147
|
63 }
|
Chris@147
|
64
|
Chris@147
|
65 WaveFileModel::~WaveFileModel()
|
Chris@147
|
66 {
|
Chris@147
|
67 m_exiting = true;
|
Chris@147
|
68 if (m_fillThread) m_fillThread->wait();
|
Chris@175
|
69 if (m_myReader) delete m_reader;
|
Chris@147
|
70 m_reader = 0;
|
Chris@147
|
71 }
|
Chris@147
|
72
|
Chris@147
|
73 bool
|
Chris@147
|
74 WaveFileModel::isOK() const
|
Chris@147
|
75 {
|
Chris@147
|
76 return m_reader && m_reader->isOK();
|
Chris@147
|
77 }
|
Chris@147
|
78
|
Chris@147
|
79 bool
|
Chris@147
|
80 WaveFileModel::isReady(int *completion) const
|
Chris@147
|
81 {
|
Chris@147
|
82 bool ready = (isOK() && (m_fillThread == 0));
|
Chris@147
|
83 double c = double(m_lastFillExtent) / double(getEndFrame() - getStartFrame());
|
Chris@147
|
84 if (completion) *completion = int(c * 100.0 + 0.01);
|
Chris@180
|
85 std::cerr << "WaveFileModel::isReady(): ready = " << ready << ", completion = " << (completion ? *completion : -1) << std::endl;
|
Chris@147
|
86 return ready;
|
Chris@147
|
87 }
|
Chris@147
|
88
|
Chris@147
|
89 Model *
|
Chris@147
|
90 WaveFileModel::clone() const
|
Chris@147
|
91 {
|
Chris@147
|
92 WaveFileModel *model = new WaveFileModel(m_path);
|
Chris@147
|
93 return model;
|
Chris@147
|
94 }
|
Chris@147
|
95
|
Chris@147
|
96 size_t
|
Chris@147
|
97 WaveFileModel::getFrameCount() const
|
Chris@147
|
98 {
|
Chris@147
|
99 if (!m_reader) return 0;
|
Chris@147
|
100 return m_reader->getFrameCount();
|
Chris@147
|
101 }
|
Chris@147
|
102
|
Chris@147
|
103 size_t
|
Chris@147
|
104 WaveFileModel::getChannelCount() const
|
Chris@147
|
105 {
|
Chris@147
|
106 if (!m_reader) return 0;
|
Chris@147
|
107 return m_reader->getChannelCount();
|
Chris@147
|
108 }
|
Chris@147
|
109
|
Chris@147
|
110 size_t
|
Chris@147
|
111 WaveFileModel::getSampleRate() const
|
Chris@147
|
112 {
|
Chris@147
|
113 if (!m_reader) return 0;
|
Chris@147
|
114 return m_reader->getSampleRate();
|
Chris@147
|
115 }
|
Chris@147
|
116
|
Chris@147
|
117 size_t
|
Chris@147
|
118 WaveFileModel::getValues(int channel, size_t start, size_t end,
|
Chris@147
|
119 float *buffer) const
|
Chris@147
|
120 {
|
Chris@147
|
121 // Always read these directly from the file.
|
Chris@147
|
122 // This is used for e.g. audio playback.
|
Chris@147
|
123 // Could be much more efficient (although compiler optimisation will help)
|
Chris@147
|
124
|
Chris@147
|
125 if (end < start) {
|
Chris@147
|
126 std::cerr << "ERROR: WaveFileModel::getValues[float]: end < start ("
|
Chris@147
|
127 << end << " < " << start << ")" << std::endl;
|
Chris@147
|
128 assert(end >= start);
|
Chris@147
|
129 }
|
Chris@147
|
130
|
Chris@147
|
131 if (!m_reader || !m_reader->isOK()) return 0;
|
Chris@147
|
132
|
Chris@175
|
133 // std::cerr << "WaveFileModel::getValues(" << channel << ", "
|
Chris@175
|
134 // << start << ", " << end << "): calling reader" << std::endl;
|
Chris@175
|
135
|
Chris@147
|
136 SampleBlock frames;
|
Chris@147
|
137 m_reader->getInterleavedFrames(start, end - start, frames);
|
Chris@147
|
138
|
Chris@147
|
139 size_t i = 0;
|
Chris@147
|
140
|
Chris@147
|
141 int ch0 = channel, ch1 = channel, channels = getChannelCount();
|
Chris@147
|
142 if (channel == -1) {
|
Chris@147
|
143 ch0 = 0;
|
Chris@147
|
144 ch1 = channels - 1;
|
Chris@147
|
145 }
|
Chris@147
|
146
|
Chris@147
|
147 while (i < end - start) {
|
Chris@147
|
148
|
Chris@147
|
149 buffer[i] = 0.0;
|
Chris@147
|
150
|
Chris@147
|
151 for (int ch = ch0; ch <= ch1; ++ch) {
|
Chris@147
|
152
|
Chris@147
|
153 size_t index = i * channels + ch;
|
Chris@147
|
154 if (index >= frames.size()) break;
|
Chris@147
|
155
|
Chris@147
|
156 float sample = frames[index];
|
Chris@147
|
157 buffer[i] += sample;
|
Chris@147
|
158 }
|
Chris@147
|
159
|
Chris@147
|
160 ++i;
|
Chris@147
|
161 }
|
Chris@147
|
162
|
Chris@147
|
163 return i;
|
Chris@147
|
164 }
|
Chris@147
|
165
|
Chris@147
|
166 size_t
|
Chris@147
|
167 WaveFileModel::getValues(int channel, size_t start, size_t end,
|
Chris@147
|
168 double *buffer) const
|
Chris@147
|
169 {
|
Chris@147
|
170 if (end < start) {
|
Chris@147
|
171 std::cerr << "ERROR: WaveFileModel::getValues[double]: end < start ("
|
Chris@147
|
172 << end << " < " << start << ")" << std::endl;
|
Chris@147
|
173 assert(end >= start);
|
Chris@147
|
174 }
|
Chris@147
|
175
|
Chris@147
|
176 if (!m_reader || !m_reader->isOK()) return 0;
|
Chris@147
|
177
|
Chris@147
|
178 SampleBlock frames;
|
Chris@147
|
179 m_reader->getInterleavedFrames(start, end - start, frames);
|
Chris@147
|
180
|
Chris@147
|
181 size_t i = 0;
|
Chris@147
|
182
|
Chris@147
|
183 int ch0 = channel, ch1 = channel, channels = getChannelCount();
|
Chris@147
|
184 if (channel == -1) {
|
Chris@147
|
185 ch0 = 0;
|
Chris@147
|
186 ch1 = channels - 1;
|
Chris@147
|
187 }
|
Chris@147
|
188
|
Chris@147
|
189 while (i < end - start) {
|
Chris@147
|
190
|
Chris@147
|
191 buffer[i] = 0.0;
|
Chris@147
|
192
|
Chris@147
|
193 for (int ch = ch0; ch <= ch1; ++ch) {
|
Chris@147
|
194
|
Chris@147
|
195 size_t index = i * channels + ch;
|
Chris@147
|
196 if (index >= frames.size()) break;
|
Chris@147
|
197
|
Chris@147
|
198 float sample = frames[index];
|
Chris@147
|
199 buffer[i] += sample;
|
Chris@147
|
200 }
|
Chris@147
|
201
|
Chris@147
|
202 ++i;
|
Chris@147
|
203 }
|
Chris@147
|
204
|
Chris@147
|
205 return i;
|
Chris@147
|
206 }
|
Chris@147
|
207
|
Chris@147
|
208 WaveFileModel::RangeBlock
|
Chris@147
|
209 WaveFileModel::getRanges(size_t channel, size_t start, size_t end,
|
Chris@147
|
210 size_t &blockSize) const
|
Chris@147
|
211 {
|
Chris@147
|
212 RangeBlock ranges;
|
Chris@147
|
213 if (!isOK()) return ranges;
|
Chris@147
|
214
|
Chris@147
|
215 if (end <= start) {
|
Chris@147
|
216 std::cerr << "WARNING: Internal error: end <= start in WaveFileModel::getRanges (end = " << end << ", start = " << start << ", blocksize = " << blockSize << ")" << std::endl;
|
Chris@147
|
217 return ranges;
|
Chris@147
|
218 }
|
Chris@147
|
219
|
Chris@147
|
220 int cacheType = 0;
|
Chris@179
|
221 int power = m_zoomConstraint.getMinCachePower();
|
Chris@179
|
222 blockSize = m_zoomConstraint.getNearestBlockSize
|
Chris@179
|
223 (blockSize, cacheType, power, ZoomConstraint::RoundUp);
|
Chris@147
|
224
|
Chris@147
|
225 size_t channels = getChannelCount();
|
Chris@147
|
226
|
Chris@147
|
227 if (cacheType != 0 && cacheType != 1) {
|
Chris@147
|
228
|
Chris@147
|
229 // We need to read directly from the file. We haven't got
|
Chris@147
|
230 // this cached. Hope the requested area is small. This is
|
Chris@147
|
231 // not optimal -- we'll end up reading the same frames twice
|
Chris@147
|
232 // for stereo files, in two separate calls to this method.
|
Chris@147
|
233 // We could fairly trivially handle this for most cases that
|
Chris@147
|
234 // matter by putting a single cache in getInterleavedFrames
|
Chris@147
|
235 // for short queries.
|
Chris@147
|
236
|
Chris@147
|
237 SampleBlock frames;
|
Chris@147
|
238 m_reader->getInterleavedFrames(start, end - start, frames);
|
Chris@147
|
239 float max = 0.0, min = 0.0, total = 0.0;
|
Chris@147
|
240 size_t i = 0, count = 0;
|
Chris@147
|
241
|
Chris@147
|
242 while (i < end - start) {
|
Chris@147
|
243
|
Chris@147
|
244 size_t index = i * channels + channel;
|
Chris@147
|
245 if (index >= frames.size()) break;
|
Chris@147
|
246
|
Chris@147
|
247 float sample = frames[index];
|
Chris@147
|
248 if (sample > max || count == 0) max = sample;
|
Chris@147
|
249 if (sample < min || count == 0) min = sample;
|
Chris@147
|
250 total += fabsf(sample);
|
Chris@147
|
251
|
Chris@147
|
252 ++i;
|
Chris@147
|
253 ++count;
|
Chris@147
|
254
|
Chris@147
|
255 if (count == blockSize) {
|
Chris@147
|
256 ranges.push_back(Range(min, max, total / count));
|
Chris@147
|
257 min = max = total = 0.0f;
|
Chris@147
|
258 count = 0;
|
Chris@147
|
259 }
|
Chris@147
|
260 }
|
Chris@147
|
261
|
Chris@147
|
262 if (count > 0) {
|
Chris@147
|
263 ranges.push_back(Range(min, max, total / count));
|
Chris@147
|
264 }
|
Chris@147
|
265
|
Chris@147
|
266 return ranges;
|
Chris@147
|
267
|
Chris@147
|
268 } else {
|
Chris@147
|
269
|
Chris@147
|
270 QMutexLocker locker(&m_mutex);
|
Chris@147
|
271
|
Chris@147
|
272 const RangeBlock &cache = m_cache[cacheType];
|
Chris@147
|
273
|
Chris@147
|
274 size_t cacheBlock, div;
|
Chris@147
|
275
|
Chris@147
|
276 if (cacheType == 0) {
|
Chris@179
|
277 cacheBlock = (1 << m_zoomConstraint.getMinCachePower());
|
Chris@147
|
278 div = (1 << power) / cacheBlock;
|
Chris@147
|
279 } else {
|
Chris@179
|
280 cacheBlock = ((unsigned int)((1 << m_zoomConstraint.getMinCachePower()) * sqrt(2) + 0.01));
|
Chris@147
|
281 div = ((unsigned int)((1 << power) * sqrt(2) + 0.01)) / cacheBlock;
|
Chris@147
|
282 }
|
Chris@147
|
283
|
Chris@147
|
284 size_t startIndex = start / cacheBlock;
|
Chris@147
|
285 size_t endIndex = end / cacheBlock;
|
Chris@147
|
286
|
Chris@147
|
287 float max = 0.0, min = 0.0, total = 0.0;
|
Chris@147
|
288 size_t i = 0, count = 0;
|
Chris@147
|
289
|
Chris@147
|
290 //cerr << "blockSize is " << blockSize << ", cacheBlock " << cacheBlock << ", start " << start << ", end " << end << ", power is " << power << ", div is " << div << ", startIndex " << startIndex << ", endIndex " << endIndex << endl;
|
Chris@147
|
291
|
Chris@147
|
292 for (i = 0; i < endIndex - startIndex; ) {
|
Chris@147
|
293
|
Chris@147
|
294 size_t index = (i + startIndex) * channels + channel;
|
Chris@147
|
295 if (index >= cache.size()) break;
|
Chris@147
|
296
|
Chris@147
|
297 const Range &range = cache[index];
|
Chris@147
|
298 if (range.max > max || count == 0) max = range.max;
|
Chris@147
|
299 if (range.min < min || count == 0) min = range.min;
|
Chris@147
|
300 total += range.absmean;
|
Chris@147
|
301
|
Chris@147
|
302 ++i;
|
Chris@147
|
303 ++count;
|
Chris@147
|
304
|
Chris@147
|
305 if (count == div) {
|
Chris@147
|
306 ranges.push_back(Range(min, max, total / count));
|
Chris@147
|
307 min = max = total = 0.0f;
|
Chris@147
|
308 count = 0;
|
Chris@147
|
309 }
|
Chris@147
|
310 }
|
Chris@147
|
311
|
Chris@147
|
312 if (count > 0) {
|
Chris@147
|
313 ranges.push_back(Range(min, max, total / count));
|
Chris@147
|
314 }
|
Chris@147
|
315 }
|
Chris@147
|
316
|
Chris@147
|
317 //cerr << "returning " << ranges.size() << " ranges" << endl;
|
Chris@147
|
318 return ranges;
|
Chris@147
|
319 }
|
Chris@147
|
320
|
Chris@147
|
321 WaveFileModel::Range
|
Chris@147
|
322 WaveFileModel::getRange(size_t channel, size_t start, size_t end) const
|
Chris@147
|
323 {
|
Chris@147
|
324 Range range;
|
Chris@147
|
325 if (!isOK()) return range;
|
Chris@147
|
326
|
Chris@147
|
327 if (end <= start) {
|
Chris@147
|
328 std::cerr << "WARNING: Internal error: end <= start in WaveFileModel::getRange (end = " << end << ", start = " << start << ")" << std::endl;
|
Chris@147
|
329 return range;
|
Chris@147
|
330 }
|
Chris@147
|
331
|
Chris@147
|
332 size_t blockSize;
|
Chris@147
|
333 for (blockSize = 1; blockSize <= end - start; blockSize *= 2);
|
Chris@147
|
334 blockSize /= 2;
|
Chris@147
|
335
|
Chris@147
|
336 bool first = false;
|
Chris@147
|
337
|
Chris@147
|
338 size_t blockStart = (start / blockSize) * blockSize;
|
Chris@147
|
339 size_t blockEnd = (end / blockSize) * blockSize;
|
Chris@147
|
340
|
Chris@147
|
341 if (blockStart < start) blockStart += blockSize;
|
Chris@147
|
342
|
Chris@147
|
343 if (blockEnd > blockStart) {
|
Chris@147
|
344 RangeBlock ranges = getRanges(channel, blockStart, blockEnd, blockSize);
|
Chris@147
|
345 for (size_t i = 0; i < ranges.size(); ++i) {
|
Chris@147
|
346 if (first || ranges[i].min < range.min) range.min = ranges[i].min;
|
Chris@147
|
347 if (first || ranges[i].max > range.max) range.max = ranges[i].max;
|
Chris@147
|
348 if (first || ranges[i].absmean < range.absmean) range.absmean = ranges[i].absmean;
|
Chris@147
|
349 first = false;
|
Chris@147
|
350 }
|
Chris@147
|
351 }
|
Chris@147
|
352
|
Chris@147
|
353 if (blockStart > start) {
|
Chris@147
|
354 Range startRange = getRange(channel, start, blockStart);
|
Chris@147
|
355 range.min = std::min(range.min, startRange.min);
|
Chris@147
|
356 range.max = std::max(range.max, startRange.max);
|
Chris@147
|
357 range.absmean = std::min(range.absmean, startRange.absmean);
|
Chris@147
|
358 }
|
Chris@147
|
359
|
Chris@147
|
360 if (blockEnd < end) {
|
Chris@147
|
361 Range endRange = getRange(channel, blockEnd, end);
|
Chris@147
|
362 range.min = std::min(range.min, endRange.min);
|
Chris@147
|
363 range.max = std::max(range.max, endRange.max);
|
Chris@147
|
364 range.absmean = std::min(range.absmean, endRange.absmean);
|
Chris@147
|
365 }
|
Chris@147
|
366
|
Chris@147
|
367 return range;
|
Chris@147
|
368 }
|
Chris@147
|
369
|
Chris@147
|
370 void
|
Chris@147
|
371 WaveFileModel::fillCache()
|
Chris@147
|
372 {
|
Chris@147
|
373 m_mutex.lock();
|
Chris@147
|
374 m_updateTimer = new QTimer(this);
|
Chris@147
|
375 connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(fillTimerTimedOut()));
|
Chris@147
|
376 m_updateTimer->start(100);
|
Chris@147
|
377 m_fillThread = new RangeCacheFillThread(*this);
|
Chris@147
|
378 connect(m_fillThread, SIGNAL(finished()), this, SLOT(cacheFilled()));
|
Chris@147
|
379 m_mutex.unlock();
|
Chris@147
|
380 m_fillThread->start();
|
Chris@175
|
381 std::cerr << "WaveFileModel::fillCache: started fill thread" << std::endl;
|
Chris@147
|
382 }
|
Chris@147
|
383
|
Chris@147
|
384 void
|
Chris@147
|
385 WaveFileModel::fillTimerTimedOut()
|
Chris@147
|
386 {
|
Chris@147
|
387 if (m_fillThread) {
|
Chris@147
|
388 size_t fillExtent = m_fillThread->getFillExtent();
|
Chris@175
|
389 cerr << "WaveFileModel::fillTimerTimedOut: extent = " << fillExtent << endl;
|
Chris@147
|
390 if (fillExtent > m_lastFillExtent) {
|
Chris@147
|
391 emit modelChanged(m_lastFillExtent, fillExtent);
|
Chris@147
|
392 m_lastFillExtent = fillExtent;
|
Chris@147
|
393 }
|
Chris@147
|
394 } else {
|
Chris@175
|
395 cerr << "WaveFileModel::fillTimerTimedOut: no thread" << std::endl;
|
Chris@147
|
396 emit modelChanged();
|
Chris@147
|
397 }
|
Chris@147
|
398 }
|
Chris@147
|
399
|
Chris@147
|
400 void
|
Chris@147
|
401 WaveFileModel::cacheFilled()
|
Chris@147
|
402 {
|
Chris@147
|
403 m_mutex.lock();
|
Chris@147
|
404 delete m_fillThread;
|
Chris@147
|
405 m_fillThread = 0;
|
Chris@147
|
406 delete m_updateTimer;
|
Chris@147
|
407 m_updateTimer = 0;
|
Chris@147
|
408 m_mutex.unlock();
|
Chris@147
|
409 emit modelChanged();
|
Chris@175
|
410 cerr << "WaveFileModel::cacheFilled" << endl;
|
Chris@175
|
411 }
|
Chris@175
|
412
|
Chris@175
|
413 void
|
Chris@147
|
414 WaveFileModel::RangeCacheFillThread::run()
|
Chris@147
|
415 {
|
Chris@147
|
416 size_t cacheBlockSize[2];
|
Chris@179
|
417 cacheBlockSize[0] = (1 << m_model.m_zoomConstraint.getMinCachePower());
|
Chris@179
|
418 cacheBlockSize[1] = ((unsigned int)((1 << m_model.m_zoomConstraint.getMinCachePower()) *
|
Chris@147
|
419 sqrt(2) + 0.01));
|
Chris@147
|
420
|
Chris@147
|
421 size_t frame = 0;
|
Chris@147
|
422 size_t readBlockSize = 16384;
|
Chris@147
|
423 SampleBlock block;
|
Chris@147
|
424
|
Chris@147
|
425 if (!m_model.isOK()) return;
|
Chris@147
|
426
|
Chris@147
|
427 size_t channels = m_model.getChannelCount();
|
Chris@187
|
428 bool updating = m_model.m_reader->isUpdating();
|
Chris@187
|
429
|
Chris@187
|
430 if (updating) {
|
Chris@187
|
431 while (channels == 0 && !m_model.m_exiting) {
|
Chris@187
|
432 std::cerr << "WaveFileModel::fill: Waiting for channels..." << std::endl;
|
Chris@187
|
433 sleep(1);
|
Chris@187
|
434 channels = m_model.getChannelCount();
|
Chris@187
|
435 }
|
Chris@187
|
436 }
|
Chris@147
|
437
|
Chris@147
|
438 Range *range = new Range[2 * channels];
|
Chris@147
|
439 size_t count[2];
|
Chris@147
|
440 count[0] = count[1] = 0;
|
Chris@176
|
441
|
Chris@176
|
442 bool first = true;
|
Chris@176
|
443
|
Chris@176
|
444 while (first || updating) {
|
Chris@176
|
445
|
Chris@176
|
446 updating = m_model.m_reader->isUpdating();
|
Chris@187
|
447 m_frameCount = m_model.getFrameCount();
|
Chris@175
|
448
|
Chris@175
|
449 std::cerr << "WaveFileModel::fill: frame = " << frame << ", count = " << m_frameCount << std::endl;
|
Chris@147
|
450
|
Chris@176
|
451 while (frame < m_frameCount) {
|
Chris@147
|
452
|
Chris@176
|
453 if (updating && (frame + readBlockSize > m_frameCount)) break;
|
Chris@176
|
454
|
Chris@176
|
455 m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block);
|
Chris@176
|
456
|
Chris@176
|
457 for (size_t i = 0; i < readBlockSize; ++i) {
|
Chris@147
|
458
|
Chris@176
|
459 for (size_t ch = 0; ch < size_t(channels); ++ch) {
|
Chris@147
|
460
|
Chris@176
|
461 size_t index = channels * i + ch;
|
Chris@176
|
462 if (index >= block.size()) continue;
|
Chris@176
|
463 float sample = block[index];
|
Chris@176
|
464
|
Chris@176
|
465 for (size_t ct = 0; ct < 2; ++ct) {
|
Chris@176
|
466
|
Chris@176
|
467 size_t rangeIndex = ch * 2 + ct;
|
Chris@176
|
468
|
Chris@176
|
469 if (sample > range[rangeIndex].max || count[ct] == 0) {
|
Chris@176
|
470 range[rangeIndex].max = sample;
|
Chris@176
|
471 }
|
Chris@176
|
472 if (sample < range[rangeIndex].min || count[ct] == 0) {
|
Chris@176
|
473 range[rangeIndex].min = sample;
|
Chris@176
|
474 }
|
Chris@176
|
475 range[rangeIndex].absmean += fabsf(sample);
|
Chris@176
|
476 }
|
Chris@176
|
477 }
|
Chris@147
|
478
|
Chris@176
|
479 QMutexLocker locker(&m_model.m_mutex);
|
Chris@147
|
480 for (size_t ct = 0; ct < 2; ++ct) {
|
Chris@176
|
481 if (++count[ct] == cacheBlockSize[ct]) {
|
Chris@176
|
482 for (size_t ch = 0; ch < size_t(channels); ++ch) {
|
Chris@176
|
483 size_t rangeIndex = ch * 2 + ct;
|
Chris@176
|
484 range[rangeIndex].absmean /= count[ct];
|
Chris@176
|
485 m_model.m_cache[ct].push_back(range[rangeIndex]);
|
Chris@176
|
486 range[rangeIndex] = Range();
|
Chris@176
|
487 }
|
Chris@176
|
488 count[ct] = 0;
|
Chris@176
|
489 }
|
Chris@176
|
490 }
|
Chris@147
|
491
|
Chris@176
|
492 ++frame;
|
Chris@147
|
493 }
|
Chris@147
|
494
|
Chris@176
|
495 if (m_model.m_exiting) break;
|
Chris@176
|
496
|
Chris@176
|
497 m_fillExtent = frame;
|
Chris@147
|
498 }
|
Chris@147
|
499
|
Chris@176
|
500 first = false;
|
Chris@177
|
501 if (m_model.m_exiting) break;
|
Chris@187
|
502 if (updating) {
|
Chris@187
|
503 sleep(1);
|
Chris@187
|
504 }
|
Chris@147
|
505 }
|
Chris@147
|
506
|
Chris@177
|
507 if (!m_model.m_exiting) {
|
Chris@177
|
508
|
Chris@177
|
509 QMutexLocker locker(&m_model.m_mutex);
|
Chris@177
|
510 for (size_t ct = 0; ct < 2; ++ct) {
|
Chris@177
|
511 if (count[ct] > 0) {
|
Chris@177
|
512 for (size_t ch = 0; ch < size_t(channels); ++ch) {
|
Chris@177
|
513 size_t rangeIndex = ch * 2 + ct;
|
Chris@177
|
514 range[rangeIndex].absmean /= count[ct];
|
Chris@177
|
515 m_model.m_cache[ct].push_back(range[rangeIndex]);
|
Chris@177
|
516 range[rangeIndex] = Range();
|
Chris@177
|
517 }
|
Chris@177
|
518 count[ct] = 0;
|
Chris@147
|
519 }
|
Chris@177
|
520
|
Chris@177
|
521 const Range &rr = *m_model.m_cache[ct].begin();
|
Chris@177
|
522 MUNLOCK(&rr, m_model.m_cache[ct].capacity() * sizeof(Range));
|
Chris@147
|
523 }
|
Chris@147
|
524 }
|
Chris@147
|
525
|
Chris@147
|
526 delete[] range;
|
Chris@147
|
527
|
Chris@175
|
528 m_fillExtent = m_frameCount;
|
Chris@147
|
529
|
Chris@175
|
530 for (size_t ct = 0; ct < 2; ++ct) {
|
Chris@175
|
531 cerr << "Cache type " << ct << " now contains " << m_model.m_cache[ct].size() << " ranges" << endl;
|
Chris@175
|
532 }
|
Chris@147
|
533 }
|
Chris@147
|
534
|
Chris@163
|
535 void
|
Chris@163
|
536 WaveFileModel::toXml(QTextStream &out,
|
Chris@163
|
537 QString indent,
|
Chris@163
|
538 QString extraAttributes) const
|
Chris@163
|
539 {
|
Chris@163
|
540 Model::toXml(out, indent,
|
Chris@163
|
541 QString("type=\"wavefile\" file=\"%1\" %2")
|
Chris@163
|
542 .arg(m_path).arg(extraAttributes));
|
Chris@163
|
543 }
|
Chris@163
|
544
|
Chris@147
|
545 QString
|
Chris@147
|
546 WaveFileModel::toXmlString(QString indent,
|
Chris@147
|
547 QString extraAttributes) const
|
Chris@147
|
548 {
|
Chris@147
|
549 return Model::toXmlString(indent,
|
Chris@147
|
550 QString("type=\"wavefile\" file=\"%1\" %2")
|
Chris@147
|
551 .arg(m_path).arg(extraAttributes));
|
Chris@147
|
552 }
|
Chris@147
|
553
|
Chris@147
|
554
|
Chris@147
|
555 #ifdef INCLUDE_MOCFILES
|
Chris@147
|
556 #ifdef INCLUDE_MOCFILES
|
Chris@147
|
557 #include "WaveFileModel.moc.cpp"
|
Chris@147
|
558 #endif
|
Chris@147
|
559 #endif
|
Chris@147
|
560
|