adamstark@93: //======================================================================= adamstark@93: /** @file CircularBuffer.h adamstark@93: * @brief A class for calculating onset detection functions adamstark@93: * @author Adam Stark adamstark@93: * @copyright Copyright (C) 2008-2014 Queen Mary University of London adamstark@93: * adamstark@93: * This program is free software: you can redistribute it and/or modify adamstark@93: * it under the terms of the GNU General Public License as published by adamstark@93: * the Free Software Foundation, either version 3 of the License, or adamstark@93: * (at your option) any later version. adamstark@93: * adamstark@93: * This program is distributed in the hope that it will be useful, adamstark@93: * but WITHOUT ANY WARRANTY; without even the implied warranty of adamstark@93: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the adamstark@93: * GNU General Public License for more details. adamstark@93: * adamstark@93: * You should have received a copy of the GNU General Public License adamstark@93: * along with this program. If not, see . adamstark@93: */ adamstark@93: //======================================================================= adamstark@89: adamstark@89: #ifndef CircularBuffer_h adamstark@89: #define CircularBuffer_h adamstark@89: adamstark@89: #include adamstark@89: adamstark@93: //======================================================================= adamstark@93: /** A circular buffer that allows you to add new samples to the end adamstark@93: * whilst removing them from the beginning. This is implemented in an adamstark@93: * efficient way which doesn't involve any memory allocation adamstark@93: */ adamstark@89: class CircularBuffer adamstark@89: { adamstark@89: public: adamstark@93: adamstark@93: /** Constructor */ adamstark@93: CircularBuffer() adamstark@93: : writeIndex (0) adamstark@89: { adamstark@89: adamstark@89: } adamstark@89: adamstark@93: /** Access the ith element in the buffer */ adamstark@93: double &operator[] (int i) adamstark@89: { adamstark@89: int index = (i + writeIndex) % buffer.size(); adamstark@89: return buffer[index]; adamstark@89: } adamstark@93: adamstark@93: /** Add a new sample to the end of the buffer */ adamstark@89: void addSampleToEnd (double v) adamstark@89: { adamstark@89: buffer[writeIndex] = v; adamstark@89: writeIndex = (writeIndex + 1) % buffer.size(); adamstark@89: } adamstark@89: adamstark@93: /** Resize the buffer */ adamstark@93: void resize (int size) adamstark@89: { adamstark@92: buffer.resize (size); adamstark@89: writeIndex = 0; adamstark@89: } adamstark@89: adamstark@89: private: adamstark@89: adamstark@89: std::vector buffer; adamstark@89: int writeIndex; adamstark@89: }; adamstark@89: adamstark@89: #endif /* CircularBuffer_hpp */