blob: 4f5703120b71b900a15e1f7a7d2b448b96ba21a4 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000019#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc
22{
23
andrew@webrtc.org5cdec9e2011-08-16 16:39:32 +000024// TODO(andrew): the implementation depends on the exact values of this enum.
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000025// It should be rewritten in a less fragile way.
niklase@google.com470e71d2011-07-07 08:21:25 +000026enum 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
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000038// TODO(andrew): doesn't need to be part of the interface.
niklase@google.com470e71d2011-07-07 08:21:25 +000039enum ResamplerMode
40{
41 kResamplerMode1To1,
42 kResamplerMode1To2,
43 kResamplerMode1To3,
44 kResamplerMode1To4,
45 kResamplerMode1To6,
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000046 kResamplerMode1To12,
niklase@google.com470e71d2011-07-07 08:21:25 +000047 kResamplerMode2To3,
48 kResamplerMode2To11,
49 kResamplerMode4To11,
50 kResamplerMode8To11,
51 kResamplerMode11To16,
52 kResamplerMode11To32,
53 kResamplerMode2To1,
54 kResamplerMode3To1,
55 kResamplerMode4To1,
56 kResamplerMode6To1,
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000057 kResamplerMode12To1,
niklase@google.com470e71d2011-07-07 08:21:25 +000058 kResamplerMode3To2,
59 kResamplerMode11To2,
60 kResamplerMode11To4,
61 kResamplerMode11To8
62};
63
64class Resampler
65{
66
67public:
68 Resampler();
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000069 // TODO(andrew): use an init function instead.
niklase@google.com470e71d2011-07-07 08:21:25 +000070 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
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000080 int Push(const int16_t* samplesIn, int lengthIn, int16_t* samplesOut,
niklase@google.com470e71d2011-07-07 08:21:25 +000081 int maxLen, int &outLen);
82
83 // Asynchronous resampling, input
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000084 int Insert(int16_t* samplesIn, int lengthIn);
niklase@google.com470e71d2011-07-07 08:21:25 +000085
86 // Asynchronous resampling output, remaining samples are buffered
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000087 int Pull(int16_t* samplesOut, int desiredLen, int &outLen);
niklase@google.com470e71d2011-07-07 08:21:25 +000088
89private:
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
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000096 int16_t* in_buffer_;
97 int16_t* out_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +000098 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
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000114} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
116#endif // WEBRTC_RESAMPLER_RESAMPLER_H_