You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "modules/audio_coding/neteq/accelerate.h"
  11. #include <assert.h>
  12. #include "api/array_view.h"
  13. #include "modules/audio_coding/neteq/audio_multi_vector.h"
  14. namespace webrtc {
  15. Accelerate::ReturnCodes Accelerate::Process(const int16_t* input,
  16. size_t input_length,
  17. bool fast_accelerate,
  18. AudioMultiVector* output,
  19. size_t* length_change_samples) {
  20. // Input length must be (almost) 30 ms.
  21. static const size_t k15ms = 120; // 15 ms = 120 samples at 8 kHz sample rate.
  22. if (num_channels_ == 0 ||
  23. input_length / num_channels_ < (2 * k15ms - 1) * fs_mult_) {
  24. // Length of input data too short to do accelerate. Simply move all data
  25. // from input to output.
  26. output->PushBackInterleaved(
  27. rtc::ArrayView<const int16_t>(input, input_length));
  28. return kError;
  29. }
  30. return TimeStretch::Process(input, input_length, fast_accelerate, output,
  31. length_change_samples);
  32. }
  33. void Accelerate::SetParametersForPassiveSpeech(size_t /*len*/,
  34. int16_t* best_correlation,
  35. size_t* /*peak_index*/) const {
  36. // When the signal does not contain any active speech, the correlation does
  37. // not matter. Simply set it to zero.
  38. *best_correlation = 0;
  39. }
  40. Accelerate::ReturnCodes Accelerate::CheckCriteriaAndStretch(
  41. const int16_t* input,
  42. size_t input_length,
  43. size_t peak_index,
  44. int16_t best_correlation,
  45. bool active_speech,
  46. bool fast_mode,
  47. AudioMultiVector* output) const {
  48. // Check for strong correlation or passive speech.
  49. // Use 8192 (0.5 in Q14) in fast mode.
  50. const int correlation_threshold = fast_mode ? 8192 : kCorrelationThreshold;
  51. if ((best_correlation > correlation_threshold) || !active_speech) {
  52. // Do accelerate operation by overlap add.
  53. // Pre-calculate common multiplication with |fs_mult_|.
  54. // 120 corresponds to 15 ms.
  55. size_t fs_mult_120 = fs_mult_ * 120;
  56. if (fast_mode) {
  57. // Fit as many multiples of |peak_index| as possible in fs_mult_120.
  58. // TODO(henrik.lundin) Consider finding multiple correlation peaks and
  59. // pick the one with the longest correlation lag in this case.
  60. peak_index = (fs_mult_120 / peak_index) * peak_index;
  61. }
  62. assert(fs_mult_120 >= peak_index); // Should be handled in Process().
  63. // Copy first part; 0 to 15 ms.
  64. output->PushBackInterleaved(
  65. rtc::ArrayView<const int16_t>(input, fs_mult_120 * num_channels_));
  66. // Copy the |peak_index| starting at 15 ms to |temp_vector|.
  67. AudioMultiVector temp_vector(num_channels_);
  68. temp_vector.PushBackInterleaved(rtc::ArrayView<const int16_t>(
  69. &input[fs_mult_120 * num_channels_], peak_index * num_channels_));
  70. // Cross-fade |temp_vector| onto the end of |output|.
  71. output->CrossFade(temp_vector, peak_index);
  72. // Copy the last unmodified part, 15 ms + pitch period until the end.
  73. output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
  74. &input[(fs_mult_120 + peak_index) * num_channels_],
  75. input_length - (fs_mult_120 + peak_index) * num_channels_));
  76. if (active_speech) {
  77. return kSuccess;
  78. } else {
  79. return kSuccessLowEnergy;
  80. }
  81. } else {
  82. // Accelerate not allowed. Simply move all data from decoded to outData.
  83. output->PushBackInterleaved(
  84. rtc::ArrayView<const int16_t>(input, input_length));
  85. return kNoStretch;
  86. }
  87. }
  88. Accelerate* AccelerateFactory::Create(
  89. int sample_rate_hz,
  90. size_t num_channels,
  91. const BackgroundNoise& background_noise) const {
  92. return new Accelerate(sample_rate_hz, num_channels, background_noise);
  93. }
  94. } // namespace webrtc