[3e4acc]: libkwave / KwaveSampleArray.cpp Maximize Restore History

Download this file

KwaveSampleArray.cpp    175 lines (152 with data), 5.2 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
/*************************************************************************
KwaveSampleArray.cpp - array with Kwave's internal sample_t
-------------------
begin : Wed Jan 02 2008
copyright : (C) 2008 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 "config.h"
#include <stdlib.h>
#include "memcpy.h"
#include "KwaveSampleArray.h"
//***************************************************************************
Kwave::SampleArray::SampleArray()
{
m_storage = new SampleStorage;
}
//***************************************************************************
Kwave::SampleArray::SampleArray(unsigned int size)
{
m_storage = new SampleStorage;
resize(size);
}
//***************************************************************************
Kwave::SampleArray::~SampleArray()
{
}
//***************************************************************************
void Kwave::SampleArray::setRawData(sample_t *data, unsigned int size)
{
if (!m_storage) return;
resize(0);
m_storage->m_raw_data = data;
m_storage->m_size = size;
}
//***************************************************************************
void Kwave::SampleArray::resetRawData()
{
if (!m_storage) return;
m_storage->m_raw_data = 0;
m_storage->m_size = 0;
}
//***************************************************************************
void Kwave::SampleArray::fill(sample_t value)
{
if (!m_storage) return;
sample_t *p = data();
Q_ASSERT(p);
if (!p) return;
unsigned int count = m_storage->m_size;
Q_ASSERT(count);
while (count--) {
*p = value;
p++;
}
}
//***************************************************************************
sample_t & Kwave::SampleArray::operator [] (unsigned int index)
{
static sample_t dummy;
sample_t *p = data();
return (p) ? (*(p + index)) : dummy;
}
//***************************************************************************
const sample_t & Kwave::SampleArray::operator [] (unsigned int index) const
{
static const sample_t dummy = 0;
const sample_t *p = data();
return (p) ? (*(p + index)) : dummy;
}
//***************************************************************************
void Kwave::SampleArray::resize(unsigned int size)
{
if (!m_storage) return;
if (size == m_storage->m_size) return;
Q_ASSERT(m_storage->m_raw_data == 0);
m_storage->resize(size);
}
//***************************************************************************
unsigned int Kwave::SampleArray::size() const
{
return m_storage->m_size;
}
//***************************************************************************
Kwave::SampleArray::SampleStorage::SampleStorage()
:QSharedData()
{
m_size = 0;
m_data = 0;
m_raw_data = 0;
}
//***************************************************************************
Kwave::SampleArray::SampleStorage::SampleStorage(const SampleStorage &other)
:QSharedData(other)
{
m_data = 0;
m_size = 0;
m_raw_data = 0;
Q_ASSERT(other.m_raw_data == 0);
if (other.m_size) {
m_data = static_cast<sample_t *>(
::malloc(other.m_size * sizeof(sample_t))
);
if (m_data) {
m_size = other.m_size;
MEMCPY(m_data,
(other.m_raw_data) ? other.m_raw_data : other.m_data,
m_size * sizeof(sample_t)
);
}
}
}
//***************************************************************************
Kwave::SampleArray::SampleStorage::~SampleStorage()
{
Q_ASSERT(m_raw_data == 0);
if (m_data) ::free(m_data);
}
//***************************************************************************
void Kwave::SampleArray::SampleStorage::resize(unsigned int size)
{
Q_ASSERT(m_raw_data == 0);
if (size) {
// resize using realloc, keep existing data
sample_t *new_data = static_cast<sample_t *>(
::realloc(m_data, size * sizeof(sample_t)));
if (new_data) {
// successful
// NOTE: if we grew, the additional memory is *not* initialized!
m_data = new_data;
m_size = size;
} else {
qWarning("OOM in Kwave::SampleArray::SampleStorage::resize(%u)",
size);
}
} else {
// resize to zero == delete/free memory
Q_ASSERT(m_data);
::free(m_data);
m_data = 0;
m_size = 0;
}
}
//***************************************************************************
//***************************************************************************