[3e4acc]: libkwave / Stripe.h Maximize Restore History

Download this file

Stripe.h    326 lines (263 with data), 9.8 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
/***************************************************************************
Stripe.h - continuous block of samples
-------------------
begin : Feb 09 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. *
* *
***************************************************************************/
#ifndef _STRIPE_H_
#define _STRIPE_H_
#include "config.h"
#include <QMutex>
#include <QSharedData>
#include <QExplicitlySharedDataPointer>
#include <kdemacros.h>
#include "libkwave/KwaveSampleArray.h"
#include "libkwave/MemoryManager.h"
#include "libkwave/Sample.h"
//***************************************************************************
class KDE_EXPORT Stripe
{
public:
/**
* Default constructor. Creates an empty stripe with zero-length.
*/
Stripe();
/** Copy constructor */
Stripe(const Stripe &other);
/**
* Constructor. Creates a new zero-length stripe.
* @param start position within the track
*/
Stripe(sample_index_t start);
/**
* Constructor. Creates a stripe that already contains samples,
* copied from a buffer with samples.
*
* @param start position within the track
* @param samples array with initial samples
*/
Stripe(sample_index_t start, const Kwave::SampleArray& samples);
/**
* Constructor. Creates a stripe that already contains samples,
* copied from another stripe with offset.
*
* @param start position within the track
* @param stripe source stripe to copy from
* @param offset offset within the source stripe
*/
Stripe(sample_index_t start, Stripe& stripe, unsigned int offset);
/**
* Destructor.
*/
virtual ~Stripe();
/**
* Returns the start position of the stripe within the track.
*/
sample_index_t start() const;
/**
* Sets a new start position for the stripe
*/
void setStart(sample_index_t start);
/**
* Returns the current length of the stripe in samples.
*/
unsigned int length() const;
/**
* Returns the position of the last sample of the stripe,
* same as (start() + length() ? (length() - 1))
*/
sample_index_t end() const;
/**
* Resizes the stripe to a new number of samples. If the array
* size is reduced, samples from the end are thrown away. If
* the size is increased, samples with zero value will be added
* to the end.
* @param length new length of the array [samples]
* @param initialize if true, initialize all new areas with
* zeroes if the size has been increased
* (default = true)
* @return new length [samples]
*/
unsigned int resize(unsigned int length, bool initialize = true);
/**
* Appends an array of samples to the end of the stripe.
* @param samples array with the samples
* @param offset the offset within the array
* @param count number of samples in the array
* @return number of samples appended
*/
unsigned int append(const Kwave::SampleArray &samples,
unsigned int offset,
unsigned int count);
/**
* Deletes a range of samples
* @param offset index of the first sample, relative to the start of
* the stripe [0...length()-1]
* @param length number of samples
*/
void deleteRange(unsigned int offset, unsigned int length);
/**
* Copies the content of an array of samples into the stripe.
* @param offset the offset within the stripe (target)
* @param source array of samples to be copied
* @param srcoff offset within the source array
* @param srclen length of the data in the source array
* @warning this method is intended to be used only internally
* and lacks any error-checking in order to be fast!
*/
void overwrite(unsigned int offset, const Kwave::SampleArray &source,
unsigned int srcoff, unsigned int srclen);
/**
* Reads out samples from the stripe into a buffer
*
* @param buffer array for samples to be read (destination)
* @param dstoff offset within the destination buffer
* @param offset the offset within the stripe (source)
* @param length number of samples to read
* @return number of samples read
* @warning this method is intended to be used only internally
* and lacks any error-checking in order to be fast!
*/
unsigned int read(Kwave::SampleArray &buffer, unsigned int dstoff,
unsigned int offset, unsigned int length);
/**
* Returns the minumum and maximum sample value within a range
* of samples.
* @param first index of the first sample
* @param last index of the last sample
* @param min receives the lowest value (must be initialized)
* @param max receives the highest value (must be initialized)
*/
void minMax(unsigned int first, unsigned int last,
sample_t &min, sample_t &max);
/**
* Operator for appending an array of samples to the
* end of the stripe.
*/
Stripe &operator << (const Kwave::SampleArray &samples);
/** compare operator */
bool operator == (const Stripe &other) const;
/** assignment operator */
Stripe &operator = (const Stripe &other);
protected:
/**
* Resizes the internal storage.
* @param length the new length in samples
* @return the length after the resize operation. Should be equal
* to the length that has been given as parameter. If not,
* something has failed.
*/
unsigned int resizeStorage(unsigned int length);
private:
/**
* Wrapper for mapping the storage into memory and accessing
* it like a normal QMemArray<sample_t>. Should be used like
* a guard, internally uses a MapStorageGuard.
*/
class MappedArray: public Kwave::SampleArray
{
public:
/**
* Constructor
* @param stripe should be *this of the stripe
*/
MappedArray(Stripe &stripe);
/** Destructor */
virtual ~MappedArray();
/**
* Copy a portion of samples to another location,
* within the same storage.
*
* @param dst destination index [samples]
* @param src source index [samples]
* @param cnt number of samples
* @return cnt if succeeded or zero if the mapping has failed
* @note this is optimized for speed, no range checks!
*/
unsigned int copy(unsigned int dst, unsigned int src,
unsigned int cnt);
/**
* Copy a portion of samples from an array of samples.
*
* @param dst destination index [samples]
* @param source array with samples to copy from
* @param offset offset within the source array to start copy
* @param cnt number of samples
* @return cnt if succeeded or zero if the mapping has failed
* @note this is optimized for speed, no range checks!
*/
unsigned int copy(unsigned int dst,
const Kwave::SampleArray &source,
unsigned int offset, unsigned int cnt);
/**
* Read a portion of samples into an array of samples.
*
* @param buffer array for samples to be read (destination)
* @param dstoff offset within the destination buffer
* @param offset the offset within the stripe (source)
* @param length number of samples to read
* @return length if succeeded or zero if the mapping has failed
* @warning this method is intended to be used only internally
* and lacks any error-checking in order to be fast!
*/
unsigned int read(Kwave::SampleArray &buffer, unsigned int dstoff,
unsigned int offset, unsigned int length);
private:
/** stripe which gets it's storage mapped */
Stripe &m_stripe;
/** pointer to the memory used for storage */
sample_t *m_storage;
/** length in samples */
unsigned int m_length;
};
private:
/** maps the storage into memory */
sample_t *mapStorage();
/** unmaps the storage from memory */
void unmapStorage();
private:
class StripeStorage : public QSharedData {
public:
/** default constructor */
StripeStorage();
/** copy constructor */
StripeStorage(const StripeStorage &other);
/** destructor */
virtual ~StripeStorage();
/** maps the storage into memory */
sample_t *map();
/** unmaps the storage from memory */
void unmap();
/** returns the map count */
inline int mapCount() const { return m_map_count; }
public:
/** start position within the track */
sample_index_t m_start;
/** number of samples */
unsigned int m_length;
/** pointer/handle to a storage object */
Kwave::Handle m_storage;
private:
/** mutex for locking map/unmap */
QMutex m_lock;
/** usage count of mapped storage */
int m_map_count;
/** mapped storage */
sample_t *m_mapped_storage;
};
private:
/** mutex for locking map/unmap */
QMutex m_lock;
/** pointer to the shared data */
QExplicitlySharedDataPointer<StripeStorage> m_data;
};
#endif /* _STRIPE_H_ */