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
|
/***************************************************************************
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/Stripe.h"
#include "libkwave/Utils.h"
#include "libkwave/memcpy.h"
//***************************************************************************
//***************************************************************************
Kwave::Stripe::Stripe()
:m_lock(), m_start(0), m_data()
{
}
//***************************************************************************
Kwave::Stripe::Stripe(const Stripe &other)
:m_lock(), m_start(other.m_start), m_data(other.m_data)
{
}
//***************************************************************************
Kwave::Stripe::Stripe(Stripe &&other)
:m_lock(), m_start(other.m_start), m_data(other.m_data)
{
other.m_start = 0;
other.m_data.resize(0);
}
//***************************************************************************
Kwave::Stripe::Stripe(sample_index_t start)
:m_lock(), m_start(start), m_data()
{
}
//***************************************************************************
Kwave::Stripe::Stripe(sample_index_t start, const Kwave::SampleArray &samples)
:m_lock(), m_start(start), m_data(samples)
{
}
//***************************************************************************
Kwave::Stripe::Stripe(sample_index_t start,
Kwave::Stripe &stripe,
unsigned int offset)
:m_lock(), m_start(start), m_data()
{
Q_ASSERT(offset < stripe.length());
if (offset >= stripe.length()) return;
unsigned int length = stripe.length() - offset;
if (!resize(length)) return; // out of memory
const sample_t *src = stripe.m_data.constData();
sample_t *dst = m_data.data();
unsigned int len = length * sizeof(sample_t);
MEMCPY(dst, src + offset, len);
}
//***************************************************************************
Kwave::Stripe::~Stripe()
{
QMutexLocker lock(&m_lock);
}
//***************************************************************************
Kwave::Stripe & Kwave::Stripe::operator = (Kwave::Stripe &&other) noexcept
{
if (this != &other) {
m_start = other.m_start;
m_data = other.m_data;
other.m_start = 0;
other.m_data.resize(0);
}
return *this;
}
//***************************************************************************
sample_index_t Kwave::Stripe::start() const
{
return m_start;
}
//***************************************************************************
void Kwave::Stripe::setStart(sample_index_t start)
{
QMutexLocker lock(&m_lock);
m_start = start;
}
//***************************************************************************
unsigned int Kwave::Stripe::length() const
{
return m_data.size();
}
//***************************************************************************
sample_index_t Kwave::Stripe::end() const
{
const sample_index_t size = m_data.size();
return (size) ? (m_start + size - 1) : 0;
}
//***************************************************************************
unsigned int Kwave::Stripe::resize(unsigned int length)
{
QMutexLocker lock(&m_lock);
unsigned int old_length = m_data.size();
if (old_length == length) return old_length; // nothing to do
if (!m_data.resize(length)) {
qWarning("Stripe::resize(%u) failed, out of memory ?", length);
return m_data.size();
}
return length;
}
//***************************************************************************
unsigned int Kwave::Stripe::append(const Kwave::SampleArray &samples,
unsigned int offset,
unsigned int count)
{
if (!count) return 0; // nothing to do
Q_ASSERT(offset + count <= samples.size());
if (offset + count > samples.size()) return 0;
QMutexLocker lock(&m_lock);
unsigned int old_length = m_data.size();
unsigned int new_length = old_length + count;
if (!m_data.resize(new_length))
return 0; // out of memory
// append to the end of the area
unsigned int cnt = new_length - old_length;
MEMCPY(m_data.data() + old_length,
samples.constData() + offset,
cnt * sizeof(sample_t)
);
return cnt;
}
//***************************************************************************
void Kwave::Stripe::deleteRange(unsigned int offset, unsigned int length)
{
if (!length) return; // nothing to do
QMutexLocker lock(&m_lock);
const unsigned int size = m_data.size();
if (!size) return;
unsigned int first = offset;
unsigned int last = offset + length - 1;
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 src = last + 1;
unsigned int len = size - src;
if (len) {
unsigned int dst = first;
sample_t *p = m_data.data();
memmove(p + dst, p + src, len * sizeof(sample_t));
}
// resize the buffer to it's new size
m_data.resize(size - length);
}
//***************************************************************************
bool Kwave::Stripe::combine(unsigned int offset, Kwave::Stripe &other)
{
// resize the storage if necessary
const unsigned int combined_len = offset + other.length();
if (!resize(combined_len))
return false; // resizing failed, maybe OOM ?
// copy the data from the other stripe
QMutexLocker lock(&m_lock);
const sample_t *src = other.m_data.constData();
sample_t *dst = this->m_data.data();
unsigned int len = other.length() * sizeof(sample_t);
if (!src || !dst) return false; // src or dst does not exist
MEMCPY(dst + offset, src, len);
return true;
}
//***************************************************************************
void Kwave::Stripe::overwrite(unsigned int offset,
const Kwave::SampleArray &source,
unsigned int srcoff, unsigned int srclen)
{
QMutexLocker lock(&m_lock);
const sample_t *src = source.constData();
sample_t *dst = this->m_data.data();
unsigned int len = srclen * sizeof(sample_t);
MEMCPY(dst + offset, src + srcoff, len);
}
//***************************************************************************
unsigned int Kwave::Stripe::read(Kwave::SampleArray &buffer,
unsigned int dstoff,
unsigned int offset,
unsigned int length)
{
QMutexLocker lock(&m_lock);
if (!length || m_data.isEmpty()) return 0; // nothing to do !?
unsigned int current_len = m_data.size();
Q_ASSERT(offset < current_len);
if (offset >= current_len) return 0;
if ((offset + length) > current_len)
length = current_len - offset;
Q_ASSERT(length);
if (!length) return 0;
// directly memcpy
const sample_t *src = m_data.constData();
sample_t *dst = buffer.data();
unsigned int len = length * sizeof(sample_t);
MEMCPY(dst + dstoff, src + offset, len);
return length;
}
//***************************************************************************
void Kwave::Stripe::minMax(unsigned int first, unsigned int last,
sample_t &min, sample_t &max)
{
QMutexLocker lock(&m_lock);
if (m_data.isEmpty()) return;
const sample_t *buffer = m_data.constData();
if (!buffer) return;
// loop over the storage to get min/max
sample_t lo = min;
sample_t hi = max;
Q_ASSERT(first < m_data.size());
Q_ASSERT(first <= last);
Q_ASSERT(last < m_data.size());
buffer += first;
unsigned int remaining = last - first + 1;
// speedup: process a block of 8 samples at once, to allow loop unrolling
const unsigned int block = 8;
while (Q_LIKELY(remaining >= block)) {
for (unsigned int count = 0; Q_LIKELY(count < block); count++) {
sample_t s = *(buffer++);
if (Q_UNLIKELY(s < lo)) lo = s;
if (Q_UNLIKELY(s > hi)) hi = s;
}
remaining -= block;
}
while (Q_LIKELY(remaining)) {
sample_t s = *(buffer++);
if (Q_UNLIKELY(s < lo)) lo = s;
if (Q_UNLIKELY(s > hi)) hi = s;
remaining--;
}
min = lo;
max = hi;
}
//***************************************************************************
Kwave::Stripe &Kwave::Stripe::operator << (const Kwave::SampleArray &samples)
{
unsigned int appended = append(samples, 0, samples.size());
if (appended != samples.size()) {
qWarning("Stripe::operator << FAILED");
}
return *this;
}
//***************************************************************************
bool Kwave::Stripe::operator == (const Kwave::Stripe &other) const
{
return ((start() == other.start()) &&
(end() == other.end()));
}
//***************************************************************************
Kwave::Stripe &Kwave::Stripe::operator = (const Kwave::Stripe &other)
{
m_data = other.m_data;
return *this;
}
//***************************************************************************
//***************************************************************************
|