Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound 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 namespace ost {
00049 
00050 typedef enum
00051 {
00052         SAMPLE_RATE_UNKNOWN,
00053         SAMPLE_RATE_6KHZ = 6000,
00054         SAMPLE_RATE_8KHZ = 8000,
00055         SAMPLE_RATE_44KHZ = 44100
00056 }       samplerate_t;
00057 
00058 typedef enum
00059 {
00060         UNKNOWN_AUDIO_ENCODING = 0,
00061         G721_ADPCM_ENCODING,
00062         G722_AUDIO_ENCODING,
00063         G722_7BIT_ENCODING,
00064         G722_6BIT_ENCODING,
00065         G723_3BIT_ENCODING,
00066         G723_5BIT_ENCODING,
00067         GSM_VOICE_ENCODING,
00068         MULAW_AUDIO_ENCODING,
00069         ALAW_AUDIO_ENCODING,
00070         OKI_ADPCM_ENCODING,
00071         DIALOGIC_ADPCM_ENCODING,
00072         CDA_STEREO_ENCODING,
00073         CDA_MONO_ENCODING,
00074         PCM8_STEREO_ENCODING,
00075         PCM8_AUDIO_ENCODING,
00076         PCM16_STEREO_ENCODING,
00077         PCM16_AUDIO_ENCODING,
00078         PCM32_STEREO_ENCODING,
00079         PCM32_AUDIO_ENCODING
00080 }       audioencoding_t;
00081 
00082 typedef enum
00083 {
00084         AUDIO_FORMAT_RAW,
00085         AUDIO_FORMAT_SUN,
00086         AUDIO_FORMAT_RIFF,
00087         AUDIO_FORMAT_WAVE
00088 } audioformat_t;
00089 
00090 typedef enum
00091 {
00092         AUDIO_SUCCESS = 0,
00093         AUDIO_READ_LASTFRAME,
00094         AUDIO_NOT_OPENED,
00095         AUDIO_END_OF_FILE,
00096         AUDIO_START_OF_FILE,
00097         AUDIO_RATE_UNSUPPORTED,
00098         AUDIO_ENCODING_UNSUPPORTED,
00099         AUDIO_READ_INTERRUPTED,
00100         AUDIO_WRITE_INTERRUPTED,
00101         AUDIO_READ_FAILURE,
00102         AUDIO_WRITE_FAILURE,
00103         AUDIO_READ_INCOMPLETE,
00104         AUDIO_WRITE_INCOMPLETE,
00105         AUDIO_REQUEST_INVALID,
00106         AUDIO_TOC_FAILED,
00107         AUDIO_STAT_FAILED,
00108         AUDIO_INVALID_TRACK,
00109         AUDIO_PLAYBACK_FAILED,
00110         AUDIO_NOT_PLAYING
00111 } audioerror_t;
00112 
00113 typedef struct
00114 {
00115         audioformat_t format;
00116         audioencoding_t encoding;
00117         unsigned rate;
00118         unsigned order;
00119         char *annotation;
00120 }       audioinfo_t;
00121 
00122 bool ismono(audioencoding_t encoding);
00123 bool issterio(audioencoding_t encoding);
00124 samplerate_t samplerate(audioencoding_t encoding);
00125 int sampleframe(audioencoding_t encoding, int samples = 0);
00126 int samplecount(audioencoding_t);
00127 unsigned long tosamples(audioencoding_t encoding, size_t bytes);
00128 unsigned long tobytes(audioencoding_t encoding, unsigned long samples);
00129 void samplefill(unsigned char *addr, int samples, audioencoding_t encoding);
00130 
00139 class __EXPORT AudioFile
00140 {
00141 private:
00142         char *pathname;
00143         audioerror_t error;
00144         audioinfo_t info;
00145         unsigned long header;           // offset to start of audio
00146         unsigned long minimum;          // minimum sample size required
00147 
00148         void Initialize(void);
00149         void getWaveFormat(int size);
00150 
00151 protected:
00152         union
00153         {
00154                 int fd;
00155                 void *handle;
00156         } file;
00157 
00158         virtual bool afCreate(const char *path);
00159         virtual bool afOpen(const char *path);
00160         virtual bool afPeek(unsigned char *data, unsigned size);
00161         virtual int afRead(unsigned char *data, unsigned size);
00162         virtual int afWrite(unsigned char *data, unsigned size);
00163         virtual bool afSeek(unsigned long pos);
00164         virtual void afClose(void);
00165 
00166         virtual char *getContinuation(void)
00167                 {return NULL;};
00168 
00169         audioerror_t setError(audioerror_t err);
00170 
00171         unsigned short getaushort(unsigned char *data);
00172         void setaushort(unsigned char *data, unsigned short value);
00173         unsigned long getaulong(unsigned char *data);
00174         void setaulong(unsigned char *data, unsigned long value);
00175 
00176 public:
00177         AudioFile(const char *fname, unsigned long samples = 0);
00178         AudioFile(const char *fname, audioinfo_t *info, unsigned long min = 0);
00179 
00180         AudioFile()
00181                 {Initialize();};
00182 
00183         ~AudioFile()
00184                 {Close();};
00185 
00186         void Open(const char *fname);
00187         void Create(const char *fname, audioinfo_t *info);
00188         void Close(void);
00189         int getBuffer(void *addr, unsigned len);
00190         int putBuffer(void *attr, unsigned len);        
00191         audioerror_t getSamples(void *addr, unsigned samples);
00192         audioerror_t putSamples(void *addr, unsigned samples);
00193         audioerror_t Skip(long samples);
00194         audioerror_t setPosition(unsigned long samples = ~0l);
00195         audioerror_t getInfo(audioinfo_t *info);
00196         audioerror_t setMinimum(unsigned long samples);
00197         unsigned long getPosition(void);
00198         virtual bool isOpen(void);
00199         virtual bool hasPositioning(void)
00200                 {return true;};
00201 
00202         inline audioencoding_t getEncoding(void)
00203                 {return info.encoding;};
00204 
00205         inline audioformat_t getFormat(void)
00206                 {return info.format;};
00207 
00208         inline unsigned getSampleRate(void)
00209                 {return info.rate;};
00210 
00211         inline char *getAnnotation(void)
00212                 {return info.annotation;};
00213 
00214         inline audioerror_t getError(void)
00215                 {return error;};
00216 
00217         inline bool operator!(void)
00218                 {return !isOpen();};
00219 };
00220 
00229 class __EXPORT CDAudio
00230 {
00231 private:
00232         union
00233         {
00234                 int fd;
00235         } file;
00236         unsigned char v0, v1;
00237 #ifdef  __WIN32__
00238         CRITICAL_SECTION crit;
00239         bool paused;
00240         bool opened;
00241         char position[20];
00242         char endmark[24];
00243         char ret[256];
00244         DWORD command(char *fmt, ...);
00245 #endif
00246         audioerror_t err;
00247 
00248 public:
00249         CDAudio(int devnbr = 0);
00250         ~CDAudio();
00251 
00252         audioerror_t Play(int start, int end = 0);      
00253         audioerror_t Stop(void);
00254         audioerror_t Pause(void);
00255         audioerror_t Resume(void);
00256         audioerror_t Eject(void);
00257         audioerror_t Reload(void);
00258         int getFirst(void);
00259         int getLast(void);
00260         bool isPaused(void);
00261         bool isAudio(int track);
00262         bool isOpen(void);
00263 
00264         unsigned char getVolume(int speaker);
00265         void setVolume(unsigned char left, unsigned char right);
00266 
00267         inline unsigned char getVolumeLeft(void)
00268                 {return getVolume(0);};
00269 
00270         inline unsigned char getVolumeRight(void)
00271                 {return getVolume(1);};
00272 
00273         inline audioerror_t getError(void)
00274                 {return err;};
00275 
00276         inline bool operator!(void)
00277                 {return !isOpen();};
00278 };
00279 
00286 class __EXPORT AudioSample
00287 {
00288 protected:
00289         friend class AudioCopy;
00290 
00291         audioencoding_t encoding;
00292         unsigned rate;
00293         unsigned count;
00294         unsigned char *samples;
00295 
00296 public:
00297         AudioSample(unsigned frame, audioencoding_t coding = PCM16_AUDIO_ENCODING, unsigned rate = 8000);
00298         ~AudioSample();
00299 
00300         inline unsigned getCount(void)
00301                 {return count;};
00302 
00303         inline unsigned getRate(void)
00304                 {return rate;};
00305 
00306         inline audioencoding_t getEncoding(void)
00307                 {return encoding;};
00308 
00309         inline unsigned char *getSamples(void)
00310                 {return samples;};
00311 };      
00312 
00325 class __EXPORT AudioCodec : public AudioSample
00326 {
00327 protected:
00328         AudioCodec(unsigned frames, audioencoding_t encoding, unsigned rate);
00329         unsigned length;
00330 
00331 public:
00339         virtual void Encode(unsigned short *buffer, unsigned samples = 0, int channel = 0) = 0;
00340 
00348         virtual bool Encode(AudioSample *sample) = 0;
00349 
00357         virtual unsigned Decode(unsigned short *buffer, int channel = 0) = 0;
00358 
00365         virtual bool Decode(AudioSample *sample) = 0;
00366 
00376         virtual unsigned Load(unsigned char *data, unsigned length);
00377 
00387         virtual unsigned Save(unsigned char *data, unsigned length);
00388          
00389 
00395         inline unsigned getLength(void)
00396                 {return length;};       
00397 
00401         unsigned getFrameSize(void);
00402 
00406         virtual unsigned getChannels(void);
00407 };
00408 
00418 class AudioRegisterCodec
00419 {
00420 private:
00421         static AudioRegisterCodec *first;
00422         AudioRegisterCodec *next;
00423 
00424 protected:
00425         unsigned minframe, framecnt;
00426         AudioRegisterCodec(unsigned min, unsigned def = 1);
00427 
00428 public:
00434         virtual audioencoding_t getEncoding(void) = 0;
00435 
00441         virtual unsigned getRate(void)
00442                 {samplerate(getEncoding());};
00443 
00449         virtual char *getName(void) = 0;
00450 
00458         virtual AudioCodec *getCodec(unsigned frames) = 0;
00459 
00471         virtual AudioSample *getTone(unsigned f1, unsigned f2, unsigned maxsize, unsigned maxtone = 0) = 0;     
00472 
00478         inline unsigned getFrameSize(void)
00479                 {return minframe;};
00480 
00487         inline unsigned getFrameCount(void)
00488                 {return framecnt;};
00489 
00496         friend AudioRegisterCodec *findCodec(audioencoding_t encoding, unsigned rate = 0);
00497 
00503         friend AudioRegisterCodec *findCodec(const char *name);
00504 
00513         friend AudioCodec *getCodec(audioencoding_t encoding, unsigned rate = 0, unsigned frames = 0);
00514 
00522         friend AudioCodec *getCodec(const char *name, unsigned frame = 0);
00523 };
00524         
00533 class AudioTone : public AudioSample
00534 {
00535 protected:
00536         double p1, p2, v1, v2, fa1, fa2;
00537 
00538 public:
00539         AudioTone(unsigned size, unsigned f1 = 0, unsigned f2 = 0, unsigned rate = 8000);
00540 
00544         void Fill(unsigned max = 0);
00545 
00546         /*
00547          * Set frequency.
00548          *
00549          * @param f1 first frequency.
00550          * @param f2 second frequency.
00551          */
00552         void setFreq(unsigned f1, unsigned f2 = 0);
00553 };
00554 
00565 class AudioCopy : public AudioSample
00566 {
00567 protected:
00568         unsigned char *next;
00569         unsigned left;
00570 
00571         virtual AudioSample *Fill(void) = 0;
00572 
00573 public:
00574         AudioCopy(unsigned frame, audioencoding_t encoding = PCM16_AUDIO_ENCODING, unsigned rate = 8000);
00575         bool Copy(void);
00576 
00577         inline bool isEmpty(void)
00578                 {return next == NULL;};
00579 };
00580 
00581 };
00582 
00583 #endif
00584 

Generated at Sat Nov 3 09:46:01 2001 for ccAudio by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001