You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
6.2 KiB

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