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 #include "modules/audio_coding/neteq/comfort_noise.h" 12 13 14 #include <cstdint> 15 #include <memory> 16 17 #include "api/array_view.h" 18 #include "modules/audio_coding/codecs/cng/webrtc_cng.h" 19 #include "modules/audio_coding/neteq/audio_multi_vector.h" 20 #include "modules/audio_coding/neteq/audio_vector.h" 21 #include "modules/audio_coding/neteq/decoder_database.h" 22 #include "modules/audio_coding/neteq/dsp_helper.h" 23 #include "modules/audio_coding/neteq/sync_buffer.h" 24 #include "rtc_base/buffer.h" 25 #include "rtc_base/checks.h" 26 #include "rtc_base/logging.h" 27 28 namespace webrtc { 29 Reset()30void ComfortNoise::Reset() { 31 first_call_ = true; 32 } 33 UpdateParameters(const Packet & packet)34int ComfortNoise::UpdateParameters(const Packet& packet) { 35 // Get comfort noise decoder. 36 if (decoder_database_->SetActiveCngDecoder(packet.payload_type) != kOK) { 37 return kUnknownPayloadType; 38 } 39 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); 40 RTC_DCHECK(cng_decoder); 41 cng_decoder->UpdateSid(packet.payload); 42 return kOK; 43 } 44 Generate(size_t requested_length,AudioMultiVector * output)45int ComfortNoise::Generate(size_t requested_length, AudioMultiVector* output) { 46 // TODO(hlundin): Change to an enumerator and skip assert. 47 RTC_DCHECK(fs_hz_ == 8000 || fs_hz_ == 16000 || fs_hz_ == 32000 || 48 fs_hz_ == 48000); 49 // Not adapted for multi-channel yet. 50 if (output->Channels() != 1) { 51 RTC_LOG(LS_ERROR) << "No multi-channel support"; 52 return kMultiChannelNotSupported; 53 } 54 55 size_t number_of_samples = requested_length; 56 bool new_period = false; 57 if (first_call_) { 58 // Generate noise and overlap slightly with old data. 59 number_of_samples = requested_length + overlap_length_; 60 new_period = true; 61 } 62 output->AssertSize(number_of_samples); 63 // Get the decoder from the database. 64 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); 65 if (!cng_decoder) { 66 RTC_LOG(LS_ERROR) << "Unknwown payload type"; 67 return kUnknownPayloadType; 68 } 69 70 std::unique_ptr<int16_t[]> temp(new int16_t[number_of_samples]); 71 if (!cng_decoder->Generate( 72 rtc::ArrayView<int16_t>(temp.get(), number_of_samples), new_period)) { 73 // Error returned. 74 output->Zeros(requested_length); 75 RTC_LOG(LS_ERROR) 76 << "ComfortNoiseDecoder::Genererate failed to generate comfort noise"; 77 return kInternalError; 78 } 79 (*output)[0].OverwriteAt(temp.get(), number_of_samples, 0); 80 81 if (first_call_) { 82 // Set tapering window parameters. Values are in Q15. 83 int16_t muting_window; // Mixing factor for overlap data. 84 int16_t muting_window_increment; // Mixing factor increment (negative). 85 int16_t unmuting_window; // Mixing factor for comfort noise. 86 int16_t unmuting_window_increment; // Mixing factor increment. 87 if (fs_hz_ == 8000) { 88 muting_window = DspHelper::kMuteFactorStart8kHz; 89 muting_window_increment = DspHelper::kMuteFactorIncrement8kHz; 90 unmuting_window = DspHelper::kUnmuteFactorStart8kHz; 91 unmuting_window_increment = DspHelper::kUnmuteFactorIncrement8kHz; 92 } else if (fs_hz_ == 16000) { 93 muting_window = DspHelper::kMuteFactorStart16kHz; 94 muting_window_increment = DspHelper::kMuteFactorIncrement16kHz; 95 unmuting_window = DspHelper::kUnmuteFactorStart16kHz; 96 unmuting_window_increment = DspHelper::kUnmuteFactorIncrement16kHz; 97 } else if (fs_hz_ == 32000) { 98 muting_window = DspHelper::kMuteFactorStart32kHz; 99 muting_window_increment = DspHelper::kMuteFactorIncrement32kHz; 100 unmuting_window = DspHelper::kUnmuteFactorStart32kHz; 101 unmuting_window_increment = DspHelper::kUnmuteFactorIncrement32kHz; 102 } else { // fs_hz_ == 48000 103 muting_window = DspHelper::kMuteFactorStart48kHz; 104 muting_window_increment = DspHelper::kMuteFactorIncrement48kHz; 105 unmuting_window = DspHelper::kUnmuteFactorStart48kHz; 106 unmuting_window_increment = DspHelper::kUnmuteFactorIncrement48kHz; 107 } 108 109 // Do overlap-add between new vector and overlap. 110 size_t start_ix = sync_buffer_->Size() - overlap_length_; 111 for (size_t i = 0; i < overlap_length_; i++) { 112 /* overlapVec[i] = WinMute * overlapVec[i] + WinUnMute * outData[i] */ 113 // The expression (*output)[0][i] is the i-th element in the first 114 // channel. 115 (*sync_buffer_)[0][start_ix + i] = 116 (((*sync_buffer_)[0][start_ix + i] * muting_window) + 117 ((*output)[0][i] * unmuting_window) + 16384) >> 118 15; 119 muting_window += muting_window_increment; 120 unmuting_window += unmuting_window_increment; 121 } 122 // Remove `overlap_length_` samples from the front of `output` since they 123 // were mixed into `sync_buffer_` above. 124 output->PopFront(overlap_length_); 125 } 126 first_call_ = false; 127 return kOK; 128 } 129 130 } // namespace webrtc 131