split out, clean up and template CircBuf
This commit is contained in:
@@ -744,56 +744,6 @@ void RageSound::SetFadeLength( float fSeconds )
|
|||||||
fade_length = 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.
|
Copyright (c) 2002-2003 by the person(s) listed below. All rights reserved.
|
||||||
|
|||||||
@@ -3,22 +3,7 @@
|
|||||||
|
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include "RageTimer.h"
|
#include "RageTimer.h"
|
||||||
|
#include "RageUtil_CircularBuffer.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);
|
|
||||||
};
|
|
||||||
|
|
||||||
class SoundReader;
|
class SoundReader;
|
||||||
|
|
||||||
@@ -90,7 +75,7 @@ private:
|
|||||||
RageSound *original;
|
RageSound *original;
|
||||||
|
|
||||||
SoundReader *Sample;
|
SoundReader *Sample;
|
||||||
CircBuf databuf;
|
CircBuf<char> databuf;
|
||||||
int FillBuf(int bytes);
|
int FillBuf(int bytes);
|
||||||
|
|
||||||
/* Sound blocks we've sent out recently through GetPCM. We keep track
|
/* Sound blocks we've sent out recently through GetPCM. We keep track
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in New Issue
Block a user