1/* 2 * Copyright (C) 2010, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25#include "config.h" 26 27#if ENABLE(WEB_AUDIO) 28 29#include "platform/audio/AudioResamplerKernel.h" 30 31#include <algorithm> 32#include "platform/audio/AudioResampler.h" 33 34namespace blink { 35 36const size_t AudioResamplerKernel::MaxFramesToProcess = 128; 37 38AudioResamplerKernel::AudioResamplerKernel(AudioResampler* resampler) 39 : m_resampler(resampler) 40 // The buffer size must be large enough to hold up to two extra sample frames for the linear interpolation. 41 , m_sourceBuffer(2 + static_cast<int>(MaxFramesToProcess * AudioResampler::MaxRate)) 42 , m_virtualReadIndex(0.0) 43 , m_fillIndex(0) 44{ 45 m_lastValues[0] = 0.0f; 46 m_lastValues[1] = 0.0f; 47} 48 49float* AudioResamplerKernel::getSourcePointer(size_t framesToProcess, size_t* numberOfSourceFramesNeededP) 50{ 51 ASSERT(framesToProcess <= MaxFramesToProcess); 52 53 // Calculate the next "virtual" index. After process() is called, m_virtualReadIndex will equal this value. 54 double nextFractionalIndex = m_virtualReadIndex + framesToProcess * rate(); 55 56 // Because we're linearly interpolating between the previous and next sample we need to round up so we include the next sample. 57 int endIndex = static_cast<int>(nextFractionalIndex + 1.0); // round up to next integer index 58 59 // Determine how many input frames we'll need. 60 // We need to fill the buffer up to and including endIndex (so add 1) but we've already buffered m_fillIndex frames from last time. 61 size_t framesNeeded = 1 + endIndex - m_fillIndex; 62 if (numberOfSourceFramesNeededP) 63 *numberOfSourceFramesNeededP = framesNeeded; 64 65 // Do bounds checking for the source buffer. 66 bool isGood = m_fillIndex < m_sourceBuffer.size() && m_fillIndex + framesNeeded <= m_sourceBuffer.size(); 67 ASSERT(isGood); 68 if (!isGood) 69 return 0; 70 71 return m_sourceBuffer.data() + m_fillIndex; 72} 73 74void AudioResamplerKernel::process(float* destination, size_t framesToProcess) 75{ 76 ASSERT(framesToProcess <= MaxFramesToProcess); 77 78 float* source = m_sourceBuffer.data(); 79 80 double rate = this->rate(); 81 rate = std::max(0.0, rate); 82 rate = std::min(AudioResampler::MaxRate, rate); 83 84 // Start out with the previous saved values (if any). 85 if (m_fillIndex > 0) { 86 source[0] = m_lastValues[0]; 87 source[1] = m_lastValues[1]; 88 } 89 90 // Make a local copy. 91 double virtualReadIndex = m_virtualReadIndex; 92 93 // Sanity check source buffer access. 94 ASSERT(framesToProcess > 0); 95 ASSERT(virtualReadIndex >= 0 && 1 + static_cast<unsigned>(virtualReadIndex + (framesToProcess - 1) * rate) < m_sourceBuffer.size()); 96 97 // Do the linear interpolation. 98 int n = framesToProcess; 99 while (n--) { 100 unsigned readIndex = static_cast<unsigned>(virtualReadIndex); 101 double interpolationFactor = virtualReadIndex - readIndex; 102 103 double sample1 = source[readIndex]; 104 double sample2 = source[readIndex + 1]; 105 106 double sample = (1.0 - interpolationFactor) * sample1 + interpolationFactor * sample2; 107 108 *destination++ = static_cast<float>(sample); 109 110 virtualReadIndex += rate; 111 } 112 113 // Save the last two sample-frames which will later be used at the beginning of the source buffer the next time around. 114 int readIndex = static_cast<int>(virtualReadIndex); 115 m_lastValues[0] = source[readIndex]; 116 m_lastValues[1] = source[readIndex + 1]; 117 m_fillIndex = 2; 118 119 // Wrap the virtual read index back to the start of the buffer. 120 virtualReadIndex -= readIndex; 121 122 // Put local copy back into member variable. 123 m_virtualReadIndex = virtualReadIndex; 124} 125 126void AudioResamplerKernel::reset() 127{ 128 m_virtualReadIndex = 0.0; 129 m_fillIndex = 0; 130 m_lastValues[0] = 0.0f; 131 m_lastValues[1] = 0.0f; 132} 133 134double AudioResamplerKernel::rate() const 135{ 136 return m_resampler->rate(); 137} 138 139} // namespace blink 140 141#endif // ENABLE(WEB_AUDIO) 142