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