pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_TRANSIENT_DYADIC_DECIMATOR_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_TRANSIENT_DYADIC_DECIMATOR_H_ |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 13 | |
| 14 | #include <cstdlib> |
| 15 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 16 | #include "typedefs.h" // NOLINT(build/include) |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 17 | |
| 18 | // Provides a set of static methods to perform dyadic decimations. |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // Returns the proper length of the output buffer that you should use for the |
| 23 | // given |in_length| and decimation |odd_sequence|. |
| 24 | // Return -1 on error. |
| 25 | inline size_t GetOutLengthToDyadicDecimate(size_t in_length, |
| 26 | bool odd_sequence) { |
| 27 | size_t out_length = in_length / 2; |
| 28 | |
| 29 | if (in_length % 2 == 1 && !odd_sequence) { |
| 30 | ++out_length; |
| 31 | } |
| 32 | |
| 33 | return out_length; |
| 34 | } |
| 35 | |
| 36 | // Performs a dyadic decimation: removes every odd/even member of a sequence |
| 37 | // halving its overall length. |
| 38 | // Arguments: |
| 39 | // in: array of |in_length|. |
| 40 | // odd_sequence: If false, the odd members will be removed (1, 3, 5, ...); |
| 41 | // if true, the even members will be removed (0, 2, 4, ...). |
| 42 | // out: array of |out_length|. |out_length| must be large enough to |
| 43 | // hold the decimated output. The necessary length can be provided by |
| 44 | // GetOutLengthToDyadicDecimate(). |
| 45 | // Must be previously allocated. |
| 46 | // Returns the number of output samples, -1 on error. |
| 47 | template<typename T> |
| 48 | static size_t DyadicDecimate(const T* in, |
| 49 | size_t in_length, |
| 50 | bool odd_sequence, |
| 51 | T* out, |
| 52 | size_t out_length) { |
| 53 | size_t half_length = GetOutLengthToDyadicDecimate(in_length, odd_sequence); |
| 54 | |
| 55 | if (!in || !out || in_length <= 0 || out_length < half_length) { |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | size_t output_samples = 0; |
| 60 | size_t index_adjustment = odd_sequence ? 1 : 0; |
| 61 | for (output_samples = 0; output_samples < half_length; ++output_samples) { |
| 62 | out[output_samples] = in[output_samples * 2 + index_adjustment]; |
| 63 | } |
| 64 | |
| 65 | return output_samples; |
| 66 | } |
| 67 | |
| 68 | } // namespace webrtc |
| 69 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 70 | #endif // MODULES_AUDIO_PROCESSING_TRANSIENT_DYADIC_DECIMATOR_H_ |