blob: 7cc4fb75e4c8a600a4d3a9dea6a704b28f5af4d6 [file] [log] [blame]
Per Åhgren71652f42020-03-17 13:23:58 +01001/*
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
16namespace webrtc {
17
18int ProcessAudioFrame(AudioProcessing* ap, AudioFrame* frame) {
19 if (!frame || !ap) {
20 return AudioProcessing::Error::kNullPointerError;
21 }
22
Henrik Lundin64253a92022-02-04 09:02:48 +000023 StreamConfig input_config(frame->sample_rate_hz_, frame->num_channels_);
24 StreamConfig output_config(frame->sample_rate_hz_, frame->num_channels_);
Per Åhgren71652f42020-03-17 13:23:58 +010025 RTC_DCHECK_EQ(frame->samples_per_channel(), input_config.num_frames());
26
Per Åhgren71652f42020-03-17 13:23:58 +010027 int result = ap->ProcessStream(frame->data(), input_config, output_config,
Per Åhgrendc5522b2020-03-19 14:55:58 +010028 frame->mutable_data());
Per Åhgren71652f42020-03-17 13:23:58 +010029
Per Åhgrendc5522b2020-03-19 14:55:58 +010030 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;
Per Åhgren71652f42020-03-17 13:23:58 +010036 }
37
38 return result;
39}
40
41int 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
Henrik Lundin64253a92022-02-04 09:02:48 +000058 StreamConfig input_config(frame->sample_rate_hz_, frame->num_channels_);
59 StreamConfig output_config(frame->sample_rate_hz_, frame->num_channels_);
Per Åhgren71652f42020-03-17 13:23:58 +010060
61 int result = ap->ProcessReverseStream(frame->data(), input_config,
62 output_config, frame->mutable_data());
63 return result;
64}
65
66} // namespace webrtc