?? cbuf.cpp
字號:
//// Circular buffer member functions//// Bruce T. Lowerre, Public domain, 1995, 1997//// $Log: cbuf.cc,v $// Revision 1.6 1997/08/06 19:35:15 lowerre// removed sampling rate from classes//// Revision 1.5 1997/07/30 19:26:29 lowerre// modifies read and peek routines, again!//// Revision 1.4 1997/07/21 22:13:11 lowerre// found bugs in the read routines, reader not being updated properly//// Revision 1.3 1997/06/04 18:50:03 lowerre// added eod check to available read// fixed eod check in peek and read//// Revision 1.2 1997/06/04 18:14:42 lowerre// added eod boolean to read and peek//// Revision 1.1 1997/05/14 20:02:24 lowerre// Initial revision///////* * This defines the cbuf (circular buffer) class routines. This class is used to * handle speech utterances, which are either pre-recorded or live. */#include "cbuf.h"#include <string.h>/* * CBUF_CHAR::CBUF_CHAR - the class constructor for a circular buffer of size usize * It is assumed that the samples are bytes (either mu-law or a-law). * The buffer length is allocated with 4 additional bytes because * the start and end markers take room. */cbuf_char::cbuf_char( long usize // size of circular buffer){ buffer = new char[usize + 4]; // the actual buffer size = usize; reset ();} // end cbuf_char::cbuf_char/* * CBUF_SHORT::CBUF_SHORT - the class constructor for a circular buffer of size usize * It is assumed that the samples are shorts. * The buffer length is allocated with 4 additional shorts because * the start and end markers take room. */cbuf_short::cbuf_short( long usize // size of circular buffer){ buffer = new short[usize + 4]; // the actual buffer size = usize; reset ();} // end cbuf_short::cbuf_short/* * CBUF_LONG::CBUF_LONG - the class constructor for a circular buffer of size usize * It is assumed that the samples are longs. * The buffer length is allocated with 4 additional longs because * the start and end markers take room. */cbuf_long::cbuf_long( long usize // size of circular buffer){ buffer = new long[usize + 4]; // the actual buffer size = usize; reset ();} // end cbuf_long::cbuf_long/* * CBUF_FLOAT::CBUF_FLOAT - the class constructor for a circular buffer of size usize * It is assumed that the samples are floats. * The buffer length is allocated with 4 additional shorts because * the start and end markers take room. */cbuf_float::cbuf_float( long usize // size of circular buffer){ buffer = new float[usize + 4]; // the actual buffer size = usize; reset ();} // end cbuf_float::cbuf_float/* * CBUF::AVAILABLE_READ - get the number of elements available to read * This is complicated by the circular nature of the * buffer plus the eod marker. */long cbuf::available_read( BOOLEAN ckeod // if True, then check eod marker){ long first = reader, // first actual element available last = writer; // last actual element available + 1 if (ckeod && eod >= 0) last = eod; // end of data has been set if (last < first) last += size; // wraps around return (last - first);} // end cbuf::available_read/* * CBUF::AVAILABLE_READALL - get the number of elements available to readall * This is complicated by the circular nature of the * buffer plus the eod marker. */long cbuf::available_readall (){ long first, // first actual element available last; // last actual element available + 1 if (keeper < 0) first = reader; // keeper not set else first = keeper; if (eod >= 0) last = eod; // set last available else if ((last = writer - 1) < 0) last += size; // wrap around if (last < first) last += size; // wraps around return (last - first);} // end cbuf::available_readall/* * CBUF::AVAILABLE_WRITE - get the number of empty elements available to write * This is complicated by the circular nature of the * buffer plus the keeper pointer. */long cbuf::available_write (){ long first = writer, // first element available last = reader; // last element available + 1 if (keeper >= 0) last = keeper; // keeper has been set if (last <= first) last += size; // wraps around return (last - first);} // end cbuf::available_write/* * CBUF::SETKEEPER - Set the keeper pointer count number of elements before * the current read pointer. There is no error for wrap around. */void cbuf::setkeeper( long count // set the keeper count elements before reader){ keeper = reader - count; while (keeper < 0) keeper += size; // wraps around} // end cbuf::setkeeper/* * CBUF::SETEOD - Set the eod (end of data) pointer count number of elements before * the current read pointer. There is no error for wrap around. */void cbuf::seteod( long count // set the eod count elements before reader){ if (count < 0) eod = writer; // set to the writer pointer else { eod = reader - count; while (eod < 0) eod += size; // wraps around }} // end cbuf::seteod/* * CBUF::ADVANCEOD - Advance the eod (end of data) pointer the specified number of elements. */void cbuf::advanceod( long count // number of elements to advance){ eod += count; while (eod >= size) eod -= size;} // end cbuf::advanceod/* * CBUF_CHAR::READ - Read up to howmany elements from the circular buffer and place * them in where. Return number of elements actually read. * The reader pointer is updated. */long cbuf_char::read( char *where, // where to put it long howmany, // how many to read BOOLEAN ckeod // if True, don't read past eod marker){ long count, lastw; if (ckeod && eod >= 0) lastw = eod; else lastw = writer; for (count = 0; count < howmany; reader++) { if (reader >= size) reader -= size; // wrap around if (reader == lastw) break; where[count++] = buffer[reader];// move one element } return (count);} // end cbuf_char::read/* * CBUF_SHORT::READ - Read up to howmany elements from the circular buffer and place * them in where. Return number of elements actually read. * The reader pointer is updated. */long cbuf_short::read( short *where, // where to put it long howmany, // how many to read BOOLEAN ckeod // if True, don't read past eod marker){ long count, lastw; if (ckeod && eod >= 0) lastw = eod; else lastw = writer; for (count = 0; count < howmany; reader++) { if (reader >= size) reader -= size; // wrap around if (reader == lastw) break; where[count++] = buffer[reader];// move one element } return (count);} // end cbuf_short::read/* * CBUF_LONG::READ - Read up to howmany elements from the circular buffer and place * them in where. Return number of elements actually read. * The reader pointer is updated. */long cbuf_long::read( long *where, // where to put it long howmany, // how many to read BOOLEAN ckeod // if True, don't read past eod marker){ long count, lastw; if (ckeod && eod >= 0) lastw = eod; else lastw = writer; for (count = 0; count < howmany; reader++) { if (reader >= size) reader -= size; // wrap around if (reader == lastw) break; where[count++] = buffer[reader];// move one element } return (count);} // end cbuf_long::read/* * CBUF_FLOAT::READ - Read up to howmany elements from the circular buffer and place * them in where. Return number of elements actually read. * The reader pointer is updated. */long cbuf_float::read( float *where, // where to put it long howmany, // how many to read BOOLEAN ckeod // if True, don't read past eod marker){ long count, lastw; if (ckeod && eod >= 0) lastw = eod; else lastw = writer; for (count = 0; count < howmany; reader++) { if (reader >= size) reader -= size; // wrap around if (reader == lastw) break; where[count++] = buffer[reader];// move one element } return (count);} // end cbuf_float::read/* * CBUF_CHAR::PEEK - Read up to howmany elements from the circular buffer and place * them in where. Return number of elements actually read. * Same as read but the reader pointer is NOT updated. */long cbuf_char::peek( char *where, // where to put it long howmany, // how many to read BOOLEAN ckeod // if True, don't read past eod marker){ long count, rdr = reader, lastw; if (ckeod && eod >= 0) lastw = eod; else lastw = writer; for (count = 0; count < howmany; rdr++) { if (rdr >= size) rdr -= size; // wrap around if (rdr == lastw) break; where[count++] = buffer[rdr]; // move one element } return (count);} // end cbuf_char::peek/* * CBUF_SHORT::PEEK - Read up to howmany elements from the circular buffer and place * them in where. Return number of elements actually read. * Same as read but the reader pointer is NOT updated. */long cbuf_short::peek( short *where, // where to put it long howmany, // how many to read BOOLEAN ckeod // if True, don't read past eod marker){ long count, rdr = reader, lastw; if (ckeod && eod >= 0) lastw = eod; else lastw = writer; for (count = 0; count < howmany; rdr++) { if (rdr >= size) rdr -= size; // wrap around if (rdr == lastw) break; where[count++] = buffer[rdr]; // move one element } return (count);} // end cbuf_short::peek/* * CBUF_LONG::PEEK - Read up to howmany elements from the circular buffer and place * them in where. Return number of elements actually read. * Same as read but the reader pointer is NOT updated. */long cbuf_long::peek( long *where, // where to put it long howmany, // how many to read BOOLEAN ckeod // if True, don't read past eod marker){ long count, rdr = reader, lastw; if (ckeod && eod >= 0) lastw = eod; else lastw = writer; for (count = 0; count < howmany; rdr++) { if (rdr >= size) rdr -= size; // wrap around if (rdr == lastw) break; where[count++] = buffer[rdr]; // move one element
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -