1
/* $Id: resamplesubs.c 3085 2010-02-01 11:23:54Z nanang $ */
3
* Digital Audio Resampling Home Page located at
4
* http://www-ccrma.stanford.edu/~jos/resample/.
6
* SOFTWARE FOR SAMPLING-RATE CONVERSION AND FIR DIGITAL FILTER DESIGN
8
* Snippet from the resample.1 man page:
12
* The first version of this software was written by Julius O. Smith III
13
* <jos@ccrma.stanford.edu> at CCRMA <http://www-ccrma.stanford.edu> in
14
* 1981. It was called SRCONV and was written in SAIL for PDP-10
15
* compatible machines. The algorithm was first published in
17
* Smith, Julius O. and Phil Gossett. ``A Flexible Sampling-Rate
18
* Conversion Method,'' Proceedings (2): 19.4.1-19.4.4, IEEE Conference
19
* on Acoustics, Speech, and Signal Processing, San Diego, March 1984.
21
* An expanded tutorial based on this paper is available at the Digital
22
* Audio Resampling Home Page given above.
24
* Circa 1988, the SRCONV program was translated from SAIL to C by
25
* Christopher Lee Fraley working with Roger Dannenberg at CMU.
27
* Since then, the C version has been maintained by jos.
29
* Sndlib support was added 6/99 by John Gibson <jgg9c@virginia.edu>.
31
* The resample program is free software distributed in accordance
32
* with the Lesser GNU Public License (LGPL). There is NO warranty; not
33
* even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
36
/* PJMEDIA modification:
37
* - remove resample(), just use SrcUp, SrcUD, and SrcLinear directly.
38
* - move FilterUp() and FilterUD() from filterkit.c
39
* - move stddefs.h and resample.h to this file.
40
* - const correctness.
43
#include <resamplesubs.h>
50
# pragma warning(push, 3)
51
//# pragma warning(disable: 4245) // Conversion from uint to ushort
52
# pragma warning(disable: 4244) // Conversion from double to uint
53
# pragma warning(disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
54
# pragma warning(disable: 4761) // integral size mismatch in argument; conversion supplied
57
#if defined(RESAMPLE_HAS_SMALL_FILTER) && RESAMPLE_HAS_SMALL_FILTER!=0
58
# include "smallfilter.h"
60
# define SMALL_FILTER_NMULT 0
61
# define SMALL_FILTER_SCALE 0
62
# define SMALL_FILTER_NWING 0
63
# define SMALL_FILTER_IMP NULL
64
# define SMALL_FILTER_IMPD NULL
67
#if defined(RESAMPLE_HAS_LARGE_FILTER) && RESAMPLE_HAS_LARGE_FILTER!=0
68
# include "largefilter.h"
70
# define LARGE_FILTER_NMULT 0
71
# define LARGE_FILTER_SCALE 0
72
# define LARGE_FILTER_NWING 0
73
# define LARGE_FILTER_IMP NULL
74
# define LARGE_FILTER_IMPD NULL
87
static INLINE RES_HWORD WordToHword(RES_WORD v, int scl)
90
RES_WORD llsb = (1<<(scl-1));
91
v += llsb; /* round */
95
} else if (v < MIN_HWORD) {
102
/* Sampling rate conversion using linear interpolation for maximum speed.
105
SrcLinear(const RES_HWORD X[], RES_HWORD Y[], double pFactor, RES_UHWORD nx)
110
RES_HWORD *Ystart, *Yend;
113
double dt; /* Step through input signal */
114
RES_UWORD dtb; /* Fixed-point version of Dt */
115
RES_UWORD endTime; /* When time reaches EndTime, return to user */
117
dt = 1.0/pFactor; /* Output sampling period */
118
dtb = dt*(1<<Np) + 0.5; /* Fixed-point representation */
121
Yend = Ystart + (unsigned)(nx * pFactor + 0.5);
122
endTime = time + (1<<Np)*(RES_WORD)nx;
124
// Integer round down in dtb calculation may cause (endTime % dtb > 0),
125
// so it may cause resample write pass the output buffer (Y >= Yend).
126
// while (time < endTime)
129
iconst = (time) & Pmask;
130
xp = &X[(time)>>Np]; /* Ptr to current input sample */
133
x1 *= ((1<<Np)-iconst);
136
*Y++ = WordToHword(v,Np); /* Deposit output */
137
time += dtb; /* Move to next sample by time increment */
139
return (Y - Ystart); /* Return number of output samples */
142
static RES_WORD FilterUp(const RES_HWORD Imp[], const RES_HWORD ImpD[],
143
RES_UHWORD Nwing, RES_BOOL Interp,
144
const RES_HWORD *Xp, RES_HWORD Ph, RES_HWORD Inc)
147
const RES_HWORD *Hdp = NULL;
148
const RES_HWORD *End;
159
if (Inc == 1) /* If doing right wing... */
160
{ /* ...drop extra coeff, so when Ph is */
161
End--; /* 0.5, we don't do too many mult's */
162
if (Ph == 0) /* If the phase is zero... */
163
{ /* ...then we've already skipped the */
164
Hp += Npc; /* first sample, so we must also */
165
Hdp += Npc; /* skip ahead in Imp[] and ImpD[] */
170
t = *Hp; /* Get filter coeff */
171
t += (((RES_WORD)*Hdp)*a)>>Na; /* t is now interp'd filter coeff */
172
Hdp += Npc; /* Filter coeff differences step */
173
t *= *Xp; /* Mult coeff by input sample */
174
if (t & (1<<(Nhxn-1))) /* Round, if needed */
176
t >>= Nhxn; /* Leave some guard bits, but come back some */
177
v += t; /* The filter output */
178
Hp += Npc; /* Filter coeff step */
180
Xp += Inc; /* Input signal step. NO CHECK ON BOUNDS */
184
t = *Hp; /* Get filter coeff */
185
t *= *Xp; /* Mult coeff by input sample */
186
if (t & (1<<(Nhxn-1))) /* Round, if needed */
188
t >>= Nhxn; /* Leave some guard bits, but come back some */
189
v += t; /* The filter output */
190
Hp += Npc; /* Filter coeff step */
191
Xp += Inc; /* Input signal step. NO CHECK ON BOUNDS */
197
static RES_WORD FilterUD(const RES_HWORD Imp[], const RES_HWORD ImpD[],
198
RES_UHWORD Nwing, RES_BOOL Interp,
199
const RES_HWORD *Xp, RES_HWORD Ph, RES_HWORD Inc, RES_UHWORD dhb)
202
const RES_HWORD *Hp, *Hdp, *End;
207
Ho = (Ph*(RES_UWORD)dhb)>>Np;
209
if (Inc == 1) /* If doing right wing... */
210
{ /* ...drop extra coeff, so when Ph is */
211
End--; /* 0.5, we don't do too many mult's */
212
if (Ph == 0) /* If the phase is zero... */
213
Ho += dhb; /* ...then we've already skipped the */
214
} /* first sample, so we must also */
215
/* skip ahead in Imp[] and ImpD[] */
217
while ((Hp = &Imp[Ho>>Na]) < End) {
218
t = *Hp; /* Get IR sample */
219
Hdp = &ImpD[Ho>>Na]; /* get interp (lower Na) bits from diff table*/
220
a = Ho & Amask; /* a is logically between 0 and 1 */
221
t += (((RES_WORD)*Hdp)*a)>>Na; /* t is now interp'd filter coeff */
222
t *= *Xp; /* Mult coeff by input sample */
223
if (t & 1<<(Nhxn-1)) /* Round, if needed */
225
t >>= Nhxn; /* Leave some guard bits, but come back some */
226
v += t; /* The filter output */
227
Ho += dhb; /* IR step */
228
Xp += Inc; /* Input signal step. NO CHECK ON BOUNDS */
231
while ((Hp = &Imp[Ho>>Na]) < End) {
232
t = *Hp; /* Get IR sample */
233
t *= *Xp; /* Mult coeff by input sample */
234
if (t & 1<<(Nhxn-1)) /* Round, if needed */
236
t >>= Nhxn; /* Leave some guard bits, but come back some */
237
v += t; /* The filter output */
238
Ho += dhb; /* IR step */
239
Xp += Inc; /* Input signal step. NO CHECK ON BOUNDS */
244
/* Sampling rate up-conversion only subroutine;
245
* Slightly faster than down-conversion;
247
static int SrcUp(const RES_HWORD X[], RES_HWORD Y[], double pFactor,
248
RES_UHWORD nx, RES_UHWORD pNwing, RES_UHWORD pLpScl,
249
const RES_HWORD pImp[], const RES_HWORD pImpD[], RES_BOOL Interp)
252
RES_HWORD *Ystart, *Yend;
255
double dt; /* Step through input signal */
256
RES_UWORD dtb; /* Fixed-point version of Dt */
258
RES_UWORD endTime; /* When time reaches EndTime, return to user */
260
dt = 1.0/pFactor; /* Output sampling period */
261
dtb = dt*(1<<Np) + 0.5; /* Fixed-point representation */
264
Yend = Ystart + (unsigned)(nx * pFactor + 0.5);
265
endTime = time + (1<<Np)*(RES_WORD)nx;
267
// Integer round down in dtb calculation may cause (endTime % dtb > 0),
268
// so it may cause resample write pass the output buffer (Y >= Yend).
269
// while (time < endTime)
272
xp = &X[time>>Np]; /* Ptr to current input sample */
273
/* Perform left-wing inner product */
275
v = FilterUp(pImp, pImpD, pNwing, Interp, xp, (RES_HWORD)(time&Pmask),-1);
277
/* Perform right-wing inner product */
278
v += FilterUp(pImp, pImpD, pNwing, Interp, xp+1, (RES_HWORD)((-time)&Pmask),1);
280
v >>= Nhg; /* Make guard bits */
281
v *= pLpScl; /* Normalize for unity filter gain */
282
*Y++ = WordToHword(v,NLpScl); /* strip guard bits, deposit output */
283
time += dtb; /* Move to next sample by time increment */
285
return (Y - Ystart); /* Return the number of output samples */
289
/* Sampling rate conversion subroutine */
291
static int SrcUD(const RES_HWORD X[], RES_HWORD Y[], double pFactor,
292
RES_UHWORD nx, RES_UHWORD pNwing, RES_UHWORD pLpScl,
293
const RES_HWORD pImp[], const RES_HWORD pImpD[], RES_BOOL Interp)
296
RES_HWORD *Ystart, *Yend;
299
double dh; /* Step through filter impulse response */
300
double dt; /* Step through input signal */
302
RES_UWORD endTime; /* When time reaches EndTime, return to user */
303
RES_UWORD dhb, dtb; /* Fixed-point versions of Dh,Dt */
305
dt = 1.0/pFactor; /* Output sampling period */
306
dtb = dt*(1<<Np) + 0.5; /* Fixed-point representation */
308
dh = MIN(Npc, pFactor*Npc); /* Filter sampling period */
309
dhb = dh*(1<<Na) + 0.5; /* Fixed-point representation */
312
Yend = Ystart + (unsigned)(nx * pFactor + 0.5);
313
endTime = time + (1<<Np)*(RES_WORD)nx;
315
// Integer round down in dtb calculation may cause (endTime % dtb > 0),
316
// so it may cause resample write pass the output buffer (Y >= Yend).
317
// while (time < endTime)
320
xp = &X[time>>Np]; /* Ptr to current input sample */
321
v = FilterUD(pImp, pImpD, pNwing, Interp, xp, (RES_HWORD)(time&Pmask),
322
-1, dhb); /* Perform left-wing inner product */
323
v += FilterUD(pImp, pImpD, pNwing, Interp, xp+1, (RES_HWORD)((-time)&Pmask),
324
1, dhb); /* Perform right-wing inner product */
325
v >>= Nhg; /* Make guard bits */
326
v *= pLpScl; /* Normalize for unity filter gain */
327
*Y++ = WordToHword(v,NLpScl); /* strip guard bits, deposit output */
328
time += dtb; /* Move to next sample by time increment */
330
return (Y - Ystart); /* Return the number of output samples */
334
DECL(int) res_SrcLinear(const RES_HWORD X[], RES_HWORD Y[],
335
double pFactor, RES_UHWORD nx)
337
return SrcLinear(X, Y, pFactor, nx);
340
DECL(int) res_Resample(const RES_HWORD X[], RES_HWORD Y[], double pFactor,
341
RES_UHWORD nx, RES_BOOL LargeF, RES_BOOL Interp)
346
return SrcUp(X, Y, pFactor, nx,
347
LARGE_FILTER_NWING, LARGE_FILTER_SCALE,
348
LARGE_FILTER_IMP, LARGE_FILTER_IMPD, Interp);
350
return SrcUp(X, Y, pFactor, nx,
351
SMALL_FILTER_NWING, SMALL_FILTER_SCALE,
352
SMALL_FILTER_IMP, SMALL_FILTER_IMPD, Interp);
357
return SrcUD(X, Y, pFactor, nx,
358
LARGE_FILTER_NWING, LARGE_FILTER_SCALE * pFactor + 0.5,
359
LARGE_FILTER_IMP, LARGE_FILTER_IMPD, Interp);
361
return SrcUD(X, Y, pFactor, nx,
362
SMALL_FILTER_NWING, SMALL_FILTER_SCALE * pFactor + 0.5,
363
SMALL_FILTER_IMP, SMALL_FILTER_IMPD, Interp);
368
DECL(int) res_GetXOFF(double pFactor, RES_BOOL LargeF)
371
return (LARGE_FILTER_NMULT + 1) / 2.0 *
372
MAX(1.0, 1.0/pFactor);
374
return (SMALL_FILTER_NMULT + 1) / 2.0 *
375
MAX(1.0, 1.0/pFactor);