sound.h

Go to the documentation of this file.
00001 /*
00002  * sound.h
00003  *
00004  * Sound interface class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Log: sound.h,v $
00030  * Revision 1.40  2007/04/13 07:03:19  rjongbloed
00031  * Added WAV file audio device "plug in".
00032  *
00033  * Revision 1.39  2007/02/01 03:16:15  csoutheren
00034  * Implement GetName for sound channels
00035  *
00036  * Revision 1.38  2006/10/19 12:05:32  rjongbloed
00037  * Added missing Close() function apss thru on PSoundChannel wrapper class.
00038  *
00039  * Revision 1.37  2006/06/21 03:28:41  csoutheren
00040  * Various cleanups thanks for Frederic Heem
00041  *
00042  * Revision 1.36  2005/11/25 03:43:47  csoutheren
00043  * Fixed function argument comments to be compatible with Doxygen
00044  *
00045  * Revision 1.35  2005/08/09 09:08:09  rjongbloed
00046  * Merged new video code from branch back to the trunk.
00047  *
00048  * Revision 1.34.2.2  2005/07/17 11:47:45  rjongbloed
00049  * Fixed backward compatibility
00050  *
00051  * Revision 1.34.2.1  2005/07/17 09:27:04  rjongbloed
00052  * Major revisions of the PWLib video subsystem including:
00053  *   removal of F suffix on colour formats for vertical flipping, all done with existing bool
00054  *   working through use of RGB and BGR formats so now consistent
00055  *   cleaning up the plug in system to use virtuals instead of pointers to functions.
00056  *   rewrite of SDL to be a plug in compatible video output device.
00057  *   extensive enhancement of video test program
00058  *
00059  * Revision 1.34  2005/07/13 13:02:35  csoutheren
00060  * Unified interface across Windows and Unix
00061  *
00062  * Revision 1.33  2005/07/04 11:35:03  shorne
00063  * Added ability to play sound to specfied device (Win32).
00064  *
00065  * Revision 1.32  2005/01/04 07:44:03  csoutheren
00066  * More changes to implement the new configuration methodology, and also to
00067  * attack the global static problem
00068  *
00069  * Revision 1.31  2004/11/01 23:16:59  ykiryanov
00070  * Added macro declaring sound plugin to be static for BeOS
00071  *
00072  * Revision 1.30  2003/12/28 02:03:18  csoutheren
00073  * Fixed problem with GetLastReadCount/GetLastWriteCount  on Windows sound devices
00074  *
00075  * Revision 1.29  2003/11/18 10:50:26  csoutheren
00076  * Changed name of Windows sound device
00077  *
00078  * Revision 1.28  2003/11/14 05:59:09  csoutheren
00079  * Added Read function, thanks to Derek Smithies
00080  *
00081  * Revision 1.27  2003/11/12 10:25:41  csoutheren
00082  * Changes to allow operation of static plugins under Windows
00083  *
00084  * Revision 1.26  2003/11/12 05:18:04  csoutheren
00085  * Added more backwards compatibility functions for PSoundChannel
00086  *
00087  * Revision 1.25  2003/11/12 04:33:32  csoutheren
00088  * Fixed problem with static linking of sound plugins
00089  * Fixed problem with Windows sound driver
00090  *
00091  * Revision 1.24  2003/11/12 03:29:51  csoutheren
00092  * Initial version of plugin code from Snark of GnomeMeeting with changes
00093  *    by Craig Southeren of Post Increment
00094  *
00095  * Revision 1.23.2.2  2003/10/13 02:42:39  dereksmithies
00096  * Add additional functions, so plugins work better.
00097  *
00098  * Revision 1.23.2.1  2003/10/07 03:02:28  csoutheren
00099  * Initial checkin of pwlib code to do plugins.
00100  * Modified from original code and concept provided by Snark of Gnomemeeting
00101  *
00102  * Revision 1.23  2003/09/17 05:41:59  csoutheren
00103  * Removed recursive includes
00104  *
00105  * Revision 1.22  2003/09/17 01:18:02  csoutheren
00106  * Removed recursive include file system and removed all references
00107  * to deprecated coooperative threading support
00108  *
00109  * Revision 1.21  2002/09/16 01:08:59  robertj
00110  * Added #define so can select if #pragma interface/implementation is used on
00111  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00112  *
00113  * Revision 1.20  2002/02/08 09:58:44  robertj
00114  * Slight adjustment to API and documentation for volume functions.
00115  *
00116  * Revision 1.19  2002/02/07 20:57:21  dereks
00117  * add SetVolume and GetVolume methods to PSoundChannel
00118  *
00119  * Revision 1.18  2001/05/22 12:49:32  robertj
00120  * Did some seriously wierd rewrite of platform headers to eliminate the
00121  *   stupid GNU compiler warning about braces not matching.
00122  *
00123  * Revision 1.17  2001/02/07 03:33:43  craigs
00124  * Added functions to get sound channel parameters
00125  *
00126  * Revision 1.16  2000/03/04 10:15:32  robertj
00127  * Added simple play functions for sound files.
00128  *
00129  * Revision 1.15  1999/05/28 14:04:10  robertj
00130  * Added function to get default audio device.
00131  *
00132  * Revision 1.14  1999/03/09 02:59:51  robertj
00133  * Changed comments to doc++ compatible documentation.
00134  *
00135  * Revision 1.13  1999/02/22 10:15:14  robertj
00136  * Sound driver interface implementation to Linux OSS specification.
00137  *
00138  * Revision 1.12  1999/02/16 06:02:27  robertj
00139  * Major implementation to Linux OSS model
00140  *
00141  * Revision 1.11  1998/09/23 06:21:27  robertj
00142  * Added open source copyright license.
00143  *
00144  * Revision 1.10  1995/06/17 11:13:26  robertj
00145  * Documentation update.
00146  *
00147  * Revision 1.9  1995/03/14 12:42:40  robertj
00148  * Updated documentation to use HTML codes.
00149  *
00150  * Revision 1.8  1995/01/16  09:42:05  robertj
00151  * Documentation.
00152  *
00153  * Revision 1.7  1994/08/23  11:32:52  robertj
00154  * Oops
00155  *
00156  * Revision 1.6  1994/08/22  00:46:48  robertj
00157  * Added pragma fro GNU C++ compiler.
00158  *
00159  * Revision 1.5  1994/06/25  11:55:15  robertj
00160  * Unix version synchronisation.
00161  *
00162  * Revision 1.4  1994/01/03  04:42:23  robertj
00163  * Mass changes to common container classes and interactors etc etc etc.
00164  *
00165  * Revision 1.3  1993/09/29  03:06:30  robertj
00166  * Added unix compatibility to Beep()
00167  *
00168  * Revision 1.2  1993/07/14  12:49:16  robertj
00169  * Fixed RCS keywords.
00170  *
00171  */
00172 
00173 
00174 #ifndef _PSOUND
00175 #define _PSOUND
00176 
00177 #ifdef P_USE_PRAGMA
00178 #pragma interface
00179 #endif
00180 
00181 #include <ptlib/plugin.h>
00182 #include <ptlib/pluginmgr.h>
00183 
00191 class PSound : public PBYTEArray
00192 {
00193   PCLASSINFO(PSound, PBYTEArray);
00194 
00195   public:
00204     PSound(
00205       unsigned numChannels = 1,    
00206       unsigned sampleRate = 8000,  
00207       unsigned bitsPerSample = 16, 
00208       PINDEX   bufferSize = 0,     
00209       const BYTE * data = NULL     
00210     );
00211 
00214     PSound(
00215       const PFilePath & filename   
00216     );
00217 
00220     PSound & operator=(
00221       const PBYTEArray & data  
00222     );
00224 
00236     BOOL Load(
00237       const PFilePath & filename   
00238     );
00239 
00246     BOOL Save(
00247       const PFilePath & filename   
00248     );
00250 
00253 
00254     BOOL Play();
00255 
00257     BOOL Play(const PString & device);
00258 
00262     void SetFormat(
00263       unsigned numChannels,   
00264       unsigned sampleRate,    
00265       unsigned bitsPerSample  
00266     );
00267 
00271     unsigned GetEncoding()   const { return encoding; }
00272 
00274     unsigned GetChannels()   const { return numChannels; }
00275 
00277     unsigned GetSampleRate() const { return sampleRate; }
00278 
00280     unsigned GetSampleSize() const { return sampleSize; }
00281 
00283     DWORD    GetErrorCode()  const { return dwLastError; }
00284 
00286     PINDEX   GetFormatInfoSize()  const { return formatInfo.GetSize(); }
00287 
00289     const void * GetFormatInfoData() const { return (const BYTE *)formatInfo; }
00291 
00302     static BOOL PlayFile(
00303       const PFilePath & file, 
00304       BOOL wait = TRUE        
00305     );
00306 
00308     static void Beep();
00310 
00311   protected:
00313     unsigned   encoding;      
00315     unsigned   numChannels;   
00317     unsigned   sampleRate;    
00319     unsigned   sampleSize;    
00321     DWORD      dwLastError;   
00323     PBYTEArray formatInfo;    
00324 };
00325 
00326 
00357 class PSoundChannel : public PChannel
00358 {
00359   PCLASSINFO(PSoundChannel, PChannel);
00360 
00361   public:
00364     enum Directions {
00365       Recorder,
00366       Player
00367     };
00368 
00370     PSoundChannel();
00371 
00375     PSoundChannel(
00376       const PString & device,       
00377       Directions dir,               
00378       unsigned numChannels = 1,     
00379       unsigned sampleRate = 8000,   
00380       unsigned bitsPerSample = 16   
00381     );
00382     // 
00383 
00384     virtual ~PSoundChannel();
00385     // Destroy and close the sound driver
00387 
00392     static PStringList GetDriverNames(
00393       PPluginManager * pluginMgr = NULL   
00394     );
00395 
00400     static PStringList GetDriversDeviceNames(
00401       const PString & driverName,         
00402       Directions direction,               
00403       PPluginManager * pluginMgr = NULL   
00404     );
00405 
00406     // For backward compatibility
00407     static inline PStringList GetDeviceNames(
00408       const PString & driverName,
00409       Directions direction,
00410       PPluginManager * pluginMgr = NULL
00411     ) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
00412 
00415     static PSoundChannel * CreateChannel (
00416       const PString & driverName,         
00417       PPluginManager * pluginMgr = NULL   
00418     );
00419 
00420     /* Create the matching sound channel that corresponds to the device name.
00421        So, for "fake" return a device that will generate fake video.
00422        For "Phillips 680 webcam" (eg) will return appropriate grabber.
00423        Note that Phillips will return the appropriate grabber also.
00424 
00425        This is typically used with the return values from GetDeviceNames().
00426      */
00427     static PSoundChannel * CreateChannelByName(
00428       const PString & deviceName,         
00429       Directions direction,               
00430       PPluginManager * pluginMgr = NULL   
00431     );
00432 
00438     static PSoundChannel * CreateOpenedChannel(
00439       const PString & driverName,         
00440       const PString & deviceName,         
00441       Directions direction,               
00442       unsigned numChannels = 1,           
00443       unsigned sampleRate = 8000,         
00444       unsigned bitsPerSample = 16,        
00445       PPluginManager * pluginMgr = NULL   
00446     );
00447 
00460     static PString GetDefaultDevice(
00461       Directions dir    // Sound I/O direction
00462     );
00463 
00472     static PStringList GetDeviceNames(
00473       Directions direction,               
00474       PPluginManager * pluginMgr = NULL   
00475     );
00476 
00483     virtual BOOL Open(
00484       const PString & device,       
00485       Directions dir,               
00486       unsigned numChannels = 1,     
00487       unsigned sampleRate = 8000,   
00488       unsigned bitsPerSample = 16   
00489     );
00490 
00496     virtual BOOL IsOpen() const;
00497 
00502     virtual BOOL Close();
00503 
00509     virtual int GetHandle() const;
00510 
00512     virtual PString GetName() const;
00513 
00519     virtual BOOL Abort();
00521 
00533     virtual BOOL SetFormat(
00534       unsigned numChannels = 1,     
00535       unsigned sampleRate = 8000,   
00536       unsigned bitsPerSample = 16   
00537     );
00538 
00540     virtual unsigned GetChannels() const;
00541 
00543     virtual unsigned GetSampleRate() const;
00544 
00546     virtual unsigned GetSampleSize() const;
00547 
00556     virtual BOOL SetBuffers(
00557       PINDEX size,      
00558       PINDEX count = 2  
00559     );
00560 
00566     virtual BOOL GetBuffers(
00567       PINDEX & size,    // Size of each buffer
00568       PINDEX & count    // Number of buffers
00569     );
00570 
00571     enum {
00572       MaxVolume = 100
00573     };
00574 
00581     virtual BOOL SetVolume(
00582       unsigned volume   
00583     );
00584 
00591     virtual BOOL GetVolume(
00592       unsigned & volume   
00593     );
00595 
00598 
00610     virtual BOOL Write(const void * buf, PINDEX len);
00611 
00612     virtual PINDEX GetLastWriteCount() const;
00613 
00630     virtual BOOL PlaySound(
00631       const PSound & sound,   
00632       BOOL wait = TRUE        
00633     );
00649     virtual BOOL PlayFile(
00650       const PFilePath & file, 
00651       BOOL wait = TRUE        
00652     );
00653 
00660     virtual BOOL HasPlayCompleted();
00661 
00668     virtual BOOL WaitForPlayCompletion();
00669 
00671 
00686     virtual BOOL Read(
00687       void * buf,   
00688       PINDEX len    
00689     );
00690 
00691     PINDEX GetLastReadCount() const;
00692 
00710     virtual BOOL RecordSound(
00711       PSound & sound 
00712     );
00713 
00726     virtual BOOL RecordFile(
00727       const PFilePath & file 
00728     );
00729 
00736     virtual BOOL StartRecording();
00737 
00745     virtual BOOL IsRecordBufferFull();
00746 
00755     virtual BOOL AreAllRecordBuffersFull();
00756 
00764     virtual BOOL WaitForRecordBufferFull();
00765 
00774     virtual BOOL WaitForAllRecordBuffersFull();
00776 
00777   protected:
00778     PSoundChannel * baseChannel;
00779 };
00780 
00781 
00783 
00784 // define the sound plugin service descriptor
00785 
00786 template <class className> class PSoundChannelPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00787 {
00788   public:
00789     virtual PObject *   CreateInstance(int /*userData*/) const { return new className; }
00790     virtual PStringList GetDeviceNames(int userData) const { return className::GetDeviceNames((PSoundChannel::Directions)userData); }
00791 };
00792 
00793 #define PCREATE_SOUND_PLUGIN(name, className) \
00794   static PSoundChannelPluginServiceDescriptor<className> className##_descriptor; \
00795   PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)
00796 
00797 #endif
00798 
00799 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Mar 7 06:25:03 2008 for PTLib by  doxygen 1.5.1