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.

93 lines
2.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2019 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_processing/voice_detection.h"
  11. #include "api/audio/audio_frame.h"
  12. #include "common_audio/vad/include/webrtc_vad.h"
  13. #include "modules/audio_processing/audio_buffer.h"
  14. #include "rtc_base/checks.h"
  15. namespace webrtc {
  16. class VoiceDetection::Vad {
  17. public:
  18. Vad() {
  19. state_ = WebRtcVad_Create();
  20. RTC_CHECK(state_);
  21. int error = WebRtcVad_Init(state_);
  22. RTC_DCHECK_EQ(0, error);
  23. }
  24. ~Vad() { WebRtcVad_Free(state_); }
  25. Vad(Vad&) = delete;
  26. Vad& operator=(Vad&) = delete;
  27. VadInst* state() { return state_; }
  28. private:
  29. VadInst* state_ = nullptr;
  30. };
  31. VoiceDetection::VoiceDetection(int sample_rate_hz, Likelihood likelihood)
  32. : sample_rate_hz_(sample_rate_hz),
  33. frame_size_samples_(static_cast<size_t>(sample_rate_hz_ / 100)),
  34. likelihood_(likelihood),
  35. vad_(new Vad()) {
  36. int mode = 2;
  37. switch (likelihood) {
  38. case VoiceDetection::kVeryLowLikelihood:
  39. mode = 3;
  40. break;
  41. case VoiceDetection::kLowLikelihood:
  42. mode = 2;
  43. break;
  44. case VoiceDetection::kModerateLikelihood:
  45. mode = 1;
  46. break;
  47. case VoiceDetection::kHighLikelihood:
  48. mode = 0;
  49. break;
  50. default:
  51. RTC_NOTREACHED();
  52. break;
  53. }
  54. int error = WebRtcVad_set_mode(vad_->state(), mode);
  55. RTC_DCHECK_EQ(0, error);
  56. }
  57. VoiceDetection::~VoiceDetection() {}
  58. bool VoiceDetection::ProcessCaptureAudio(AudioBuffer* audio) {
  59. RTC_DCHECK_GE(AudioBuffer::kMaxSplitFrameLength,
  60. audio->num_frames_per_band());
  61. std::array<int16_t, AudioBuffer::kMaxSplitFrameLength> mixed_low_pass_data;
  62. rtc::ArrayView<const int16_t> mixed_low_pass(mixed_low_pass_data.data(),
  63. audio->num_frames_per_band());
  64. if (audio->num_channels() == 1) {
  65. FloatS16ToS16(audio->split_bands_const(0)[kBand0To8kHz],
  66. audio->num_frames_per_band(), mixed_low_pass_data.data());
  67. } else {
  68. const int num_channels = static_cast<int>(audio->num_channels());
  69. for (size_t i = 0; i < audio->num_frames_per_band(); ++i) {
  70. int32_t value =
  71. FloatS16ToS16(audio->split_channels_const(kBand0To8kHz)[0][i]);
  72. for (int j = 1; j < num_channels; ++j) {
  73. value += FloatS16ToS16(audio->split_channels_const(kBand0To8kHz)[j][i]);
  74. }
  75. mixed_low_pass_data[i] = value / num_channels;
  76. }
  77. }
  78. int vad_ret = WebRtcVad_Process(vad_->state(), sample_rate_hz_,
  79. mixed_low_pass.data(), frame_size_samples_);
  80. RTC_DCHECK(vad_ret == 0 || vad_ret == 1);
  81. return vad_ret == 0 ? false : true;
  82. }
  83. } // namespace webrtc