19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*
29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
39a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *
49a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
59a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
69a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
79a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
89a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
99a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org */
109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
11e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_COMFORT_NOISE_H_
12e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_COMFORT_NOISE_H_
139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
14774b3d38a4a0f1a8ec08972a3c543cb5d607ce13henrike@webrtc.org#include "webrtc/base/constructormagic.h"
15e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "webrtc/typedefs.h"
179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgnamespace webrtc {
199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Forward declarations.
219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgclass DecoderDatabase;
229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgclass SyncBuffer;
239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgstruct Packet;
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// This class acts as an interface to the CNG generator.
269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgclass ComfortNoise {
279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org public:
289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  enum ReturnCodes {
299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    kOK = 0,
309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    kUnknownPayloadType,
319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    kInternalError,
329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    kMultiChannelNotSupported
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  };
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  ComfortNoise(int fs_hz, DecoderDatabase* decoder_database,
369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org               SyncBuffer* sync_buffer)
379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      : fs_hz_(fs_hz),
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        first_call_(true),
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        overlap_length_(5 * fs_hz_ / 8000),
409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        decoder_database_(decoder_database),
419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        sync_buffer_(sync_buffer),
429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        internal_error_code_(0) {
439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Resets the state. Should be called before each new comfort noise period.
469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void Reset();
479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Update the comfort noise generator with the parameters in |packet|.
499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Will delete the packet.
509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int UpdateParameters(Packet* packet);
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Generates |requested_length| samples of comfort noise and writes to
539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // |output|. If this is the first in call after Reset (or first after creating
549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // the object), it will also mix in comfort noise at the end of the
559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // SyncBuffer object provided in the constructor.
560e9c399746f45ceaf46f12b11ba93c09cca0c2bbhenrik.lundin@webrtc.org  int Generate(size_t requested_length, AudioMultiVector* output);
579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Returns the last error code that was produced by the comfort noise
599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // decoder. Returns 0 if no error has been encountered since the last reset.
609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int internal_error_code() { return internal_error_code_; }
619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org private:
639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int fs_hz_;
649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  bool first_call_;
659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  size_t overlap_length_;
669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DecoderDatabase* decoder_database_;
679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  SyncBuffer* sync_buffer_;
689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int internal_error_code_;
699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DISALLOW_COPY_AND_ASSIGN(ComfortNoise);
709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org};
719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}  // namespace webrtc
73e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_COMFORT_NOISE_H_
74