blob: 65c310c4896011594c6ec7c36d7e7d731ab92f80 [file] [log] [blame]
aleloi6321b492016-12-05 01:46:09 -08001/*
2 * Copyright (c) 2012 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_
12#define AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_
aleloi6321b492016-12-05 01:46:09 -080013
14#include <stddef.h>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <stdint.h>
aleloi6321b492016-12-05 01:46:09 -080016
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020017#include "api/audio/audio_frame.h"
Alex Loikob4977de2019-01-28 16:38:38 +010018#include "rtc_base/deprecation.h"
aleloi6321b492016-12-05 01:46:09 -080019
20namespace webrtc {
21
aleloi6321b492016-12-05 01:46:09 -080022// TODO(andrew): consolidate this with utility.h and audio_frame_manipulator.h.
23// Change reference parameters to pointers. Consider using a namespace rather
24// than a class.
25class AudioFrameOperations {
26 public:
27 // Add samples in |frame_to_add| with samples in |result_frame|
28 // putting the results in |results_frame|. The fields
29 // |vad_activity_| and |speech_type_| of the result frame are
30 // updated. If |result_frame| is empty (|samples_per_channel_|==0),
31 // the samples in |frame_to_add| are added to it. The number of
32 // channels and number of samples per channel must match except when
33 // |result_frame| is empty.
34 static void Add(const AudioFrame& frame_to_add, AudioFrame* result_frame);
35
aleloi6321b492016-12-05 01:46:09 -080036 // |frame.num_channels_| will be updated. This version checks for sufficient
Alex Loikob4977de2019-01-28 16:38:38 +010037 // buffer size and that |num_channels_| is mono. Use UpmixChannels
38 // instead. TODO(bugs.webrtc.org/8649): remove.
39 RTC_DEPRECATED static int MonoToStereo(AudioFrame* frame);
jens.nielsen228c2682017-03-01 05:11:22 -080040
aleloi6321b492016-12-05 01:46:09 -080041 // |frame.num_channels_| will be updated. This version checks that
Alex Loikob4977de2019-01-28 16:38:38 +010042 // |num_channels_| is stereo. Use DownmixChannels
43 // instead. TODO(bugs.webrtc.org/8649): remove.
44 RTC_DEPRECATED static int StereoToMono(AudioFrame* frame);
aleloi6321b492016-12-05 01:46:09 -080045
jens.nielsen228c2682017-03-01 05:11:22 -080046 // Downmixes 4 channels |src_audio| to stereo |dst_audio|. This is an in-place
47 // operation, meaning |src_audio| and |dst_audio| may point to the same
48 // buffer.
49 static void QuadToStereo(const int16_t* src_audio,
50 size_t samples_per_channel,
51 int16_t* dst_audio);
52
53 // |frame.num_channels_| will be updated. This version checks that
54 // |num_channels_| is 4 channels.
55 static int QuadToStereo(AudioFrame* frame);
56
jens.nielsen228c2682017-03-01 05:11:22 -080057 // Downmixes |src_channels| |src_audio| to |dst_channels| |dst_audio|.
58 // This is an in-place operation, meaning |src_audio| and |dst_audio|
59 // may point to the same buffer. Supported channel combinations are
60 // Stereo to Mono, Quad to Mono, and Quad to Stereo.
61 static void DownmixChannels(const int16_t* src_audio,
62 size_t src_channels,
63 size_t samples_per_channel,
64 size_t dst_channels,
65 int16_t* dst_audio);
66
67 // |frame.num_channels_| will be updated. This version checks that
Alex Loikob4977de2019-01-28 16:38:38 +010068 // |num_channels_| and |dst_channels| are valid and performs relevant downmix.
69 // Supported channel combinations are N channels to Mono, and Quad to Stereo.
70 static void DownmixChannels(size_t dst_channels, AudioFrame* frame);
71
72 // |frame.num_channels_| will be updated. This version checks that
jens.nielsen228c2682017-03-01 05:11:22 -080073 // |num_channels_| and |dst_channels| are valid and performs relevant
Alex Loikob4977de2019-01-28 16:38:38 +010074 // downmix. Supported channel combinations are Mono to N
75 // channels. The single channel is replicated.
76 static void UpmixChannels(size_t target_number_of_channels,
77 AudioFrame* frame);
jens.nielsen228c2682017-03-01 05:11:22 -080078
aleloi6321b492016-12-05 01:46:09 -080079 // Swap the left and right channels of |frame|. Fails silently if |frame| is
80 // not stereo.
81 static void SwapStereoChannels(AudioFrame* frame);
82
83 // Conditionally zero out contents of |frame| for implementing audio mute:
84 // |previous_frame_muted| && |current_frame_muted| - Zero out whole frame.
85 // |previous_frame_muted| && !|current_frame_muted| - Fade-in at frame start.
86 // !|previous_frame_muted| && |current_frame_muted| - Fade-out at frame end.
87 // !|previous_frame_muted| && !|current_frame_muted| - Leave frame untouched.
88 static void Mute(AudioFrame* frame,
89 bool previous_frame_muted,
90 bool current_frame_muted);
91
92 // Zero out contents of frame.
93 static void Mute(AudioFrame* frame);
94
95 // Halve samples in |frame|.
96 static void ApplyHalfGain(AudioFrame* frame);
97
oprypin67fdb802017-03-09 06:25:06 -080098 static int Scale(float left, float right, AudioFrame* frame);
aleloi6321b492016-12-05 01:46:09 -080099
oprypin67fdb802017-03-09 06:25:06 -0800100 static int ScaleWithSat(float scale, AudioFrame* frame);
aleloi6321b492016-12-05 01:46:09 -0800101};
102
103} // namespace webrtc
104
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200105#endif // AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_