split out, clean up and template CircBuf

This commit is contained in:
Glenn Maynard
2004-01-14 09:38:14 +00:00
parent c237bf2364
commit cad991074a
3 changed files with 71 additions and 68 deletions
-50
View File
@@ -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<unsigned>(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.
+3 -18
View File
@@ -3,22 +3,7 @@
#include <deque>
#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<char> databuf;
int FillBuf(int bytes);
/* Sound blocks we've sent out recently through GetPCM. We keep track
+68
View File
@@ -0,0 +1,68 @@
#ifndef RAGE_UTIL_CIRCULAR_BUFFER
#define RAGE_UTIL_CIRCULAR_BUFFER
template<class T>
class CircBuf
{
basic_string<T> 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<unsigned>(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