home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Audio / Resampler / Resampler.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  6.7 KB  |  187 lines

  1. // Resampler.h: interface for the CResampler class.
  2. /* 
  3.  *  Resampler.h
  4.  * 
  5.  *  Original code from Julius O. Smith III< jos@ccrma.stanford.edu>
  6.  *
  7.  *    Copyright (C) Alberto Vigata - January 2000
  8.  *
  9.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  10.  *    
  11.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  12.  *  it under the terms of the GNU General Public License as published by
  13.  *  the Free Software Foundation; either version 2, or (at your option)
  14.  *  any later version.
  15.  *   
  16.  *  FlasKMPEG is distributed in the hope that it will be useful,
  17.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  *  GNU General Public License for more details.
  20.  *   
  21.  *  You should have received a copy of the GNU General Public License
  22.  *  along with GNU Make; see the file COPYING.  If not, write to
  23.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  24.  *
  25.  */
  26. #if !defined(AFX_RESAMPLER_H__00FC5C61_A4AA_11D2_94DE_00000100CF13__INCLUDED_)
  27. #define AFX_RESAMPLER_H__00FC5C61_A4AA_11D2_94DE_00000100CF13__INCLUDED_
  28.  
  29. #if _MSC_VER > 1000
  30. #pragma once
  31. #endif // _MSC_VER > 1000
  32. /*
  33.  * FILE: resample.h
  34.  *
  35.  * The configuration constants below govern
  36.  * the number of bits in the input sample and filter coefficients, the 
  37.  * number of bits to the right of the binary-point for fixed-point math, etc.
  38.  *
  39.  */
  40.  
  41. #include "stdefs.h"
  42.  
  43. #include "..\..\ASyncBuffer.h"
  44.  
  45. #define IBUFFSIZE 4096                         /* Input buffer size */
  46. #include "smallfilter.h"
  47. #include "largefilter.h"
  48.  
  49. /* Conversion constants */
  50. #define Nhc       8
  51. #define Na        7
  52. #define Np       (Nhc+Na)
  53. #define Npc      (1<<Nhc)
  54. #define Amask    ((1<<Na)-1)
  55. #define Pmask    ((1<<Np)-1)
  56. #define Nh       16
  57. #define Nb       16
  58. #define Nhxn     14
  59. #define Nhg      (Nh-Nhxn)
  60. #define NLpScl   13
  61.  
  62. /* Description of constants:
  63.  *
  64.  * Npc - is the number of look-up values available for the lowpass filter
  65.  *    between the beginning of its impulse response and the "cutoff time"
  66.  *    of the filter.  The cutoff time is defined as the reciprocal of the
  67.  *    lowpass-filter cut off frequence in Hz.  For example, if the
  68.  *    lowpass filter were a sinc function, Npc would be the index of the
  69.  *    impulse-response lookup-table corresponding to the first zero-
  70.  *    crossing of the sinc function.  (The inverse first zero-crossing
  71.  *    time of a sinc function equals its nominal cutoff frequency in Hz.)
  72.  *    Npc must be a power of 2 due to the details of the current
  73.  *    implementation. The default value of 512 is sufficiently high that
  74.  *    using linear interpolation to fill in between the table entries
  75.  *    gives approximately 16-bit accuracy in filter coefficients.
  76.  *
  77.  * Nhc - is log base 2 of Npc.
  78.  *
  79.  * Na - is the number of bits devoted to linear interpolation of the
  80.  *    filter coefficients.
  81.  *
  82.  * Np - is Na + Nhc, the number of bits to the right of the binary point
  83.  *    in the integer "time" variable. To the left of the point, it indexes
  84.  *    the input array (X), and to the right, it is interpreted as a number
  85.  *    between 0 and 1 sample of the input X.  Np must be less than 16 in
  86.  *    this implementation.
  87.  *
  88.  * Nh - is the number of bits in the filter coefficients. The sum of Nh and
  89.  *    the number of bits in the input data (typically 16) cannot exceed 32.
  90.  *    Thus Nh should be 16.  The largest filter coefficient should nearly
  91.  *    fill 16 bits (32767).
  92.  *
  93.  * Nb - is the number of bits in the input data. The sum of Nb and Nh cannot
  94.  *    exceed 32.
  95.  *
  96.  * Nhxn - is the number of bits to right shift after multiplying each input
  97.  *    sample times a filter coefficient. It can be as great as Nh and as
  98.  *    small as 0. Nhxn = Nh-2 gives 2 guard bits in the multiply-add
  99.  *    accumulation.  If Nhxn=0, the accumulation will soon overflow 32 bits.
  100.  *
  101.  * Nhg - is the number of guard bits in mpy-add accumulation (equal to Nh-Nhxn)
  102.  *
  103.  * NLpScl - is the number of bits allocated to the unity-gain normalization
  104.  *    factor.  The output of the lowpass filter is multiplied by LpScl and
  105.  *    then right-shifted NLpScl bits. To avoid overflow, we must have 
  106.  *    Nb+Nhg+NLpScl < 32.
  107.  */
  108.  
  109. class CResampler: public CDataSource
  110. {
  111. public:
  112.  
  113.     double factor;
  114.  
  115.     CResampler();
  116.     CResampler(    double factor,        /* factor = Sndout/Sndin */
  117.                     int nChans,                /* number of sound channels (1 or 2) */
  118.                     CAsyncBuffer *ABuffer    /* Buffer to read data from */  
  119.     );
  120.     void ResampleFastStart();
  121.     void ResampleFastStop();
  122.     int read(char *buffer);
  123.     virtual ~CResampler();
  124. int Resample(            /* number of output sample returned */
  125.     BOOL interpFilt,        /* TRUE means interpolate filter coeffs */
  126.     int fastMode,        /* 0 = highest quality, slowest speed */
  127.     BOOL largeFilter        /* TRUE means use 65-tap FIR filter */
  128. );
  129.  
  130. private:
  131.     //RESAMPLE FAST
  132.     URWORD Time, Time2;        /* Current time/pos in input sample */
  133.     UHRWORD Xp, Ncreep, Xoff, Xread;
  134.     int OBUFFSIZE;
  135.     HRWORD X1[IBUFFSIZE]; /* I/O buffers */
  136.     HRWORD X2[IBUFFSIZE]; /* I/O buffers */
  137.     HRWORD *Y1;
  138.     HRWORD *Y2;
  139.  
  140.     UHRWORD Nout, Nx;
  141.     int i, Ycount, last;
  142.  
  143.  
  144.     CAsyncBuffer *ABuffer;
  145.     unsigned int framecount;
  146.     int eosFlag;
  147.     int nChans;
  148.     int outCount;
  149.     int inCount;
  150.  
  151. int ResampleWithFilter(  /* number of output samples returned */
  152.     BOOL interpFilt,        /* TRUE means interpolate filter coeffs */
  153.     HRWORD Imp[], HRWORD ImpD[],
  154.     UHRWORD LpScl, UHRWORD Nmult, UHRWORD Nwing);
  155. int ResampleFast( short *oData );
  156.  
  157. int err_ret(char *s);
  158.  
  159.  
  160. int SrcUD(HRWORD X[], HRWORD Y[], double factor, URWORD *Time,
  161.          UHRWORD Nx, UHRWORD Nwing, UHRWORD LpScl,
  162.          HRWORD Imp[], HRWORD ImpD[], BOOL Interp);
  163.  
  164. int SrcUp(HRWORD X[], HRWORD Y[], double factor, URWORD *Time,
  165.          UHRWORD Nx, UHRWORD Nwing, UHRWORD LpScl,
  166.          HRWORD Imp[], HRWORD ImpD[], BOOL Interp);
  167. int 
  168.   SrcLinear(HRWORD X[], HRWORD Y[], double factor, URWORD *Time, UHRWORD Nx);
  169.  
  170. INLINE HRWORD RWORDToHRWORD(RWORD v, int scl);
  171. int readData(
  172.          HRWORD *outPtr1,      /* array receiving left chan samps */
  173.          HRWORD *outPtr2,      /* array receiving right chan samps */
  174.          int   dataArraySize, /* size of these arrays */
  175.          int   Xoff) ;
  176. RWORD FilterUD( HRWORD Imp[], HRWORD ImpD[],
  177.              UHRWORD Nwing, BOOL Interp,
  178.              HRWORD *Xp, HRWORD Ph, HRWORD Inc, UHRWORD dhb);
  179. RWORD FilterUp(HRWORD Imp[], HRWORD ImpD[], 
  180.                          UHRWORD Nwing, BOOL Interp,
  181.                          HRWORD *Xp, HRWORD Ph, HRWORD Inc);
  182.     void write(int samples,HRWORD *Y1, HRWORD *Y2);
  183.     void write(int samples, HRWORD *Y1, HRWORD *Y2, short *oData);
  184. };
  185.  
  186. #endif // !defined(AFX_RESAMPLER_H__00FC5C61_A4AA_11D2_94DE_00000100CF13__INCLUDED_)
  187.