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