1 /*
2  *  Copyright (c) 2020 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_processing/include/audio_frame_proxies.h"
12 
13 #include "api/audio/audio_frame.h"
14 #include "modules/audio_processing/include/audio_processing.h"
15 
16 namespace webrtc {
17 
ProcessAudioFrame(AudioProcessing * ap,AudioFrame * frame)18 int ProcessAudioFrame(AudioProcessing* ap, AudioFrame* frame) {
19   if (!frame || !ap) {
20     return AudioProcessing::Error::kNullPointerError;
21   }
22 
23   StreamConfig input_config(frame->sample_rate_hz_, frame->num_channels_,
24                             /*has_keyboard=*/false);
25   StreamConfig output_config(frame->sample_rate_hz_, frame->num_channels_,
26                              /*has_keyboard=*/false);
27   RTC_DCHECK_EQ(frame->samples_per_channel(), input_config.num_frames());
28 
29   int result = ap->ProcessStream(frame->data(), input_config, output_config,
30                                  frame->mutable_data());
31 
32   AudioProcessingStats stats = ap->GetStatistics();
33 
34   if (stats.voice_detected) {
35     frame->vad_activity_ = *stats.voice_detected
36                                ? AudioFrame::VADActivity::kVadActive
37                                : AudioFrame::VADActivity::kVadPassive;
38   }
39 
40   return result;
41 }
42 
ProcessReverseAudioFrame(AudioProcessing * ap,AudioFrame * frame)43 int ProcessReverseAudioFrame(AudioProcessing* ap, AudioFrame* frame) {
44   if (!frame || !ap) {
45     return AudioProcessing::Error::kNullPointerError;
46   }
47 
48   // Must be a native rate.
49   if (frame->sample_rate_hz_ != AudioProcessing::NativeRate::kSampleRate8kHz &&
50       frame->sample_rate_hz_ != AudioProcessing::NativeRate::kSampleRate16kHz &&
51       frame->sample_rate_hz_ != AudioProcessing::NativeRate::kSampleRate32kHz &&
52       frame->sample_rate_hz_ != AudioProcessing::NativeRate::kSampleRate48kHz) {
53     return AudioProcessing::Error::kBadSampleRateError;
54   }
55 
56   if (frame->num_channels_ <= 0) {
57     return AudioProcessing::Error::kBadNumberChannelsError;
58   }
59 
60   StreamConfig input_config(frame->sample_rate_hz_, frame->num_channels_,
61                             /*has_keyboard=*/false);
62   StreamConfig output_config(frame->sample_rate_hz_, frame->num_channels_,
63                              /*has_keyboard=*/false);
64 
65   int result = ap->ProcessReverseStream(frame->data(), input_config,
66                                         output_config, frame->mutable_data());
67   return result;
68 }
69 
70 }  // namespace webrtc
71