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 /*
13  * A wrapper for resampling a numerous amount of sampling combinations.
14  */
15 
16 #ifndef COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_
17 #define COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_
18 
19 #include <stddef.h>
20 
21 #include "typedefs.h"  // NOLINT(build/include)
22 
23 namespace webrtc {
24 
25 // All methods return 0 on success and -1 on failure.
26 class Resampler {
27  public:
28   Resampler();
29   Resampler(int inFreq, int outFreq, size_t num_channels);
30   ~Resampler();
31 
32   // Reset all states
33   int Reset(int inFreq, int outFreq, size_t num_channels);
34 
35   // Reset all states if any parameter has changed
36   int ResetIfNeeded(int inFreq, int outFreq, size_t num_channels);
37 
38   // Resample samplesIn to samplesOut.
39   int Push(const int16_t* samplesIn, size_t lengthIn, int16_t* samplesOut,
40            size_t maxLen, size_t& outLen);  // NOLINT: to avoid changing APIs
41 
42  private:
43   enum ResamplerMode {
44     kResamplerMode1To1,
45     kResamplerMode1To2,
46     kResamplerMode1To3,
47     kResamplerMode1To4,
48     kResamplerMode1To6,
49     kResamplerMode1To12,
50     kResamplerMode2To3,
51     kResamplerMode2To11,
52     kResamplerMode4To11,
53     kResamplerMode8To11,
54     kResamplerMode11To16,
55     kResamplerMode11To32,
56     kResamplerMode2To1,
57     kResamplerMode3To1,
58     kResamplerMode4To1,
59     kResamplerMode6To1,
60     kResamplerMode12To1,
61     kResamplerMode3To2,
62     kResamplerMode11To2,
63     kResamplerMode11To4,
64     kResamplerMode11To8
65   };
66 
67   // Computes the resampler mode for a given sampling frequency pair.
68   // Returns -1 for unsupported frequency pairs.
69   static int ComputeResamplerMode(int in_freq_hz,
70                                   int out_freq_hz,
71                                   ResamplerMode* mode);
72 
73   // Generic pointers since we don't know what states we'll need
74   void* state1_;
75   void* state2_;
76   void* state3_;
77 
78   // Storage if needed
79   int16_t* in_buffer_;
80   int16_t* out_buffer_;
81   size_t in_buffer_size_;
82   size_t out_buffer_size_;
83   size_t in_buffer_size_max_;
84   size_t out_buffer_size_max_;
85 
86   int my_in_frequency_khz_;
87   int my_out_frequency_khz_;
88   ResamplerMode my_mode_;
89   size_t num_channels_;
90 
91   // Extra instance for stereo
92   Resampler* slave_left_;
93   Resampler* slave_right_;
94 };
95 
96 }  // namespace webrtc
97 
98 #endif  // COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_
99