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 typedef enum
00049 {
00050         SAMPLE_RATE_UNKNOWN,
00051         SAMPLE_RATE_6KHZ = 6000,
00052         SAMPLE_RATE_8KHZ = 8000,
00053         SAMPLE_RATE_44KHZ = 44100
00054 }       samplerate_t;
00055 
00056 typedef enum
00057 {
00058         UNKNOWN_AUDIO_ENCODING = 0,
00059         G721_ADPCM_ENCODING,
00060         G722_AUDIO_ENCODING,
00061         G722_7BIT_ENCODING,
00062         G722_6BIT_ENCODING,
00063         G723_3BIT_ENCODING,
00064         G723_5BIT_ENCODING,
00065         GSM_VOICE_ENCODING,
00066         MULAW_AUDIO_ENCODING,
00067         ALAW_AUDIO_ENCODING,
00068         OKI_ADPCM_ENCODING,
00069         DIALOGIC_ADPCM_ENCODING,
00070         CDA_STEREO_ENCODING,
00071         CDA_MONO_ENCODING,
00072         PCM8_STEREO_ENCODING,
00073         PCM8_AUDIO_ENCODING,
00074         PCM16_STEREO_ENCODING,
00075         PCM16_AUDIO_ENCODING,
00076         PCM32_STEREO_ENCODING,
00077         PCM32_AUDIO_ENCODING
00078 }       audioencoding_t;
00079 
00080 typedef enum
00081 {
00082         AUDIO_FORMAT_RAW,
00083         AUDIO_FORMAT_SUN,
00084         AUDIO_FORMAT_RIFF,
00085         AUDIO_FORMAT_WAVE
00086 } audioformat_t;
00087 
00088 typedef enum
00089 {
00090         AUDIO_SUCCESS = 0,
00091         AUDIO_READ_LASTFRAME,
00092         AUDIO_NOT_OPENED,
00093         AUDIO_END_OF_FILE,
00094         AUDIO_START_OF_FILE,
00095         AUDIO_RATE_UNSUPPORTED,
00096         AUDIO_ENCODING_UNSUPPORTED,
00097         AUDIO_READ_INTERRUPTED,
00098         AUDIO_WRITE_INTERRUPTED,
00099         AUDIO_READ_FAILURE,
00100         AUDIO_WRITE_FAILURE,
00101         AUDIO_READ_INCOMPLETE,
00102         AUDIO_WRITE_INCOMPLETE,
00103         AUDIO_REQUEST_INVALID,
00104         AUDIO_TOC_FAILED,
00105         AUDIO_STAT_FAILED,
00106         AUDIO_INVALID_TRACK,
00107         AUDIO_PLAYBACK_FAILED,
00108         AUDIO_NOT_PLAYING
00109 } audioerror_t;
00110 
00111 typedef struct
00112 {
00113         audioformat_t format;
00114         audioencoding_t encoding;
00115         unsigned rate;
00116         unsigned order;
00117         char *annotation;
00118 }       audioinfo_t;
00119 
00120 bool ismono(audioencoding_t encoding);
00121 bool issterio(audioencoding_t encoding);
00122 samplerate_t samplerate(audioencoding_t encoding);
00123 int sampleframe(audioencoding_t encoding, int samples = 0);
00124 int samplecount(audioencoding_t);
00125 unsigned long tosamples(audioencoding_t encoding, size_t bytes);
00126 unsigned long tobytes(audioencoding_t encoding, unsigned long samples);
00127 void samplefill(unsigned char *addr, int samples, audioencoding_t encoding);
00128 
00137 class __EXPORT AudioFile
00138 {
00139 private:
00140         char *pathname;
00141         audioerror_t error;
00142         audioinfo_t info;
00143         unsigned long header;           // offset to start of audio
00144         unsigned long minimum;          // minimum sample size required
00145 
00146         void Initialize(void);
00147         void getWaveFormat(int size);
00148 
00149 protected:
00150         union
00151         {
00152                 int fd;
00153                 void *handle;
00154         } file;
00155 
00156         virtual bool afCreate(const char *path);
00157         virtual bool afOpen(const char *path);
00158         virtual bool afPeek(unsigned char *data, unsigned size);
00159         virtual int afRead(unsigned char *data, unsigned size);
00160         virtual int afWrite(unsigned char *data, unsigned size);
00161         virtual bool afSeek(unsigned long pos);
00162         virtual void afClose(void);
00163 
00164         virtual char *getContinuation(void)
00165                 {return NULL;};
00166 
00167         audioerror_t setError(audioerror_t err);
00168 
00169         unsigned short getaushort(unsigned char *data);
00170         void setaushort(unsigned char *data, unsigned short value);
00171         unsigned long getaulong(unsigned char *data);
00172         void setaulong(unsigned char *data, unsigned long value);
00173 
00174 public:
00175         AudioFile(const char *fname, unsigned long samples = 0);
00176         AudioFile(const char *fname, audioinfo_t *info, unsigned long min = 0);
00177 
00178         AudioFile()
00179                 {Initialize();};
00180 
00181         ~AudioFile()
00182                 {Close();};
00183 
00184         void Open(const char *fname);
00185         void Create(const char *fname, audioinfo_t *info);
00186         void Close(void);
00187         int getBuffer(void *addr, unsigned len);
00188         int putBuffer(void *attr, unsigned len);        
00189         audioerror_t getSamples(void *addr, unsigned samples);
00190         audioerror_t putSamples(void *addr, unsigned samples);
00191         audioerror_t Skip(long samples);
00192         audioerror_t setPosition(unsigned long samples = ~0l);
00193         audioerror_t getInfo(audioinfo_t *info);
00194         audioerror_t setMinimum(unsigned long samples);
00195         unsigned long getPosition(void);
00196         virtual bool isOpen(void);
00197         virtual bool hasPositioning(void)
00198                 {return true;};
00199 
00200         inline audioencoding_t getEncoding(void)
00201                 {return info.encoding;};
00202 
00203         inline audioformat_t getFormat(void)
00204                 {return info.format;};
00205 
00206         inline unsigned getSampleRate(void)
00207                 {return info.rate;};
00208 
00209         inline char *getAnnotation(void)
00210                 {return info.annotation;};
00211 
00212         inline audioerror_t getError(void)
00213                 {return error;};
00214 
00215         inline bool operator!(void)
00216                 {return !isOpen();};
00217 };
00218 
00227 class __EXPORT CDAudio
00228 {
00229 private:
00230         union
00231         {
00232                 int fd;
00233         } file;
00234         unsigned char v0, v1;
00235 #ifdef  __WIN32__
00236         CRITICAL_SECTION crit;
00237         bool paused;
00238         bool opened;
00239         char position[20];
00240         char endmark[24];
00241         char ret[256];
00242         DWORD command(char *fmt, ...);
00243 #endif
00244         audioerror_t err;
00245 
00246 public:
00247         CDAudio(int devnbr = 0);
00248         ~CDAudio();
00249 
00250         audioerror_t Play(int start, int end = 0);      
00251         audioerror_t Stop(void);
00252         audioerror_t Pause(void);
00253         audioerror_t Resume(void);
00254         audioerror_t Eject(void);
00255         audioerror_t Reload(void);
00256         int getFirst(void);
00257         int getLast(void);
00258         bool isPaused(void);
00259         bool isAudio(int track);
00260         bool isOpen(void);
00261 
00262         unsigned char getVolume(int speaker);
00263         void setVolume(unsigned char left, unsigned char right);
00264 
00265         inline unsigned char getVolumeLeft(void)
00266                 {return getVolume(0);};
00267 
00268         inline unsigned char getVolumeRight(void)
00269                 {return getVolume(1);};
00270 
00271         inline audioerror_t getError(void)
00272                 {return err;};
00273 
00274         inline bool operator!(void)
00275                 {return !isOpen();};
00276 };
00277 
00284 class __EXPORT AudioSample
00285 {
00286 protected:
00287         audioencoding_t encoding;
00288         unsigned rate;
00289         unsigned count;
00290         unsigned char *samples;
00291 
00292 public:
00293         AudioSample(unsigned frame, audioencoding_t coding = PCM16_AUDIO_ENCODING, unsigned rate = 8000);
00294         ~AudioSample();
00295 
00296         inline unsigned getCount(void)
00297                 {return count;};
00298 
00299         inline unsigned getRate(void)
00300                 {return rate;};
00301 
00302         inline audioencoding_t getEncoding(void)
00303                 {return encoding;};
00304 
00305         inline unsigned char *getSamples(void)
00306                 {return samples;};
00307 };      
00308 
00321 class __EXPORT AudioCodec : public AudioSample
00322 {
00323 protected:
00324         AudioCodec(unsigned frames, audioencoding_t encoding, unsigned rate);
00325         unsigned length;
00326 
00327 public:
00335         virtual void Encode(unsigned short *buffer, unsigned samples = 0, int channel = 0) = 0;
00336 
00344         virtual unsigned Decode(unsigned short *buffer, int channel = 0) = 0;
00345 
00355         virtual unsigned Load(unsigned char *data, unsigned length);
00356 
00366         virtual unsigned Save(unsigned char *data, unsigned length);
00367          
00368 
00374         inline unsigned getLength(void)
00375                 {return length;};       
00376 
00380         unsigned getFrameSize(void);
00381 
00385         virtual unsigned getChannels(void);
00386 };
00387 
00388 
00398 class AudioRegisterCodec
00399 {
00400 private:
00401         static AudioRegisterCodec *first;
00402         AudioRegisterCodec *next;
00403 
00404 protected:
00405         unsigned minframe, framecnt;
00406         AudioRegisterCodec(unsigned min, unsigned def = 1);
00407 
00408 public:
00414         virtual audioencoding_t getEncoding(void) = 0;
00415 
00421         virtual unsigned getRate(void)
00422                 {samplerate(getEncoding());};
00423 
00429         virtual char *getName(void) = 0;
00430 
00438         virtual AudioCodec *getCodec(unsigned frames) = 0;
00439 
00445         inline unsigned getFrameSize(void)
00446                 {return minframe;};
00447 
00454         inline unsigned getFrameCount(void)
00455                 {return framecnt;};
00456 
00463         friend AudioRegisterCodec *findCodec(audioencoding_t encoding, unsigned rate = 0);
00464 
00470         friend AudioRegisterCodec *findCodec(const char *name);
00471 
00480         friend AudioCodec *getCodec(audioencoding_t encoding, unsigned rate = 0, unsigned frames = 0);
00481 
00489         friend AudioCodec *getCodec(const char *name, unsigned frame = 0);
00490 };
00491         
00500 class AudioTone : public AudioSample
00501 {
00502 private:
00503         unsigned f1, f2;
00504 
00505 public:
00506         AudioTone(unsigned size, unsigned f1, unsigned rate = 8000);
00507         AudioTone(unsigned size, unsigned f1, unsigned f2, unsigned rate  = 8000);
00508 
00509         inline unsigned getFreq1(void)
00510                 {return f1;};
00511 
00512         inline unsigned getFreq2(void)
00513                 {return f2;};
00514 };
00515 #endif
00516 

Generated at Thu Jun 28 11:02:40 2001 for ccaudio by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000