andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 | [diff] [blame] | 1 | /* |
peah | faed4ab | 2016-04-05 21:57:48 | [diff] [blame] | 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 | [diff] [blame] | 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 | */ |
peah | c54aad6 | 2016-04-05 07:02:33 | [diff] [blame] | 10 | #ifndef WEBRTC_COMMON_AUDIO_AUDIO_RING_BUFFER_H_ |
| 11 | #define WEBRTC_COMMON_AUDIO_AUDIO_RING_BUFFER_H_ |
andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 | [diff] [blame] | 12 | |
peah | faed4ab | 2016-04-05 21:57:48 | [diff] [blame] | 13 | #include <stddef.h> |
kwiberg | bfefb03 | 2016-05-01 21:53:46 | [diff] [blame] | 14 | |
| 15 | #include <memory> |
peah | faed4ab | 2016-04-05 21:57:48 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
| 18 | struct RingBuffer; |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // A ring buffer tailored for float deinterleaved audio. Any operation that |
| 23 | // cannot be performed as requested will cause a crash (e.g. insufficient data |
| 24 | // in the buffer to fulfill a read request.) |
| 25 | class AudioRingBuffer final { |
| 26 | public: |
| 27 | // Specify the number of channels and maximum number of frames the buffer will |
| 28 | // contain. |
| 29 | AudioRingBuffer(size_t channels, size_t max_frames); |
| 30 | ~AudioRingBuffer(); |
| 31 | |
| 32 | // Copies |data| to the buffer and advances the write pointer. |channels| must |
| 33 | // be the same as at creation time. |
| 34 | void Write(const float* const* data, size_t channels, size_t frames); |
| 35 | |
| 36 | // Copies from the buffer to |data| and advances the read pointer. |channels| |
| 37 | // must be the same as at creation time. |
| 38 | void Read(float* const* data, size_t channels, size_t frames); |
| 39 | |
| 40 | size_t ReadFramesAvailable() const; |
| 41 | size_t WriteFramesAvailable() const; |
| 42 | |
| 43 | // Moves the read position. The forward version advances the read pointer |
| 44 | // towards the write pointer and the backward verison withdraws the read |
| 45 | // pointer away from the write pointer (i.e. flushing and stuffing the buffer |
| 46 | // respectively.) |
| 47 | void MoveReadPositionForward(size_t frames); |
| 48 | void MoveReadPositionBackward(size_t frames); |
| 49 | |
| 50 | private: |
| 51 | // TODO(kwiberg): Use std::vector<std::unique_ptr<RingBuffer>> instead. |
| 52 | std::vector<RingBuffer*> buffers_; |
| 53 | }; |
| 54 | |
| 55 | } // namespace webrtc |
Andrew MacDonald | 2d627a6 | 2015-06-17 18:39:36 | [diff] [blame] | 56 | |
peah | c54aad6 | 2016-04-05 07:02:33 | [diff] [blame] | 57 | #endif // WEBRTC_COMMON_AUDIO_AUDIO_RING_BUFFER_H_ |