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