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
|
/*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* Copyright 2009-2011 Jörg Hermann Müller
*
* This file is part of AudaSpace.
*
* Audaspace 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.
*
* AudaSpace is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Audaspace; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file audaspace/intern/AUD_JOSResampleReader.cpp
* \ingroup audaspaceintern
*/
#include "AUD_JOSResampleReader.h"
#include "AUD_JOSResampleReaderCoeff.cpp"
#include <cmath>
#include <cstring>
#include <iostream>
/* MSVC does not have lrint */
#ifdef _MSC_VER
#ifdef _M_X64
#include <emmintrin.h>
static inline int lrint(double d)
{
return _mm_cvtsd_si32(_mm_load_sd(&d));
}
#else
static inline int lrint(double d)
{
int i;
_asm{
fld d
fistp i
};
return i;
}
#endif
#endif
#define CC m_channels + channel
#define AUD_RATE_MAX 256
#define SHIFT_BITS 12
#define double_to_fp(x) (lrint(x * double(1 << SHIFT_BITS)))
#define int_to_fp(x) (x << SHIFT_BITS)
#define fp_to_int(x) (x >> SHIFT_BITS)
#define fp_to_double(x) (x * 1.0/(1 << SHIFT_BITS))
#define fp_rest(x) (x & ((1 << SHIFT_BITS) - 1))
#define fp_rest_to_double(x) fp_to_double(fp_rest(x))
AUD_JOSResampleReader::AUD_JOSResampleReader(AUD_Reference<AUD_IReader> reader, AUD_Specs specs) :
AUD_ResampleReader(reader, specs.rate),
m_channels(AUD_CHANNELS_INVALID),
m_n(0),
m_P(0),
m_cache_valid(0),
m_last_factor(0)
{
}
void AUD_JOSResampleReader::reset()
{
m_cache_valid = 0;
m_n = 0;
m_P = 0;
m_last_factor = 0;
}
void AUD_JOSResampleReader::updateBuffer(int size, double factor, int samplesize)
{
unsigned int len;
double num_samples = double(m_len) / double(m_L);
// first calculate what length we need right now
if(factor >= 1)
len = ceil(num_samples);
else
len = (unsigned int)(ceil(num_samples / factor));
// then check if afterwards the length is enough for the maximum rate
if(len + size < num_samples * AUD_RATE_MAX)
len = size - num_samples * AUD_RATE_MAX;
if(m_n > len)
{
sample_t* buf = m_buffer.getBuffer();
len = m_n - len;
memmove(buf, buf + len * m_channels, (m_cache_valid - len) * samplesize);
m_n -= len;
m_cache_valid -= len;
}
m_buffer.assureSize((m_cache_valid + size) * samplesize, true);
}
#define RESAMPLE_METHOD(name, left, right) void AUD_JOSResampleReader::name(double target_factor, int length, sample_t* buffer)\
{\
sample_t* buf = m_buffer.getBuffer();\
\
int P, l, end, channel, i;\
double eta, v, f_increment, factor;\
\
m_sums.assureSize(m_channels * sizeof(double));\
double* sums = reinterpret_cast<double*>(m_sums.getBuffer());\
sample_t* data;\
const float* coeff = m_coeff;\
\
unsigned int P_increment;\
\
for(unsigned int t = 0; t < length; t++)\
{\
factor = (m_last_factor * (length - t) + target_factor * t) / length;\
\
if(factor >= 1)\
f_increment = m_L;\
else\
f_increment = factor * m_L;\
\
P_increment = double_to_fp(f_increment);\
P = double_to_fp(m_P * f_increment);\
\
end = (int_to_fp(m_len) - P) / P_increment - 1;\
if(m_n < end)\
end = m_n;\
\
memset(sums, 0, sizeof(double) * m_channels);\
\
P += P_increment * end;\
data = buf + (m_n - end) * m_channels;\
l = fp_to_int(P);\
\
for(i = 0; i <= end; i++)\
{\
eta = fp_rest_to_double(P);\
v = coeff[l] + eta * (coeff[l+1] - coeff[l]);\
P -= P_increment;\
l = fp_to_int(P);\
left\
}\
\
P = -P;\
\
end = (int_to_fp(m_len) - P) / P_increment - 1;\
if(m_cache_valid - m_n - 2 < end)\
end = m_cache_valid - m_n - 2;\
\
P += P_increment * end;\
data = buf + (m_n + 2 + end) * m_channels - 1;\
l = fp_to_int(P);\
\
for(i = 0; i <= end; i++)\
{\
eta = fp_rest_to_double(P);\
v = coeff[l] + eta * (coeff[l+1] - coeff[l]);\
P -= P_increment;\
l = fp_to_int(P);\
right\
}\
\
for(channel = 0; channel < m_channels; channel++)\
{\
*buffer = f_increment / m_L * sums[channel];\
buffer++;\
}\
\
m_P += fmod(1.0 / factor, 1.0);\
m_n += floor(1.0 / factor);\
\
if(m_P >= 1.0)\
{\
m_P -= 1.0;\
m_n++;\
}\
}\
}
RESAMPLE_METHOD(resample, {
channel = 0;
do
{
sums[channel] += *data * v;
channel++;
data++;
}
while(channel < m_channels);
}, {
channel = m_channels;
do
{
channel--;
sums[channel] += *data * v;
data--;
}
while(channel);
})
RESAMPLE_METHOD(resample_mono, {
*sums += *data * v;
data++;
}, {
*sums += *data * v;
data--;
})
RESAMPLE_METHOD(resample_stereo, {
sums[0] += data[0] * v;
sums[1] += data[1] * v;
data+=2;
}, {
data-=2;
sums[0] += data[1] * v;
sums[1] += data[2] * v;
})
void AUD_JOSResampleReader::seek(int position)
{
position = floor(position * double(m_reader->getSpecs().rate) / double(m_rate));
m_reader->seek(position);
reset();
}
int AUD_JOSResampleReader::getLength() const
{
return floor(m_reader->getLength() * double(m_rate) / double(m_reader->getSpecs().rate));
}
int AUD_JOSResampleReader::getPosition() const
{
return floor((m_reader->getPosition() + double(m_P))
* m_rate / m_reader->getSpecs().rate);
}
AUD_Specs AUD_JOSResampleReader::getSpecs() const
{
AUD_Specs specs = m_reader->getSpecs();
specs.rate = m_rate;
return specs;
}
void AUD_JOSResampleReader::read(int& length, bool& eos, sample_t* buffer)
{
if(length == 0)
return;
AUD_Specs specs = m_reader->getSpecs();
int samplesize = AUD_SAMPLE_SIZE(specs);
double target_factor = double(m_rate) / double(specs.rate);
eos = false;
int len;
double num_samples = double(m_len) / double(m_L);
// check for channels changed
if(specs.channels != m_channels)
{
m_channels = specs.channels;
reset();
switch(m_channels)
{
case AUD_CHANNELS_MONO:
m_resample = &AUD_JOSResampleReader::resample_mono;
break;
case AUD_CHANNELS_STEREO:
m_resample = &AUD_JOSResampleReader::resample_stereo;
break;
default:
m_resample = &AUD_JOSResampleReader::resample;
break;
}
}
if(m_last_factor == 0)
m_last_factor = target_factor;
if(target_factor == 1 && m_last_factor == 1 && (m_P == 0))
{
// can read directly!
len = length - (m_cache_valid - m_n);
updateBuffer(len, target_factor, samplesize);
sample_t* buf = m_buffer.getBuffer();
m_reader->read(len, eos, buf + m_cache_valid * m_channels);
m_cache_valid += len;
length = m_cache_valid - m_n;
if(length > 0)
{
memcpy(buffer, buf + m_n * m_channels, length * samplesize);
m_n += length;
}
return;
}
// use minimum for the following calculations
double factor = AUD_MIN(target_factor, m_last_factor);
if(factor >= 1)
len = (m_n - m_cache_valid) + int(ceil(length / factor)) + ceil(num_samples);
else
len = (m_n - m_cache_valid) + int(ceil(length / factor) + ceil(num_samples / factor));
if(len > 0)
{
int should = len;
updateBuffer(len, factor, samplesize);
m_reader->read(len, eos, m_buffer.getBuffer() + m_cache_valid * m_channels);
m_cache_valid += len;
if(len < should)
{
if(len == 0 && eos)
length = 0;
else
{
// use maximum for the following calculations
factor = AUD_MAX(target_factor, m_last_factor);
if(eos)
{
// end of stream, let's check how many more samples we can produce
len = floor((m_cache_valid - m_n) * factor);
if(len < length)
length = len;
}
else
{
// not enough data available yet, so we recalculate how many samples we can calculate
if(factor >= 1)
len = floor((num_samples + m_cache_valid - m_n) * factor);
else
len = floor((num_samples * factor + m_cache_valid - m_n) * factor);
if(len < length)
length = len;
}
}
}
}
(this->*m_resample)(target_factor, length, buffer);
m_last_factor = target_factor;
if(m_n > m_cache_valid)
{
m_n = m_cache_valid;
}
eos = eos && ((m_n == m_cache_valid) || (length == 0));
}
|