OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010, Google Inc. All rights reserved. | 2 * Copyright (C) 2010, 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 | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 28 matching lines...) Expand all Loading... |
39 2 + static_cast<int>(MaxFramesToProcess * AudioResampler::MaxRate)), | 39 2 + static_cast<int>(MaxFramesToProcess * AudioResampler::MaxRate)), |
40 m_virtualReadIndex(0.0), | 40 m_virtualReadIndex(0.0), |
41 m_fillIndex(0) { | 41 m_fillIndex(0) { |
42 m_lastValues[0] = 0.0f; | 42 m_lastValues[0] = 0.0f; |
43 m_lastValues[1] = 0.0f; | 43 m_lastValues[1] = 0.0f; |
44 } | 44 } |
45 | 45 |
46 float* AudioResamplerKernel::getSourcePointer( | 46 float* AudioResamplerKernel::getSourcePointer( |
47 size_t framesToProcess, | 47 size_t framesToProcess, |
48 size_t* numberOfSourceFramesNeededP) { | 48 size_t* numberOfSourceFramesNeededP) { |
49 DCHECK_LE(framesToProcess, MaxFramesToProcess); | 49 ASSERT(framesToProcess <= MaxFramesToProcess); |
50 | 50 |
51 // Calculate the next "virtual" index. After process() is called, | 51 // Calculate the next "virtual" index. After process() is called, |
52 // m_virtualReadIndex will equal this value. | 52 // m_virtualReadIndex will equal this value. |
53 double nextFractionalIndex = m_virtualReadIndex + framesToProcess * rate(); | 53 double nextFractionalIndex = m_virtualReadIndex + framesToProcess * rate(); |
54 | 54 |
55 // Because we're linearly interpolating between the previous and next sample | 55 // Because we're linearly interpolating between the previous and next sample |
56 // we need to round up so we include the next sample. | 56 // we need to round up so we include the next sample. |
57 int endIndex = static_cast<int>(nextFractionalIndex + | 57 int endIndex = static_cast<int>(nextFractionalIndex + |
58 1.0); // round up to next integer index | 58 1.0); // round up to next integer index |
59 | 59 |
60 // Determine how many input frames we'll need. | 60 // Determine how many input frames we'll need. |
61 // We need to fill the buffer up to and including endIndex (so add 1) but | 61 // We need to fill the buffer up to and including endIndex (so add 1) but |
62 // we've already buffered m_fillIndex frames from last time. | 62 // we've already buffered m_fillIndex frames from last time. |
63 size_t framesNeeded = 1 + endIndex - m_fillIndex; | 63 size_t framesNeeded = 1 + endIndex - m_fillIndex; |
64 if (numberOfSourceFramesNeededP) | 64 if (numberOfSourceFramesNeededP) |
65 *numberOfSourceFramesNeededP = framesNeeded; | 65 *numberOfSourceFramesNeededP = framesNeeded; |
66 | 66 |
67 // Do bounds checking for the source buffer. | 67 // Do bounds checking for the source buffer. |
68 bool isGood = m_fillIndex < m_sourceBuffer.size() && | 68 bool isGood = m_fillIndex < m_sourceBuffer.size() && |
69 m_fillIndex + framesNeeded <= m_sourceBuffer.size(); | 69 m_fillIndex + framesNeeded <= m_sourceBuffer.size(); |
70 DCHECK(isGood); | 70 DCHECK(isGood); |
71 if (!isGood) | 71 if (!isGood) |
72 return 0; | 72 return 0; |
73 | 73 |
74 return m_sourceBuffer.data() + m_fillIndex; | 74 return m_sourceBuffer.data() + m_fillIndex; |
75 } | 75 } |
76 | 76 |
77 void AudioResamplerKernel::process(float* destination, size_t framesToProcess) { | 77 void AudioResamplerKernel::process(float* destination, size_t framesToProcess) { |
78 DCHECK_LE(framesToProcess, MaxFramesToProcess); | 78 ASSERT(framesToProcess <= MaxFramesToProcess); |
79 | 79 |
80 float* source = m_sourceBuffer.data(); | 80 float* source = m_sourceBuffer.data(); |
81 | 81 |
82 double rate = this->rate(); | 82 double rate = this->rate(); |
83 rate = clampTo(rate, 0.0, AudioResampler::MaxRate); | 83 rate = clampTo(rate, 0.0, AudioResampler::MaxRate); |
84 | 84 |
85 // Start out with the previous saved values (if any). | 85 // Start out with the previous saved values (if any). |
86 if (m_fillIndex > 0) { | 86 if (m_fillIndex > 0) { |
87 source[0] = m_lastValues[0]; | 87 source[0] = m_lastValues[0]; |
88 source[1] = m_lastValues[1]; | 88 source[1] = m_lastValues[1]; |
89 } | 89 } |
90 | 90 |
91 // Make a local copy. | 91 // Make a local copy. |
92 double virtualReadIndex = m_virtualReadIndex; | 92 double virtualReadIndex = m_virtualReadIndex; |
93 | 93 |
94 // Sanity check source buffer access. | 94 // Sanity check source buffer access. |
95 DCHECK_GT(framesToProcess, 0u); | 95 ASSERT(framesToProcess > 0); |
96 DCHECK_GE(virtualReadIndex, 0); | 96 ASSERT(virtualReadIndex >= 0 && |
97 DCHECK_LT(1 + static_cast<unsigned>(virtualReadIndex + | 97 1 + static_cast<unsigned>(virtualReadIndex + |
98 (framesToProcess - 1) * rate), | 98 (framesToProcess - 1) * rate) < |
99 m_sourceBuffer.size()); | 99 m_sourceBuffer.size()); |
100 | 100 |
101 // Do the linear interpolation. | 101 // Do the linear interpolation. |
102 int n = framesToProcess; | 102 int n = framesToProcess; |
103 while (n--) { | 103 while (n--) { |
104 unsigned readIndex = static_cast<unsigned>(virtualReadIndex); | 104 unsigned readIndex = static_cast<unsigned>(virtualReadIndex); |
105 double interpolationFactor = virtualReadIndex - readIndex; | 105 double interpolationFactor = virtualReadIndex - readIndex; |
106 | 106 |
107 double sample1 = source[readIndex]; | 107 double sample1 = source[readIndex]; |
108 double sample2 = source[readIndex + 1]; | 108 double sample2 = source[readIndex + 1]; |
109 | 109 |
(...skipping 24 matching lines...) Expand all Loading... |
134 m_fillIndex = 0; | 134 m_fillIndex = 0; |
135 m_lastValues[0] = 0.0f; | 135 m_lastValues[0] = 0.0f; |
136 m_lastValues[1] = 0.0f; | 136 m_lastValues[1] = 0.0f; |
137 } | 137 } |
138 | 138 |
139 double AudioResamplerKernel::rate() const { | 139 double AudioResamplerKernel::rate() const { |
140 return m_resampler->rate(); | 140 return m_resampler->rate(); |
141 } | 141 } |
142 | 142 |
143 } // namespace blink | 143 } // namespace blink |
OLD | NEW |