diff --git a/stepmania/src/RageSound.cpp b/stepmania/src/RageSound.cpp index e67d3e9153..29045195db 100644 --- a/stepmania/src/RageSound.cpp +++ b/stepmania/src/RageSound.cpp @@ -744,56 +744,6 @@ void RageSound::SetFadeLength( float fSeconds ) fade_length = fSeconds; } -void CircBuf::reserve(unsigned n) -{ - clear(); - buf.erase(); - buf.insert(buf.end(), n, 0); -} - -void CircBuf::clear() -{ - cnt = start = 0; -} - -void CircBuf::write(const char *buffer, unsigned buffer_size) -{ - ASSERT(size() + buffer_size <= capacity()); /* overflow */ - - while(buffer_size) - { - unsigned write_pos = start + size(); - if(write_pos >= buf.size()) write_pos -= buf.size(); - - int cpy = int(min(buffer_size, buf.size() - write_pos)); - buf.replace(write_pos, cpy, buffer, cpy); - - cnt += cpy; - - buffer += cpy; - buffer_size -= cpy; - } -} - -void CircBuf::read(char *buffer, unsigned buffer_size) -{ - ASSERT(size() >= buffer_size); /* underflow */ - - while(buffer_size) - { - unsigned total = static_cast(min(buf.size() - start, size())); - unsigned cpy = min(buffer_size, total); - buf.copy(buffer, cpy, start); - - start += cpy; - if(start == buf.size()) start = 0; - cnt -= cpy; - - buffer += cpy; - buffer_size -= cpy; - } -} - /* ----------------------------------------------------------------------------- Copyright (c) 2002-2003 by the person(s) listed below. All rights reserved. diff --git a/stepmania/src/RageSound.h b/stepmania/src/RageSound.h index 902fb3bbda..45f2d4cddc 100644 --- a/stepmania/src/RageSound.h +++ b/stepmania/src/RageSound.h @@ -3,22 +3,7 @@ #include #include "RageTimer.h" - -class CircBuf -{ - string buf; - unsigned cnt, start; - -public: - - CircBuf() { clear(); } - unsigned size() const { return cnt; } - unsigned capacity() const { return buf.size(); } - void reserve(unsigned n); - void clear(); - void write(const char *buf, unsigned size); - void read(char *buf, unsigned size); -}; +#include "RageUtil_CircularBuffer.h" class SoundReader; @@ -40,7 +25,7 @@ public: /* If cache == true (1), we'll preload the entire file into memory if * it's small enough. If this is done, a large number of copies of the * sound can be played without much performance penalty. This is useful - * for BM (keyed sounds), and for rapidly-repeating sound effects, such + * for BM (keyed sounds), and for rapidly-repeating sound effects, such * as the music wheel. * * If cache == false (0), we'll never preload the file (always stream @@ -90,7 +75,7 @@ private: RageSound *original; SoundReader *Sample; - CircBuf databuf; + CircBuf databuf; int FillBuf(int bytes); /* Sound blocks we've sent out recently through GetPCM. We keep track diff --git a/stepmania/src/RageUtil_CircularBuffer.h b/stepmania/src/RageUtil_CircularBuffer.h new file mode 100644 index 0000000000..4926e50b6e --- /dev/null +++ b/stepmania/src/RageUtil_CircularBuffer.h @@ -0,0 +1,68 @@ +#ifndef RAGE_UTIL_CIRCULAR_BUFFER +#define RAGE_UTIL_CIRCULAR_BUFFER + +template +class CircBuf +{ + basic_string buf; + unsigned cnt, start; + +public: + CircBuf() { clear(); } + unsigned size() const { return cnt; } + unsigned capacity() const { return buf.size(); } + + void reserve( unsigned n ) + { + clear(); + buf.erase(); + buf.insert( buf.end(), n, 0 ); + } + + void clear() + { + cnt = start = 0; + } + + void write( const T *buffer, unsigned buffer_size ) + { + ASSERT( size() + buffer_size <= capacity() ); /* overflow */ + + while( buffer_size ) + { + unsigned write_pos = start + size(); + if( write_pos >= buf.size() ) + write_pos -= buf.size(); + + const int cpy = int(min(buffer_size, buf.size() - write_pos)); + buf.replace( write_pos, cpy, buffer, cpy ); + + cnt += cpy; + + buffer += cpy; + buffer_size -= cpy; + } + } + + void read( T *buffer, unsigned buffer_size ) + { + ASSERT( size() >= buffer_size ); /* underflow */ + + while( buffer_size ) + { + const unsigned total = static_cast(min(buf.size() - start, size())); + const unsigned cpy = min( buffer_size, total ); + buf.copy( buffer, cpy, start ); + + start += cpy; + if( start == buf.size() ) + start = 0; + cnt -= cpy; + + buffer += cpy; + buffer_size -= cpy; + } + } +}; + +#endif