WvStreams
wvrateadjust.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * WvRateAdjust is a WvEncoder that makes sure data comes out of it at a
00006  * given average rate.  It adds duplicate samples or removes extra samples
00007  * from the input in order to achieve this.
00008  * 
00009  * The current version doesn't do anything fancy like linear or spline
00010  * interpolation, since it's being built mainly for minor rate adjustments
00011  * like 8000 Hz to 8020 Hz, and interpolation probably doesn't help too 
00012  * much there.  It's also nicer to not have to worry explicitly about the
00013  * number format, which you would have to do if you were doing actual math.
00014  * 
00015  * You can also use this as a slightly (not terribly) inefficient rate
00016  * *estimator* for the input stream.
00017  * 
00018  * NOTE: Time is of the essence of this encoder.
00019  */ 
00020 #ifndef __WVRATEADJUST_H
00021 #define __WVRATEADJUST_H
00022 
00023 #include "wvencoder.h"
00024 #include "wvtimeutils.h"
00025 
00026 class WvRateAdjust : public WvEncoder
00027 {
00028 public:
00029     WvRateAdjust *match_rate;
00030  
00031     int sampsize, irate_n, irate_d, orate_n, orate_d;
00032     WvTime epoch; // the time when sampling started
00033     
00034     // the token bucket holds the "remainder" of our fake integer division
00035     // operation.  With this, we can make sure we always average out to
00036     // exactly the right rate.
00037     // 
00038     // Basically:
00039     //    bucket = ((orate*insamps) - (irate*outsamps)) * irate_d * orate_d
00040     // 
00041     // ...but with some care thrown in to prevent overflows.
00042     int bucket;
00043     
00044     WvRateAdjust(int _sampsize, int _irate_base, int _orate);
00045     WvRateAdjust(int _sampsize, int _irate_base, WvRateAdjust *_match_rate);
00046     
00047     int getirate()
00048         { return irate_n / irate_d; }
00049     int getorate()
00050         { return orate_n / orate_d; }
00051     
00052 protected:
00053     void init(int _sampsize, int _irate_base);
00054     
00055     virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush);
00056 };
00057 
00058 #endif // __WVRATEADJUST_H