xref: /aosp_15_r20/external/webrtc/modules/audio_processing/include/audio_frame_proxies.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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   StreamConfig output_config(frame->sample_rate_hz_, frame->num_channels_);
25   RTC_DCHECK_EQ(frame->samples_per_channel(), input_config.num_frames());
26 
27   int result = ap->ProcessStream(frame->data(), input_config, output_config,
28                                  frame->mutable_data());
29 
30   AudioProcessingStats stats = ap->GetStatistics();
31 
32   if (stats.voice_detected) {
33     frame->vad_activity_ = *stats.voice_detected
34                                ? AudioFrame::VADActivity::kVadActive
35                                : AudioFrame::VADActivity::kVadPassive;
36   }
37 
38   return result;
39 }
40 
ProcessReverseAudioFrame(AudioProcessing * ap,AudioFrame * frame)41 int ProcessReverseAudioFrame(AudioProcessing* ap, AudioFrame* frame) {
42   if (!frame || !ap) {
43     return AudioProcessing::Error::kNullPointerError;
44   }
45 
46   // Must be a native rate.
47   if (frame->sample_rate_hz_ != AudioProcessing::NativeRate::kSampleRate8kHz &&
48       frame->sample_rate_hz_ != AudioProcessing::NativeRate::kSampleRate16kHz &&
49       frame->sample_rate_hz_ != AudioProcessing::NativeRate::kSampleRate32kHz &&
50       frame->sample_rate_hz_ != AudioProcessing::NativeRate::kSampleRate48kHz) {
51     return AudioProcessing::Error::kBadSampleRateError;
52   }
53 
54   if (frame->num_channels_ <= 0) {
55     return AudioProcessing::Error::kBadNumberChannelsError;
56   }
57 
58   StreamConfig input_config(frame->sample_rate_hz_, frame->num_channels_);
59   StreamConfig output_config(frame->sample_rate_hz_, frame->num_channels_);
60 
61   int result = ap->ProcessReverseStream(frame->data(), input_config,
62                                         output_config, frame->mutable_data());
63   return result;
64 }
65 
66 }  // namespace webrtc
67