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
|
/***************************************************************************
SampleReader.cpp - stream for reading samples from a track
-------------------
begin : Apr 25 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 <QApplication>
#include "libkwave/Sample.h"
#include "libkwave/SampleReader.h"
#include "libkwave/Stripe.h"
#include "libkwave/Utils.h"
#include "libkwave/memcpy.h"
/** minimum time between emitting the "progress()" signal [ms] */
#define MIN_PROGRESS_INTERVAL 100
//***************************************************************************
Kwave::SampleReader::SampleReader(Kwave::ReaderMode mode,
Kwave::Stripe::List stripes)
:m_mode(mode), m_stripes(stripes),
m_src_position(stripes.left()), m_first(stripes.left()),
m_last(stripes.right()), m_buffer(blockSize()),
m_buffer_used(0), m_buffer_position(0),
m_progress_time(), m_last_seek_pos(stripes.right())
{
m_progress_time.start();
}
//***************************************************************************
Kwave::SampleReader::~SampleReader()
{
}
//***************************************************************************
void Kwave::SampleReader::reset()
{
m_src_position = m_first;
m_buffer_used = 0;
m_buffer_position = 0;
emit proceeded();
}
//***************************************************************************
static inline void padBuffer(Kwave::SampleArray &buffer,
unsigned int offset, unsigned int len)
{
memset((&buffer[offset]), 0x00,
len * sizeof(const_cast<const Kwave::SampleArray &>(buffer)[0]));
}
//***************************************************************************
void Kwave::SampleReader::fillBuffer()
{
Q_ASSERT(m_buffer_position >= m_buffer_used);
m_buffer_used = 0;
m_buffer_position = 0;
if (eof()) return;
if (m_buffer.isEmpty()) return; // we had a OOM before?
unsigned int rest = m_buffer.size();/* - m_buffer_used (is 0) */
if (m_src_position + rest > m_last)
rest = Kwave::toUint(m_last - m_src_position + 1);
Q_ASSERT(rest <= m_buffer.size());
if (rest > m_buffer.size()) rest = m_buffer.size();
Q_ASSERT(rest);
unsigned int len = readSamples(m_src_position, m_buffer, 0, rest);
Q_ASSERT(len == rest);
m_buffer_used += len;
// inform others that we proceeded
if (m_progress_time.elapsed() > MIN_PROGRESS_INTERVAL) {
m_progress_time.restart();
emit proceeded();
QApplication::sendPostedEvents();
}
}
//***************************************************************************
void Kwave::SampleReader::minMax(sample_index_t first, sample_index_t last,
sample_t &min, sample_t &max)
{
bool empty = true;
min = SAMPLE_MAX;
max = SAMPLE_MIN;
foreach (Kwave::Stripe s, m_stripes) {
if (!s.length()) continue;
sample_index_t start = s.start();
sample_index_t end = s.end();
if (end < first) continue; // not yet in range
if (start > last) break; // done
// overlap -> not empty
empty = false;
// get min/max from the stripe
unsigned int s1 = Kwave::toUint(
(first > start) ? (first - start) : 0);
unsigned int s2 = Kwave::toUint(
(last < end) ? (last - start) : (end - start));
s.minMax(s1, s2, min, max);
}
// special case: no signal in that range -> set to zero
if (empty) {
min = 0;
max = 0;
}
}
//***************************************************************************
unsigned int Kwave::SampleReader::read(Kwave::SampleArray &buffer,
unsigned int dstoff,
unsigned int length)
{
if (eof() || !length) return 0; // already done or nothing to do
// just a sanity check
Q_ASSERT(buffer.size());
Q_ASSERT(dstoff < buffer.size());
if (dstoff >= buffer.size()) return 0;
unsigned int count = 0;
unsigned int rest = length;
if (dstoff + rest > buffer.size()) rest = buffer.size() - dstoff;
Q_ASSERT(rest);
if (!rest) return 0;
// first try to read from the current buffer
if (m_buffer_position < m_buffer_used) {
unsigned int cnt = rest;
unsigned int src = m_buffer_position;
unsigned int dst = dstoff;
if (m_buffer_position + cnt > m_buffer_used)
cnt = m_buffer_used - m_buffer_position;
m_buffer_position += cnt;
count += cnt;
rest -= cnt;
dstoff += cnt;
qDebug("filling from buffer dstoff=%u, cnt=%u",dstoff,cnt);
const Kwave::SampleArray &in = m_buffer;
MEMCPY(&(buffer[dst]), &(in[src]), cnt * sizeof(sample_t));
if (m_buffer_position >= m_buffer_used) {
// buffer is empty now
m_buffer_position = m_buffer_used = 0;
}
if (!rest) {
// inform others that we proceeded
if (m_progress_time.elapsed() > MIN_PROGRESS_INTERVAL) {
m_progress_time.restart();
emit proceeded();
QApplication::sendPostedEvents();
}
return count; // done
}
}
// take the rest directly out of the stripe(s)
if (m_src_position + rest > (m_last + 1)) // clip to end of reader range
rest = Kwave::toUint((m_last + 1) - m_src_position);
if (dstoff + rest > buffer.size()) // clip to end of buffer
rest = buffer.size() - dstoff;
Q_ASSERT(dstoff + rest <= buffer.size());
unsigned int len = readSamples(m_src_position, buffer, dstoff, rest);
Q_ASSERT(len == rest);
count += len;
// inform others that we proceeded
if (m_progress_time.elapsed() > MIN_PROGRESS_INTERVAL) {
m_progress_time.restart();
emit proceeded();
QApplication::sendPostedEvents();
}
return count;
}
//***************************************************************************
void Kwave::SampleReader::skip(sample_index_t count)
{
if (m_buffer_position + count < m_buffer_used) {
// skip within the buffer
m_buffer_position += count;
} else {
// skip out of the buffer
count -= m_buffer_used;
m_src_position += count;
m_buffer_position = m_buffer_used = 0;
}
// if this reader is of "single pass forward only" type: remove all
// stripes that we have passed -> there is no way back!
if (m_mode == Kwave::SinglePassForward) {
while (!m_stripes.isEmpty() &&
(m_stripes.first().end() < m_src_position))
{
m_stripes.removeFirst();
}
}
}
//***************************************************************************
void Kwave::SampleReader::seek(sample_index_t pos)
{
const sample_index_t current_pos = m_src_position +
m_buffer_position - m_buffer_used;
if (pos == current_pos) return; // nothing to do
if (pos < current_pos) {
// if we are in SinglePassReverse mode, discard all stripes
// that we already have passed, up to the end
if (m_mode == Kwave::SinglePassReverse) {
while (!m_stripes.isEmpty() &&
(m_stripes.last().start() > m_last_seek_pos))
{
// qDebug("SampleReader: removing stripe [%9u ... %9u] (end=%9u)",
// m_stripes.last().start(),
// m_stripes.last().end(),
// m_last_seek_pos);
m_stripes.removeLast();
}
}
// seek backwards
const sample_index_t count = current_pos - pos;
if (count <= m_buffer_position) {
// go back within the buffer
m_buffer_position -= count;
} else {
// skip out of the buffer
m_src_position = pos;
m_buffer_position = m_buffer_used = 0;
}
} else {
// seek forward
skip(pos - current_pos);
}
m_last_seek_pos = m_src_position;
}
//***************************************************************************
Kwave::SampleReader &Kwave::SampleReader::operator >> (sample_t &sample)
{
// get new buffer if end of last buffer reached
if (m_buffer_position >= m_buffer_used) fillBuffer();
const Kwave::SampleArray &buffer = m_buffer;
sample = (m_buffer_position < m_buffer_used) ?
buffer[m_buffer_position++] : 0;
return *this;
}
//***************************************************************************
Kwave::SampleReader &Kwave::SampleReader::operator >> (
Kwave::SampleArray &buffer)
{
unsigned int size = buffer.size();
unsigned int count = read(buffer, 0, size);
if (count != size) {
bool ok = buffer.resize(count);
Q_ASSERT(ok); // shrinking should always be possible
if (!ok) {
qWarning("Kwave::SampleReader::operator >> - OOM?");
}
}
return *this;
}
//***************************************************************************
void Kwave::SampleReader::goOn()
{
Kwave::SampleArray buffer(blockSize());
(*this) >> buffer;
emit output(buffer);
}
//***************************************************************************
unsigned int Kwave::SampleReader::readSamples(sample_index_t offset,
Kwave::SampleArray &buffer,
unsigned int buf_offset,
unsigned int length)
{
Q_ASSERT(length);
if (!length) return 0; // nothing to do !?
Q_ASSERT(buf_offset + length <= buffer.size());
unsigned int rest = length;
sample_index_t left = offset;
sample_index_t right = offset + length - 1;
foreach (Kwave::Stripe s, m_stripes) {
if (!s.length()) continue;
sample_index_t start = s.start();
sample_index_t end = s.end();
if (left < start) {
// gap before the stripe -> pad
sample_index_t pad = Kwave::toUint(start - left);
if (pad > rest) pad = rest;
padBuffer(buffer, buf_offset, Kwave::toUint(pad));
buf_offset += pad;
rest -= pad;
left += pad;
if (!rest) break;
}
if (start > right) break; // done, we are after the range
if (left <= end) {
// some kind of overlap
Q_ASSERT(left >= start);
unsigned int ofs = Kwave::toUint(left - start);
unsigned int len = Kwave::toUint(end - left + 1);
if (len > rest) len = rest;
unsigned int count = s.read(buffer, buf_offset, ofs, len);
Q_ASSERT(count == len);
buf_offset += count;
rest -= count;
left += count;
if (!rest) break;
}
}
// pad at the end
if (rest) padBuffer(buffer, buf_offset, rest);
m_src_position += length;
// if this reader is of "single pass forward only" type: remove all
// stripes that we have passed -> there is no way back!
if (m_mode == Kwave::SinglePassForward) {
while (!m_stripes.isEmpty() &&
(m_stripes.first().end() < m_src_position))
{
m_stripes.removeFirst();
}
}
return length;
}
//***************************************************************************
//***************************************************************************
#include "moc_SampleReader.cpp"
|