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#ifndef AudioResamplerKernel_h
26#define AudioResamplerKernel_h
27
28#include "platform/PlatformExport.h"
29#include "platform/audio/AudioArray.h"
30
31namespace blink {
32
33class AudioResampler;
34
35// AudioResamplerKernel does resampling on a single mono channel.
36// It uses a simple linear interpolation for good performance.
37
38class PLATFORM_EXPORT AudioResamplerKernel {
39public:
40    AudioResamplerKernel(AudioResampler*);
41
42    // getSourcePointer() should be called each time before process() is called.
43    // Given a number of frames to process (for subsequent call to process()), it returns a pointer and numberOfSourceFramesNeeded
44    // where sample data should be copied. This sample data provides the input to the resampler when process() is called.
45    // framesToProcess must be less than or equal to MaxFramesToProcess.
46    float* getSourcePointer(size_t framesToProcess, size_t* numberOfSourceFramesNeeded);
47
48    // process() resamples framesToProcess frames from the source into destination.
49    // Each call to process() must be preceded by a call to getSourcePointer() so that source input may be supplied.
50    // framesToProcess must be less than or equal to MaxFramesToProcess.
51    void process(float* destination, size_t framesToProcess);
52
53    // Resets the processing state.
54    void reset();
55
56    static const size_t MaxFramesToProcess;
57
58private:
59    double rate() const;
60
61    AudioResampler* m_resampler;
62    AudioFloatArray m_sourceBuffer;
63
64    // This is a (floating point) read index on the input stream.
65    double m_virtualReadIndex;
66
67    // We need to have continuity from one call of process() to the next.
68    // m_lastValues stores the last two sample values from the last call to process().
69    // m_fillIndex represents how many buffered samples we have which can be as many as 2.
70    // For the first call to process() (or after reset()) there will be no buffered samples.
71    float m_lastValues[2];
72    unsigned m_fillIndex;
73};
74
75} // namespace blink
76
77#endif // AudioResamplerKernel_h
78