File: OverViewCache.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (425 lines) | stat: -rw-r--r-- 14,430 bytes parent folder | download
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
/***************************************************************************
      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 <math.h>

#include <QColor>
#include <QPainter>

#include "libkwave/MultiTrackReader.h"
#include "libkwave/SampleReader.h"
#include "libkwave/SignalManager.h"
#include "libkwave/Track.h"
#include "libkwave/Utils.h"

#include "libgui/OverViewCache.h"

#define CACHE_SIZE 8192           /**< number of cache entries */

//***************************************************************************
Kwave::OverViewCache::OverViewCache(Kwave::SignalManager &signal,
                                    sample_index_t src_offset,
                                    sample_index_t src_length,
                                    const QVector<unsigned int> *src_tracks)
    :m_signal(signal),
     m_selection(&signal, src_offset, src_length, src_tracks),
     m_min(), m_max(), m_state(), m_minmax(),
     m_scale(1),
     m_lock()
{

    connect(&m_selection, SIGNAL(sigTrackInserted(QUuid)),
            this,         SLOT(slotTrackInserted(QUuid)));
    connect(&m_selection, SIGNAL(sigTrackDeleted(QUuid)),
            this,         SLOT(slotTrackDeleted(QUuid)));
    connect(&m_selection, SIGNAL(sigLengthChanged(sample_index_t)),
            this,         SLOT(slotLengthChanged(sample_index_t)));
    connect(
        &m_selection,
        SIGNAL(sigInvalidated(const QUuid*,sample_index_t,sample_index_t)),
        this,
        SLOT(slotInvalidated(const QUuid*,sample_index_t,sample_index_t))
    );

    // take over the initial list of tracks
    foreach (const QUuid &uuid, m_selection.allTracks())
        slotTrackInserted(uuid);
}

//***************************************************************************
Kwave::OverViewCache::~OverViewCache()
{
    QMutexLocker lock(&m_lock);

    m_state.clear();
    m_min.clear();
    m_max.clear();
}

