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 | |
| 16 | #ifndef WEBRTC_RESAMPLER_RESAMPLER_H_ |
| 17 | #define WEBRTC_RESAMPLER_RESAMPLER_H_ |
| 18 | |
pbos@webrtc.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame] | 19 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc |
| 22 | { |
| 23 | |
andrew@webrtc.org | 5cdec9e | 2011-08-16 16:39:32 +0000 | [diff] [blame] | 24 | // TODO(andrew): the implementation depends on the exact values of this enum. |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 25 | // It should be rewritten in a less fragile way. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 38 | // TODO(andrew): doesn't need to be part of the interface. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | enum ResamplerMode |
| 40 | { |
| 41 | kResamplerMode1To1, |
| 42 | kResamplerMode1To2, |
| 43 | kResamplerMode1To3, |
| 44 | kResamplerMode1To4, |
| 45 | kResamplerMode1To6, |
bjornv@webrtc.org | 48b68c0 | 2011-11-23 13:50:41 +0000 | [diff] [blame] | 46 | kResamplerMode1To12, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | kResamplerMode2To3, |
| 48 | kResamplerMode2To11, |
| 49 | kResamplerMode4To11, |
| 50 | kResamplerMode8To11, |
| 51 | kResamplerMode11To16, |
| 52 | kResamplerMode11To32, |
| 53 | kResamplerMode2To1, |
| 54 | kResamplerMode3To1, |
| 55 | kResamplerMode4To1, |
| 56 | kResamplerMode6To1, |
bjornv@webrtc.org | 48b68c0 | 2011-11-23 13:50:41 +0000 | [diff] [blame] | 57 | kResamplerMode12To1, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | kResamplerMode3To2, |
| 59 | kResamplerMode11To2, |
| 60 | kResamplerMode11To4, |
| 61 | kResamplerMode11To8 |
| 62 | }; |
| 63 | |
| 64 | class Resampler |
| 65 | { |
| 66 | |
| 67 | public: |
| 68 | Resampler(); |
andrew@webrtc.org | 19eefdc | 2011-09-14 17:02:44 +0000 | [diff] [blame] | 69 | // TODO(andrew): use an init function instead. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 80 | int Push(const int16_t* samplesIn, int lengthIn, int16_t* samplesOut, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | int maxLen, int &outLen); |
| 82 | |
| 83 | // Asynchronous resampling, input |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 84 | int Insert(int16_t* samplesIn, int lengthIn); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
| 86 | // Asynchronous resampling output, remaining samples are buffered |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 87 | int Pull(int16_t* samplesOut, int desiredLen, int &outLen); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 |
pbos@webrtc.org | b091307 | 2013-04-09 16:40:28 +0000 | [diff] [blame] | 96 | int16_t* in_buffer_; |
| 97 | int16_t* out_buffer_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 114 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | |
| 116 | #endif // WEBRTC_RESAMPLER_RESAMPLER_H_ |