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 WEBRTC_RESAMPLER_RESAMPLER_H_
17 #define WEBRTC_RESAMPLER_RESAMPLER_H_
18 
19 #include "typedefs.h"
20 
21 namespace webrtc
22 {
23 
24 // TODO(andrew): the implementation depends on the exact values of this enum.
25 // It should be rewritten in a less fragile way.
26 enum ResamplerType
27 {
28     // 4 MSB = Number of channels
29     // 4 LSB = Synchronous or asynchronous
30 
31     kResamplerSynchronous = 0x10,
32     kResamplerAsynchronous = 0x11,
33     kResamplerSynchronousStereo = 0x20,
34     kResamplerAsynchronousStereo = 0x21,
35     kResamplerInvalid = 0xff
36 };
37 
38 // TODO(andrew): doesn't need to be part of the interface.
39 enum ResamplerMode
40 {
41     kResamplerMode1To1,
42     kResamplerMode1To2,
43     kResamplerMode1To3,
44     kResamplerMode1To4,
45     kResamplerMode1To6,
46     kResamplerMode1To12,
47     kResamplerMode2To3,
48     kResamplerMode2To11,
49     kResamplerMode4To11,
50     kResamplerMode8To11,
51     kResamplerMode11To16,
52     kResamplerMode11To32,
53     kResamplerMode2To1,
54     kResamplerMode3To1,
55     kResamplerMode4To1,
56     kResamplerMode6To1,
57     kResamplerMode12To1,
58     kResamplerMode3To2,
59     kResamplerMode11To2,
60     kResamplerMode11To4,
61     kResamplerMode11To8
62 };
63 
64 class Resampler
65 {
66 
67 public:
68     Resampler();
69     // TODO(andrew): use an init function instead.
70     Resampler(int inFreq, int outFreq, ResamplerType type);
71     ~Resampler();
72 
73     // Reset all states
74     int Reset(int inFreq, int outFreq, ResamplerType type);
75 
76     // Reset all states if any parameter has changed
77     int ResetIfNeeded(int inFreq, int outFreq, ResamplerType type);
78 
79     // Synchronous resampling, all output samples are written to samplesOut
80     int Push(const WebRtc_Word16* samplesIn, int lengthIn, WebRtc_Word16* samplesOut,
81              int maxLen, int &outLen);
82 
83     // Asynchronous resampling, input
84     int Insert(WebRtc_Word16* samplesIn, int lengthIn);
85 
86     // Asynchronous resampling output, remaining samples are buffered
87     int Pull(WebRtc_Word16* samplesOut, int desiredLen, int &outLen);
88 
89 private:
90     // Generic pointers since we don't know what states we'll need
91     void* state1_;
92     void* state2_;
93     void* state3_;
94 
95     // Storage if needed
96     WebRtc_Word16* in_buffer_;
97     WebRtc_Word16* out_buffer_;
98     int in_buffer_size_;
99     int out_buffer_size_;
100     int in_buffer_size_max_;
101     int out_buffer_size_max_;
102 
103     // State
104     int my_in_frequency_khz_;
105     int my_out_frequency_khz_;
106     ResamplerMode my_mode_;
107     ResamplerType my_type_;
108 
109     // Extra instance for stereo
110     Resampler* slave_left_;
111     Resampler* slave_right_;
112 };
113 
114 } // namespace webrtc
115 
116 #endif // WEBRTC_RESAMPLER_RESAMPLER_H_
117