blob: fec2c1a7bf2883f756d640887fda4e56cdfaf45a [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#ifndef COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_
17#define COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000018
Peter Kastingdce40cf2015-08-24 14:52:23 -070019#include <stddef.h>
20
Mirko Bonadei71207422017-09-15 13:58:09 +020021#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000022
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070023namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
wtc@chromium.org45539412015-03-20 23:28:07 +000025// All methods return 0 on success and -1 on failure.
oprypin67fdb802017-03-09 06:25:06 -080026class Resampler {
27 public:
28 Resampler();
29 Resampler(int inFreq, int outFreq, size_t num_channels);
30 ~Resampler();
niklase@google.com470e71d2011-07-07 08:21:25 +000031
oprypin67fdb802017-03-09 06:25:06 -080032 // Reset all states
33 int Reset(int inFreq, int outFreq, size_t num_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000034
oprypin67fdb802017-03-09 06:25:06 -080035 // Reset all states if any parameter has changed
36 int ResetIfNeeded(int inFreq, int outFreq, size_t num_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000037
oprypin67fdb802017-03-09 06:25:06 -080038 // 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
niklase@google.com470e71d2011-07-07 08:21:25 +000041
oprypin67fdb802017-03-09 06:25:06 -080042 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 };
niklase@google.com470e71d2011-07-07 08:21:25 +000066
Sam Zackrisson9da7c742017-10-30 10:05:10 +010067 // 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
oprypin67fdb802017-03-09 06:25:06 -080073 // Generic pointers since we don't know what states we'll need
74 void* state1_;
75 void* state2_;
76 void* state3_;
Andrew MacDonald2c9c83d2015-03-30 10:08:22 -070077
oprypin67fdb802017-03-09 06:25:06 -080078 // 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_;
niklase@google.com470e71d2011-07-07 08:21:25 +000085
oprypin67fdb802017-03-09 06:25:06 -080086 int my_in_frequency_khz_;
87 int my_out_frequency_khz_;
88 ResamplerMode my_mode_;
89 size_t num_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +000090
oprypin67fdb802017-03-09 06:25:06 -080091 // Extra instance for stereo
92 Resampler* slave_left_;
93 Resampler* slave_right_;
niklase@google.com470e71d2011-07-07 08:21:25 +000094};
95
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000096} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000097
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020098#endif // COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_