1 | /* |
2 | Copyright 2018 Google Inc. All Rights Reserved. |
3 | |
4 | Licensed under the Apache License, Version 2.0 (the "License"); |
5 | you may not use this file except in compliance with the License. |
6 | You may obtain a copy of the License at |
7 | |
8 | http://www.apache.org/licenses/LICENSE-2.0 |
9 | |
10 | Unless required by applicable law or agreed to in writing, software |
11 | distributed under the License is distributed on an "AS-IS" BASIS, |
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | See the License for the specific language governing permissions and |
14 | limitations under the License. |
15 | */ |
16 | |
17 | #ifndef RESONANCE_AUDIO_DSP_CIRCULAR_BUFFER_H_ |
18 | #define RESONANCE_AUDIO_DSP_CIRCULAR_BUFFER_H_ |
19 | |
20 | #include "base/audio_buffer.h" |
21 | |
22 | namespace vraudio { |
23 | // Class that implements a simple mono circular buffer, accepting input and |
24 | // output of different length. |
25 | class CircularBuffer { |
26 | public: |
27 | // Constructs a circular buffer. |
28 | // |
29 | // @param buffer_length The length of the Circular buffer. This value must be |
30 | // at least |num_input_frames| + |num_output_frames| so that we can always |
31 | // either add or remove data. |
32 | // @param num_input_frames Length of the input buffers in frames. |
33 | // @param num_output_frames Length of the output buffers in frames. |
34 | CircularBuffer(size_t buffer_length, size_t num_input_frames, |
35 | size_t num_output_frames); |
36 | |
37 | // Inserts a buffer of mono input into the |CircularBuffer| if space permits. |
38 | // |
39 | // @param input A channel of input data |num_input_frames| in length. |
40 | // @return True if there was space in the buffer and the input was |
41 | // successfully inserted, false otherwise. |
42 | bool InsertBuffer(const AudioBuffer::Channel& input); |
43 | |
44 | // Retrieves a buffer of output from the |CircularBuffer| if it contains |
45 | // sufficient data. |
46 | // |
47 | // @param output A channel to hold |num_output_frames| of output data. The |
48 | // channel may be greater in length than |num_output_frames|, in this |
49 | // case only the first |num_output_frames| will be overwritten. |
50 | // @return True if there was sufficient data in the buffer and the output was |
51 | // successfully retrieved. |
52 | bool RetrieveBuffer(AudioBuffer::Channel* output); |
53 | |
54 | // Retrieves a buffer of output from the |CircularBuffer| to an offset |
55 | // location in an output channel, provided it contains sufficient data. |
56 | // |
57 | // @param offset Number of samples of offset into the |output| channel. |
58 | // @param output A channel to hold |num_output_frames| of output data. The |
59 | // channel may be greater in length than |num_output_frames| + |offset|, |
60 | // in this case only the first |num_output_frames| after |offset| will be |
61 | // overwritten. |
62 | // @return True if there was sufficient data in the buffer and the output was |
63 | // successfully retrieved. |
64 | bool RetrieveBufferWithOffset(size_t offset, AudioBuffer::Channel* output); |
65 | |
66 | // Returns the number of samples of data currently in the |CircularBuffer|. |
67 | // |
68 | // @return The number of samples of data currently in the buffer. |
69 | size_t GetOccupancy() const { return num_valid_frames_; } |
70 | |
71 | // Resets the |CircularBuffer|. |
72 | void Clear(); |
73 | |
74 | private: |
75 | // Number of input frames to be inserted into the buffer. |
76 | const size_t num_input_frames_; |
77 | |
78 | // Number of output frames to be retrieved from the buffer. |
79 | const size_t num_output_frames_; |
80 | |
81 | // Mono audio buffer to hold the data. |
82 | AudioBuffer buffer_; |
83 | |
84 | // Position at which we are writing into the buffer. |
85 | size_t write_cursor_; |
86 | |
87 | // position at which we are reading from the buffer. |
88 | size_t read_cursor_; |
89 | |
90 | // Number of frames of data currently stored within the buffer. |
91 | size_t num_valid_frames_; |
92 | }; |
93 | |
94 | } // namespace vraudio |
95 | |
96 | #endif // RESONANCE_AUDIO_DSP_CIRCULAR_BUFFER_H_ |
97 | |