[84b6f5]: libgui / OverViewCache.cpp Maximize Restore History

Download this file

OverViewCache.cpp    566 lines (479 with data), 17.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/***************************************************************************
OverViewCache.cpp - fast cache for sample data overview
-------------------
begin : Mon May 20 2002
copyright : (C) 2000 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include "math.h"
#include <QMutableListIterator>
#include <QColor>
#include <QPainter>
#include "libkwave/MultiTrackReader.h"
#include "libkwave/Sample.h"
#include "libkwave/SampleReader.h"
#include "libkwave/SignalManager.h"
#include "libkwave/Track.h"
#include "OverViewCache.h"
#define CACHE_SIZE 8192 /* number of cache entries */
//***************************************************************************
OverViewCache::OverViewCache(SignalManager &signal, sample_index_t src_offset,
sample_index_t src_length,
const QList<unsigned int> *src_tracks)
:m_signal(signal), m_min(), m_max(), m_state(), m_count(0), m_scale(1),
m_lock(QMutex::Recursive), m_src_offset(src_offset),
m_src_length(src_length), m_src_tracks(), m_src_deleted()
{
// connect to the signal manager
SignalManager *sig = &signal;
Q_ASSERT(sig);
connect(sig, SIGNAL(sigTrackInserted(unsigned int, Kwave::Track *)),
this, SLOT(slotTrackInserted(unsigned int, Kwave::Track *)));
connect(sig, SIGNAL(sigTrackDeleted(unsigned int)),
this, SLOT(slotTrackDeleted(unsigned int)));
connect(sig, SIGNAL(sigSamplesDeleted(unsigned int, sample_index_t,
sample_index_t)),
this, SLOT(slotSamplesDeleted(unsigned int, sample_index_t,
sample_index_t)));
connect(sig, SIGNAL(sigSamplesInserted(unsigned int, sample_index_t,
sample_index_t)),
this, SLOT(slotSamplesInserted(unsigned int, sample_index_t,
sample_index_t)));
connect(sig, SIGNAL(sigSamplesModified(unsigned int, sample_index_t,
sample_index_t)),
this, SLOT(slotSamplesModified(unsigned int, sample_index_t,
sample_index_t)));
if (src_tracks && !src_tracks->isEmpty()) {
// already having a list of selected tracks
foreach (unsigned int track, *src_tracks) {
m_src_deleted.append(track);
slotTrackInserted(track, 0);
}
} else {
// take over all tracks from the signal manager
QList<unsigned int> tracks = signal.allTracks();
foreach (unsigned int track, tracks) {
slotTrackInserted(track, 0);
}
}
}
//***************************************************************************
OverViewCache::~OverViewCache()
{
QMutexLocker lock(&m_lock);
m_state.clear();
m_min.clear();
m_max.clear();
}
//***************************************************************************
sample_index_t OverViewCache::sourceLength()
{
return (m_src_length) ? m_src_length : m_signal.length();
}
//***************************************************************************
void OverViewCache::scaleUp()
{
QMutexLocker lock(&m_lock);
Q_ASSERT(m_scale);
if (!m_scale) return;
// calculate the new scale
const sample_index_t len = sourceLength();
unsigned int shrink = len / (m_scale * CACHE_SIZE);
Q_ASSERT(shrink);
while (len > CACHE_SIZE * m_scale * shrink) {
shrink++;
}
Q_ASSERT(shrink > 1);
if (shrink <= 1) return; // nothing to shrink, just ignore new scale
// loop over all tracks
for (int t=0; t < m_state.count(); ++t) {
unsigned int dst = 0;
unsigned int count = CACHE_SIZE / shrink;
Q_ASSERT(count <= CACHE_SIZE);
// source pointers
sample_t *smin = m_min[t].data();
sample_t *smax = m_max[t].data();
CacheState *sstate = m_state[t].data();
// destination pointers
sample_t *dmin = smin;
sample_t *dmax = smax;
CacheState *dstate = sstate;
// loop over all entries to be shrinked
while (dst < count) {
sample_t min = SAMPLE_MAX;
sample_t max = SAMPLE_MIN;
CacheState state = Unused;
for (unsigned int i = 0; i < shrink; ++i) {
if (*smin < min) min = *smin;
if (*smax > max) max = *smax;
if (*sstate < state) state = *sstate;
++smin;
++smax;
++sstate;
}
*dmin = min;
*dmax = max;
*dstate = state;
++dmin;
++dmax;
++dstate;
++dst;
}
// the rest will be unused
while (dst++ < CACHE_SIZE) {
*dstate = Unused;
dstate++;
}
}
m_scale *= shrink;
}
//***************************************************************************
void OverViewCache::scaleDown()
{
QMutexLocker lock(&m_lock);
const sample_index_t len = sourceLength();
unsigned int new_scale = static_cast<unsigned int>(
rint(ceil(len / CACHE_SIZE)));
if (!new_scale) new_scale = 1;
if (m_scale == new_scale) return;
m_scale = new_scale;
for (int track = 0; track < m_state.count(); ++track) {
invalidateCache(track, 0, (len / m_scale) + 1);
}
}
//***************************************************************************
int OverViewCache::trackIndex(unsigned int track_nr)
{
QMutexLocker lock(&m_lock);
if (!m_src_tracks.isEmpty() || !m_src_deleted.isEmpty()) {
return m_src_tracks.indexOf(track_nr);
} else {
return track_nr;
}
}
//***************************************************************************
void OverViewCache::invalidateCache(unsigned int track, unsigned int first,
unsigned int last)
{
QMutexLocker lock(&m_lock);
// qDebug("OverViewCache::invalidateCache(%u, %u, %u)",track,first,last);
if (m_state.isEmpty()) return;
int cache_track = trackIndex(track);
if (cache_track < 0) return;
if (cache_track >= m_state.count()) return;
QVector<CacheState> &state = m_state[cache_track];
if (last >= CACHE_SIZE) last = CACHE_SIZE - 1;
unsigned int pos;
for (pos = first; pos <= last; ++pos) {
state[pos] = Invalid;
}
}
//***************************************************************************
void OverViewCache::slotTrackInserted(unsigned int index, Kwave::Track *)
{
QMutexLocker lock(&m_lock);
// just to be sure: check scale again, maybe it was the first track
if ((sourceLength() / m_scale) > CACHE_SIZE)
scaleUp();
if ((sourceLength() / m_scale) < (CACHE_SIZE/4))
scaleDown();
// "selected tracks" mode -> adjust indices in track lists
if (!m_src_tracks.isEmpty() || !m_src_deleted.isEmpty()) {
QMutableListIterator<unsigned int> it_s(m_src_tracks);
QMutableListIterator<unsigned int> it_d(m_src_deleted);
unsigned int track;
if (it_d.findNext(index)) {
// deleted selected track came back again
it_d.remove();
it_d.toFront();
while (it_d.hasNext())
if ((track = it_d.next()) >= index) it_d.setValue(--track);
while (it_s.hasNext())
if ((track = it_s.next()) >= index) it_s.setValue(++track);
Q_ASSERT(!m_src_tracks.contains(index));
m_src_tracks.append(index);
dumpTracks();
} else {
// inserted new/unknown track
while (it_d.hasNext())
if ((track = it_d.next()) >= index) it_d.setValue(--track);
while (it_s.hasNext())
if ((track = it_s.next()) >= index) it_s.setValue(++track);
dumpTracks();
return;
}
}
QVector<CacheState> state(CACHE_SIZE);
QVector<sample_t> min(CACHE_SIZE);
QVector<sample_t> max(CACHE_SIZE);
min.fill(SAMPLE_MAX);
max.fill(SAMPLE_MIN);
state.fill(Unused);
int cache_index = trackIndex(index);
m_min.insert(cache_index, min);
m_max.insert(cache_index, max);
m_state.insert(cache_index, state);
// mark the new cache content as invalid
if (sourceLength()) {
invalidateCache(index, 0, (sourceLength() / m_scale) + 1);
} else {
invalidateCache(index, 0, CACHE_SIZE - 1);
}
emit changed();
}
//***************************************************************************
void OverViewCache::slotTrackDeleted(unsigned int index)
{
QMutexLocker lock(&m_lock);
int cache_track = trackIndex(index);
if (cache_track >= 0) {
m_min.removeAt(cache_track);
m_max.removeAt(cache_track);
m_state.removeAt(cache_track);
}
if (!m_src_tracks.isEmpty() || !m_src_deleted.isEmpty()) {
// "selected tracks" mode -> adjust indices in track lists
QMutableListIterator<unsigned int> it_s(m_src_tracks);
QMutableListIterator<unsigned int> it_d(m_src_deleted);
unsigned int track;
dumpTracks();
if (it_s.findNext(index)) {
// remove selected track
it_s.remove();
it_s.toFront();
m_src_deleted.append(index);
}
while (it_d.hasNext())
if ((track = it_d.next()) >= index) it_d.setValue(++track);
while (it_s.hasNext())
if ((track = it_s.next()) > index) it_s.setValue(--track);
dumpTracks();
}
emit changed();
}
//***************************************************************************
void OverViewCache::slotSamplesInserted(unsigned int track,
sample_index_t offset, sample_index_t length)
{
QMutexLocker lock(&m_lock);
if ((sourceLength() / m_scale) > CACHE_SIZE)
scaleUp();
// not in our selection
if (!m_src_tracks.isEmpty() && !(m_src_tracks.contains(track))) return;
// right out of our range -> out of interest
const sample_index_t len = sourceLength();
if (offset >= (m_src_offset + ((len) ? (len - 1) : 1))) return;
// left from us -> just move our own offset right
if (offset < m_src_offset) {
m_src_offset += length;
return;
}
// in our range -> increase length and invalidate all
// samples from offset to end of file
if (m_src_length) m_src_length += length;
if ((sourceLength() / m_scale) > CACHE_SIZE) scaleUp();
sample_index_t first = (offset - m_src_offset) / m_scale;
sample_index_t last = (sourceLength() / m_scale) + 1;
invalidateCache(track, first, last);
emit changed();
}
//***************************************************************************
void OverViewCache::slotSamplesDeleted(unsigned int track,
sample_index_t offset, sample_index_t length)
{
QMutexLocker lock(&m_lock);
if ((sourceLength() / m_scale) < (CACHE_SIZE/4))
scaleDown();
if (!length) return; // nothing to do
// not in our selection
if (!m_src_tracks.isEmpty() && !(m_src_tracks.contains(track))) return;
// right out of our range -> out of interest
if (offset > (m_src_offset + sourceLength())) return;
// completely left from us -> just move our own offset left
if (offset + length <= m_src_offset) {
m_src_offset -= length;
return;
}
// overlapping
if (offset < m_src_offset) {
sample_index_t overlap = (offset + length) - m_src_offset;
m_src_offset -= length - overlap;
length -= overlap;
offset = m_src_offset;
}
if (!length) return; // nothing more to do ?
// in our range -> invalidate all samples from offset to end of file
if (m_src_length) {
if (m_src_length > length)
m_src_length -= length;
else
m_src_length = 1;
}
unsigned int first = (offset - m_src_offset) / m_scale;
unsigned int last = (sourceLength() / m_scale) + 1;
invalidateCache(track, first, last);
emit changed();
}
//***************************************************************************
void OverViewCache::slotSamplesModified(unsigned int track,
sample_index_t offset, sample_index_t length)
{
QMutexLocker lock(&m_lock);
if (!length) return; // nothing to do
// not in our selection
if (!m_src_tracks.isEmpty() && !(m_src_tracks.contains(track))) return;
// right out of our range -> out of interest
if (offset > (m_src_offset + sourceLength())) return;
// completely left from us -> out of interest
if (offset + length < m_src_offset) return;
// overlapping
sample_index_t first = offset;
sample_index_t last = offset + length - 1;
if (first < m_src_offset) first = m_src_offset;
if (last > m_src_offset + sourceLength() - 1)
last = m_src_offset + sourceLength() - 1;
first -= m_src_offset;
last -= m_src_offset;
first = static_cast<unsigned int>(floor(first / m_scale));
last = static_cast<unsigned int>(ceil(last / m_scale));
invalidateCache(track, first, last);
emit changed();
}
//***************************************************************************
QImage OverViewCache::getOverView(int width, int height,
const QColor &fg, const QColor &bg,
double gain)
{
QMutexLocker lock(&m_lock);
QImage bitmap(width, height, QImage::Format_ARGB32_Premultiplied);
if (!width || !height || bitmap.isNull()) return bitmap;
QPainter p;
p.begin(&bitmap);
p.fillRect(bitmap.rect(), bg);
p.setPen(fg);
const unsigned int length = sourceLength();
if (!length) {
p.end();
return bitmap; // stay empty if no data available
}
QList<unsigned int> track_list;
if (!m_src_tracks.isEmpty() || !m_src_deleted.isEmpty()) {
for (int i=0; i < m_src_tracks.count(); ++i)
track_list.append(m_src_tracks[i]);
} else {
track_list = m_signal.allTracks();
}
MultiTrackReader src(Kwave::SinglePassForward,
m_signal, track_list, m_src_offset,
m_src_offset + length - 1);
Q_ASSERT(m_min.count() == m_max.count());
Q_ASSERT(m_min.count() == m_state.count());
// abort if the track count has recently changed
if (m_state.count() != static_cast<int>(src.tracks())) {
qWarning("OverViewCache::getOverView(): track count has changed");
p.end();
return bitmap;
}
// loop over all min/max buffers and make their content valid
for (int t = 0; (t < m_state.count()) && !src.isEmpty(); ++t) {
unsigned int count = length / m_scale;
if (count > CACHE_SIZE) count = 0;
sample_t *min = m_min[t].data();
sample_t *max = m_max[t].data();
CacheState *state = m_state[t].data();
SampleReader *reader = src[t];
Q_ASSERT(reader);
if (!reader) continue;
for (unsigned int ofs = 0; ofs < count; ++ofs) {
if (state[ofs] == Valid) continue;
if (state[ofs] == Unused) continue;
// get min/max
const unsigned int first = m_src_offset + (ofs * m_scale);
const unsigned int last = first + m_scale - 1;
reader->minMax(first, last, min[ofs], max[ofs]);
state[ofs] = Valid;
}
}
if ((width < 2) || (height < 2) || (length / m_scale < 2)) {
p.end();
return bitmap; // empty ?
}
// loop over all min/max buffers
for (int x = 0; (x < width) && (m_state.count()) && !src.isEmpty(); ++x) {
unsigned int count = length / m_scale;
if (count > CACHE_SIZE) count = 1;
// get the corresponding cache index
unsigned int index = ((count-1) * x) / (width-1);
unsigned int last_index = ((count-1) * (x+1)) / (width-1);
Q_ASSERT(index < CACHE_SIZE);
if (index >= CACHE_SIZE) index = CACHE_SIZE-1;
if (last_index > index) last_index--;
if (last_index >= CACHE_SIZE) last_index = CACHE_SIZE-1;
// loop over all cache indices
sample_t minimum = SAMPLE_MAX;
sample_t maximum = SAMPLE_MIN;
for (; index <= last_index; ++index) {
// loop over all tracks
Q_ASSERT(m_min.count() == m_state.count());
Q_ASSERT(m_max.count() == m_state.count());
for (int t = 0; t < m_state.count(); ++t) {
Q_ASSERT(t < m_min.count());
Q_ASSERT(t < m_max.count());
sample_t *min = m_min[t].data();
sample_t *max = m_max[t].data();
CacheState *state = m_state[t].data();
Q_ASSERT(state);
if (!state) continue;
if (state[index] != Valid) {
if (minimum > 0) minimum = 0;
if (maximum < 0) maximum = 0;
continue;
}
if (min[index] < minimum) minimum = min[index];
if (max[index] > maximum) maximum = max[index];
}
}
// update the bitmap
const int middle = (height >> 1);
const double scale = static_cast<double>(middle) /
static_cast<double>(SAMPLE_MAX);
double min = minimum * scale;
double max = maximum * scale;
if (gain != 1.0) {
min *= gain;
max *= gain;
if (min < -middle) min = -middle;
if (min > +middle) min = +middle;
if (max < -middle) max = -middle;
if (max > +middle) max = +middle;
}
p.drawLine(x, middle - static_cast<int>(min),
x, middle - static_cast<int>(max));
}
p.end();
return bitmap;
}
//***************************************************************************
void OverViewCache::dumpTracks()
{
QString list = "OverViewCache - selected:";
foreach (unsigned int track, m_src_tracks)
list += " " + list.number(track);
list += " --- deleted:";
foreach (unsigned int track, m_src_deleted)
list += " " + list.number(track);
qDebug("%s", list.toLocal8Bit().data());
}
//***************************************************************************
#include "OverViewCache.moc"
//***************************************************************************
//***************************************************************************