Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | Examples

AsyncAudioIO.h

Go to the documentation of this file.
00001 
00036 #ifndef ASYNC_AUDIO_IO_INCLUDED
00037 #define ASYNC_AUDIO_IO_INCLUDED
00038 
00039 
00040 /****************************************************************************
00041  *
00042  * System Includes
00043  *
00044  ****************************************************************************/
00045 
00046 #include <sigc++/signal_system.h>
00047 
00048 #include <cstdio>
00049 #include <string>
00050 
00051 
00052 /****************************************************************************
00053  *
00054  * Project Includes
00055  *
00056  ****************************************************************************/
00057 
00058 #include <AsyncFdWatch.h>
00059 #include <AsyncTimer.h>
00060 
00061 
00062 /****************************************************************************
00063  *
00064  * Local Includes
00065  *
00066  ****************************************************************************/
00067 
00068 
00069 
00070 /****************************************************************************
00071  *
00072  * Forward declarations
00073  *
00074  ****************************************************************************/
00075 
00076 
00077 
00078 /****************************************************************************
00079  *
00080  * Namespace
00081  *
00082  ****************************************************************************/
00083 
00084 namespace Async
00085 {
00086 
00087 /****************************************************************************
00088  *
00089  * Forward declarations of classes inside of the declared namespace
00090  *
00091  ****************************************************************************/
00092 
00093 class AudioDevice;
00094 class SampleFifo;
00095 
00096 
00097 /****************************************************************************
00098  *
00099  * Defines & typedefs
00100  *
00101  ****************************************************************************/
00102 
00103 
00104   
00105 /****************************************************************************
00106  *
00107  * Exported Global Variables
00108  *
00109  ****************************************************************************/
00110 
00111 
00112 
00113 /****************************************************************************
00114  *
00115  * Class definitions
00116  *
00117  ****************************************************************************/
00118 
00130 class AudioIO : public SigC::Object
00131 {
00132   public:
00136     typedef enum
00137     {
00138       MODE_NONE,  
00139       MODE_RD,    
00140       MODE_WR,    
00141       MODE_RDWR   
00142     } Mode;
00143   
00147     AudioIO(const std::string& dev_name);
00148     
00152     ~AudioIO(void);
00153   
00159     bool isFullDuplexCapable(void);
00160   
00167     bool open(Mode mode);
00168   
00172     void close(void);
00173   
00181     int write(short *buf, int count);
00182     
00193     int samplesToWrite(void) const;
00194     
00195     /*
00196      * @brief   Call this method to flush all samples in the buffer
00197      *
00198      * This method is used to flush all the samples that are in the buffer.
00199      * That is, all samples in the buffer will be written to the audio device
00200      * and when finished, emit the allSamplesFlushed signal.
00201      */
00202     void flushSamples(void);
00203     
00204     /*
00205      * @brief   Call this method to clear all samples in the buffer
00206      *
00207      * This method is used to clear all the samples that are in the buffer.
00208      * That is, all samples in the buffer will be thrown away. Remaining
00209      * samples that have already been written to the sound card will be
00210      * flushed and when finished, the allSamplesFlushed signal is emitted.
00211      */
00212     void clearSamples(void);
00213     
00214     /*
00215      * @brief   Check if the audio device is busy flushing samples
00216      * @return  Returns \em true if flushing the buffer or else \em false
00217      */
00218     bool isFlushing(void) const { return is_flushing; }
00219     
00220     /*
00221      * @brief   Find out the current IO mode
00222      * @return  Returns the current IO mode
00223      */
00224     Mode mode(void) const { return io_mode; }    
00225     
00232     SigC::Signal2<int, short *, int> audioRead;
00233 
00239     SigC::Signal1<void, bool> writeBufferFull;
00240     
00249     SigC::Signal0<void> allSamplesFlushed;
00250 
00251             
00252   protected:
00253     
00254   private:
00255     static const int  RATE = 8000;
00256     static const int  CHANNELS = 1;
00257     //static const int  SIZE = 16;
00258     //static const int  FRAG_COUNT = 32;    // 32 frags ~ one second
00259     //static const int  FRAG_SIZE_LOG2 = 8; // 256 bytes/frag
00260     static const int  BUF_FRAG_COUNT = 4;
00261         
00262     //int                     fd;
00263     Mode              io_mode;
00264     //Async::FdWatch *  read_watch;
00265     //Async::FdWatch *  write_watch;
00266     //char *                  read_buf;
00267     
00268     //int             file;
00269     //Mode                    old_mode;
00270     
00271     AudioDevice       *audio_dev;
00272     SampleFifo        *write_fifo;
00273     SigC::Connection  read_con;
00274     bool              do_flush;
00275     Async::Timer      *flush_timer;
00276     bool              is_flushing;
00277     int               lead_in_pos;
00278 
00279     void audioReadHandler(Async::FdWatch *watch);
00280     void flushSamplesInDevice(int extra_samples=0);
00281     void flushDone(Timer *timer);
00282     
00283       // Methods accessed by the Async::AudioDevice class
00284     friend class AudioDevice;
00285     AudioDevice *device(void) const { return audio_dev; }
00286     SampleFifo &writeFifo(void) const { return *write_fifo; }
00287     int readSamples(short *samples, int count);
00288     bool doFlush(void) const { return do_flush; }
00289     
00290 };  /* class AudioIO */
00291 
00292 
00293 } /* namespace */
00294 
00295 #endif /* ASYNC_AUDIO_IO_INCLUDED */
00296 
00297 
00298 
00299 /*
00300  * This file has not been truncated
00301  */
00302 

Generated on Sat Dec 3 00:20:38 2005 for Async by  doxygen 1.4.4