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  * $Revision: 19008 $
00030  * $Author: rjongbloed $
00031  * $Date: 2007-11-29 09:17:41 +0000 (Thu, 29 Nov 2007) $
00032  */
00033 
00034 
00035 #ifndef _PSOUND
00036 #define _PSOUND
00037 
00038 #ifdef P_USE_PRAGMA
00039 #pragma interface
00040 #endif
00041 
00042 #include <ptlib/plugin.h>
00043 #include <ptlib/pluginmgr.h>
00044 
00052 class PSound : public PBYTEArray
00053 {
00054   PCLASSINFO(PSound, PBYTEArray);
00055 
00056   public:
00065     PSound(
00066       unsigned numChannels = 1,    
00067       unsigned sampleRate = 8000,  
00068       unsigned bitsPerSample = 16, 
00069       PINDEX   bufferSize = 0,     
00070       const BYTE * data = NULL     
00071     );
00072 
00075     PSound(
00076       const PFilePath & filename   
00077     );
00078 
00081     PSound & operator=(
00082       const PBYTEArray & data  
00083     );
00085 
00097     PBoolean Load(
00098       const PFilePath & filename   
00099     );
00100 
00107     PBoolean Save(
00108       const PFilePath & filename   
00109     );
00111 
00114 
00115     PBoolean Play();
00116 
00118     PBoolean Play(const PString & device);
00119 
00123     void SetFormat(
00124       unsigned numChannels,   
00125       unsigned sampleRate,    
00126       unsigned bitsPerSample  
00127     );
00128 
00132     unsigned GetEncoding()   const { return encoding; }
00133 
00135     unsigned GetChannels()   const { return numChannels; }
00136 
00138     unsigned GetSampleRate() const { return sampleRate; }
00139 
00141     unsigned GetSampleSize() const { return sampleSize; }
00142 
00144     DWORD    GetErrorCode()  const { return dwLastError; }
00145 
00147     PINDEX   GetFormatInfoSize()  const { return formatInfo.GetSize(); }
00148 
00150     const void * GetFormatInfoData() const { return (const BYTE *)formatInfo; }
00152 
00163     static PBoolean PlayFile(
00164       const PFilePath & file, 
00165       PBoolean wait = PTrue        
00166     );
00167 
00169     static void Beep();
00171 
00172   protected:
00174     unsigned   encoding;      
00176     unsigned   numChannels;   
00178     unsigned   sampleRate;    
00180     unsigned   sampleSize;    
00182     DWORD      dwLastError;   
00184     PBYTEArray formatInfo;    
00185 };
00186 
00187 
00218 class PSoundChannel : public PChannel
00219 {
00220   PCLASSINFO(PSoundChannel, PChannel);
00221 
00222   public:
00225     enum Directions {
00226       Recorder,
00227       Player
00228     };
00229 
00231     PSoundChannel();
00232 
00236     PSoundChannel(
00237       const PString & device,       
00238       Directions dir,               
00239       unsigned numChannels = 1,     
00240       unsigned sampleRate = 8000,   
00241       unsigned bitsPerSample = 16   
00242     );
00243     // 
00244 
00245     virtual ~PSoundChannel();
00246     // Destroy and close the sound driver
00248 
00253     static PStringList GetDriverNames(
00254       PPluginManager * pluginMgr = NULL   
00255     );
00256 
00261     static PStringList GetDriversDeviceNames(
00262       const PString & driverName,         
00263       Directions direction,               
00264       PPluginManager * pluginMgr = NULL   
00265     );
00266 
00267     // For backward compatibility
00268     static inline PStringList GetDeviceNames(
00269       const PString & driverName,
00270       Directions direction,
00271       PPluginManager * pluginMgr = NULL
00272     ) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
00273 
00276     static PSoundChannel * CreateChannel (
00277       const PString & driverName,         
00278       PPluginManager * pluginMgr = NULL   
00279     );
00280 
00281     /* Create the matching sound channel that corresponds to the device name.
00282        So, for "fake" return a device that will generate fake video.
00283        For "Phillips 680 webcam" (eg) will return appropriate grabber.
00284        Note that Phillips will return the appropriate grabber also.
00285 
00286        This is typically used with the return values from GetDeviceNames().
00287      */
00288     static PSoundChannel * CreateChannelByName(
00289       const PString & deviceName,         
00290       Directions direction,               
00291       PPluginManager * pluginMgr = NULL   
00292     );
00293 
00299     static PSoundChannel * CreateOpenedChannel(
00300       const PString & driverName,         
00301       const PString & deviceName,         
00302       Directions direction,               
00303       unsigned numChannels = 1,           
00304       unsigned sampleRate = 8000,         
00305       unsigned bitsPerSample = 16,        
00306       PPluginManager * pluginMgr = NULL   
00307     );
00308 
00321     static PString GetDefaultDevice(
00322       Directions dir    // Sound I/O direction
00323     );
00324 
00333     static PStringList GetDeviceNames(
00334       Directions direction,               
00335       PPluginManager * pluginMgr = NULL   
00336     );
00337 
00344     virtual PBoolean Open(
00345       const PString & device,       
00346       Directions dir,               
00347       unsigned numChannels = 1,     
00348       unsigned sampleRate = 8000,   
00349       unsigned bitsPerSample = 16   
00350     );
00351 
00357     virtual PBoolean IsOpen() const;
00358 
00363     virtual PBoolean Close();
00364 
00370     virtual int GetHandle() const;
00371 
00373     virtual PString GetName() const;
00374 
00380     virtual PBoolean Abort();
00382 
00394     virtual PBoolean SetFormat(
00395       unsigned numChannels = 1,     
00396       unsigned sampleRate = 8000,   
00397       unsigned bitsPerSample = 16   
00398     );
00399 
00401     virtual unsigned GetChannels() const;
00402 
00404     virtual unsigned GetSampleRate() const;
00405 
00407     virtual unsigned GetSampleSize() const;
00408 
00417     virtual PBoolean SetBuffers(
00418       PINDEX size,      
00419       PINDEX count = 2  
00420     );
00421 
00427     virtual PBoolean GetBuffers(
00428       PINDEX & size,    // Size of each buffer
00429       PINDEX & count    // Number of buffers
00430     );
00431 
00432     enum {
00433       MaxVolume = 100
00434     };
00435 
00442     virtual PBoolean SetVolume(
00443       unsigned volume   
00444     );
00445 
00452     virtual PBoolean GetVolume(
00453       unsigned & volume   
00454     );
00456 
00459 
00471     virtual PBoolean Write(const void * buf, PINDEX len);
00472 
00473     virtual PINDEX GetLastWriteCount() const;
00474 
00491     virtual PBoolean PlaySound(
00492       const PSound & sound,   
00493       PBoolean wait = PTrue        
00494     );
00510     virtual PBoolean PlayFile(
00511       const PFilePath & file, 
00512       PBoolean wait = PTrue        
00513     );
00514 
00521     virtual PBoolean HasPlayCompleted();
00522 
00529     virtual PBoolean WaitForPlayCompletion();
00530 
00532 
00547     virtual PBoolean Read(
00548       void * buf,   
00549       PINDEX len    
00550     );
00551 
00552     PINDEX GetLastReadCount() const;
00553 
00571     virtual PBoolean RecordSound(
00572       PSound & sound 
00573     );
00574 
00587     virtual PBoolean RecordFile(
00588       const PFilePath & file 
00589     );
00590 
00597     virtual PBoolean StartRecording();
00598 
00606     virtual PBoolean IsRecordBufferFull();
00607 
00616     virtual PBoolean AreAllRecordBuffersFull();
00617 
00625     virtual PBoolean WaitForRecordBufferFull();
00626 
00635     virtual PBoolean WaitForAllRecordBuffersFull();
00637 
00638   protected:
00639     PSoundChannel * baseChannel;
00640 };
00641 
00642 
00644 
00645 // define the sound plugin service descriptor
00646 
00647 template <class className> class PSoundChannelPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00648 {
00649   public:
00650     virtual PObject *   CreateInstance(int /*userData*/) const { return new className; }
00651     virtual PStringList GetDeviceNames(int userData) const { return className::GetDeviceNames((PSoundChannel::Directions)userData); }
00652 };
00653 
00654 #define PCREATE_SOUND_PLUGIN(name, className) \
00655   static PSoundChannelPluginServiceDescriptor<className> className##_descriptor; \
00656   PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)
00657 
00658 #endif
00659 
00660 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Dec 10 11:18:57 2007 for PTLib by  doxygen 1.5.1