OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 double a0 = 0.5 * (1.0 - alpha); | 49 double a0 = 0.5 * (1.0 - alpha); |
50 double a1 = 0.5; | 50 double a1 = 0.5; |
51 double a2 = 0.5 * alpha; | 51 double a2 = 0.5 * alpha; |
52 | 52 |
53 int n = DefaultKernelSize; | 53 int n = DefaultKernelSize; |
54 int halfSize = n / 2; | 54 int halfSize = n / 2; |
55 | 55 |
56 // Half-band filter. | 56 // Half-band filter. |
57 double sincScaleFactor = 0.5; | 57 double sincScaleFactor = 0.5; |
58 | 58 |
59 // Compute only the odd terms because the even ones are zero, except | 59 // Compute only the odd terms because the even ones are zero, except right in |
60 // right in the middle at halfSize, which is 0.5 and we'll handle specially du
ring processing | 60 // the middle at halfSize, which is 0.5 and we'll handle specially during |
61 // after doing the main convolution using m_reducedKernel. | 61 // processing after doing the main convolution using m_reducedKernel. |
62 for (int i = 1; i < n; i += 2) { | 62 for (int i = 1; i < n; i += 2) { |
63 // Compute the sinc() with offset. | 63 // Compute the sinc() with offset. |
64 double s = sincScaleFactor * piDouble * (i - halfSize); | 64 double s = sincScaleFactor * piDouble * (i - halfSize); |
65 double sinc = !s ? 1.0 : sin(s) / s; | 65 double sinc = !s ? 1.0 : sin(s) / s; |
66 sinc *= sincScaleFactor; | 66 sinc *= sincScaleFactor; |
67 | 67 |
68 // Compute Blackman window, matching the offset of the sinc(). | 68 // Compute Blackman window, matching the offset of the sinc(). |
69 double x = static_cast<double>(i) / n; | 69 double x = static_cast<double>(i) / n; |
70 double window = | 70 double window = |
71 a0 - a1 * cos(twoPiDouble * x) + a2 * cos(twoPiDouble * 2.0 * x); | 71 a0 - a1 * cos(twoPiDouble * x) + a2 * cos(twoPiDouble * 2.0 * x); |
72 | 72 |
73 // Window the sinc() function. | 73 // Window the sinc() function. |
74 // Then store only the odd terms in the kernel. | 74 // Then store only the odd terms in the kernel. |
75 // In a sense, this is shifting forward in time by one sample-frame at the d
estination sample-rate. | 75 // In a sense, this is shifting forward in time by one sample-frame at the |
| 76 // destination sample-rate. |
76 m_reducedKernel[(i - 1) / 2] = sinc * window; | 77 m_reducedKernel[(i - 1) / 2] = sinc * window; |
77 } | 78 } |
78 } | 79 } |
79 | 80 |
80 void DownSampler::process(const float* sourceP, | 81 void DownSampler::process(const float* sourceP, |
81 float* destP, | 82 float* destP, |
82 size_t sourceFramesToProcess) { | 83 size_t sourceFramesToProcess) { |
83 bool isInputBlockSizeGood = sourceFramesToProcess == m_inputBlockSize; | 84 bool isInputBlockSizeGood = sourceFramesToProcess == m_inputBlockSize; |
84 ASSERT(isInputBlockSizeGood); | 85 ASSERT(isInputBlockSizeGood); |
85 if (!isInputBlockSizeGood) | 86 if (!isInputBlockSizeGood) |
(...skipping 16 matching lines...) Expand all Loading... |
102 // Copy source samples to 2nd half of input buffer. | 103 // Copy source samples to 2nd half of input buffer. |
103 bool isInputBufferGood = m_inputBuffer.size() == sourceFramesToProcess * 2 && | 104 bool isInputBufferGood = m_inputBuffer.size() == sourceFramesToProcess * 2 && |
104 halfSize <= sourceFramesToProcess; | 105 halfSize <= sourceFramesToProcess; |
105 ASSERT(isInputBufferGood); | 106 ASSERT(isInputBufferGood); |
106 if (!isInputBufferGood) | 107 if (!isInputBufferGood) |
107 return; | 108 return; |
108 | 109 |
109 float* inputP = m_inputBuffer.data() + sourceFramesToProcess; | 110 float* inputP = m_inputBuffer.data() + sourceFramesToProcess; |
110 memcpy(inputP, sourceP, sizeof(float) * sourceFramesToProcess); | 111 memcpy(inputP, sourceP, sizeof(float) * sourceFramesToProcess); |
111 | 112 |
112 // Copy the odd sample-frames from sourceP, delayed by one sample-frame (desti
nation sample-rate) | 113 // Copy the odd sample-frames from sourceP, delayed by one sample-frame |
113 // to match shifting forward in time in m_reducedKernel. | 114 // (destination sample-rate) to match shifting forward in time in |
| 115 // m_reducedKernel. |
114 float* oddSamplesP = m_tempBuffer.data(); | 116 float* oddSamplesP = m_tempBuffer.data(); |
115 for (unsigned i = 0; i < destFramesToProcess; ++i) | 117 for (unsigned i = 0; i < destFramesToProcess; ++i) |
116 oddSamplesP[i] = *((inputP - 1) + i * 2); | 118 oddSamplesP[i] = *((inputP - 1) + i * 2); |
117 | 119 |
118 // Actually process oddSamplesP with m_reducedKernel for efficiency. | 120 // Actually process oddSamplesP with m_reducedKernel for efficiency. |
119 // The theoretical kernel is double this size with 0 values for even terms (ex
cept center). | 121 // The theoretical kernel is double this size with 0 values for even terms |
| 122 // (except center). |
120 m_convolver.process(&m_reducedKernel, oddSamplesP, destP, | 123 m_convolver.process(&m_reducedKernel, oddSamplesP, destP, |
121 destFramesToProcess); | 124 destFramesToProcess); |
122 | 125 |
123 // Now, account for the 0.5 term right in the middle of the kernel. | 126 // Now, account for the 0.5 term right in the middle of the kernel. |
124 // This amounts to a delay-line of length halfSize (at the source sample-rate)
, | 127 // This amounts to a delay-line of length halfSize (at the source |
125 // scaled by 0.5. | 128 // sample-rate), scaled by 0.5. |
126 | 129 |
127 // Sum into the destination. | 130 // Sum into the destination. |
128 for (unsigned i = 0; i < destFramesToProcess; ++i) | 131 for (unsigned i = 0; i < destFramesToProcess; ++i) |
129 destP[i] += 0.5 * *((inputP - halfSize) + i * 2); | 132 destP[i] += 0.5 * *((inputP - halfSize) + i * 2); |
130 | 133 |
131 // Copy 2nd half of input buffer to 1st half. | 134 // Copy 2nd half of input buffer to 1st half. |
132 memcpy(m_inputBuffer.data(), inputP, sizeof(float) * sourceFramesToProcess); | 135 memcpy(m_inputBuffer.data(), inputP, sizeof(float) * sourceFramesToProcess); |
133 } | 136 } |
134 | 137 |
135 void DownSampler::reset() { | 138 void DownSampler::reset() { |
136 m_convolver.reset(); | 139 m_convolver.reset(); |
137 m_inputBuffer.zero(); | 140 m_inputBuffer.zero(); |
138 } | 141 } |
139 | 142 |
140 size_t DownSampler::latencyFrames() const { | 143 size_t DownSampler::latencyFrames() const { |
141 // Divide by two since this is a linear phase kernel and the delay is at the c
enter of the kernel. | 144 // Divide by two since this is a linear phase kernel and the delay is at the |
| 145 // center of the kernel. |
142 return m_reducedKernel.size() / 2; | 146 return m_reducedKernel.size() / 2; |
143 } | 147 } |
144 | 148 |
145 } // namespace blink | 149 } // namespace blink |
OLD | NEW |