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> |
| 15 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 16 | #include "typedefs.h" // NOLINT(build/include) |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | class AudioFrame; |
| 21 | |
| 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: |
| 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 | |
| 36 | // Upmixes mono |src_audio| to stereo |dst_audio|. This is an out-of-place |
| 37 | // operation, meaning src_audio and dst_audio must point to different |
| 38 | // buffers. It is the caller's responsibility to ensure that |dst_audio| is |
| 39 | // sufficiently large. |
| 40 | static void MonoToStereo(const int16_t* src_audio, |
| 41 | size_t samples_per_channel, |
| 42 | int16_t* dst_audio); |
jens.nielsen | 228c268 | 2017-03-01 05:11:22 -0800 | [diff] [blame] | 43 | |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 44 | // |frame.num_channels_| will be updated. This version checks for sufficient |
| 45 | // buffer size and that |num_channels_| is mono. |
| 46 | static int MonoToStereo(AudioFrame* frame); |
| 47 | |
| 48 | // Downmixes stereo |src_audio| to mono |dst_audio|. This is an in-place |
| 49 | // operation, meaning |src_audio| and |dst_audio| may point to the same |
| 50 | // buffer. |
| 51 | static void StereoToMono(const int16_t* src_audio, |
| 52 | size_t samples_per_channel, |
| 53 | int16_t* dst_audio); |
jens.nielsen | 228c268 | 2017-03-01 05:11:22 -0800 | [diff] [blame] | 54 | |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 55 | // |frame.num_channels_| will be updated. This version checks that |
| 56 | // |num_channels_| is stereo. |
| 57 | static int StereoToMono(AudioFrame* frame); |
| 58 | |
jens.nielsen | 228c268 | 2017-03-01 05:11:22 -0800 | [diff] [blame] | 59 | // Downmixes 4 channels |src_audio| to stereo |dst_audio|. This is an in-place |
| 60 | // operation, meaning |src_audio| and |dst_audio| may point to the same |
| 61 | // buffer. |
| 62 | static void QuadToStereo(const int16_t* src_audio, |
| 63 | size_t samples_per_channel, |
| 64 | int16_t* dst_audio); |
| 65 | |
| 66 | // |frame.num_channels_| will be updated. This version checks that |
| 67 | // |num_channels_| is 4 channels. |
| 68 | static int QuadToStereo(AudioFrame* frame); |
| 69 | |
| 70 | // Downmixes 4 channels |src_audio| to mono |dst_audio|. This is an in-place |
| 71 | // operation, meaning |src_audio| and |dst_audio| may point to the same |
| 72 | // buffer. |
| 73 | static void QuadToMono(const int16_t* src_audio, |
| 74 | size_t samples_per_channel, |
| 75 | int16_t* dst_audio); |
| 76 | |
| 77 | // |frame.num_channels_| will be updated. This version checks that |
| 78 | // |num_channels_| is 4 channels. |
| 79 | static int QuadToMono(AudioFrame* frame); |
| 80 | |
| 81 | // Downmixes |src_channels| |src_audio| to |dst_channels| |dst_audio|. |
| 82 | // This is an in-place operation, meaning |src_audio| and |dst_audio| |
| 83 | // may point to the same buffer. Supported channel combinations are |
| 84 | // Stereo to Mono, Quad to Mono, and Quad to Stereo. |
| 85 | static void DownmixChannels(const int16_t* src_audio, |
| 86 | size_t src_channels, |
| 87 | size_t samples_per_channel, |
| 88 | size_t dst_channels, |
| 89 | int16_t* dst_audio); |
| 90 | |
| 91 | // |frame.num_channels_| will be updated. This version checks that |
| 92 | // |num_channels_| and |dst_channels| are valid and performs relevant |
| 93 | // downmix. Supported channel combinations are Stereo to Mono, Quad to Mono, |
| 94 | // and Quad to Stereo. |
| 95 | static int DownmixChannels(size_t dst_channels, AudioFrame* frame); |
| 96 | |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 97 | // Swap the left and right channels of |frame|. Fails silently if |frame| is |
| 98 | // not stereo. |
| 99 | static void SwapStereoChannels(AudioFrame* frame); |
| 100 | |
| 101 | // Conditionally zero out contents of |frame| for implementing audio mute: |
| 102 | // |previous_frame_muted| && |current_frame_muted| - Zero out whole frame. |
| 103 | // |previous_frame_muted| && !|current_frame_muted| - Fade-in at frame start. |
| 104 | // !|previous_frame_muted| && |current_frame_muted| - Fade-out at frame end. |
| 105 | // !|previous_frame_muted| && !|current_frame_muted| - Leave frame untouched. |
| 106 | static void Mute(AudioFrame* frame, |
| 107 | bool previous_frame_muted, |
| 108 | bool current_frame_muted); |
| 109 | |
| 110 | // Zero out contents of frame. |
| 111 | static void Mute(AudioFrame* frame); |
| 112 | |
| 113 | // Halve samples in |frame|. |
| 114 | static void ApplyHalfGain(AudioFrame* frame); |
| 115 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 116 | static int Scale(float left, float right, AudioFrame* frame); |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 117 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 118 | static int ScaleWithSat(float scale, AudioFrame* frame); |
aleloi | 6321b49 | 2016-12-05 01:46:09 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | } // namespace webrtc |
| 122 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 123 | #endif // AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_ |