Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

audio.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2001 Open Source Telecom Corporation.
00002 //  
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 // 
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 // 
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software 
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 // 
00017 // As a special exception to the GNU General Public License, permission is 
00018 // granted for additional uses of the text contained in its release 
00019 // of ccaudio.
00020 // 
00021 // The exception is that, if you link the ccaudio library with other
00022 // files to produce an executable, this does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public License.
00024 // Your use of that executable is in no way restricted on account of
00025 // linking the ccaudio library code into it.
00026 // 
00027 // This exception does not however invalidate any other reasons why
00028 // the executable file might be covered by the GNU General Public License.
00029 // 
00030 // This exception applies only to the code released under the 
00031 // name ccaudio.  If you copy code from other releases into a copy of
00032 // ccaudio, as the General Public License permits, the exception does
00033 // not apply to the code that you add in this way.  To avoid misleading
00034 // anyone as to the status of such modified files, you must delete
00035 // this exception notice from them.
00036 // 
00037 // If you write modifications of your own for ccaudio, it is your choice
00038 // whether to permit this exception to apply to your modifications.
00039 // If you do not wish that, delete this exception notice.  
00040 
00041 #ifndef __CCXX_AUDIO_H__
00042 #define __CCXX_AUDIO_H__
00043 
00044 #ifndef __EXPORT
00045 #define __EXPORT
00046 #endif
00047 
00048 #ifndef __CCXX_CONFIG_H__
00049 #include <cc++/config.h>
00050 #endif
00051 
00052 #ifdef  __NAMESPACES__
00053 namespace ost {
00054 #endif
00055 
00056 typedef enum
00057 {
00058         SAMPLE_RATE_UNKNOWN,
00059         SAMPLE_RATE_6KHZ = 6000,
00060         SAMPLE_RATE_8KHZ = 8000,
00061         SAMPLE_RATE_44KHZ = 44100
00062 }       samplerate_t;
00063 
00064 typedef enum
00065 {
00066         UNKNOWN_AUDIO_ENCODING = 0,
00067         G721_ADPCM_ENCODING,
00068         G722_AUDIO_ENCODING,
00069         G722_7BIT_ENCODING,
00070         G722_6BIT_ENCODING,
00071         G723_3BIT_ENCODING,
00072         G723_5BIT_ENCODING,
00073         GSM_VOICE_ENCODING,
00074         MULAW_AUDIO_ENCODING,
00075         ALAW_AUDIO_ENCODING,
00076         OKI_ADPCM_ENCODING,
00077         DIALOGIC_ADPCM_ENCODING,
00078         CDA_STEREO_ENCODING,
00079         CDA_MONO_ENCODING,
00080         PCM8_STEREO_ENCODING,
00081         PCM8_AUDIO_ENCODING,
00082         PCM16_STEREO_ENCODING,
00083         PCM16_AUDIO_ENCODING,
00084         PCM32_STEREO_ENCODING,
00085         PCM32_AUDIO_ENCODING
00086 }       audioencoding_t;
00087 
00088 typedef enum
00089 {
00090         AUDIO_FORMAT_RAW,
00091         AUDIO_FORMAT_SUN,
00092         AUDIO_FORMAT_RIFF,
00093         AUDIO_FORMAT_WAVE
00094 } audioformat_t;
00095 
00096 typedef enum
00097 {
00098         AUDIO_SUCCESS = 0,
00099         AUDIO_READ_LASTFRAME,
00100         AUDIO_NOT_OPENED,
00101         AUDIO_END_OF_FILE,
00102         AUDIO_START_OF_FILE,
00103         AUDIO_RATE_UNSUPPORTED,
00104         AUDIO_ENCODING_UNSUPPORTED,
00105         AUDIO_READ_INTERRUPTED,
00106         AUDIO_WRITE_INTERRUPTED,
00107         AUDIO_READ_FAILURE,
00108         AUDIO_WRITE_FAILURE,
00109         AUDIO_READ_INCOMPLETE,
00110         AUDIO_WRITE_INCOMPLETE,
00111         AUDIO_REQUEST_INVALID,
00112         AUDIO_TOC_FAILED,
00113         AUDIO_STAT_FAILED,
00114         AUDIO_INVALID_TRACK,
00115         AUDIO_PLAYBACK_FAILED,
00116         AUDIO_NOT_PLAYING
00117 } audioerror_t;
00118 
00119 typedef struct
00120 {
00121         audioformat_t format;
00122         audioencoding_t encoding;
00123         unsigned rate;
00124         unsigned order;
00125         char *annotation;
00126 }       audioinfo_t;
00127 
00128 CCXX_EXPORT(bool) ismono(audioencoding_t encoding);
00129 CCXX_EXPORT(bool) issterio(audioencoding_t encoding);
00130 CCXX_EXPORT(samplerate_t) samplerate(audioencoding_t encoding);
00131 CCXX_EXPORT(int) sampleframe(audioencoding_t encoding, int samples = 0);
00132 CCXX_EXPORT(int) samplecount(audioencoding_t);
00133 CCXX_EXPORT(unsigned) long tosamples(audioencoding_t encoding, size_t bytes);
00134 CCXX_EXPORT(unsigned) long tobytes(audioencoding_t encoding, unsigned long samples);
00135 CCXX_EXPORT(void) samplefill(unsigned char *addr, int samples, audioencoding_t encoding);
00136 
00145 class CCXX_CLASS_EXPORT AudioFile
00146 {
00147 private:
00148         char *pathname;
00149         audioerror_t error;
00150         audioinfo_t info;
00151         unsigned long header;           // offset to start of audio
00152         unsigned long minimum;          // minimum sample size required
00153 
00154         void Initialize(void);
00155         void getWaveFormat(int size);
00156 
00157 protected:
00158         union
00159         {
00160                 int fd;
00161                 void *handle;
00162         } file;
00163 
00164         virtual bool afCreate(const char *path);
00165         virtual bool afOpen(const char *path);
00166         virtual bool afPeek(unsigned char *data, unsigned size);
00167         virtual int afRead(unsigned char *data, unsigned size);
00168         virtual int afWrite(unsigned char *data, unsigned size);
00169         virtual bool afSeek(unsigned long pos);
00170         virtual void afClose(void);
00171 
00172         virtual char *getContinuation(void)
00173                 {return NULL;};
00174 
00175         audioerror_t setError(audioerror_t err);
00176 
00177         unsigned short getaushort(unsigned char *data);
00178         void setaushort(unsigned char *data, unsigned short value);
00179         unsigned long getaulong(unsigned char *data);
00180         void setaulong(unsigned char *data, unsigned long value);
00181 
00182 public:
00183         AudioFile(const char *fname, unsigned long samples = 0);
00184         AudioFile(const char *fname, audioinfo_t *info, unsigned long min = 0);
00185 
00186         AudioFile()
00187                 {Initialize();};
00188 
00189         ~AudioFile()
00190                 {Close();};
00191 
00192         void Open(const char *fname);
00193         void Create(const char *fname, audioinfo_t *info);
00194         void Close(void);
00195         int getBuffer(void *addr, unsigned len);
00196         int putBuffer(void *attr, unsigned len);        
00197         audioerror_t getSamples(void *addr, unsigned samples);
00198         audioerror_t putSamples(void *addr, unsigned samples);
00199         audioerror_t Skip(long samples);
00200         audioerror_t setPosition(unsigned long samples = ~0l);
00201         audioerror_t getInfo(audioinfo_t *info);
00202         audioerror_t setMinimum(unsigned long samples);
00203         unsigned long getPosition(void);
00204         virtual bool isOpen(void);
00205         virtual bool hasPositioning(void)
00206                 {return true;};
00207 
00208         inline audioencoding_t getEncoding(void)
00209                 {return info.encoding;};
00210 
00211         inline audioformat_t getFormat(void)
00212                 {return info.format;};
00213 
00214         inline unsigned getSampleRate(void)
00215                 {return info.rate;};
00216         inline char *getAnnotation(void)
00217                 {return info.annotation;};
00218 
00219         inline audioerror_t getError(void)
00220                 {return error;};
00221 
00222         inline bool operator!(void)
00223                 {return !isOpen();};
00224 };
00225 
00234 class CCXX_CLASS_EXPORT CDAudio
00235 {
00236 private:
00237         union
00238         {
00239                 int fd;
00240         } file;
00241         unsigned char v0, v1;
00242 #ifdef  __WIN32__
00243         CRITICAL_SECTION crit;
00244         bool paused;
00245         bool opened;
00246         char position[20];
00247         char endmark[24];
00248         char ret[256];
00249         DWORD command(char *fmt, ...);
00250 #endif
00251         audioerror_t err;
00252 
00253 public:
00254         CDAudio(int devnbr = 0);
00255         ~CDAudio();
00256 
00257         audioerror_t Play(int start, int end = 0);      
00258         audioerror_t Stop(void);
00259         audioerror_t Pause(void);
00260         audioerror_t Resume(void);
00261         audioerror_t Eject(void);
00262         audioerror_t Reload(void);
00263         int getFirst(void);
00264         int getLast(void);
00265         bool isPaused(void);
00266         bool isAudio(int track);
00267         bool isOpen(void);
00268 
00269         unsigned char getVolume(int speaker);
00270         void setVolume(unsigned char left, unsigned char right);
00271 
00272         inline unsigned char getVolumeLeft(void)
00273                 {return getVolume(0);};
00274 
00275         inline unsigned char getVolumeRight(void)
00276                 {return getVolume(1);};
00277 
00278         inline audioerror_t getError(void)
00279                 {return err;};
00280 
00281         inline bool operator!(void)
00282                 {return !isOpen();};
00283 };
00284 
00291 class CCXX_CLASS_EXPORT AudioSample
00292 {
00293 protected:
00294         friend class AudioCopy;
00295 
00296         audioencoding_t encoding;
00297         unsigned rate;
00298         unsigned count;
00299         unsigned char *samples;
00300 
00301 public:
00302         AudioSample(unsigned frame, audioencoding_t coding = PCM16_AUDIO_ENCODING, unsigned rate = 8000);
00303         ~AudioSample();
00304 
00305         inline unsigned getCount(void)
00306                 {return count;};
00307 
00308         inline unsigned getRate(void)
00309                 {return rate;};
00310 
00311         inline audioencoding_t getEncoding(void)
00312                 {return encoding;};
00313 
00314         inline unsigned char *getSamples(void)
00315                 {return samples;};
00316 };      
00317 
00330 class CCXX_CLASS_EXPORT AudioCodec : public AudioSample
00331 {
00332 protected:
00333         AudioCodec(unsigned frames, audioencoding_t encoding, unsigned rate);
00334         unsigned length;
00335 
00336 public:
00344         virtual void Encode(unsigned short *buffer, unsigned samples = 0, int channel = 0) = 0;
00345 
00353         virtual bool Encode(AudioSample *sample) = 0;
00354 
00362         virtual unsigned Decode(unsigned short *buffer, int channel = 0) = 0;
00363 
00370         virtual bool Decode(AudioSample *sample) = 0;
00371 
00381         virtual unsigned Load(unsigned char *data, unsigned length);
00382 
00392         virtual unsigned Save(unsigned char *data, unsigned length);
00393          
00394 
00400         inline unsigned getLength(void)
00401                 {return length;};       
00402 
00406         unsigned getFrameSize(void);
00407 
00411         virtual unsigned getChannels(void);
00412 };
00413 
00423 class CCXX_CLASS_EXPORT AudioRegisterCodec
00424 {
00425 private:
00426         static AudioRegisterCodec *first;
00427         AudioRegisterCodec *next;
00428 
00429 protected:
00430         unsigned minframe, framecnt;
00431         AudioRegisterCodec(unsigned min, unsigned def = 1);
00432 
00433 public:
00439         virtual audioencoding_t getEncoding(void) = 0;
00440 
00446         virtual unsigned getRate(void)
00447                 {samplerate(getEncoding());};
00448 
00454         virtual char *getName(void) = 0;
00455 
00463         virtual AudioCodec *getCodec(unsigned frames) = 0;
00464 
00476         virtual AudioSample *getTone(unsigned f1, unsigned f2, unsigned maxsize, unsigned maxtone = 0) = 0;     
00477 
00483         inline unsigned getFrameSize(void)
00484                 {return minframe;};
00485 
00492         inline unsigned getFrameCount(void)
00493                 {return framecnt;};
00494 
00501         friend AudioRegisterCodec *findCodec(audioencoding_t encoding, unsigned rate = 0);
00502 
00508         friend AudioRegisterCodec *findCodec(const char *name);
00509 
00518         friend AudioCodec *getCodec(audioencoding_t encoding, unsigned rate = 0, unsigned frames = 0);
00519 
00527         friend AudioCodec *getCodec(const char *name, unsigned frame = 0);
00528 };
00529         
00538 class CCXX_CLASS_EXPORT AudioTone : public AudioSample
00539 {
00540 protected:
00541         double p1, p2, v1, v2, fa1, fa2;
00542 
00543 public:
00544         AudioTone(unsigned size, unsigned f1 = 0, unsigned f2 = 0, unsigned rate = 8000);
00545 
00549         void Fill(unsigned max = 0);
00550 
00551         /*
00552          * Set frequency.
00553          *
00554          * @param f1 first frequency.
00555          * @param f2 second frequency.
00556          */
00557         void setFreq(unsigned f1, unsigned f2 = 0);
00558 };
00559 
00570 class CCXX_CLASS_EXPORT AudioCopy : public AudioSample
00571 {
00572 protected:
00573         unsigned char *next;
00574         unsigned left;
00575 
00576         virtual AudioSample *Fill(void) = 0;
00577 
00578 public:
00579         AudioCopy(unsigned frame, audioencoding_t encoding = PCM16_AUDIO_ENCODING, unsigned rate = 8000);
00580         bool Copy(void);
00581 
00582         inline bool isEmpty(void)
00583                 {return next == NULL;};
00584 };
00585 
00586 #ifdef  __NAMESPACES__
00587 };
00588 #endif
00589 
00590 #endif
00591 

Generated at Tue Nov 20 16:55:21 2001 for ccAudio by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001