niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #ifndef COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_ |
| 17 | #define COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 21 | #include "typedefs.h" // NOLINT(build/include) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 23 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
wtc@chromium.org | 4553941 | 2015-03-20 23:28:07 +0000 | [diff] [blame] | 25 | // All methods return 0 on success and -1 on failure. |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 26 | class Resampler { |
| 27 | public: |
| 28 | Resampler(); |
| 29 | Resampler(int inFreq, int outFreq, size_t num_channels); |
| 30 | ~Resampler(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 32 | // Reset all states |
| 33 | int Reset(int inFreq, int outFreq, size_t num_channels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 35 | // Reset all states if any parameter has changed |
| 36 | int ResetIfNeeded(int inFreq, int outFreq, size_t num_channels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 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 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 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 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | |
Sam Zackrisson | 9da7c74 | 2017-10-30 10:05:10 +0100 | [diff] [blame] | 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 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 73 | // Generic pointers since we don't know what states we'll need |
| 74 | void* state1_; |
| 75 | void* state2_; |
| 76 | void* state3_; |
Andrew MacDonald | 2c9c83d | 2015-03-30 10:08:22 -0700 | [diff] [blame] | 77 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 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_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 86 | int my_in_frequency_khz_; |
| 87 | int my_out_frequency_khz_; |
| 88 | ResamplerMode my_mode_; |
| 89 | size_t num_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 91 | // Extra instance for stereo |
| 92 | Resampler* slave_left_; |
| 93 | Resampler* slave_right_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 96 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 98 | #endif // COMMON_AUDIO_RESAMPLER_INCLUDE_RESAMPLER_H_ |