Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[351460]: libkwave / Stripe.cpp Maximize Restore History

Download this file

Stripe.cpp    508 lines (424 with data), 15.1 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
/***************************************************************************
Stripe.cpp - continuous block of samples
-------------------
begin : Feb 10 2001
copyright : (C) 2001 by Thomas Eschenbacher
email : Thomas Eschenbacher <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 <string.h> // for some speed-ups like memmove, memcpy ...
#include "libkwave/memcpy.h"
#include "libkwave/MemoryManager.h"
#include "libkwave/Stripe.h"
// define this for using only slow Qt array functions
#undef STRICTLY_QT
//***************************************************************************
//***************************************************************************
Stripe::MapStorageGuard::MapStorageGuard(Stripe &stripe)
:m_stripe(stripe), m_storage(0)
{
m_storage = m_stripe.mapStorage();
}
//***************************************************************************
Stripe::MapStorageGuard::~MapStorageGuard()
{
m_stripe.unmapStorage();
}
//***************************************************************************
sample_t *Stripe::MapStorageGuard::storage()
{
return m_storage;
}
//***************************************************************************
//***************************************************************************
Stripe::MappedArray::MappedArray(Stripe &stripe, unsigned int length)
:m_guard(stripe), m_length(length)
{
sample_t *samples = m_guard.storage();
Q_ASSERT(samples);
Q_ASSERT(m_length);
if (!samples) m_length = 0;
if (m_length) setRawData(samples, m_length);
}
//***************************************************************************
Stripe::MappedArray::~MappedArray()
{
if (m_length) resetRawData();
}
//***************************************************************************
unsigned int Stripe::MappedArray::copy(unsigned int dst, unsigned int src,
unsigned int cnt)
{
// qDebug(" Stripe::MappedArray::copy(%u, %u, %u)", dst, src, cnt);
Q_ASSERT(m_length);
if (!m_length) return 0;
#ifdef STRICTLY_QT
unsigned int rest = cnt;
while (rest--) {
(*this)[dst++] = (*this)[src++];
}
#else
sample_t *_samples = m_guard.storage();
Q_ASSERT(_samples);
if (!_samples) return 0;
// no MEMCPY here !
memmove(&(_samples[dst]), &(_samples[src]), cnt * sizeof(sample_t));
#endif
return cnt;
}
//***************************************************************************
unsigned int Stripe::MappedArray::copy(unsigned int dst,
const Kwave::SampleArray &source, unsigned int offset,
unsigned int cnt)
{
// qDebug(" Stripe::MappedArray::copy(%u, ... ,%u, %u)", dst,
// offset, cnt);
Q_ASSERT(m_length);
if (!m_length) return 0;
#ifdef STRICTLY_QT
unsigned int rest = cnt;
while (rest--) {
(*this)[dst++] = source[offset++];
}
#else
sample_t *_samples = m_guard.storage();
Q_ASSERT(_samples);
if (!_samples) return 0;
MEMCPY(&(_samples[dst]), &(source[offset]),
cnt * sizeof(sample_t));
#endif
return cnt;
}
//***************************************************************************
unsigned int Stripe::MappedArray::read(Kwave::SampleArray &buffer,
unsigned int dstoff, unsigned int offset, unsigned int length)
{
// qDebug(" Stripe::MappedArray::read(..., %u, %u, %u)", dstoff,
// offset, length);
Q_ASSERT(m_length);
if (!m_length) return 0;
#ifdef STRICTLY_QT
unsigned int cnt = length;
while (cnt--) {
buffer[dstoff++] = (*this)[offset++];
}
#else
sample_t *_samples = m_guard.storage();
Q_ASSERT(_samples);
if (!_samples) return 0;
MEMCPY(&(buffer[dstoff]), &(_samples[offset]),
length * sizeof(sample_t));
#endif
return length;
}
//***************************************************************************
//***************************************************************************
Stripe::Stripe()
:QObject(), m_start(0), m_length(0), m_storage(0), m_lock_samples(),
m_map_count(0), m_mapped_storage(0)
{
}
//***************************************************************************
Stripe::Stripe(unsigned int start)
:QObject(), m_start(start), m_length(0), m_storage(0), m_lock_samples(),
m_map_count(0), m_mapped_storage(0)
{
}
//***************************************************************************
Stripe::Stripe(unsigned int start, const Kwave::SampleArray &samples)
:QObject(), m_start(start), m_length(0), m_storage(0), m_lock_samples(),
m_map_count(0), m_mapped_storage(0)
{
if (samples.size()) append(samples, 0, samples.size());
}
//***************************************************************************
Stripe::Stripe(unsigned int start, Stripe &stripe, unsigned int offset)
:QObject(), m_start(start), m_length(0), m_storage(0), m_lock_samples(),
m_map_count(0), m_mapped_storage(0)
{
Q_ASSERT(offset < stripe.length());
if (offset >= stripe.length()) return;
unsigned int length = stripe.length() - offset;
if (resizeStorage(length) != length) return; // out of memory
if (length) {
MappedArray _samples(*this, m_length);
if (!stripe.read(_samples, 0, offset, length)) resize(0);
}
}
//***************************************************************************
Stripe::~Stripe()
{
QMutexLocker lock(&m_lock_samples);
Q_ASSERT(!m_map_count);
Q_ASSERT(!m_mapped_storage);
resizeStorage(0);
}
//***************************************************************************
unsigned int Stripe::start()
{
QMutexLocker lock(&m_lock_samples);
return m_start;
}
//***************************************************************************
void Stripe::setStart(unsigned int start)
{
QMutexLocker lock(&m_lock_samples);
m_start = start;
}
//***************************************************************************
unsigned int Stripe::length()
{
QMutexLocker lock(&m_lock_samples);
return m_length;
}
//***************************************************************************
unsigned int Stripe::end()
{
QMutexLocker lock(&m_lock_samples);
return m_start + ((m_length) ? (m_length - 1) : 0);
}
//***************************************************************************
unsigned int Stripe::resizeStorage(unsigned int length)
{
if (m_length == length) return length; // nothing to do
// qDebug("Stripe::resizeStorage(%u)", length);
MemoryManager &mem = MemoryManager::instance();
Q_ASSERT(!m_map_count);
if (length == 0) {
// delete the array
mem.free(m_storage);
m_storage = 0;
m_length = 0;
return 0;
}
if (!m_length || !m_storage) {
// allocate new storage
void *new_storage = mem.allocate(length * sizeof(sample_t));
if (!new_storage) {
// allocation failed
qWarning("Stripe::resizeStorage(%u) failed! (1)", length);
return m_length;
}
m_storage = new_storage;
m_length = length;
return length;
}
// resize the array to another size
void *new_storage = mem.resize(m_storage, length * sizeof(sample_t));
Q_ASSERT(new_storage);
if (!new_storage) {
// resize failed
qWarning("Stripe::resizeStorage(%u) failed! (2)", length);
return m_length;
}
m_storage = new_storage;
m_length = length;
return length;
}
//***************************************************************************
unsigned int Stripe::resize(unsigned int length, bool initialize)
{
unsigned int old_length = 0;
{
QMutexLocker lock(&m_lock_samples);
old_length = m_length;
if (m_length == length) return old_length; // nothing to do
// qDebug("Stripe::resize() from %d to %d samples", old_length, length);
Q_ASSERT(!m_map_count);
if (resizeStorage(length) != length) {
qWarning("Stripe::resize(%u) failed, out of memory ?", length);
return m_length;
}
// fill new samples with zero
if (initialize && length) {
Q_ASSERT(!m_map_count);
unsigned int pos = old_length;
#ifdef STRICTLY_QT
MappedArray _samples(*this, m_length);
if (_samples.size() != m_length) return 0;
while (pos < length) {
_samples[pos++] = 0;
}
#else
MapStorageGuard _map(*this);
sample_t *samples = _map.storage();
Q_ASSERT(samples);
if (!samples) return 0;
if (pos < length) {
memset(&(samples[pos]), 0, (length-pos)*sizeof(sample_t));
}
#endif
}
}
return length;
}
//***************************************************************************
unsigned int Stripe::append(const Kwave::SampleArray &samples,
unsigned int offset,
unsigned int count)
{
unsigned int old_length;
unsigned int appended = 0;
if (!count) return 0; // nothing to do
{
QMutexLocker lock(&m_lock_samples);
Q_ASSERT(offset + count <= samples.size());
if (offset + count > samples.size()) return 0;
// qDebug("Stripe::append: adding %d samples", count);
old_length = m_length;
unsigned int new_length = old_length + count;
Q_ASSERT(!m_map_count);
if (resizeStorage(new_length) != new_length)
return 0; // out of memory
// append to the end of the area
unsigned int cnt = new_length - old_length;
Q_ASSERT(!m_map_count);
appended = MemoryManager::instance().writeTo(m_storage,
old_length * sizeof(sample_t),
&(samples[offset]), cnt * sizeof(sample_t))
/ sizeof(sample_t);
}
// qDebug("Stripe::append(): resized to %d", m_length);
return appended;
}
//***************************************************************************
void Stripe::deleteRange(unsigned int offset, unsigned int length)
{
// qDebug(" Stripe::deleteRange(offset=%u, length=%u)", offset, length);
if (!length) return; // nothing to do
{
QMutexLocker lock(&m_lock_samples);
const unsigned int size = m_length;
if (!size) return;
unsigned int first = offset;
unsigned int last = offset + length - 1;
// qDebug(" Stripe::deleteRange, me=[%u ... %u] del=[%u ... %u]",
// m_start, m_start+size-1, m_start + first, m_start + last);
Q_ASSERT(first < size);
if (first >= size) return;
// put first/last into our area
if (last >= size) last = size - 1;
Q_ASSERT(last >= first);
if (last < first) return;
// move all samples after the deleted area to the left
unsigned int dst = first;
unsigned int src = last+1;
unsigned int len = size - src;
// qDebug(" Stripe: deleting %u ... %u", dst, src-1);
if (len) {
MappedArray _samples(*this, m_length);
Q_ASSERT(src + len <= size);
Q_ASSERT(dst + len <= size);
if (!_samples.copy(dst, src, len)) return;
}
// resize the buffer to it's new size
resizeStorage(size - length);
}
}
//***************************************************************************
void Stripe::overwrite(unsigned int offset,
const Kwave::SampleArray &source,
unsigned int srcoff, unsigned int srclen)
{
QMutexLocker lock(&m_lock_samples);
Q_ASSERT(!m_map_count);
MemoryManager::instance().writeTo(m_storage,
offset * sizeof(sample_t),
&(source[srcoff]), srclen * sizeof(sample_t));
}
//***************************************************************************
unsigned int Stripe::read(Kwave::SampleArray &buffer, unsigned int dstoff,
unsigned int offset, unsigned int length)
{
Q_ASSERT(length);
if (!length) return 0; // nothing to do !?
QMutexLocker lock(&m_lock_samples);
// for (unsigned int x=dstoff; (dstoff+x < length) && (x < buffer.size()); x++)
// buffer[x] = -(SAMPLE_MAX >> 2);
// qDebug("Stripe::read(), me=[%u ... %u] (size=%u), offset=%u, length=%u",
// m_start, m_start+m_length-1, m_length, offset, length);
Q_ASSERT(offset < m_length);
if (offset >= m_length) return 0;
if (offset+length > m_length) length = m_length - offset;
Q_ASSERT(length);
// if (!length) qDebug("--- [%u ... %u] (%u), offset=%u",
// m_start, m_start+m_length-1, m_length, offset);
if (!length) return 0;
// read directly through the memory manager, fastest path
Q_ASSERT(!m_map_count);
length = MemoryManager::instance().readFrom(m_storage,
offset * sizeof(sample_t),
&buffer[dstoff], length * sizeof(sample_t)) / sizeof(sample_t);
// qDebug("read done, length=%u", length);
return length;
}
//***************************************************************************
void Stripe::minMax(unsigned int first, unsigned int last,
sample_t &min, sample_t &max)
{
QMutexLocker lock(&m_lock_samples);
min = 0;
max = 0;
MapStorageGuard _samples(*this);
sample_t *buffer = _samples.storage();
Q_ASSERT(buffer);
if (!buffer) return;
// loop over the mapped storage to get min/max
register sample_t lo = SAMPLE_MAX;
register sample_t hi = SAMPLE_MIN;
Q_ASSERT(first < m_length);
Q_ASSERT(first <= last);
Q_ASSERT(last < m_length);
buffer += first;
while (first++ <= last) {
register sample_t s = *(buffer++);
if (s < lo) lo = s;
if (s > hi) hi = s;
}
min = lo;
max = hi;
}
//***************************************************************************
Stripe &Stripe::operator << (const Kwave::SampleArray &samples)
{
unsigned int appended = append(samples, 0, samples.size());
Q_ASSERT(appended == samples.size());
Q_UNUSED(appended);
return *this;
}
//***************************************************************************
sample_t *Stripe::mapStorage()
{
// qDebug(" Stripe::mapStorage()");
if (!m_map_count) {
MemoryManager &mem = MemoryManager::instance();
m_mapped_storage = reinterpret_cast<sample_t*>(mem.map(m_storage));
}
m_map_count++;
return m_mapped_storage;
}
//***************************************************************************
void Stripe::unmapStorage()
{
// qDebug(" Stripe::unmapStorage()");
Q_ASSERT(m_map_count);
if (!m_map_count) return;
m_map_count--;
if (!m_map_count) {
MemoryManager &mem = MemoryManager::instance();
mem.unmap(m_storage);
m_mapped_storage = 0;
}
}
//***************************************************************************
#include "Stripe.moc"
//***************************************************************************
//***************************************************************************