/usr/include/ptlib/unix/ptlib/resampler.h is in libpt-dev 2.10.10~dfsg-4.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* pwlib/src/ptlib/unix/beaudio/Resampler.h
$Log: resampler.h,v $
Revision 1.2 2005/11/30 12:47:38 csoutheren
Removed tabs, reformatted some code, and changed tags for Doxygen
Revision 1.1 2004/05/14 08:37:33 ykiryanov
BeOS resampler related declarations
Revision 1.4 2002/02/04 03:15:47 yurik
VC compiler complained of invoking new on array with paramters in constructor. Fixed it.
Revision 1.3 2001/07/09 06:16:15 yurik
Jac Goudsmit's BeOS changes of July,6th. Cleaning up media subsystem etc.
Copyright 2001, Be Incorporated. All Rights Reserved.
This file may be used under the terms of the Be Sample Code License.
*/
#include <stdlib.h>
template <class T>
class History
{
// This class stores a number of input samples in a circular buffer
// The data is actually stored twice so that a wrap-around is never
// needed for retrieving the data (it IS needed for storing it, but
// because most resampling algorithms use interpolation of a number
// of input samples, retrieving happens much more frequent than
// storing).
// The history is always filled completely: the start of the history
// is padded with neutral samples (e.g. 0 for signed integer samples).
// Therefore we only need one index into the array: a pointer to
// the oldest sample (we keep a couple of additional pointers for
// efficiency).
// For example: let's say the object is constructed with size=3
// and samples A B C D E are added. The internal representation and the
// way that the retrieve function sees it are as follows:
// Internal ('>' is head): retrieved data
// >0 0 0 0 0 0 {0,0,0}
// A>0 0 A 0 0 {0,0,A}
// A B>0 A B 0 {0,A,B}
// >A B C A B C {A,B,C}
// D>B C D B C {B,C,D}
// D E>C D E C {C,D,E}
// Notice that the last element in the buffer is never retrieved, so
// we could make the buffer one element smaller, but then we would
// have to add another "if" to the storage code to not store the second
// copy of the data when the head pointer reaches the end of the buffer.
// Note: this class is not multi-thread compatible.
private:
size_t mSize; // number of unique copies of each sample in buffer
T *mBuf; // twice the size indicated in mSize
T *mHead; // pointer to oldest sample
T *mLoop; // kept equal to mBuf+mSize
T *mSecHead; // kept equal to mHead+mSize
public:
History() {}; // default constructor to please vc's new for arrays.
void Init(size_t size, T neutralvalue) // init with original constructor values
{
// The data is stored twice, so create a buffer that's double the requested size
mBuf=mHead=mSecHead=mLoop=new T[2*(mSize=size)];
// Fill the buffer with empty samples
for (unsigned i=0; i<mSize; i++) {
*mSecHead++=neutralvalue;
}
mLoop=mSecHead;
}
History(size_t size, T neutralvalue) // original constructor
{
Init(size, neutralvalue);
}
~History() { delete[] mBuf; }
// Write one sample into the history
void Write(T sample)
{
// replace oldest sample with this sample
*mHead++ = *mSecHead++ = sample;
// now next sample is the oldest
if (mHead==mLoop) {
mHead=mBuf;
mSecHead=mLoop;
}
}
// Read samples from the history
T operator[](unsigned index) { return mHead[index]; }
// Additional utilities
size_t Size() const { return mSize; }
};
template <class ISample, class IntSample, class OSample>
class BaseResampler
{
// - A "correspondance" is defined as the point in time when
// an input sample's time corresponds with its output sample's time.
// - A "block" is defined as the time that passes between two correspondances
// - By mathematical rule, if input frequency and output frequency are
// expressed in samples per second, the number of correspondances per
// second is the Highest Common Factor (HCF) of the two frequencies.
// - The resolution at which time needs to be measured within a block is
// defined as a "tick". The number of ticks per block is the result of
// multiplying the number of input samples per block and the number of
// output samples per block.
protected:
////////// Types
typedef History<IntSample> SampleHistory;
////////// Configuration
// This data doesn't change after setup
// The number of ticks per block is mInum*mOnum.
// Therefore the number of ticks per input sample is (mInum*mOnum)/mInum=mOnum. The
// number of ticks per output sample is (mInum*mOnum)/mOnum=mInum.
// float mIFreq; // input frequency in frames/second
// float mOFreq; // output frequency in frames/second
unsigned mIChannels; // number of input channels
unsigned mOChannels; // number of output channels
// unsigned mHCF; // Highest Common Factor of mIFreq/mOFreq
unsigned mINum; // number of input samples per block = mIFreq/mHCF
unsigned mONum; // number of output samples per block = mOFreq/mHCF
#define mITPS mONum // input ticks per sample
#define mOTPS mINum // output ticks per sample
IntSample mNeutralValue; // neutral value
////////// Status
// This data changes with each input sample
unsigned mICount; // input samples received since last coinciding
unsigned mOCount; // output samples generated since last coinciding
unsigned mITicks; // tick count for mICount (=mICount*mITPS)
unsigned mOTicks; // tick count for mOCount (=mOCount*mOTPS)
SampleHistory *mHistory; // one sample history per channel
static size_t hcf(
size_t a,
size_t b)
{
// The Highest Common Factor (HCF) is defined as the largest number that
// two positive integer values can both be divided by without leaving
// a remainder.
// For example: HCF(12,21)=3, HCF(12,23)=1, HCF(12,24)=12.
// This function uses Euclid's algorithm to determine HCF(a,b)
// The result is only valid when a and b are both positive.
size_t r;
// the algorithm only works for a>=b
if (a<b) {
// swap a and b
r=a; a=b; b=r;
}
while ((r=a%b)!=0) {
a=b;
b=r;
}
return b;
}
public:
// Constructor/Destructor
BaseResampler(
float infreq,
float outfreq,
unsigned inchannels,
unsigned outchannels,
ISample neutralvalue,
size_t historysize=1) // must be >=1
{
// Configuration (parameterized part)
unsigned IFreq=static_cast<unsigned>(infreq);
unsigned OFreq=static_cast<unsigned>(outfreq);
mIChannels=inchannels;
mOChannels=outchannels;
mNeutralValue=ToIntSample(neutralvalue);
// Configuration (derived part)
unsigned HCF=hcf(IFreq, OFreq);
mINum=IFreq/HCF;
mONum=OFreq/HCF;
// Status
mICount=0; //mICount=(((delay+mINum-1)/mINum)*mINum-delay);
mITicks=0; //mITicks=mICount*mITPS;
mOCount=0;
mOTicks=0;
mHistory = new SampleHistory[mIChannels];
for (unsigned u = 0; u < mIChannels; u++)
mHistory[u].Init(historysize,mNeutralValue);
}
public:
virtual ~BaseResampler()
{
delete[] mHistory;
}
public:
size_t InFrameSize()
{
return mIChannels*sizeof(ISample);
}
size_t OutFrameSize()
{
return mOChannels*sizeof(OSample);
}
size_t InBlockSize()
{
return mINum*InFrameSize();
}
size_t OutBlockSize()
{
return mONum*OutFrameSize();
}
size_t InFramesFromOutBytes(size_t outbytes)
{
return outbytes*mINum/(mONum*OutFrameSize());
}
size_t OutFramesFromInBytes(size_t inbytes)
{
return inbytes*mONum/(mINum*InFrameSize());
}
size_t InBytesFromOutBytes(size_t outbytes)
{
return InFramesFromOutBytes(outbytes)*InFrameSize();
}
size_t OutBytesFromInBytes(size_t inbytes)
{
return OutFramesFromInBytes(inbytes)*OutFrameSize();
}
protected:
// Virtual function to convert sample from input format to internal format.
// The default implementation is a cast, but if the signed-ness of the formats are
// different, you will need to override this to add or subtract the neutral value
// and if the ranges of values don't match you will need to add a multiplication or
// division.
virtual IntSample ToIntSample(ISample value) { return static_cast<IntSample>(value); }
// Virtual function to convert sample from internal format to output format.
// The default implementation is a cast, but if the signed-ness of the formats are
// different, you will need to override this to add or subtract the neutral value
// and if the ranges of values don't match you will need to add a multiplication or
// division.
virtual OSample ToOSample(IntSample value) { return static_cast<OSample>(value); }
// Pure virtual function that does the actual resampling
// It should use the sample history to generate a sample.
virtual IntSample OutSample(SampleHistory &history)
{
IntSample y0=history[0];
IntSample y1=history[1];
// Note: it's possible to optimize the division in the next line
// by creating a table in the constructor with mITPS entries
// where table[n]=n/mITPS and changing the calculation to
// y0+(y1-y0)*table[deltaticks]
return y0+(IntSample)(mOTicks-mITicks)*(y1-y0)/(IntSample)mITPS;
}
// Virtual function to resample a frame.
// It needs to examine the histories for all channels and generate an
// output frame at the given pointer, and then update the pointer.
virtual void OutFrame(OSample **io_out)
{
unsigned i,j;
for (i=0, j=0; i<mIChannels && j<mOChannels; i++, j++)
{
**io_out=ToOSample(OutSample(mHistory[i]));
(*io_out)++;
}
}
public:
size_t InFrames(
const ISample **io_indata,
OSample **io_outdata,
size_t *numinframes,
size_t maxoutframes)
{
// Handles one input sample
// Generates a number of outputs samples depending on the current state
// Updates the input and output pointers.
// Returns number of output samples done
size_t out_done=0;
size_t inframe;
for (inframe=0; inframe<*numinframes && out_done!=maxoutframes; inframe++)
{
// Add new samples to the history
for (unsigned channel=0; channel<mIChannels; channel++)
{
mHistory[channel].Write(ToIntSample(**io_indata));
(*io_indata)++;
}
// Determine upper limit of output tick counter
unsigned endticks=mITicks;
if (mOTicks!=0)
{
// Keep generating output samples until the output
// tick count ends up at the input tick count.
// For the beginning of the block, that's 0 (inclusive)
// For other output samples, it's mITicks+mITPS (exclusive).
// To make the loop inclusive ('<=') in all cases, we use -1
// which is allowed because the tick count is an integer.
endticks+=mITPS-1;
}
// Generate the necessary output samples
while (mOTicks<=endticks)
{
OutFrame(io_outdata);
out_done++;
// Update the output side of the state
mOCount++;
mOTicks+=mOTPS;
// If we're out of space, bail out
if (out_done==maxoutframes)
{
break;
}
}
// update the input side of the state
mICount++;
mITicks+=mITPS;
// Reset at the end of a block
if (mICount==mINum)
{
mICount=0;
mITicks=0;
mOCount=0;
mOTicks=0;
}
}
*numinframes-=inframe;
return out_done;
}
};
typedef class BaseResampler<short, long, short> Resampler;
|