aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_ |
| 12 | #define AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_ |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 13 | |
| 14 | #include <stddef.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 15 | #include <stdint.h> |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 16 | |
Danil Chapovalov | e904161 | 2021-02-22 12:43:39 +0100 | [diff] [blame] | 17 | #include "absl/base/attributes.h" |
Fredrik Solenberg | bbf21a3 | 2018-04-12 22:44:09 +0200 | [diff] [blame] | 18 | #include "api/audio/audio_frame.h" |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 22 | // 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. |
| 25 | class AudioFrameOperations { |
| 26 | public: |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 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 |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 32 | // channels and number of samples per channel must match except when |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 33 | // `result_frame` is empty. |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 34 | static void Add(const AudioFrame& frame_to_add, AudioFrame* result_frame); |
| 35 | |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 36 | // `frame.num_channels_` will be updated. This version checks for sufficient |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 37 | // buffer size and that `num_channels_` is mono. Use UpmixChannels |
Alex Loiko | b4977de | 2019-01-28 16:38:38 +0100 | [diff] [blame] | 38 | // instead. TODO(bugs.webrtc.org/8649): remove. |
Danil Chapovalov | e904161 | 2021-02-22 12:43:39 +0100 | [diff] [blame] | 39 | ABSL_DEPRECATED("bugs.webrtc.org/8649") |
| 40 | static int MonoToStereo(AudioFrame* frame); |
jens.nielsen | 228c268 | 2017-03-01 05:11:22 -0800 | [diff] [blame] | 41 | |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 42 | // `frame.num_channels_` will be updated. This version checks that |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 43 | // `num_channels_` is stereo. Use DownmixChannels |
Alex Loiko | b4977de | 2019-01-28 16:38:38 +0100 | [diff] [blame] | 44 | // instead. TODO(bugs.webrtc.org/8649): remove. |
Danil Chapovalov | e904161 | 2021-02-22 12:43:39 +0100 | [diff] [blame] | 45 | ABSL_DEPRECATED("bugs.webrtc.org/8649") |
| 46 | static int StereoToMono(AudioFrame* frame); |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 47 | |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 48 | // Downmixes 4 channels `src_audio` to stereo `dst_audio`. This is an in-place |
| 49 | // operation, meaning `src_audio` and `dst_audio` may point to the same |
jens.nielsen | 228c268 | 2017-03-01 05:11:22 -0800 | [diff] [blame] | 50 | // buffer. |
| 51 | static void QuadToStereo(const int16_t* src_audio, |
| 52 | size_t samples_per_channel, |
| 53 | int16_t* dst_audio); |
| 54 | |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 55 | // `frame.num_channels_` will be updated. This version checks that |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 56 | // `num_channels_` is 4 channels. |
jens.nielsen | 228c268 | 2017-03-01 05:11:22 -0800 | [diff] [blame] | 57 | static int QuadToStereo(AudioFrame* frame); |
| 58 | |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 59 | // Downmixes `src_channels` `src_audio` to `dst_channels` `dst_audio`. |
| 60 | // This is an in-place operation, meaning `src_audio` and `dst_audio` |
jens.nielsen | 228c268 | 2017-03-01 05:11:22 -0800 | [diff] [blame] | 61 | // may point to the same buffer. Supported channel combinations are |
| 62 | // Stereo to Mono, Quad to Mono, and Quad to Stereo. |
| 63 | static void DownmixChannels(const int16_t* src_audio, |
| 64 | size_t src_channels, |
| 65 | size_t samples_per_channel, |
| 66 | size_t dst_channels, |
| 67 | int16_t* dst_audio); |
| 68 | |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 69 | // `frame.num_channels_` will be updated. This version checks that |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 70 | // `num_channels_` and `dst_channels` are valid and performs relevant downmix. |
Alex Loiko | b4977de | 2019-01-28 16:38:38 +0100 | [diff] [blame] | 71 | // Supported channel combinations are N channels to Mono, and Quad to Stereo. |
| 72 | static void DownmixChannels(size_t dst_channels, AudioFrame* frame); |
| 73 | |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 74 | // `frame.num_channels_` will be updated. This version checks that |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 75 | // `num_channels_` and `dst_channels` are valid and performs relevant |
Alex Loiko | b4977de | 2019-01-28 16:38:38 +0100 | [diff] [blame] | 76 | // downmix. Supported channel combinations are Mono to N |
| 77 | // channels. The single channel is replicated. |
| 78 | static void UpmixChannels(size_t target_number_of_channels, |
| 79 | AudioFrame* frame); |
jens.nielsen | 228c268 | 2017-03-01 05:11:22 -0800 | [diff] [blame] | 80 | |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 81 | // Swap the left and right channels of `frame`. Fails silently if `frame` is |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 82 | // not stereo. |
| 83 | static void SwapStereoChannels(AudioFrame* frame); |
| 84 | |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 85 | // Conditionally zero out contents of `frame` for implementing audio mute: |
| 86 | // `previous_frame_muted` && `current_frame_muted` - Zero out whole frame. |
| 87 | // `previous_frame_muted` && !`current_frame_muted` - Fade-in at frame start. |
| 88 | // !`previous_frame_muted` && `current_frame_muted` - Fade-out at frame end. |
| 89 | // !`previous_frame_muted` && !`current_frame_muted` - Leave frame untouched. |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 90 | static void Mute(AudioFrame* frame, |
| 91 | bool previous_frame_muted, |
| 92 | bool current_frame_muted); |
| 93 | |
| 94 | // Zero out contents of frame. |
| 95 | static void Mute(AudioFrame* frame); |
| 96 | |
Artem Titov | b0ea637 | 2021-07-26 11:47:07 +0200 | [diff] [blame] | 97 | // Halve samples in `frame`. |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 98 | static void ApplyHalfGain(AudioFrame* frame); |
| 99 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 100 | static int Scale(float left, float right, AudioFrame* frame); |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 101 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 102 | static int ScaleWithSat(float scale, AudioFrame* frame); |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace webrtc |
| 106 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 107 | #endif // AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_ |