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