audio_buffer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2011 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 MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
  11. #define MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <memory>
  15. #include <vector>
  16. #include "common_audio/channel_buffer.h"
  17. #include "modules/audio_processing/include/audio_processing.h"
  18. namespace webrtc {
  19. class PushSincResampler;
  20. class SplittingFilter;
  21. enum Band { kBand0To8kHz = 0, kBand8To16kHz = 1, kBand16To24kHz = 2 };
  22. // Stores any audio data in a way that allows the audio processing module to
  23. // operate on it in a controlled manner.
  24. class AudioBuffer {
  25. public:
  26. static const int kSplitBandSize = 160;
  27. static const size_t kMaxSampleRate = 384000;
  28. AudioBuffer(size_t input_rate,
  29. size_t input_num_channels,
  30. size_t buffer_rate,
  31. size_t buffer_num_channels,
  32. size_t output_rate,
  33. size_t output_num_channels);
  34. // The constructor below will be deprecated.
  35. AudioBuffer(size_t input_num_frames,
  36. size_t input_num_channels,
  37. size_t buffer_num_frames,
  38. size_t buffer_num_channels,
  39. size_t output_num_frames);
  40. virtual ~AudioBuffer();
  41. AudioBuffer(const AudioBuffer&) = delete;
  42. AudioBuffer& operator=(const AudioBuffer&) = delete;
  43. // Specify that downmixing should be done by selecting a single channel.
  44. void set_downmixing_to_specific_channel(size_t channel);
  45. // Specify that downmixing should be done by averaging all channels,.
  46. void set_downmixing_by_averaging();
  47. // Set the number of channels in the buffer. The specified number of channels
  48. // cannot be larger than the specified buffer_num_channels. The number is also
  49. // reset at each call to CopyFrom or InterleaveFrom.
  50. void set_num_channels(size_t num_channels);
  51. size_t num_channels() const { return num_channels_; }
  52. size_t num_frames() const { return buffer_num_frames_; }
  53. size_t num_frames_per_band() const { return num_split_frames_; }
  54. size_t num_bands() const { return num_bands_; }
  55. // Returns pointer arrays to the full-band channels.
  56. // Usage:
  57. // channels()[channel][sample].
  58. // Where:
  59. // 0 <= channel < |buffer_num_channels_|
  60. // 0 <= sample < |buffer_num_frames_|
  61. float* const* channels() { return data_->channels(); }
  62. const float* const* channels_const() const { return data_->channels(); }
  63. // Returns pointer arrays to the bands for a specific channel.
  64. // Usage:
  65. // split_bands(channel)[band][sample].
  66. // Where:
  67. // 0 <= channel < |buffer_num_channels_|
  68. // 0 <= band < |num_bands_|
  69. // 0 <= sample < |num_split_frames_|
  70. const float* const* split_bands_const(size_t channel) const {
  71. return split_data_.get() ? split_data_->bands(channel)
  72. : data_->bands(channel);
  73. }
  74. float* const* split_bands(size_t channel) {
  75. return split_data_.get() ? split_data_->bands(channel)
  76. : data_->bands(channel);
  77. }
  78. // Returns a pointer array to the channels for a specific band.
  79. // Usage:
  80. // split_channels(band)[channel][sample].
  81. // Where:
  82. // 0 <= band < |num_bands_|
  83. // 0 <= channel < |buffer_num_channels_|
  84. // 0 <= sample < |num_split_frames_|
  85. const float* const* split_channels_const(Band band) const {
  86. if (split_data_.get()) {
  87. return split_data_->channels(band);
  88. } else {
  89. return band == kBand0To8kHz ? data_->channels() : nullptr;
  90. }
  91. }
  92. // Copies data into the buffer.
  93. void CopyFrom(const int16_t* const interleaved_data,
  94. const StreamConfig& stream_config);
  95. void CopyFrom(const float* const* stacked_data,
  96. const StreamConfig& stream_config);
  97. // Copies data from the buffer.
  98. void CopyTo(const StreamConfig& stream_config,
  99. int16_t* const interleaved_data);
  100. void CopyTo(const StreamConfig& stream_config, float* const* stacked_data);
  101. void CopyTo(AudioBuffer* buffer) const;
  102. // Splits the buffer data into frequency bands.
  103. void SplitIntoFrequencyBands();
  104. // Recombines the frequency bands into a full-band signal.
  105. void MergeFrequencyBands();
  106. // Copies the split bands data into the integer two-dimensional array.
  107. void ExportSplitChannelData(size_t channel,
  108. int16_t* const* split_band_data) const;
  109. // Copies the data in the integer two-dimensional array into the split_bands
  110. // data.
  111. void ImportSplitChannelData(size_t channel,
  112. const int16_t* const* split_band_data);
  113. static const size_t kMaxSplitFrameLength = 160;
  114. static const size_t kMaxNumBands = 3;
  115. // Deprecated methods, will be removed soon.
  116. float* const* channels_f() { return channels(); }
  117. const float* const* channels_const_f() const { return channels_const(); }
  118. const float* const* split_bands_const_f(size_t channel) const {
  119. return split_bands_const(channel);
  120. }
  121. float* const* split_bands_f(size_t channel) { return split_bands(channel); }
  122. const float* const* split_channels_const_f(Band band) const {
  123. return split_channels_const(band);
  124. }
  125. private:
  126. FRIEND_TEST_ALL_PREFIXES(AudioBufferTest,
  127. SetNumChannelsSetsChannelBuffersNumChannels);
  128. void RestoreNumChannels();
  129. const size_t input_num_frames_;
  130. const size_t input_num_channels_;
  131. const size_t buffer_num_frames_;
  132. const size_t buffer_num_channels_;
  133. const size_t output_num_frames_;
  134. const size_t output_num_channels_;
  135. size_t num_channels_;
  136. size_t num_bands_;
  137. size_t num_split_frames_;
  138. std::unique_ptr<ChannelBuffer<float>> data_;
  139. std::unique_ptr<ChannelBuffer<float>> split_data_;
  140. std::unique_ptr<SplittingFilter> splitting_filter_;
  141. std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_;
  142. std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_;
  143. bool downmix_by_averaging_ = true;
  144. size_t channel_for_downmixing_ = 0;
  145. };
  146. } // namespace webrtc
  147. #endif // MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_