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