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