tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

audio_buffer.h (6500B)


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