//***************************************************************************
void Kwave::OverViewCache::scaleUp()
{
    Q_ASSERT(m_scale);
    if (!m_scale) return;

    // calculate the new scale
    const sample_index_t len = m_selection.length();
    unsigned int shrink = Kwave::toUint(len / (m_scale * CACHE_SIZE));
    while (len > CACHE_SIZE * m_scale * shrink) {
        shrink++;
    }
    if (shrink <= 1) return; // nothing to shrink, just ignore new scale

    // loop over all tracks
    for (QHash<QUuid, QVector <CacheState> >::iterator
        it(m_state.begin()); it != m_state.end(); ++it)
    {
        const QUuid &uuid = it.key();
        unsigned int dst = 0;
        unsigned int count = CACHE_SIZE / shrink;
        Q_ASSERT(count <= CACHE_SIZE);

        // source pointers
        sample_t *smin = m_min[uuid].data();
        sample_t *smax = m_max[uuid].data();
        CacheState *sstate = it.value().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 Kwave::OverViewCache::scaleDown()
{
    const sample_index_t len = m_selection.length();
    quint64 new_scale = static_cast<quint64>(rint(ceil(len / CACHE_SIZE)));
    if (!new_scale) new_scale = 1;
    if (m_scale == new_scale) return;

    m_scale = new_scale;
    invalidateCache(nullptr, 0, CACHE_SIZE - 1);
}

//***************************************************************************
void Kwave::OverViewCache::invalidateCache(const QUuid *track_id,
                                           unsigned int first,
                                           unsigned int last)
{
    if (track_id) {
        // invalidate a single track
        Q_ASSERT(m_state.contains(*track_id));
        if (!m_state.contains(*track_id)) return;

        if (last >= CACHE_SIZE) last = CACHE_SIZE - 1;

//      qDebug("OverViewCache[%p]::invalidateCache(%s, %u, %u)",
//             static_cast<void *>(this), DBG(track_id->toString()),
//             first, last);

        for (unsigned int pos = first; pos <= last; ++pos)
            m_state[*track_id][pos] = Invalid;
    } else {
        // invalidate all tracks
        for (QHash<QUuid, QVector <CacheState> >::const_iterator
            it(m_state.constBegin()); it != m_state.constEnd(); ++it)
        {
            const QUuid &uuid = it.key();
            invalidateCache(&uuid, first, last);
        }
    }
}

//***************************************************************************
void Kwave::OverViewCache::slotTrackInserted(const QUuid &track_id)
{
    QMutexLocker lock(&m_lock);

    // just to be sure: check scale again, maybe it was the first track
    if ((m_selection.length() / m_scale) > CACHE_SIZE)
        scaleUp();
    if ((m_selection.length() / m_scale) < (CACHE_SIZE / 4))
        scaleDown();

    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);

    m_min.insert(track_id, min);
    m_max.insert(track_id, max);
    m_state.insert(track_id, state);

    // mark the new cache content as invalid
    invalidateCache(&track_id, 0, CACHE_SIZE - 1);

    emit changed();
}

//***************************************************************************
void Kwave::OverViewCache::slotTrackDeleted(const QUuid &track_id)
{
    QMutexLocker lock(&m_lock);

    m_min.remove(track_id);
    m_max.remove(track_id);
    m_state.remove(track_id);

    emit changed();
}

//***************************************************************************
void Kwave::OverViewCache::slotInvalidated(const QUuid *track_id,
                                           sample_index_t first,
                                           sample_index_t last)
{
    QMutexLocker lock(&m_lock);

    // adjust offsets, absolute -> relative
    sample_index_t offset = m_selection.offset();
    Q_ASSERT(first >= offset);
    Q_ASSERT(last  >= offset);
    Q_ASSERT(last  >= first);
    first -= offset;
    last  -= offset;

    unsigned int first_idx = Kwave::toUint(first / m_scale);
    unsigned int last_idx;
    if (last >= (SAMPLE_INDEX_MAX - (m_scale - 1)))
        last_idx = CACHE_SIZE - 1;
    else
        last_idx = Kwave::toUint(
            qMin(Kwave::round_up(last, m_scale) / m_scale,
            quint64(CACHE_SIZE - 1))
        );

    invalidateCache(track_id, first_idx, last_idx);
    emit changed();
}

//***************************************************************************
void Kwave::OverViewCache::slotLengthChanged(sample_index_t new_length)
{
    QMutexLocker lock(&m_lock);

    // just to be sure: check scale again, maybe it was the first track
    if ((new_length / m_scale) > CACHE_SIZE)
        scaleUp();
    if ((new_length / m_scale) < (CACHE_SIZE / 4))
        scaleDown();
}

//***************************************************************************
int Kwave::OverViewCache::getMinMax(int width, MinMaxArray &minmax)
{
    QMutexLocker lock(&m_lock);
    int retval = 0;

    const sample_index_t first  = m_selection.offset();
    const sample_index_t last   = m_selection.last();
    const sample_index_t length = m_selection.length();
    if (!length)
        return 0;

    // resize the target buffer if necessary
    if (minmax.count() < width)
        minmax.resize(width);
    if (minmax.count() < width) // truncated, OOM
        width = static_cast<int>(minmax.count());

    QVector<unsigned int> track_list;
    const QList<QUuid> selected_tracks = m_selection.allTracks();
    foreach (unsigned int track, m_signal.allTracks())
        if (selected_tracks.contains(m_signal.uuidOfTrack(track)))
            track_list.append(track);
    if (track_list.isEmpty())
        return 0;

    Kwave::MultiTrackReader src(
        Kwave::SinglePassForward,
        m_signal, track_list, first, last
    );
    Q_ASSERT(m_min.count() == m_max.count());
    Q_ASSERT(m_min.count() == m_state.count());

    if ((length / m_scale < 2) || src.isEmpty() || !m_state.count())
        return 0; // empty ?

    // loop over all min/max buffers and make their content valid
    for (int index = 0; index < track_list.count(); ++index) {
        unsigned int count = qBound<unsigned int>(
            1, Kwave::toUint(length / m_scale), CACHE_SIZE);

        QUuid uuid = m_signal.uuidOfTrack(track_list[index]);
        if (uuid.isNull()) continue; // track has just been deleted

        // check: maybe slotTrackInserted has not yet been called
        //        or slotTrackDeleted has just been called
        if (!m_state.contains(uuid))
            continue;

        sample_t *min = m_min[uuid].data();
        sample_t *max = m_max[uuid].data();
        CacheState *state = m_state[uuid].data();
        Q_ASSERT(min && max && state);
        Kwave::SampleReader *reader = src[index];
        Q_ASSERT(reader);

        if (!reader || !min || !max || !state) continue;

        for (unsigned int ofs = 0; ofs < count; ++ofs) {
            if (state[ofs] == Valid)  continue;
            if (state[ofs] == Unused) continue;

            // get min/max
            sample_index_t first_idx = m_selection.offset() + (ofs * m_scale);
            sample_index_t last_idx  = first_idx + m_scale - 1;
            reader->minMax(first_idx, last_idx, min[ofs], max[ofs]);
            state[ofs] = Valid;
        }
    }

    // loop over all min/max buffers
    for (int x = 0; x < width; ++x) {
        unsigned int count = qBound<unsigned int>(
            1, Kwave::toUint(length / m_scale), CACHE_SIZE);

        // 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
            for (QHash<QUuid, QVector <CacheState> >::const_iterator
                 it(m_state.constBegin()); it != m_state.constEnd(); ++it)
            {
                const QUuid &uuid = it.key();
                sample_t *min = m_min[uuid].data();
                sample_t *max = m_max[uuid].data();
                const CacheState *state = it.value().constData();
                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];
            }
        }

        minmax[x].min = minimum;
        minmax[x].max = maximum;
        retval++;
    }

    return retval;
}

//***************************************************************************
QImage Kwave::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 < 2) || (height < 3) || bitmap.isNull()) return bitmap;

    QPainter p;
    p.begin(&bitmap);
    p.fillRect(bitmap.rect(), bg);
    p.setPen(fg);

    int count = getMinMax(width, m_minmax);
    if (count < 1) {
        p.end();
        return bitmap; // empty ?
    }

    // draw the bitmap
    for (int x = 0; x < count; ++x) {
        const int middle = (height >> 1);
        const double scale = static_cast<double>(middle) /
                             static_cast<double>(SAMPLE_MAX);
        double min = m_minmax[x].min * scale;
        double max = m_minmax[x].max * 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 - Kwave::toInt(max),
                   x, middle - Kwave::toInt(min));
    }

    p.end();
    return bitmap;
}

//***************************************************************************
//***************************************************************************

#include "moc_OverViewCache.cpp"