xref: /aosp_15_r20/external/webrtc/audio/utility/audio_frame_operations.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_
12*d9f75844SAndroid Build Coastguard Worker #define AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
15*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include "absl/base/attributes.h"
18*d9f75844SAndroid Build Coastguard Worker #include "api/audio/audio_frame.h"
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
21*d9f75844SAndroid Build Coastguard Worker 
22*d9f75844SAndroid Build Coastguard Worker // TODO(andrew): consolidate this with utility.h and audio_frame_manipulator.h.
23*d9f75844SAndroid Build Coastguard Worker // Change reference parameters to pointers. Consider using a namespace rather
24*d9f75844SAndroid Build Coastguard Worker // than a class.
25*d9f75844SAndroid Build Coastguard Worker class AudioFrameOperations {
26*d9f75844SAndroid Build Coastguard Worker  public:
27*d9f75844SAndroid Build Coastguard Worker   // Add samples in `frame_to_add` with samples in `result_frame`
28*d9f75844SAndroid Build Coastguard Worker   // putting the results in `results_frame`.  The fields
29*d9f75844SAndroid Build Coastguard Worker   // `vad_activity_` and `speech_type_` of the result frame are
30*d9f75844SAndroid Build Coastguard Worker   // updated. If `result_frame` is empty (`samples_per_channel_`==0),
31*d9f75844SAndroid Build Coastguard Worker   // the samples in `frame_to_add` are added to it.  The number of
32*d9f75844SAndroid Build Coastguard Worker   // channels and number of samples per channel must match except when
33*d9f75844SAndroid Build Coastguard Worker   // `result_frame` is empty.
34*d9f75844SAndroid Build Coastguard Worker   static void Add(const AudioFrame& frame_to_add, AudioFrame* result_frame);
35*d9f75844SAndroid Build Coastguard Worker 
36*d9f75844SAndroid Build Coastguard Worker   // `frame.num_channels_` will be updated. This version checks for sufficient
37*d9f75844SAndroid Build Coastguard Worker   // buffer size and that `num_channels_` is mono. Use UpmixChannels
38*d9f75844SAndroid Build Coastguard Worker   // instead. TODO(bugs.webrtc.org/8649): remove.
39*d9f75844SAndroid Build Coastguard Worker   ABSL_DEPRECATED("bugs.webrtc.org/8649")
40*d9f75844SAndroid Build Coastguard Worker   static int MonoToStereo(AudioFrame* frame);
41*d9f75844SAndroid Build Coastguard Worker 
42*d9f75844SAndroid Build Coastguard Worker   // `frame.num_channels_` will be updated. This version checks that
43*d9f75844SAndroid Build Coastguard Worker   // `num_channels_` is stereo. Use DownmixChannels
44*d9f75844SAndroid Build Coastguard Worker   // instead. TODO(bugs.webrtc.org/8649): remove.
45*d9f75844SAndroid Build Coastguard Worker   ABSL_DEPRECATED("bugs.webrtc.org/8649")
46*d9f75844SAndroid Build Coastguard Worker   static int StereoToMono(AudioFrame* frame);
47*d9f75844SAndroid Build Coastguard Worker 
48*d9f75844SAndroid Build Coastguard Worker   // Downmixes 4 channels `src_audio` to stereo `dst_audio`. This is an in-place
49*d9f75844SAndroid Build Coastguard Worker   // operation, meaning `src_audio` and `dst_audio` may point to the same
50*d9f75844SAndroid Build Coastguard Worker   // buffer.
51*d9f75844SAndroid Build Coastguard Worker   static void QuadToStereo(const int16_t* src_audio,
52*d9f75844SAndroid Build Coastguard Worker                            size_t samples_per_channel,
53*d9f75844SAndroid Build Coastguard Worker                            int16_t* dst_audio);
54*d9f75844SAndroid Build Coastguard Worker 
55*d9f75844SAndroid Build Coastguard Worker   // `frame.num_channels_` will be updated. This version checks that
56*d9f75844SAndroid Build Coastguard Worker   // `num_channels_` is 4 channels.
57*d9f75844SAndroid Build Coastguard Worker   static int QuadToStereo(AudioFrame* frame);
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   // Downmixes `src_channels` `src_audio` to `dst_channels` `dst_audio`.
60*d9f75844SAndroid Build Coastguard Worker   // This is an in-place operation, meaning `src_audio` and `dst_audio`
61*d9f75844SAndroid Build Coastguard Worker   // may point to the same buffer. Supported channel combinations are
62*d9f75844SAndroid Build Coastguard Worker   // Stereo to Mono, Quad to Mono, and Quad to Stereo.
63*d9f75844SAndroid Build Coastguard Worker   static void DownmixChannels(const int16_t* src_audio,
64*d9f75844SAndroid Build Coastguard Worker                               size_t src_channels,
65*d9f75844SAndroid Build Coastguard Worker                               size_t samples_per_channel,
66*d9f75844SAndroid Build Coastguard Worker                               size_t dst_channels,
67*d9f75844SAndroid Build Coastguard Worker                               int16_t* dst_audio);
68*d9f75844SAndroid Build Coastguard Worker 
69*d9f75844SAndroid Build Coastguard Worker   // `frame.num_channels_` will be updated. This version checks that
70*d9f75844SAndroid Build Coastguard Worker   // `num_channels_` and `dst_channels` are valid and performs relevant downmix.
71*d9f75844SAndroid Build Coastguard Worker   // Supported channel combinations are N channels to Mono, and Quad to Stereo.
72*d9f75844SAndroid Build Coastguard Worker   static void DownmixChannels(size_t dst_channels, AudioFrame* frame);
73*d9f75844SAndroid Build Coastguard Worker 
74*d9f75844SAndroid Build Coastguard Worker   // `frame.num_channels_` will be updated. This version checks that
75*d9f75844SAndroid Build Coastguard Worker   // `num_channels_` and `dst_channels` are valid and performs relevant
76*d9f75844SAndroid Build Coastguard Worker   // downmix. Supported channel combinations are Mono to N
77*d9f75844SAndroid Build Coastguard Worker   // channels. The single channel is replicated.
78*d9f75844SAndroid Build Coastguard Worker   static void UpmixChannels(size_t target_number_of_channels,
79*d9f75844SAndroid Build Coastguard Worker                             AudioFrame* frame);
80*d9f75844SAndroid Build Coastguard Worker 
81*d9f75844SAndroid Build Coastguard Worker   // Swap the left and right channels of `frame`. Fails silently if `frame` is
82*d9f75844SAndroid Build Coastguard Worker   // not stereo.
83*d9f75844SAndroid Build Coastguard Worker   static void SwapStereoChannels(AudioFrame* frame);
84*d9f75844SAndroid Build Coastguard Worker 
85*d9f75844SAndroid Build Coastguard Worker   // Conditionally zero out contents of `frame` for implementing audio mute:
86*d9f75844SAndroid Build Coastguard Worker   //  `previous_frame_muted` &&  `current_frame_muted` - Zero out whole frame.
87*d9f75844SAndroid Build Coastguard Worker   //  `previous_frame_muted` && !`current_frame_muted` - Fade-in at frame start.
88*d9f75844SAndroid Build Coastguard Worker   // !`previous_frame_muted` &&  `current_frame_muted` - Fade-out at frame end.
89*d9f75844SAndroid Build Coastguard Worker   // !`previous_frame_muted` && !`current_frame_muted` - Leave frame untouched.
90*d9f75844SAndroid Build Coastguard Worker   static void Mute(AudioFrame* frame,
91*d9f75844SAndroid Build Coastguard Worker                    bool previous_frame_muted,
92*d9f75844SAndroid Build Coastguard Worker                    bool current_frame_muted);
93*d9f75844SAndroid Build Coastguard Worker 
94*d9f75844SAndroid Build Coastguard Worker   // Zero out contents of frame.
95*d9f75844SAndroid Build Coastguard Worker   static void Mute(AudioFrame* frame);
96*d9f75844SAndroid Build Coastguard Worker 
97*d9f75844SAndroid Build Coastguard Worker   // Halve samples in `frame`.
98*d9f75844SAndroid Build Coastguard Worker   static void ApplyHalfGain(AudioFrame* frame);
99*d9f75844SAndroid Build Coastguard Worker 
100*d9f75844SAndroid Build Coastguard Worker   static int Scale(float left, float right, AudioFrame* frame);
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker   static int ScaleWithSat(float scale, AudioFrame* frame);
103*d9f75844SAndroid Build Coastguard Worker };
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
106*d9f75844SAndroid Build Coastguard Worker 
107*d9f75844SAndroid Build Coastguard Worker #endif  // AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_
108