webrtc_cng.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_
  11. #define MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_
  12. #include <stdint.h>
  13. #include <cstddef>
  14. #include "api/array_view.h"
  15. #include "rtc_base/buffer.h"
  16. #define WEBRTC_CNG_MAX_LPC_ORDER 12
  17. namespace webrtc {
  18. class ComfortNoiseDecoder {
  19. public:
  20. ComfortNoiseDecoder();
  21. ~ComfortNoiseDecoder() = default;
  22. ComfortNoiseDecoder(const ComfortNoiseDecoder&) = delete;
  23. ComfortNoiseDecoder& operator=(const ComfortNoiseDecoder&) = delete;
  24. void Reset();
  25. // Updates the CN state when a new SID packet arrives.
  26. // |sid| is a view of the SID packet without the headers.
  27. void UpdateSid(rtc::ArrayView<const uint8_t> sid);
  28. // Generates comfort noise.
  29. // |out_data| will be filled with samples - its size determines the number of
  30. // samples generated. When |new_period| is true, CNG history will be reset
  31. // before any audio is generated. Returns |false| if outData is too large -
  32. // currently 640 bytes (equalling 10ms at 64kHz).
  33. // TODO(ossu): Specify better limits for the size of out_data. Either let it
  34. // be unbounded or limit to 10ms in the current sample rate.
  35. bool Generate(rtc::ArrayView<int16_t> out_data, bool new_period);
  36. private:
  37. uint32_t dec_seed_;
  38. int32_t dec_target_energy_;
  39. int32_t dec_used_energy_;
  40. int16_t dec_target_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  41. int16_t dec_used_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  42. int16_t dec_filtstate_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  43. int16_t dec_filtstateLow_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  44. uint16_t dec_order_;
  45. int16_t dec_target_scale_factor_; /* Q29 */
  46. int16_t dec_used_scale_factor_; /* Q29 */
  47. };
  48. class ComfortNoiseEncoder {
  49. public:
  50. // Creates a comfort noise encoder.
  51. // |fs| selects sample rate: 8000 for narrowband or 16000 for wideband.
  52. // |interval| sets the interval at which to generate SID data (in ms).
  53. // |quality| selects the number of refl. coeffs. Maximum allowed is 12.
  54. ComfortNoiseEncoder(int fs, int interval, int quality);
  55. ~ComfortNoiseEncoder() = default;
  56. ComfortNoiseEncoder(const ComfortNoiseEncoder&) = delete;
  57. ComfortNoiseEncoder& operator=(const ComfortNoiseEncoder&) = delete;
  58. // Resets the comfort noise encoder to its initial state.
  59. // Parameters are set as during construction.
  60. void Reset(int fs, int interval, int quality);
  61. // Analyzes background noise from |speech| and appends coefficients to
  62. // |output|. Returns the number of coefficients generated. If |force_sid| is
  63. // true, a SID frame is forced and the internal sid interval counter is reset.
  64. // Will fail if the input size is too large (> 640 samples, see
  65. // ComfortNoiseDecoder::Generate).
  66. size_t Encode(rtc::ArrayView<const int16_t> speech,
  67. bool force_sid,
  68. rtc::Buffer* output);
  69. private:
  70. size_t enc_nrOfCoefs_;
  71. int enc_sampfreq_;
  72. int16_t enc_interval_;
  73. int16_t enc_msSinceSid_;
  74. int32_t enc_Energy_;
  75. int16_t enc_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  76. int32_t enc_corrVector_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  77. uint32_t enc_seed_;
  78. };
  79. } // namespace webrtc
  80. #endif // MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_