resamplesubs.c (3722B)
1 /********************************************************************** 2 3 resamplesubs.c 4 5 Real-time library interface by Dominic Mazzoni 6 7 Based on resample-1.7: 8 http://www-ccrma.stanford.edu/~jos/resample/ 9 10 Dual-licensed as LGPL and BSD; see README.md and LICENSE* files. 11 12 This file provides the routines that do sample-rate conversion 13 on small arrays, calling routines from filterkit. 14 15 **********************************************************************/ 16 17 /* Definitions */ 18 #include "resample_defs.h" 19 20 #include "filterkit.h" 21 22 #include <stdlib.h> 23 #include <stdio.h> 24 #include <math.h> 25 #include <string.h> 26 27 /* Sampling rate up-conversion only subroutine; 28 * Slightly faster than down-conversion; 29 */ 30 int lrsSrcUp(float X[], 31 float Y[], 32 double factor, 33 double *TimePtr, 34 UWORD Nx, 35 UWORD Nwing, 36 float LpScl, 37 float Imp[], 38 float ImpD[], 39 BOOL Interp) 40 { 41 float *Xp, *Ystart; 42 float v; 43 44 double CurrentTime = *TimePtr; 45 double dt; /* Step through input signal */ 46 double endTime; /* When Time reaches EndTime, return to user */ 47 48 dt = 1.0/factor; /* Output sampling period */ 49 50 Ystart = Y; 51 endTime = CurrentTime + Nx; 52 while (CurrentTime < endTime) 53 { 54 double LeftPhase = CurrentTime-floor(CurrentTime); 55 double RightPhase = 1.0 - LeftPhase; 56 57 Xp = &X[(int)CurrentTime]; /* Ptr to current input sample */ 58 /* Perform left-wing inner product */ 59 v = lrsFilterUp(Imp, ImpD, Nwing, Interp, Xp, 60 LeftPhase, -1); 61 /* Perform right-wing inner product */ 62 v += lrsFilterUp(Imp, ImpD, Nwing, Interp, Xp+1, 63 RightPhase, 1); 64 65 v *= LpScl; /* Normalize for unity filter gain */ 66 67 *Y++ = v; /* Deposit output */ 68 CurrentTime += dt; /* Move to next sample by time increment */ 69 } 70 71 *TimePtr = CurrentTime; 72 return (Y - Ystart); /* Return the number of output samples */ 73 } 74 75 /* Sampling rate conversion subroutine */ 76 77 int lrsSrcUD(float X[], 78 float Y[], 79 double factor, 80 double *TimePtr, 81 UWORD Nx, 82 UWORD Nwing, 83 float LpScl, 84 float Imp[], 85 float ImpD[], 86 BOOL Interp) 87 { 88 float *Xp, *Ystart; 89 float v; 90 91 double CurrentTime = (*TimePtr); 92 double dh; /* Step through filter impulse response */ 93 double dt; /* Step through input signal */ 94 double endTime; /* When Time reaches EndTime, return to user */ 95 96 dt = 1.0/factor; /* Output sampling period */ 97 98 dh = MIN(Npc, factor*Npc); /* Filter sampling period */ 99 100 Ystart = Y; 101 endTime = CurrentTime + Nx; 102 while (CurrentTime < endTime) 103 { 104 double LeftPhase = CurrentTime-floor(CurrentTime); 105 double RightPhase = 1.0 - LeftPhase; 106 107 Xp = &X[(int)CurrentTime]; /* Ptr to current input sample */ 108 /* Perform left-wing inner product */ 109 v = lrsFilterUD(Imp, ImpD, Nwing, Interp, Xp, 110 LeftPhase, -1, dh); 111 /* Perform right-wing inner product */ 112 v += lrsFilterUD(Imp, ImpD, Nwing, Interp, Xp+1, 113 RightPhase, 1, dh); 114 115 v *= LpScl; /* Normalize for unity filter gain */ 116 *Y++ = v; /* Deposit output */ 117 118 CurrentTime += dt; /* Move to next sample by time increment */ 119 } 120 121 *TimePtr = CurrentTime; 122 return (Y - Ystart); /* Return the number of output samples */ 123 }