niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 91b359e | 2012-02-28 17:26:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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_CODING_CODECS_CNG_WEBRTC_CNG_H_ |
| 12 | #define MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 15 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 16 | #include <cstddef> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/array_view.h" |
| 19 | #include "rtc_base/buffer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | #define WEBRTC_CNG_MAX_LPC_ORDER 12 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 23 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 25 | class ComfortNoiseDecoder { |
| 26 | public: |
| 27 | ComfortNoiseDecoder(); |
| 28 | ~ComfortNoiseDecoder() = default; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 30 | ComfortNoiseDecoder(const ComfortNoiseDecoder&) = delete; |
| 31 | ComfortNoiseDecoder& operator=(const ComfortNoiseDecoder&) = delete; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 33 | void Reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 35 | // Updates the CN state when a new SID packet arrives. |
| 36 | // |sid| is a view of the SID packet without the headers. |
| 37 | void UpdateSid(rtc::ArrayView<const uint8_t> sid); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 39 | // Generates comfort noise. |
| 40 | // |out_data| will be filled with samples - its size determines the number of |
| 41 | // samples generated. When |new_period| is true, CNG history will be reset |
| 42 | // before any audio is generated. Returns |false| if outData is too large - |
| 43 | // currently 640 bytes (equalling 10ms at 64kHz). |
| 44 | // TODO(ossu): Specify better limits for the size of out_data. Either let it |
| 45 | // be unbounded or limit to 10ms in the current sample rate. |
| 46 | bool Generate(rtc::ArrayView<int16_t> out_data, bool new_period); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 48 | private: |
| 49 | uint32_t dec_seed_; |
| 50 | int32_t dec_target_energy_; |
| 51 | int32_t dec_used_energy_; |
| 52 | int16_t dec_target_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 53 | int16_t dec_used_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 54 | int16_t dec_filtstate_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 55 | int16_t dec_filtstateLow_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 56 | uint16_t dec_order_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 57 | int16_t dec_target_scale_factor_; /* Q29 */ |
| 58 | int16_t dec_used_scale_factor_; /* Q29 */ |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 59 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 61 | class ComfortNoiseEncoder { |
| 62 | public: |
| 63 | // Creates a comfort noise encoder. |
| 64 | // |fs| selects sample rate: 8000 for narrowband or 16000 for wideband. |
| 65 | // |interval| sets the interval at which to generate SID data (in ms). |
| 66 | // |quality| selects the number of refl. coeffs. Maximum allowed is 12. |
| 67 | ComfortNoiseEncoder(int fs, int interval, int quality); |
| 68 | ~ComfortNoiseEncoder() = default; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 70 | ComfortNoiseEncoder(const ComfortNoiseEncoder&) = delete; |
| 71 | ComfortNoiseEncoder& operator=(const ComfortNoiseEncoder&) = delete; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 73 | // Resets the comfort noise encoder to its initial state. |
| 74 | // Parameters are set as during construction. |
| 75 | void Reset(int fs, int interval, int quality); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 77 | // Analyzes background noise from |speech| and appends coefficients to |
| 78 | // |output|. Returns the number of coefficients generated. If |force_sid| is |
| 79 | // true, a SID frame is forced and the internal sid interval counter is reset. |
| 80 | // Will fail if the input size is too large (> 640 samples, see |
| 81 | // ComfortNoiseDecoder::Generate). |
| 82 | size_t Encode(rtc::ArrayView<const int16_t> speech, |
| 83 | bool force_sid, |
| 84 | rtc::Buffer* output); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 86 | private: |
| 87 | size_t enc_nrOfCoefs_; |
| 88 | int enc_sampfreq_; |
| 89 | int16_t enc_interval_; |
| 90 | int16_t enc_msSinceSid_; |
| 91 | int32_t enc_Energy_; |
| 92 | int16_t enc_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 93 | int32_t enc_corrVector_[WEBRTC_CNG_MAX_LPC_ORDER + 1]; |
| 94 | uint32_t enc_seed_; |
| 95 | }; |
| 96 | |
| 97 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 99 | #endif // MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ |