• Main Page
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

ucommon/audio.h

Go to the documentation of this file.
00001 // Copyright (C) 1995-1999 David Sugar, Tycho Softworks.
00002 // Copyright (C) 1999-2005 Open Source Telecom Corp.
00003 // Copyright (C) 2005-2010 David Sugar, Tycho Softworks.
00004 //
00005 // This file is part of GNU uCommon C++.
00006 //
00007 // GNU uCommon C++ is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 3 of the License, or
00010 // (at your option) any later version.
00011 //
00012 // GNU uCommon C++ is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00019 
00033 #ifndef _UCOMMON_AUDIO_H_
00034 #define _UCOMMON_AUDIO_H_
00035 
00036 #ifndef _UCOMMON_UCOMMON_H_
00037 #include <ucommon/ucommon.h>
00038 #endif
00039 
00040 NAMESPACE_UCOMMON
00041 
00049 class  __EXPORT audio
00050 {
00051 public:
00052     typedef uint8_t *encoded_t;
00053     typedef int16_t *linear_t;
00054     typedef enum {ULAW, ALAW, LINEAR, STREAM, PACKET, FRAMED, NATIVE} type_t;
00055     typedef enum {IDLE = 0, ACTIVE, END, FAILED, INVALID, INVFILE, INVIO = FAILED} state_t;
00056 
00057     static volatile timeout_t global;
00058 
00066     class __EXPORT framer
00067     {
00068     protected:
00069         state_t state;
00070 
00071     public:
00072         
00073         virtual timeout_t frametime(void) = 0;
00074         virtual unsigned framesize(void) = 0;
00075         virtual unsigned framesize(encoded_t data) = 0;
00076         virtual encoded_t get(void);    // gives audiobuf mem...
00077         virtual unsigned get(encoded_t data);
00078         virtual unsigned put(encoded_t data);
00079         virtual encoded_t buf(void);
00080         virtual unsigned put(void);
00081         virtual unsigned push(encoded_t data, unsigned size);
00082         virtual unsigned pull(encoded_t data, unsigned size);
00083         virtual bool fill(void);
00084         virtual bool flush(void);
00085         virtual bool rewind(void);
00086         virtual bool append(void);
00087         virtual bool seek(timeout_t position);
00088         virtual bool trim(timeout_t backup = 0);
00089         virtual timeout_t skip(timeout_t offset);
00090         virtual timeout_t locate(void);
00091         virtual timeout_t length(void);
00092         virtual void release(void);
00093         virtual operator bool();
00094         
00095         inline state_t status(void)
00096             {return state;};
00097     };
00098 
00099     typedef audio::framer *framer_t;
00100 
00101     static void release(framer_t buffer);
00102     static void u2a(encoded_t data, unsigned samples);
00103     static void a2u(encoded_t data, unsigned samples);
00104     static linear_t expand(encoded_t ulaw, unsigned samples, linear_t target = NULL);
00105     static encoded_t repack(encoded_t ulaw, unsigned samples, linear_t source = NULL);
00106     static int16_t dbm(float dbm);
00107     static float dbm(int16_t linear);
00108     static void init(void);
00109 };
00110 
00117 class __EXPORT audiocodec : public LinkedObject, public audio
00118 {
00119 protected:
00120     friend class audiobuffer;
00121 
00122     audiocodec();
00123     audiocodec(const audiocodec *original);
00124 
00125     timeout_t framing;      // framing for this codec...
00126     unsigned long rate;     // rate for this codec...
00127     
00128 public:
00129     type_t type;
00130 
00139     virtual audiocodec *create(caddr_t destination = NULL, timeout_t framing = 0) const = 0;
00140 
00146     virtual const audiocodec *basetype(void) const = 0;
00147 
00153     virtual unsigned getMinimum(void) const;
00154 
00160     virtual unsigned getActual(audio::encoded_t encoded) const = 0;
00161 
00166     virtual unsigned getMaximum(void) const = 0;
00167 
00172     virtual unsigned getBuffered(void) const = 0;
00173 
00178     virtual timeout_t getFraming(void) const;
00179 
00184     virtual unsigned long getClock(void) const;
00185 
00190     virtual unsigned getChannels(void) const;
00191 
00196     virtual unsigned autype(void) const;
00197 
00202     virtual unsigned getAlignment(void) const;
00203 
00208     virtual unsigned getBuffering(void) const;
00209 
00214     virtual const char *getName(void) const = 0;
00215 
00221     virtual bool test(const char *name) const = 0;
00222 
00228     virtual unsigned getSize(void) const = 0;
00229 
00236     virtual unsigned encode(linear_t linear, encoded_t encoded);
00237 
00244     virtual unsigned decode(encoded_t encoded, linear_t linear);
00245 
00250     virtual void reset(void);
00251 
00258     static const audiocodec *find(const char *name, const audiocodec *channel = NULL);
00259 
00266     static audiocodec *get(const char *name, const audiocodec *channel = NULL);
00267 
00273     static audiocodec *first(void);
00274 };
00275 
00284 class __EXPORT audiobuffer : public audio::framer, public audio
00285 {
00286 private:
00287     static audiobuffer *alloc(const audiocodec *channel, timeout_t framing = 0);
00288 
00289     size_t offset;
00290 
00291 protected:
00292     unsigned long iocount;
00293     timeout_t limit;
00294     encoded_t buffer;
00295     unsigned backstore, allocsize, backpos, backio, convert, buffering;
00296     union {
00297         audiocodec *channel;
00298         const audiocodec *channelbase;
00299     };
00300     audiocodec *encoding;
00301 
00302     bool assign(size_t size, const audiocodec *format = NULL, timeout_t framing = 0);
00303 
00304     inline encoded_t getBuffer(void)
00305         {return buffer;};
00306 
00307     inline linear_t getConvert(void)
00308         {return (linear_t)(((caddr_t)(this)) + convert);};
00309 
00310     encoded_t transcode(encoded_t source, encoded_t target, audiocodec *from, audiocodec *to);
00311     bool transbuffer(audiocodec *from, audiocodec *to);
00312 
00313 public:
00314     timeout_t frametime(void);
00315     unsigned framesize(void);
00316     unsigned framesize(encoded_t data);
00317 
00322     inline unsigned long getContexts(void)
00323         {return iocount;};
00324 
00325     inline unsigned getConversion(void)
00326         {return backstore - convert;};
00327 
00328     inline unsigned getAvailable(void)
00329         {return allocsize - (unsigned)(buffer - ((encoded_t)(this)));};
00330 
00331     inline unsigned getBuffering(void)
00332         {return buffering;};
00333 
00334     inline const audiocodec *getChannel(void)
00335         {return channel;};
00336 
00337     inline const audiocodec *getEncoding(void)
00338         {return encoding;};
00339 
00340     inline unsigned getBackstore(void)
00341         {return allocsize - backstore;};
00342 
00343     inline unsigned getAllocated(void)
00344         {return allocsize;};
00345     
00352     linear_t expand(encoded_t data);
00353 
00354     virtual void release(void);
00355     virtual fd_t handle(void);
00356 
00357     unsigned text(char *str, size_t size);
00358     unsigned pull(encoded_t data, unsigned len);
00359     unsigned push(encoded_t data, unsigned len);
00360 
00361     static audiobuffer *create(timeout_t limit);
00362     static audiobuffer *create(const audiocodec *channel, timeout_t framing);
00363 };
00364 
00371 class __EXPORT audiofile : public audiobuffer
00372 {
00373 protected:
00374     fsys::offset_t hiwater, current, headersize;
00375     size_t maxio, minio, bufio;
00376     unsigned bufpos, bufend;
00377 
00378     uint8_t header[40]; // header buffer
00379     enum {RAW, AU, RIFF, WAV} filetype;
00380     fsys fs;
00381 
00382     void open(const char *path, fsys::access_t access, size_t objsize, timeout_t framing);
00383     bool rewind(void);
00384     bool append(void);
00385     bool flush(void);
00386     timeout_t length(void);
00387     timeout_t locate(void);
00388     bool seek(timeout_t position);
00389     bool trim(timeout_t backup);
00390     timeout_t skip(timeout_t offset);
00391     fd_t handle(void);
00392     operator bool();
00393 
00394 public:
00395     static bool info(audiobuffer *buffer, const char *path, timeout_t framing = 0);
00396 
00404     static bool access(audiobuffer *buffer, const char *path, timeout_t preferred = 0);
00405 
00416     static bool create(audiobuffer *buffer, const char *path, timeout_t preferred = 0, const char *note = NULL, const audiocodec *encoding = NULL, unsigned mode = 0640);
00417 
00425     static bool append(audiobuffer *buffer, const char *path, timeout_t preferred = 0);
00426 };
00427 
00432 class __EXPORT tonegen : private audiobuffer
00433 {
00434 };
00435 
00436 END_NAMESPACE
00437 
00438 #endif
00439 
00440 

Generated on Sun Sep 4 2011 for UCommon by  doxygen 1.7.1