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: 24867 $
00030  * $Author: rjongbloed $
00031  * $Date: 2010-11-10 01:29:17 -0600 (Wed, 10 Nov 2010) $
00032  */
00033 
00034 
00035 #ifndef PTLIB_SOUND_H
00036 #define PTLIB_SOUND_H
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 = true        
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 
00252 class PSoundChannel : public PChannel
00253 {
00254   PCLASSINFO(PSoundChannel, PChannel);
00255 
00256   public:
00259     enum Directions {
00260       Recorder,
00261       Player
00262     };
00263 
00265     PSoundChannel();
00266 
00270     PSoundChannel(
00271       const PString & device,       
00272       Directions dir,               
00273       unsigned numChannels = 1,     
00274       unsigned sampleRate = 8000,   
00275       unsigned bitsPerSample = 16   
00276     );
00277     // 
00278 
00279     virtual ~PSoundChannel();
00280     // Destroy and close the sound driver
00282 
00287     static PStringArray GetDriverNames(
00288       PPluginManager * pluginMgr = NULL   
00289     );
00290 
00295     static PStringArray GetDriversDeviceNames(
00296       const PString & driverName,         
00297       Directions direction,               
00298       PPluginManager * pluginMgr = NULL   
00299     );
00300 
00301     // For backward compatibility
00302     static inline PStringArray GetDeviceNames(
00303       const PString & driverName,
00304       Directions direction,
00305       PPluginManager * pluginMgr = NULL
00306     ) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
00307 
00310     static PSoundChannel * CreateChannel (
00311       const PString & driverName,         
00312       PPluginManager * pluginMgr = NULL   
00313     );
00314 
00315     /* Create the matching sound channel that corresponds to the device name.
00316        So, for "fake" return a device that will generate fake video.
00317        For "Phillips 680 webcam" (eg) will return appropriate grabber.
00318        Note that Phillips will return the appropriate grabber also.
00319 
00320        This is typically used with the return values from GetDeviceNames().
00321      */
00322     static PSoundChannel * CreateChannelByName(
00323       const PString & deviceName,         
00324       Directions direction,               
00325       PPluginManager * pluginMgr = NULL   
00326     );
00327 
00333     static PSoundChannel * CreateOpenedChannel(
00334       const PString & driverName,         
00335       const PString & deviceName,         
00336       Directions direction,               
00337       unsigned numChannels = 1,           
00338       unsigned sampleRate = 8000,         
00339       unsigned bitsPerSample = 16,        
00340       PPluginManager * pluginMgr = NULL   
00341     );
00342 
00355     static PString GetDefaultDevice(
00356       Directions dir    // Sound I/O direction
00357     );
00358 
00367     static PStringArray GetDeviceNames(
00368       Directions direction,               
00369       PPluginManager * pluginMgr = NULL   
00370     );
00371 
00378     virtual PBoolean Open(
00379       const PString & device,       
00380       Directions dir,               
00381       unsigned numChannels = 1,     
00382       unsigned sampleRate = 8000,   
00383       unsigned bitsPerSample = 16   
00384     );
00385 
00391     virtual PBoolean IsOpen() const;
00392 
00397     virtual PBoolean Close();
00398 
00404     virtual int GetHandle() const;
00405 
00407     virtual PString GetName() const;
00408 
00416     virtual PBoolean Abort();
00418 
00430     virtual PBoolean SetFormat(
00431       unsigned numChannels = 1,     
00432       unsigned sampleRate = 8000,   
00433       unsigned bitsPerSample = 16   
00434     );
00435 
00437     virtual unsigned GetChannels() const;
00438 
00440     virtual unsigned GetSampleRate() const;
00441 
00443     virtual unsigned GetSampleSize() const;
00444 
00453     virtual PBoolean SetBuffers(
00454       PINDEX size,      
00455       PINDEX count = 2  
00456     );
00457 
00463     virtual PBoolean GetBuffers(
00464       PINDEX & size,    // Size of each buffer
00465       PINDEX & count    // Number of buffers
00466     );
00467 
00468     enum {
00469       MaxVolume = 100
00470     };
00471 
00480     virtual PBoolean SetVolume(
00481       unsigned volume   
00482     );
00483 
00492     virtual PBoolean GetVolume(
00493       unsigned & volume   
00494     );
00496 
00499 
00521     virtual PBoolean Write(const void * buf, PINDEX len);
00522 
00523 
00547     virtual PBoolean Write(
00548       const void * buf,         
00549       PINDEX len,               
00550           const void * mark         
00551     );
00552 
00554     virtual PINDEX GetLastWriteCount() const;
00555 
00572     virtual PBoolean PlaySound(
00573       const PSound & sound,   
00574       PBoolean wait = true        
00575     );
00576 
00592     virtual PBoolean PlayFile(
00593       const PFilePath & file, 
00594       PBoolean wait = true        
00595     );
00596 
00603     virtual PBoolean HasPlayCompleted();
00604 
00611     virtual PBoolean WaitForPlayCompletion();
00612 
00614 
00645     virtual PBoolean Read(
00646       void * buf,   
00647       PINDEX len    
00648     );
00649 
00651     PINDEX GetLastReadCount() const;
00652 
00670     virtual PBoolean RecordSound(
00671       PSound & sound 
00672     );
00673 
00686     virtual PBoolean RecordFile(
00687       const PFilePath & file 
00688     );
00689 
00696     virtual PBoolean StartRecording();
00697 
00705     virtual PBoolean IsRecordBufferFull();
00706 
00715     virtual PBoolean AreAllRecordBuffersFull();
00716 
00724     virtual PBoolean WaitForRecordBufferFull();
00725 
00734     virtual PBoolean WaitForAllRecordBuffersFull();
00736 
00737   protected:
00738     PSoundChannel * baseChannel;
00739 
00744     Directions      activeDirection;
00745 };
00746 
00747 
00749 
00750 // define the sound plugin service descriptor
00751 
00752 template <class className> class PSoundChannelPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00753 {
00754   public:
00755     virtual PObject *    CreateInstance(int /*userData*/) const { return new className; }
00756     virtual PStringArray GetDeviceNames(int userData) const { return className::GetDeviceNames((PSoundChannel::Directions)userData); }
00757 };
00758 
00759 #define PCREATE_SOUND_PLUGIN(name, className) \
00760   static PSoundChannelPluginServiceDescriptor<className> className##_descriptor; \
00761   PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)
00762 
00763 #ifdef _WIN32
00764   PPLUGIN_STATIC_LOAD(WindowsMultimedia, PSoundChannel);
00765 #elif defined(__BEOS__)
00766   PPLUGIN_STATIC_LOAD(BeOS, PSoundChannel);
00767 #endif
00768 
00769 #if defined(P_DIRECTSOUND)
00770   PPLUGIN_STATIC_LOAD(DirectSound, PSoundChannel);
00771 #endif
00772 
00773 #if defined(P_WAVFILE)
00774   PPLUGIN_STATIC_LOAD(WAVFile, PSoundChannel)
00775 #endif
00776 
00777 
00778 #endif // PTLIB_SOUND_H
00779 
00780 
00781 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Oct 14 01:44:10 2011 for PTLib by  doxygen 1.4.7