Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

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.30 2003/12/28 02:03:18 csoutheren 00031 * Fixed problem with GetLastReadCount/GetLastWriteCount on Windows sound devices 00032 * 00033 * Revision 1.29 2003/11/18 10:50:26 csoutheren 00034 * Changed name of Windows sound device 00035 * 00036 * Revision 1.28 2003/11/14 05:59:09 csoutheren 00037 * Added Read function, thanks to Derek Smithies 00038 * 00039 * Revision 1.27 2003/11/12 10:25:41 csoutheren 00040 * Changes to allow operation of static plugins under Windows 00041 * 00042 * Revision 1.26 2003/11/12 05:18:04 csoutheren 00043 * Added more backwards compatibility functions for PSoundChannel 00044 * 00045 * Revision 1.25 2003/11/12 04:33:32 csoutheren 00046 * Fixed problem with static linking of sound plugins 00047 * Fixed problem with Windows sound driver 00048 * 00049 * Revision 1.24 2003/11/12 03:29:51 csoutheren 00050 * Initial version of plugin code from Snark of GnomeMeeting with changes 00051 * by Craig Southeren of Post Increment 00052 * 00053 * Revision 1.23.2.2 2003/10/13 02:42:39 dereksmithies 00054 * Add additional functions, so plugins work better. 00055 * 00056 * Revision 1.23.2.1 2003/10/07 03:02:28 csoutheren 00057 * Initial checkin of pwlib code to do plugins. 00058 * Modified from original code and concept provided by Snark of Gnomemeeting 00059 * 00060 * Revision 1.23 2003/09/17 05:41:59 csoutheren 00061 * Removed recursive includes 00062 * 00063 * Revision 1.22 2003/09/17 01:18:02 csoutheren 00064 * Removed recursive include file system and removed all references 00065 * to deprecated coooperative threading support 00066 * 00067 * Revision 1.21 2002/09/16 01:08:59 robertj 00068 * Added #define so can select if #pragma interface/implementation is used on 00069 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00070 * 00071 * Revision 1.20 2002/02/08 09:58:44 robertj 00072 * Slight adjustment to API and documentation for volume functions. 00073 * 00074 * Revision 1.19 2002/02/07 20:57:21 dereks 00075 * add SetVolume and GetVolume methods to PSoundChannel 00076 * 00077 * Revision 1.18 2001/05/22 12:49:32 robertj 00078 * Did some seriously wierd rewrite of platform headers to eliminate the 00079 * stupid GNU compiler warning about braces not matching. 00080 * 00081 * Revision 1.17 2001/02/07 03:33:43 craigs 00082 * Added functions to get sound channel parameters 00083 * 00084 * Revision 1.16 2000/03/04 10:15:32 robertj 00085 * Added simple play functions for sound files. 00086 * 00087 * Revision 1.15 1999/05/28 14:04:10 robertj 00088 * Added function to get default audio device. 00089 * 00090 * Revision 1.14 1999/03/09 02:59:51 robertj 00091 * Changed comments to doc++ compatible documentation. 00092 * 00093 * Revision 1.13 1999/02/22 10:15:14 robertj 00094 * Sound driver interface implementation to Linux OSS specification. 00095 * 00096 * Revision 1.12 1999/02/16 06:02:27 robertj 00097 * Major implementation to Linux OSS model 00098 * 00099 * Revision 1.11 1998/09/23 06:21:27 robertj 00100 * Added open source copyright license. 00101 * 00102 * Revision 1.10 1995/06/17 11:13:26 robertj 00103 * Documentation update. 00104 * 00105 * Revision 1.9 1995/03/14 12:42:40 robertj 00106 * Updated documentation to use HTML codes. 00107 * 00108 * Revision 1.8 1995/01/16 09:42:05 robertj 00109 * Documentation. 00110 * 00111 * Revision 1.7 1994/08/23 11:32:52 robertj 00112 * Oops 00113 * 00114 * Revision 1.6 1994/08/22 00:46:48 robertj 00115 * Added pragma fro GNU C++ compiler. 00116 * 00117 * Revision 1.5 1994/06/25 11:55:15 robertj 00118 * Unix version synchronisation. 00119 * 00120 * Revision 1.4 1994/01/03 04:42:23 robertj 00121 * Mass changes to common container classes and interactors etc etc etc. 00122 * 00123 * Revision 1.3 1993/09/29 03:06:30 robertj 00124 * Added unix compatibility to Beep() 00125 * 00126 * Revision 1.2 1993/07/14 12:49:16 robertj 00127 * Fixed RCS keywords. 00128 * 00129 */ 00130 00131 00132 #ifndef _PSOUND 00133 #define _PSOUND 00134 00135 #ifdef P_USE_PRAGMA 00136 #pragma interface 00137 #endif 00138 00139 #include <ptlib/pluginmgr.h> 00140 00141 #if defined(_WIN32) 00142 PWLIB_STATIC_LOAD_PLUGIN(PSoundChannel_WindowsMultimedia); 00143 #endif 00144 00145 00153 class PSound : public PBYTEArray 00154 { 00155 PCLASSINFO(PSound, PBYTEArray); 00156 00157 public: 00166 PSound( 00167 unsigned numChannels = 1, 00168 unsigned sampleRate = 8000, 00169 unsigned bitsPerSample = 16, 00170 PINDEX bufferSize = 0, 00171 const BYTE * data = NULL 00172 ); 00173 00176 PSound( 00177 const PFilePath & filename 00178 ); 00179 00182 PSound & operator=( 00183 const PBYTEArray & data // New data for sound 00184 ); 00186 00198 BOOL Load( 00199 const PFilePath & filename 00200 ); 00201 00208 BOOL Save( 00209 const PFilePath & filename // Sound file to load. 00210 ); 00212 00215 00216 BOOL Play(); 00217 00221 void SetFormat( 00222 unsigned numChannels, // Number of channels eg mono/stereo 00223 unsigned sampleRate, 00224 unsigned bitsPerSample 00225 ); 00226 00230 unsigned GetEncoding() const { return encoding; } 00231 00233 unsigned GetChannels() const { return numChannels; } 00234 00236 unsigned GetSampleRate() const { return sampleRate; } 00237 00239 unsigned GetSampleSize() const { return sampleSize; } 00240 00242 DWORD GetErrorCode() const { return dwLastError; } 00243 00245 PINDEX GetFormatInfoSize() const { return formatInfo.GetSize(); } 00246 00248 const void * GetFormatInfoData() const { return (const BYTE *)formatInfo; } 00250 00261 static BOOL PlayFile( 00262 const PFilePath & file, 00263 BOOL wait = TRUE 00264 ); 00265 00267 static void Beep(); 00269 00270 protected: 00272 unsigned encoding; 00274 unsigned numChannels; 00276 unsigned sampleRate; 00278 unsigned sampleSize; 00280 DWORD dwLastError; 00282 PBYTEArray formatInfo; 00283 }; 00284 00285 00316 class PSoundChannel : public PChannel 00317 { 00318 PCLASSINFO(PSoundChannel, PChannel); 00319 00320 public: 00323 enum Directions { 00324 Recorder, 00325 Player 00326 }; 00327 00329 PSoundChannel(); 00330 00334 PSoundChannel( 00335 const PString & device, 00336 Directions dir, 00337 unsigned numChannels = 1, 00338 unsigned sampleRate = 8000, 00339 unsigned bitsPerSample = 16 00340 ); 00341 // 00342 00343 virtual ~PSoundChannel(); 00344 // Destroy and close the sound driver 00346 00352 static PStringList GetDriverNames(PPluginManager * pluginMgr = NULL); 00353 00357 static PStringList GetDeviceNames( 00358 const PString &driverName, 00359 const PSoundChannel::Directions, 00360 PPluginManager * pluginMgr = NULL 00361 ); 00362 00366 static PSoundChannel *CreateChannel ( 00367 const PString &driverName, 00368 PPluginManager * pluginMgr = NULL 00369 ); 00370 00374 static PSoundChannel * CreateOpenedChannel( 00375 const PString & driverName, 00376 const PString & deviceName, 00377 const PSoundChannel::Directions, 00378 unsigned numChannels = 1, 00379 unsigned sampleRate = 8000, 00380 unsigned bitsPerSample = 16 00381 ); 00382 00391 static PString GetDefaultDevice( 00392 Directions dir // Sound I/O direction 00393 ); 00394 00403 static PStringList GetDeviceNames( 00404 Directions dir // Sound I/O direction 00405 ); 00406 00413 virtual BOOL Open( 00414 const PString & device, 00415 Directions dir, 00416 unsigned numChannels = 1, 00417 unsigned sampleRate = 8000, 00418 unsigned bitsPerSample = 16 00419 ); 00420 00426 virtual BOOL IsOpen() const 00427 { return (baseChannel == NULL) ? FALSE : baseChannel->PChannel::IsOpen(); } 00428 00434 virtual int GetHandle() const 00435 { return (baseChannel == NULL) ? -1 : baseChannel->PChannel::GetHandle(); } 00436 00442 virtual BOOL Abort() 00443 { return (baseChannel == NULL) ? FALSE : baseChannel->Abort(); } 00445 00457 virtual BOOL SetFormat( 00458 unsigned numChannels = 1, 00459 unsigned sampleRate = 8000, 00460 unsigned bitsPerSample = 16 00461 ) 00462 { return (baseChannel == NULL) ? FALSE : baseChannel->SetFormat(numChannels, sampleRate, bitsPerSample); } 00463 00465 virtual unsigned GetChannels() const 00466 { return (baseChannel == NULL) ? 0 : baseChannel->GetChannels(); } 00467 00469 virtual unsigned GetSampleRate() const 00470 { return (baseChannel == NULL) ? 0 : baseChannel->GetSampleRate(); } 00471 00473 virtual unsigned GetSampleSize() const 00474 { return (baseChannel == NULL) ? 0 : baseChannel->GetSampleSize(); } 00475 00484 virtual BOOL SetBuffers( 00485 PINDEX size, 00486 PINDEX count = 2 00487 ) 00488 { return (baseChannel == NULL) ? FALSE : baseChannel->SetBuffers(size, count); } 00489 00495 virtual BOOL GetBuffers( 00496 PINDEX & size, // Size of each buffer 00497 PINDEX & count // Number of buffers 00498 ) 00499 { return (baseChannel == NULL) ? FALSE : baseChannel->GetBuffers(size, count); } 00500 00501 enum { 00502 MaxVolume = 100 00503 }; 00504 00511 virtual BOOL SetVolume( 00512 unsigned volume 00513 ) 00514 { return (baseChannel == NULL) ? FALSE : baseChannel->SetVolume(volume); } 00515 00522 virtual BOOL GetVolume( 00523 unsigned & volume 00524 ) 00525 { return (baseChannel == NULL) ? FALSE : baseChannel->GetVolume(volume); } 00527 00530 00542 virtual BOOL Write(const void * buf, PINDEX len) 00543 { return (baseChannel == NULL) ? FALSE : baseChannel->Write(buf, len); } 00544 00545 PINDEX GetLastWriteCount() const 00546 { return (baseChannel == NULL) ? lastWriteCount : baseChannel->GetLastWriteCount(); } 00547 00564 virtual BOOL PlaySound( 00565 const PSound & sound, 00566 BOOL wait = TRUE 00567 ) 00568 { return (baseChannel == NULL) ? FALSE : baseChannel->PlaySound(sound, wait); } 00584 virtual BOOL PlayFile( 00585 const PFilePath & file, 00586 BOOL wait = TRUE 00587 ) 00588 { return (baseChannel == NULL) ? FALSE : baseChannel->PlayFile(file, wait); } 00589 00596 virtual BOOL HasPlayCompleted() 00597 { return (baseChannel == NULL) ? FALSE : baseChannel->HasPlayCompleted(); } 00598 00605 virtual BOOL WaitForPlayCompletion() 00606 { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForPlayCompletion(); } 00607 00609 00624 virtual BOOL Read( 00625 void * buf, 00626 PINDEX len 00627 ) 00628 { return (baseChannel == NULL) ? FALSE : baseChannel->Read(buf, len); } 00629 00630 PINDEX GetLastReadCount() const 00631 { return (baseChannel == NULL) ? lastReadCount : baseChannel->GetLastReadCount(); } 00632 00650 virtual BOOL RecordSound( 00651 PSound & sound 00652 ) 00653 { return (baseChannel == NULL) ? FALSE : baseChannel->RecordSound(sound); } 00654 00667 virtual BOOL RecordFile( 00668 const PFilePath & file 00669 ) 00670 { return (baseChannel == NULL) ? FALSE : baseChannel->RecordFile(file); } 00671 00678 virtual BOOL StartRecording() 00679 { return (baseChannel == NULL) ? FALSE : baseChannel->StartRecording(); } 00680 00688 virtual BOOL IsRecordBufferFull() 00689 { return (baseChannel == NULL) ? FALSE : baseChannel->IsRecordBufferFull(); } 00690 00699 virtual BOOL AreAllRecordBuffersFull() 00700 { return (baseChannel == NULL) ? FALSE : baseChannel->AreAllRecordBuffersFull(); } 00701 00709 virtual BOOL WaitForRecordBufferFull() 00710 { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForRecordBufferFull() ; } 00711 00720 virtual BOOL WaitForAllRecordBuffersFull() 00721 { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForAllRecordBuffersFull() ; } 00723 00724 protected: 00725 PSoundChannel * baseChannel; 00726 }; 00727 00728 00730 00731 // define the sound plugin service descriptor 00732 00733 class PSoundChannelPluginServiceDescriptor : public PPluginServiceDescriptor 00734 { 00735 public: 00736 PSoundChannelPluginServiceDescriptor( 00737 unsigned (*_GetVersion)(), 00738 PSoundChannel *(*_CreateInstance)(), 00739 PStringArray (*_GetDeviceNames)(PSoundChannel::Directions) 00740 ) 00741 : PPluginServiceDescriptor(_GetVersion), 00742 CreateInstance(_CreateInstance), 00743 GetDeviceNames(_GetDeviceNames) 00744 { } 00745 00746 PSoundChannel *(*CreateInstance)(); 00747 PStringArray (*GetDeviceNames) (PSoundChannel::Directions); 00748 }; 00749 00750 00751 #define PCREATE_SOUND_SERVICE_DESCRIPTOR(className, versionFn) \ 00752 PSoundChannel * className##_CreateInstance () \ 00753 { \ 00754 return new className; \ 00755 } \ 00756 \ 00757 PStringArray className##_GetDeviceNames (PSoundChannel::Directions dir) \ 00758 { \ 00759 return className::GetDeviceNames(dir); \ 00760 } \ 00761 \ 00762 PSoundChannelPluginServiceDescriptor className##_descriptor(\ 00763 versionFn, \ 00764 className##_CreateInstance, \ 00765 className##_GetDeviceNames \ 00766 ); \ 00767 00768 #define PCREATE_SOUND_PLUGIN(name, className) \ 00769 PCREATE_PLUGIN_VERSION_FN(name, PSoundChannel) \ 00770 PCREATE_SOUND_SERVICE_DESCRIPTOR(className, PPLUGIN_VERSION_FN(name, PSoundChannel)) \ 00771 PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor) 00772 00773 #endif 00774 00775 // End Of File ///////////////////////////////////////////////////////////////

Generated on Sat Jul 24 15:35:56 2004 for PWLib by doxygen 1.3.7