blob: ebfc9d9a9cdb47d466293ae648a811ccc3d16239 [file] [log] [blame]
andrew@webrtc.org041035b2015-01-26 21:23:53 +00001/*
2 * Copyright (c) 2015 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#include <stddef.h>
12#include <vector>
13
14struct RingBuffer;
15
16namespace webrtc {
17
18// A ring buffer tailored for float deinterleaved audio. Any operation that
19// cannot be performed as requested will cause a crash (e.g. insufficient data
20// in the buffer to fulfill a read request.)
21class AudioRingBuffer final {
22 public:
23 // Specify the number of channels and maximum number of frames the buffer will
24 // contain.
25 AudioRingBuffer(size_t channels, size_t max_frames);
26 ~AudioRingBuffer();
27
28 // Copy |data| to the buffer and advance the write pointer. |channels| must
29 // be the same as at creation time.
30 void Write(const float* const* data, size_t channels, size_t frames);
31
32 // Copy from the buffer to |data| and advance the read pointer. |channels|
33 // must be the same as at creation time.
34 void Read(float* const* data, size_t channels, size_t frames);
35
36 size_t ReadFramesAvailable() const;
37 size_t WriteFramesAvailable() const;
38
39 // Positive values advance the read pointer and negative values withdraw
40 // the read pointer (i.e. flush and stuff the buffer respectively.)
41 void MoveReadPosition(int frames);
42
43 private:
44 // We don't use a ScopedVector because it doesn't support a specialized
45 // deleter (like scoped_ptr for instance.)
46 std::vector<RingBuffer*> buffers_;
47};
48
49} // namespace webrtc