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 WEBRTC_MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_
13
14#include <string.h>  // Access to size_t.
15
16#include "webrtc/base/constructormagic.h"
17#include "webrtc/system_wrappers/interface/scoped_ptr.h"
18#include "webrtc/typedefs.h"
19
20namespace webrtc {
21
22class AudioVector {
23 public:
24  // Creates an empty AudioVector.
25  AudioVector()
26      : array_(new int16_t[kDefaultInitialSize]),
27        first_free_ix_(0),
28        capacity_(kDefaultInitialSize) {}
29
30  // Creates an AudioVector with an initial size.
31  explicit AudioVector(size_t initial_size)
32      : array_(new int16_t[initial_size]),
33        first_free_ix_(initial_size),
34        capacity_(initial_size) {
35    memset(array_.get(), 0, initial_size * sizeof(int16_t));
36  }
37
38  virtual ~AudioVector() {}
39
40  // Deletes all values and make the vector empty.
41  virtual void Clear();
42
43  // Copies all values from this vector to |copy_to|. Any contents in |copy_to|
44  // are deleted before the copy operation. After the operation is done,
45  // |copy_to| will be an exact replica of this object.
46  virtual void CopyTo(AudioVector* copy_to) const;
47
48  // Prepends the contents of AudioVector |prepend_this| to this object. The
49  // length of this object is increased with the length of |prepend_this|.
50  virtual void PushFront(const AudioVector& prepend_this);
51
52  // Same as above, but with an array |prepend_this| with |length| elements as
53  // source.
54  virtual void PushFront(const int16_t* prepend_this, size_t length);
55
56  // Same as PushFront but will append to the end of this object.
57  virtual void PushBack(const AudioVector& append_this);
58
59  // Same as PushFront but will append to the end of this object.
60  virtual void PushBack(const int16_t* append_this, size_t length);
61
62  // Removes |length| elements from the beginning of this object.
63  virtual void PopFront(size_t length);
64
65  // Removes |length| elements from the end of this object.
66  virtual void PopBack(size_t length);
67
68  // Extends this object with |extra_length| elements at the end. The new
69  // elements are initialized to zero.
70  virtual void Extend(size_t extra_length);
71
72  // Inserts |length| elements taken from the array |insert_this| and insert
73  // them at |position|. The length of the AudioVector is increased by |length|.
74  // |position| = 0 means that the new values are prepended to the vector.
75  // |position| = Size() means that the new values are appended to the vector.
76  virtual void InsertAt(const int16_t* insert_this, size_t length,
77                        size_t position);
78
79  // Like InsertAt, but inserts |length| zero elements at |position|.
80  virtual void InsertZerosAt(size_t length, size_t position);
81
82  // Overwrites |length| elements of this AudioVector with values taken from the
83  // array |insert_this|, starting at |position|. The definition of |position|
84  // is the same as for InsertAt(). If |length| and |position| are selected
85  // such that the new data extends beyond the end of the current AudioVector,
86  // the vector is extended to accommodate the new data.
87  virtual void OverwriteAt(const int16_t* insert_this,
88                           size_t length,
89                           size_t position);
90
91  // Appends |append_this| to the end of the current vector. Lets the two
92  // vectors overlap by |fade_length| samples, and cross-fade linearly in this
93  // region.
94  virtual void CrossFade(const AudioVector& append_this, size_t fade_length);
95
96  // Returns the number of elements in this AudioVector.
97  virtual size_t Size() const { return first_free_ix_; }
98
99  // Returns true if this AudioVector is empty.
100  virtual bool Empty() const { return (first_free_ix_ == 0); }
101
102  // Accesses and modifies an element of AudioVector.
103  const int16_t& operator[](size_t index) const;
104  int16_t& operator[](size_t index);
105
106 private:
107  static const size_t kDefaultInitialSize = 10;
108
109  void Reserve(size_t n);
110
111  scoped_ptr<int16_t[]> array_;
112  size_t first_free_ix_;  // The first index after the last sample in array_.
113                          // Note that this index may point outside of array_.
114  size_t capacity_;  // Allocated number of samples in the array.
115
116  DISALLOW_COPY_AND_ASSIGN(AudioVector);
117};
118
119}  // namespace webrtc
120#endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_
121