This function is a really dumb, simple way to interpolate audio, if the more general InterpolateAudio function below doesn't have enough data to work with. If the bad samples are in the middle, it's literally linear. If it's on either edge, we add some decay back to zero.
| 33 | // it's literally linear. If it's on either edge, we add some decay |
| 34 | // back to zero. |
| 35 | static void LinearInterpolateAudio(float *buffer, int len, |
| 36 | int firstBad, int numBad) |
| 37 | { |
| 38 | int i; |
| 39 | |
| 40 | float decay = 0.9f; |
| 41 | |
| 42 | if (firstBad==0) { |
| 43 | float delta = buffer[numBad] - buffer[numBad+1]; |
| 44 | float value = buffer[numBad]; |
| 45 | i = numBad - 1; |
| 46 | while (i >= 0) { |
| 47 | value += delta; |
| 48 | buffer[i] = value; |
| 49 | value *= decay; |
| 50 | delta *= decay; |
| 51 | i--; |
| 52 | } |
| 53 | } |
| 54 | else if (firstBad + numBad == len) { |
| 55 | float delta = buffer[firstBad-1] - buffer[firstBad-2]; |
| 56 | float value = buffer[firstBad-1]; |
| 57 | i = firstBad; |
| 58 | while (i < firstBad + numBad) { |
| 59 | value += delta; |
| 60 | buffer[i] = value; |
| 61 | value *= decay; |
| 62 | delta *= decay; |
| 63 | i++; |
| 64 | } |
| 65 | } |
| 66 | else { |
| 67 | float v1 = buffer[firstBad-1]; |
| 68 | float v2 = buffer[firstBad+numBad]; |
| 69 | float value = v1; |
| 70 | float delta = (v2 - v1) / (numBad+1); |
| 71 | i = firstBad; |
| 72 | while (i < firstBad + numBad) { |
| 73 | value += delta; |
| 74 | buffer[i] = value; |
| 75 | i++; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Here's the main interpolate function, using |
| 81 | // Least Squares AutoRegression (LSAR): |