adamstark@93
|
1 //=======================================================================
|
adamstark@93
|
2 /** @file CircularBuffer.h
|
adamstark@93
|
3 * @brief A class for calculating onset detection functions
|
adamstark@93
|
4 * @author Adam Stark
|
adamstark@93
|
5 * @copyright Copyright (C) 2008-2014 Queen Mary University of London
|
adamstark@93
|
6 *
|
adamstark@93
|
7 * This program is free software: you can redistribute it and/or modify
|
adamstark@93
|
8 * it under the terms of the GNU General Public License as published by
|
adamstark@93
|
9 * the Free Software Foundation, either version 3 of the License, or
|
adamstark@93
|
10 * (at your option) any later version.
|
adamstark@93
|
11 *
|
adamstark@93
|
12 * This program is distributed in the hope that it will be useful,
|
adamstark@93
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
adamstark@93
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
adamstark@93
|
15 * GNU General Public License for more details.
|
adamstark@93
|
16 *
|
adamstark@93
|
17 * You should have received a copy of the GNU General Public License
|
adamstark@93
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
adamstark@93
|
19 */
|
adamstark@93
|
20 //=======================================================================
|
adamstark@89
|
21
|
adamstark@89
|
22 #ifndef CircularBuffer_h
|
adamstark@89
|
23 #define CircularBuffer_h
|
adamstark@89
|
24
|
adamstark@89
|
25 #include <vector>
|
adamstark@89
|
26
|
adamstark@93
|
27 //=======================================================================
|
adamstark@93
|
28 /** A circular buffer that allows you to add new samples to the end
|
adamstark@93
|
29 * whilst removing them from the beginning. This is implemented in an
|
adamstark@93
|
30 * efficient way which doesn't involve any memory allocation
|
adamstark@93
|
31 */
|
adamstark@89
|
32 class CircularBuffer
|
adamstark@89
|
33 {
|
adamstark@89
|
34 public:
|
adamstark@93
|
35
|
adamstark@93
|
36 /** Constructor */
|
adamstark@93
|
37 CircularBuffer()
|
adamstark@93
|
38 : writeIndex (0)
|
adamstark@89
|
39 {
|
adamstark@89
|
40
|
adamstark@89
|
41 }
|
adamstark@89
|
42
|
adamstark@93
|
43 /** Access the ith element in the buffer */
|
adamstark@93
|
44 double &operator[] (int i)
|
adamstark@89
|
45 {
|
adamstark@89
|
46 int index = (i + writeIndex) % buffer.size();
|
adamstark@89
|
47 return buffer[index];
|
adamstark@89
|
48 }
|
adamstark@93
|
49
|
adamstark@93
|
50 /** Add a new sample to the end of the buffer */
|
adamstark@89
|
51 void addSampleToEnd (double v)
|
adamstark@89
|
52 {
|
adamstark@89
|
53 buffer[writeIndex] = v;
|
adamstark@89
|
54 writeIndex = (writeIndex + 1) % buffer.size();
|
adamstark@89
|
55 }
|
adamstark@89
|
56
|
adamstark@93
|
57 /** Resize the buffer */
|
adamstark@93
|
58 void resize (int size)
|
adamstark@89
|
59 {
|
adamstark@92
|
60 buffer.resize (size);
|
adamstark@89
|
61 writeIndex = 0;
|
adamstark@89
|
62 }
|
adamstark@89
|
63
|
adamstark@89
|
64 private:
|
adamstark@89
|
65
|
adamstark@89
|
66 std::vector<double> buffer;
|
adamstark@89
|
67 int writeIndex;
|
adamstark@89
|
68 };
|
adamstark@89
|
69
|
adamstark@89
|
70 #endif /* CircularBuffer_hpp */
|