1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 /*
12  * A wrapper for resampling a numerous amount of sampling combinations.
13  */
14 
15 #ifndef COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_
16 #define COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_
17 
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 namespace webrtc {
22 
23 // All methods return 0 on success and -1 on failure.
24 class Resampler {
25  public:
26   Resampler();
27   Resampler(int inFreq, int outFreq, size_t num_channels);
28   ~Resampler();
29 
30   // Reset all states
31   int Reset(int inFreq, int outFreq, size_t num_channels);
32 
33   // Reset all states if any parameter has changed
34   int ResetIfNeeded(int inFreq, int outFreq, size_t num_channels);
35 
36   // Resample samplesIn to samplesOut.
37   int Push(const int16_t* samplesIn,
38            size_t lengthIn,
39            int16_t* samplesOut,
40            size_t maxLen,
41            size_t& outLen);  // NOLINT: to avoid changing APIs
42 
43  private:
44   enum ResamplerMode {
45     kResamplerMode1To1,
46     kResamplerMode1To2,
47     kResamplerMode1To3,
48     kResamplerMode1To4,
49     kResamplerMode1To6,
50     kResamplerMode1To12,
51     kResamplerMode2To3,
52     kResamplerMode2To11,
53     kResamplerMode4To11,
54     kResamplerMode8To11,
55     kResamplerMode11To16,
56     kResamplerMode11To32,
57     kResamplerMode2To1,
58     kResamplerMode3To1,
59     kResamplerMode4To1,
60     kResamplerMode6To1,
61     kResamplerMode12To1,
62     kResamplerMode3To2,
63     kResamplerMode11To2,
64     kResamplerMode11To4,
65     kResamplerMode11To8
66   };
67 
68   // Computes the resampler mode for a given sampling frequency pair.
69   // Returns -1 for unsupported frequency pairs.
70   static int ComputeResamplerMode(int in_freq_hz,
71                                   int out_freq_hz,
72                                   ResamplerMode* mode);
73 
74   // Generic pointers since we don't know what states we'll need
75   void* state1_;
76   void* state2_;
77   void* state3_;
78 
79   // Storage if needed
80   int16_t* in_buffer_;
81   int16_t* out_buffer_;
82   size_t in_buffer_size_;
83   size_t out_buffer_size_;
84   size_t in_buffer_size_max_;
85   size_t out_buffer_size_max_;
86 
87   int my_in_frequency_khz_;
88   int my_out_frequency_khz_;
89   ResamplerMode my_mode_;
90   size_t num_channels_;
91 
92   // Extra instance for stereo
93   Resampler* helper_left_;
94   Resampler* helper_right_;
95 };
96 
97 }  // namespace webrtc
98 
99 #endif  // COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_
100