1d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org/* 2d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * 4d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * Use of this source code is governed by a BSD-style license 5d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * that can be found in the LICENSE file in the root of the source 6d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * tree. An additional intellectual property rights grant can be found 7d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * in the file PATENTS. All contributing project authors may 8d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org * be found in the AUTHORS file in the root of the source tree. 9d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org */ 10d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 119c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/accelerate.h" 12d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 13d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" 14d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 15d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgnamespace webrtc { 16d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 17cf808d2366e58b33540931d182f36800d9a15b0dHenrik LundinAccelerate::ReturnCodes Accelerate::Process(const int16_t* input, 18cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin size_t input_length, 19cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin bool fast_accelerate, 20cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin AudioMultiVector* output, 21dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting size_t* length_change_samples) { 22d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Input length must be (almost) 30 ms. 23dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting static const size_t k15ms = 120; // 15 ms = 120 samples at 8 kHz sample rate. 24dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting if (num_channels_ == 0 || 25dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting input_length / num_channels_ < (2 * k15ms - 1) * fs_mult_) { 26d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Length of input data too short to do accelerate. Simply move all data 27d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // from input to output. 28d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org output->PushBackInterleaved(input, input_length); 29d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org return kError; 30d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org } 31cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin return TimeStretch::Process(input, input_length, fast_accelerate, output, 32d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org length_change_samples); 33d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org} 34d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 35362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.orgvoid Accelerate::SetParametersForPassiveSpeech(size_t /*len*/, 36d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org int16_t* best_correlation, 37dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting size_t* /*peak_index*/) const { 38d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // When the signal does not contain any active speech, the correlation does 39d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // not matter. Simply set it to zero. 40d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *best_correlation = 0; 41d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org} 42d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 43d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgAccelerate::ReturnCodes Accelerate::CheckCriteriaAndStretch( 44cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin const int16_t* input, 45cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin size_t input_length, 46cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin size_t peak_index, 47cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin int16_t best_correlation, 48cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin bool active_speech, 49cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin bool fast_mode, 50fd11bbfb56b42f82e18a744a414325db7a56013fhenrik.lundin@webrtc.org AudioMultiVector* output) const { 51d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Check for strong correlation or passive speech. 52cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin // Use 8192 (0.5 in Q14) in fast mode. 53cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin const int correlation_threshold = fast_mode ? 8192 : kCorrelationThreshold; 54cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin if ((best_correlation > correlation_threshold) || !active_speech) { 55d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Do accelerate operation by overlap add. 56d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 57d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Pre-calculate common multiplication with |fs_mult_|. 58d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // 120 corresponds to 15 ms. 59d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org size_t fs_mult_120 = fs_mult_ * 120; 60d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 61cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin if (fast_mode) { 62cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin // Fit as many multiples of |peak_index| as possible in fs_mult_120. 63cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin // TODO(henrik.lundin) Consider finding multiple correlation peaks and 64cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin // pick the one with the longest correlation lag in this case. 65cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin peak_index = (fs_mult_120 / peak_index) * peak_index; 66cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin } 67cf808d2366e58b33540931d182f36800d9a15b0dHenrik Lundin 68d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org assert(fs_mult_120 >= peak_index); // Should be handled in Process(). 69d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Copy first part; 0 to 15 ms. 70d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org output->PushBackInterleaved(input, fs_mult_120 * num_channels_); 71d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Copy the |peak_index| starting at 15 ms to |temp_vector|. 72fd11bbfb56b42f82e18a744a414325db7a56013fhenrik.lundin@webrtc.org AudioMultiVector temp_vector(num_channels_); 73d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org temp_vector.PushBackInterleaved(&input[fs_mult_120 * num_channels_], 74d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org peak_index * num_channels_); 75d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Cross-fade |temp_vector| onto the end of |output|. 76d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org output->CrossFade(temp_vector, peak_index); 77d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Copy the last unmodified part, 15 ms + pitch period until the end. 78d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org output->PushBackInterleaved( 79d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org &input[(fs_mult_120 + peak_index) * num_channels_], 80d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org input_length - (fs_mult_120 + peak_index) * num_channels_); 81d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 82d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org if (active_speech) { 83d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org return kSuccess; 84d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org } else { 85d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org return kSuccessLowEnergy; 86d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org } 87d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org } else { 88d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org // Accelerate not allowed. Simply move all data from decoded to outData. 89d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org output->PushBackInterleaved(input, input_length); 90d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org return kNoStretch; 91d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org } 92d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org} 93d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org 94d9faa46d5723a14a40300daa9b6d78f4abfd659chenrik.lundin@webrtc.orgAccelerate* AccelerateFactory::Create( 95d9faa46d5723a14a40300daa9b6d78f4abfd659chenrik.lundin@webrtc.org int sample_rate_hz, 96d9faa46d5723a14a40300daa9b6d78f4abfd659chenrik.lundin@webrtc.org size_t num_channels, 97d9faa46d5723a14a40300daa9b6d78f4abfd659chenrik.lundin@webrtc.org const BackgroundNoise& background_noise) const { 98d9faa46d5723a14a40300daa9b6d78f4abfd659chenrik.lundin@webrtc.org return new Accelerate(sample_rate_hz, num_channels, background_noise); 99d9faa46d5723a14a40300daa9b6d78f4abfd659chenrik.lundin@webrtc.org} 100d9faa46d5723a14a40300daa9b6d78f4abfd659chenrik.lundin@webrtc.org 101d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org} // namespace webrtc 102