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
|
/*
* Copyright: (C) 2000 Julius O. Smith
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Julius O. Smith jos@ccrma.stanford.edu
*
*/
/* This code was modified by Bruce Forsberg (forsberg@tns.net) to make it
into a C++ class
*/
#ifndef _AFLIBCONVERTER_H_
#define _AFLIBCONVERTER_H_
/*
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
*/
#ifndef MAX
#define MAX(x,y) ((x)>(y) ?(x):(y))
#endif
#ifndef MIN
#define MIN(x,y) ((x)<(y) ?(x):(y))
#endif
#define MAX_HWORD (32767)
#define MIN_HWORD (-32768)
#define IBUFFSIZE 4096 /* Input buffer size */
/*! \class aflibConverter
\brief Provides sample rate conversion.
This class will perform audio resampling. With the constructor you can choose the
type of resampling to be done. Simple linear interpolation can be done by setting
linear_interpolation to be TRUE in the constructor. The other two flags are
ignored if this is set. If linear_interpolation is FALSE then some form of filtering
will be done. IF high_quality is FALSE then a small filter will be performed.
If high_quality is TRUE then a large filter (higher quality) will be performed. For
both the small and large filters another parameter can be specified, filter_interpolation.
With filter_interpolation set then the filter coefficients used for both the small and
large filtering will be interpolated as well.
This class was designed to stream audio data. It also expects audio data as 16 bit values.
Each time a new stream is started some initialization needs to be done. Thus the function
initialize should be called to initialize everything. This initialize function will set
the conversion factor as well as a multiplecation factor for volume. The volume only
applies to the small and large filter. Since this filter uses a history of the audio data
it is possible for it to vary in amplitude. This allows users to scale the data. This
class will work on any number of channels. Once everything is specified then resample
should be called as many times as is necessary to process all the data. The value
inCount will be returned indicating how many inArray samples were actually used to
produce the output. This value can be used to indicate where the next block of
inArray data should start. The resample function is driven by the outCount value
specified. The inArray should contain at least:
outCount / factor + extra_samples.
extra_samples depends on the type of filtering done. As a rule of thumb 50 should be
adequate for any type of filter.
*/
class aflibData;
class aflibConverter {
public:
// Available contructors and destructors
aflibConverter (
bool high_quality,
bool linear_interpolation,
bool filter_interpolation);
~aflibConverter();
void
initialize(
double factor, /* factor = Sndout/Sndin */
int channels, /* number of sound channels */
double volume = 1.0); /* factor to multiply amplitude */
int
resample( /* number of output samples returned */
int& inCount, /* number of input samples to convert */
int outCount, /* number of output samples to compute */
short inArray[], /* input array data (length inCount * nChans) */
short outArray[]);/* output array data (length outCount * nChans) */
private:
aflibConverter();
aflibConverter(const aflibConverter& op);
const aflibConverter&
operator=(const aflibConverter& op);
int
err_ret(char *s);
void
deleteMemory();
int
readData(
int inCount, /* _total_ number of frames in input file */
short inArray[], /* input data */
short *outPtr[], /* array receiving chan samps */
int dataArraySize, /* size of these arrays */
int Xoff, /* read into input array starting at this index */
bool init_count);
inline short
WordToHword(int v, int scl)
{
short out;
int llsb = (1<<(scl-1));
v += llsb; /* round */
v >>= scl;
if (v>MAX_HWORD) {
v = MAX_HWORD;
} else if (v < MIN_HWORD) {
v = MIN_HWORD;
}
out = (short) v;
return out;
};
int
SrcLinear(
short X[],
short Y[],
double factor,
unsigned int *Time,
unsigned short& Nx,
unsigned short Nout);
int
SrcUp(
short X[],
short Y[],
double factor,
unsigned int *Time,
unsigned short& Nx,
unsigned short Nout,
unsigned short Nwing,
unsigned short LpScl,
short Imp[],
short ImpD[],
bool Interp);
int
SrcUD(
short X[],
short Y[],
double factor,
unsigned int *Time,
unsigned short& Nx,
unsigned short Nout,
unsigned short Nwing,
unsigned short LpScl,
short Imp[],
short ImpD[],
bool Interp);
int
FilterUp(
short Imp[],
short ImpD[],
unsigned short Nwing,
bool Interp,
short *Xp,
short Ph,
short Inc);
int
FilterUD(
short Imp[],
short ImpD[],
unsigned short Nwing,
bool Interp,
short *Xp,
short Ph,
short Inc,
unsigned short dhb);
int
resampleFast( /* number of output samples returned */
int& inCount, /* number of input samples to convert */
int outCount, /* number of output samples to compute */
short inArray[], /* input array data (length inCount * nChans) */
short outArray[]);/* output array data (length outCount * nChans) */
int
resampleWithFilter( /* number of output samples returned */
int& inCount, /* number of input samples to convert */
int outCount, /* number of output samples to compute */
short inArray[], /* input array data (length inCount * nChans) */
short outArray[], /* output array data (length outCount * nChans) */
short Imp[], short ImpD[],
unsigned short LpScl, unsigned short Nmult, unsigned short Nwing);
static short SMALL_FILTER_IMP[];
static short LARGE_FILTER_IMP[];
bool interpFilt;
bool largeFilter;
bool linearInterp;
short ** _Xv;
short ** _Yv;
unsigned int _Time;
double _factor;
int _nChans;
bool _initial;
double _vol;
};
#endif
|