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