Mercurial > hg > btrack
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CircularBuffer.h Sat Jun 18 10:50:06 2016 +0100 @@ -0,0 +1,70 @@ +//======================================================================= +/** @file CircularBuffer.h + * @brief A class for calculating onset detection functions + * @author Adam Stark + * @copyright Copyright (C) 2008-2014 Queen Mary University of London + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +//======================================================================= + +#ifndef CircularBuffer_h +#define CircularBuffer_h + +#include <vector> + +//======================================================================= +/** A circular buffer that allows you to add new samples to the end + * whilst removing them from the beginning. This is implemented in an + * efficient way which doesn't involve any memory allocation + */ +class CircularBuffer +{ +public: + + /** Constructor */ + CircularBuffer() + : writeIndex (0) + { + + } + + /** Access the ith element in the buffer */ + double &operator[] (int i) + { + int index = (i + writeIndex) % buffer.size(); + return buffer[index]; + } + + /** Add a new sample to the end of the buffer */ + void addSampleToEnd (double v) + { + buffer[writeIndex] = v; + writeIndex = (writeIndex + 1) % buffer.size(); + } + + /** Resize the buffer */ + void resize (int size) + { + buffer.resize (size); + writeIndex = 0; + } + +private: + + std::vector<double> buffer; + int writeIndex; +}; + +#endif /* CircularBuffer_hpp */