audio_frame_operations.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #ifndef AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_
  11. #define AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include "api/audio/audio_frame.h"
  15. #include "rtc_base/deprecation.h"
  16. namespace webrtc {
  17. // TODO(andrew): consolidate this with utility.h and audio_frame_manipulator.h.
  18. // Change reference parameters to pointers. Consider using a namespace rather
  19. // than a class.
  20. class AudioFrameOperations {
  21. public:
  22. // Add samples in |frame_to_add| with samples in |result_frame|
  23. // putting the results in |results_frame|. The fields
  24. // |vad_activity_| and |speech_type_| of the result frame are
  25. // updated. If |result_frame| is empty (|samples_per_channel_|==0),
  26. // the samples in |frame_to_add| are added to it. The number of
  27. // channels and number of samples per channel must match except when
  28. // |result_frame| is empty.
  29. static void Add(const AudioFrame& frame_to_add, AudioFrame* result_frame);
  30. // |frame.num_channels_| will be updated. This version checks for sufficient
  31. // buffer size and that |num_channels_| is mono. Use UpmixChannels
  32. // instead. TODO(bugs.webrtc.org/8649): remove.
  33. RTC_DEPRECATED static int MonoToStereo(AudioFrame* frame);
  34. // |frame.num_channels_| will be updated. This version checks that
  35. // |num_channels_| is stereo. Use DownmixChannels
  36. // instead. TODO(bugs.webrtc.org/8649): remove.
  37. RTC_DEPRECATED static int StereoToMono(AudioFrame* frame);
  38. // Downmixes 4 channels |src_audio| to stereo |dst_audio|. This is an in-place
  39. // operation, meaning |src_audio| and |dst_audio| may point to the same
  40. // buffer.
  41. static void QuadToStereo(const int16_t* src_audio,
  42. size_t samples_per_channel,
  43. int16_t* dst_audio);
  44. // |frame.num_channels_| will be updated. This version checks that
  45. // |num_channels_| is 4 channels.
  46. static int QuadToStereo(AudioFrame* frame);
  47. // Downmixes |src_channels| |src_audio| to |dst_channels| |dst_audio|.
  48. // This is an in-place operation, meaning |src_audio| and |dst_audio|
  49. // may point to the same buffer. Supported channel combinations are
  50. // Stereo to Mono, Quad to Mono, and Quad to Stereo.
  51. static void DownmixChannels(const int16_t* src_audio,
  52. size_t src_channels,
  53. size_t samples_per_channel,
  54. size_t dst_channels,
  55. int16_t* dst_audio);
  56. // |frame.num_channels_| will be updated. This version checks that
  57. // |num_channels_| and |dst_channels| are valid and performs relevant downmix.
  58. // Supported channel combinations are N channels to Mono, and Quad to Stereo.
  59. static void DownmixChannels(size_t dst_channels, AudioFrame* frame);
  60. // |frame.num_channels_| will be updated. This version checks that
  61. // |num_channels_| and |dst_channels| are valid and performs relevant
  62. // downmix. Supported channel combinations are Mono to N
  63. // channels. The single channel is replicated.
  64. static void UpmixChannels(size_t target_number_of_channels,
  65. AudioFrame* frame);
  66. // Swap the left and right channels of |frame|. Fails silently if |frame| is
  67. // not stereo.
  68. static void SwapStereoChannels(AudioFrame* frame);
  69. // Conditionally zero out contents of |frame| for implementing audio mute:
  70. // |previous_frame_muted| && |current_frame_muted| - Zero out whole frame.
  71. // |previous_frame_muted| && !|current_frame_muted| - Fade-in at frame start.
  72. // !|previous_frame_muted| && |current_frame_muted| - Fade-out at frame end.
  73. // !|previous_frame_muted| && !|current_frame_muted| - Leave frame untouched.
  74. static void Mute(AudioFrame* frame,
  75. bool previous_frame_muted,
  76. bool current_frame_muted);
  77. // Zero out contents of frame.
  78. static void Mute(AudioFrame* frame);
  79. // Halve samples in |frame|.
  80. static void ApplyHalfGain(AudioFrame* frame);
  81. static int Scale(float left, float right, AudioFrame* frame);
  82. static int ScaleWithSat(float scale, AudioFrame* frame);
  83. };
  84. } // namespace webrtc
  85. #endif // AUDIO_UTILITY_AUDIO_FRAME_OPERATIONS_H_