1 /* 2 * Copyright (c) 2012 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 #ifndef MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ 12 #define MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ 13 14 #include <stdint.h> 15 16 #include <cstddef> 17 18 #include "api/array_view.h" 19 #include "rtc_base/buffer.h" 20 21 #define WEBRTC_CNG_MAX_LPC_ORDER 12 22 23 namespace webrtc { 24 25 class ComfortNoiseDecoder { 26 public: 27 ComfortNoiseDecoder(); 28 ~ComfortNoiseDecoder() = default; 29 30 ComfortNoiseDecoder(const ComfortNoiseDecoder&) = delete; 31 ComfortNoiseDecoder& operator=(const ComfortNoiseDecoder&) = delete; 32 33 void Reset(); 34 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); 38 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); 47 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_; 57 int16_t dec_target_scale_factor_; /* Q29 */ 58 int16_t dec_used_scale_factor_; /* Q29 */ 59 }; 60 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; 69 70 ComfortNoiseEncoder(const ComfortNoiseEncoder&) = delete; 71 ComfortNoiseEncoder& operator=(const ComfortNoiseEncoder&) = delete; 72 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); 76 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); 85 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 98 99 #endif // MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_ 100