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