wvaudioencoder.cc

00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * Provides a WvEncoder abstraction for the SimpleAudio audio packet format.
00006  * suitable for encoding voice at low bitrates.
00007  *
00008  * Only monaural audio is supported for now.
00009  */
00010 
00011 #include "wvaudioencoder.h"
00012 #include "wvpcmutils.h"
00013 
00014 
00015 /***** WvSimpleAudioEncoder *****/
00016 
00017 
00018 WvSimpleAudioEncoder::WvSimpleAudioEncoder(unsigned int channels,
00019         unsigned int samplerate) :
00020     _channels(channels), _samplesperframe(samplerate / 1000 * 20)
00021 {
00022 }
00023 
00024 
00025 bool WvSimpleAudioEncoder::_typedencode(IBuffer &inbuf, OBuffer &outbuf,
00026     bool flush)
00027 {
00028     static WvPCMUnnormFloatToSigned16Functor f;
00029 
00030     for (;;)
00031     {
00032         // find how much data is available 
00033         size_t count = inbuf.used();
00034         if (count == 0) return true;
00035         if (count < _samplesperframe)
00036             return ! flush; // not enough data
00037 
00038         // convert this frame
00039         while (count > 0)
00040         {
00041             size_t avail = outbuf.optallocable();
00042             if (avail == 0) return false;
00043             if (avail > count) avail = count;
00044             count -= avail;
00045 
00046             const IType *indata = inbuf.get(avail);
00047             OType *outdata = outbuf.alloc(avail);
00048             while (avail-- > 0)
00049                 *(outdata++) = f(*(indata++));
00050         }
00051 
00052         // return if we're not flushing
00053         if (! flush)
00054             return true;
00055     }
00056 }
00057 
00058 
00059 bool WvSimpleAudioEncoder::_typedfinish(OBuffer &outbuf)
00060 {
00061     return true;
00062 }
00063 
00064 
00065 /***** WvSimpleAudioDecoder *****/
00066 
00067 
00068 WvSimpleAudioDecoder::WvSimpleAudioDecoder(unsigned int channels,
00069         unsigned int samplerate) :
00070     _channels(channels), _samplesperframe(samplerate / 1000 * 20)
00071 {
00072 }
00073 
00074 
00075 bool WvSimpleAudioDecoder::_typedencode(IBuffer &inbuf, OBuffer &outbuf,
00076     bool flush)
00077 {
00078     static WvPCMSigned16ToUnnormFloatFunctor f;
00079     
00080     for (;;)
00081     {
00082         size_t avail = inbuf.used();
00083         if (avail == 0) return true;
00084         if (outbuf.free() < _samplesperframe)
00085             return false; // not enough room
00086 
00087         size_t skip = 0;
00088         if (avail > _samplesperframe)
00089         {
00090             // packet is too large but try to decode some audio anyhow
00091             skip = avail - _samplesperframe;
00092             avail -= skip;
00093         }
00094 
00095         const IType *indata = inbuf.get(avail);
00096         inbuf.skip(skip); // skip over bad data
00097         OType *outdata = outbuf.alloc(_samplesperframe);
00098         while (avail-- > 0)
00099             *(outdata++) = f(*(indata++));
00100         
00101         if (! flush)
00102             return true;
00103     }
00104 }
00105 
00106 
00107 bool WvSimpleAudioDecoder::_typedfinish(OBuffer &outbuf)
00108 {
00109     return true;
00110 }

Generated on Mon Feb 5 10:54:28 2007 for WvStreams by  doxygen 1.5.1