tor-browser

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

audio_vector.h (6437B)


      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 
     11 #ifndef MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_
     12 #define MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_
     13 
     14 #include <string.h>
     15 
     16 #include <cstdint>
     17 #include <memory>
     18 
     19 #include "api/audio/audio_view.h"
     20 #include "rtc_base/checks.h"
     21 
     22 namespace webrtc {
     23 
     24 class AudioVector final {
     25 public:
     26  // Creates an empty AudioVector.
     27  AudioVector();
     28 
     29  // Creates an AudioVector with an initial size.
     30  explicit AudioVector(size_t initial_size);
     31 
     32  ~AudioVector();
     33 
     34  AudioVector(const AudioVector&) = delete;
     35  AudioVector& operator=(const AudioVector&) = delete;
     36 
     37  // Deletes all values and make the vector empty.
     38  void Clear();
     39 
     40  // Copies all values from this vector to `copy_to`. Any contents in `copy_to`
     41  // are deleted before the copy operation. After the operation is done,
     42  // `copy_to` will be an exact replica of this object.
     43  void CopyTo(AudioVector* copy_to) const;
     44 
     45  // Copies `length` values from `position` in this vector to `copy_to`.
     46  void CopyTo(size_t length, size_t position, int16_t* copy_to) const;
     47 
     48  // Copies `dst.size()` samples from a given position of the vector.
     49  // If not enough samples are available, the function will return
     50  // false and not do any copying.
     51  bool CopyTo(size_t position, MonoView<int16_t> dst) const;
     52 
     53  // Prepends the contents of AudioVector `prepend_this` to this object. The
     54  // length of this object is increased with the length of `prepend_this`.
     55  void PushFront(const AudioVector& prepend_this);
     56 
     57  // Same as above, but with an array `prepend_this` with `length` elements as
     58  // source.
     59  void PushFront(const int16_t* prepend_this, size_t length);
     60 
     61  // Same as PushFront but will append to the end of this object.
     62  void PushBack(const AudioVector& append_this);
     63 
     64  // Appends a segment of `append_this` to the end of this object. The segment
     65  // starts from `position` and has `length` samples.
     66  void PushBack(const AudioVector& append_this, size_t length, size_t position);
     67 
     68  // Same as PushFront but will append to the end of this object.
     69  void PushBack(const int16_t* append_this, size_t length);
     70 
     71  // Removes `length` elements from the beginning of this object.
     72  void PopFront(size_t length);
     73 
     74  // Removes `length` elements from the end of this object.
     75  void PopBack(size_t length);
     76 
     77  // Extends this object with `extra_length` elements at the end. The new
     78  // elements are initialized to zero.
     79  void Extend(size_t extra_length);
     80 
     81  // Inserts `length` elements taken from the array `insert_this` and insert
     82  // them at `position`. The length of the AudioVector is increased by `length`.
     83  // `position` = 0 means that the new values are prepended to the vector.
     84  // `position` = Size() means that the new values are appended to the vector.
     85  void InsertAt(const int16_t* insert_this, size_t length, size_t position);
     86 
     87  // Like InsertAt, but inserts `length` zero elements at `position`.
     88  void InsertZerosAt(size_t length, size_t position);
     89 
     90  // Overwrites `length` elements of this AudioVector starting from `position`
     91  // with first values in `AudioVector`. The definition of `position`
     92  // is the same as for InsertAt(). If `length` and `position` are selected
     93  // such that the new data extends beyond the end of the current AudioVector,
     94  // the vector is extended to accommodate the new data.
     95  void OverwriteAt(const AudioVector& insert_this,
     96                   size_t length,
     97                   size_t position);
     98 
     99  // Overwrites `length` elements of this AudioVector with values taken from the
    100  // array `insert_this`, starting at `position`. The definition of `position`
    101  // is the same as for InsertAt(). If `length` and `position` are selected
    102  // such that the new data extends beyond the end of the current AudioVector,
    103  // the vector is extended to accommodate the new data.
    104  void OverwriteAt(const int16_t* insert_this, size_t length, size_t position);
    105 
    106  // Appends `append_this` to the end of the current vector. Lets the two
    107  // vectors overlap by `fade_length` samples, and cross-fade linearly in this
    108  // region.
    109  void CrossFade(const AudioVector& append_this, size_t fade_length);
    110 
    111  // Returns the number of elements in this AudioVector.
    112  size_t Size() const;
    113 
    114  // Returns true if this AudioVector is empty.
    115  bool Empty() const;
    116 
    117  // Accesses and modifies an element of AudioVector.
    118  inline const int16_t& operator[](size_t index) const {
    119    return array_[WrapIndex(index, begin_index_, capacity_)];
    120  }
    121 
    122  inline int16_t& operator[](size_t index) {
    123    return array_[WrapIndex(index, begin_index_, capacity_)];
    124  }
    125 
    126 private:
    127  static const size_t kDefaultInitialSize = 10;
    128 
    129  // This method is used by the [] operators to calculate an index within the
    130  // capacity of the array, but without using the modulo operation (%).
    131  static inline size_t WrapIndex(size_t index,
    132                                 size_t begin_index,
    133                                 size_t capacity) {
    134    RTC_DCHECK_LT(index, capacity);
    135    RTC_DCHECK_LT(begin_index, capacity);
    136    size_t ix = begin_index + index;
    137    RTC_DCHECK_GE(ix, index);  // Check for overflow.
    138    if (ix >= capacity) {
    139      ix -= capacity;
    140    }
    141    RTC_DCHECK_LT(ix, capacity);
    142    return ix;
    143  }
    144 
    145  void Reserve(size_t n);
    146 
    147  void InsertByPushBack(const int16_t* insert_this,
    148                        size_t length,
    149                        size_t position);
    150 
    151  void InsertByPushFront(const int16_t* insert_this,
    152                         size_t length,
    153                         size_t position);
    154 
    155  void InsertZerosByPushBack(size_t length, size_t position);
    156 
    157  void InsertZerosByPushFront(size_t length, size_t position);
    158 
    159  std::unique_ptr<int16_t[]> array_;
    160 
    161  size_t capacity_;  // Allocated number of samples in the array.
    162 
    163  // The index of the first sample in `array_`, except when
    164  // |begin_index_ == end_index_|, which indicates an empty buffer.
    165  size_t begin_index_;
    166 
    167  // The index of the sample after the last sample in `array_`.
    168  size_t end_index_;
    169 };
    170 
    171 }  // namespace webrtc
    172 #endif  // MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_