00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 #ifndef _PSOUND
00169 #define _PSOUND
00170
00171 #ifdef P_USE_PRAGMA
00172 #pragma interface
00173 #endif
00174
00175 #include <ptlib/plugin.h>
00176 #include <ptlib/pluginmgr.h>
00177
00185 class PSound : public PBYTEArray
00186 {
00187 PCLASSINFO(PSound, PBYTEArray);
00188
00189 public:
00198 PSound(
00199 unsigned numChannels = 1,
00200 unsigned sampleRate = 8000,
00201 unsigned bitsPerSample = 16,
00202 PINDEX bufferSize = 0,
00203 const BYTE * data = NULL
00204 );
00205
00208 PSound(
00209 const PFilePath & filename
00210 );
00211
00214 PSound & operator=(
00215 const PBYTEArray & data
00216 );
00218
00230 BOOL Load(
00231 const PFilePath & filename
00232 );
00233
00240 BOOL Save(
00241 const PFilePath & filename
00242 );
00244
00247
00248 BOOL Play();
00249
00251 BOOL Play(const PString & device);
00252
00256 void SetFormat(
00257 unsigned numChannels,
00258 unsigned sampleRate,
00259 unsigned bitsPerSample
00260 );
00261
00265 unsigned GetEncoding() const { return encoding; }
00266
00268 unsigned GetChannels() const { return numChannels; }
00269
00271 unsigned GetSampleRate() const { return sampleRate; }
00272
00274 unsigned GetSampleSize() const { return sampleSize; }
00275
00277 DWORD GetErrorCode() const { return dwLastError; }
00278
00280 PINDEX GetFormatInfoSize() const { return formatInfo.GetSize(); }
00281
00283 const void * GetFormatInfoData() const { return (const BYTE *)formatInfo; }
00285
00296 static BOOL PlayFile(
00297 const PFilePath & file,
00298 BOOL wait = TRUE
00299 );
00300
00302 static void Beep();
00304
00305 protected:
00307 unsigned encoding;
00309 unsigned numChannels;
00311 unsigned sampleRate;
00313 unsigned sampleSize;
00315 DWORD dwLastError;
00317 PBYTEArray formatInfo;
00318 };
00319
00320
00351 class PSoundChannel : public PChannel
00352 {
00353 PCLASSINFO(PSoundChannel, PChannel);
00354
00355 public:
00358 enum Directions {
00359 Recorder,
00360 Player
00361 };
00362
00364 PSoundChannel();
00365
00369 PSoundChannel(
00370 const PString & device,
00371 Directions dir,
00372 unsigned numChannels = 1,
00373 unsigned sampleRate = 8000,
00374 unsigned bitsPerSample = 16
00375 );
00376
00377
00378 virtual ~PSoundChannel();
00379
00381
00386 static PStringList GetDriverNames(
00387 PPluginManager * pluginMgr = NULL
00388 );
00389
00394 static PStringList GetDriversDeviceNames(
00395 const PString & driverName,
00396 Directions direction,
00397 PPluginManager * pluginMgr = NULL
00398 );
00399
00400
00401 static inline PStringList GetDeviceNames(
00402 const PString & driverName,
00403 Directions direction,
00404 PPluginManager * pluginMgr = NULL
00405 ) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
00406
00409 static PSoundChannel * CreateChannel (
00410 const PString & driverName,
00411 PPluginManager * pluginMgr = NULL
00412 );
00413
00414
00415
00416
00417
00418
00419
00420
00421 static PSoundChannel * CreateChannelByName(
00422 const PString & deviceName,
00423 Directions direction,
00424 PPluginManager * pluginMgr = NULL
00425 );
00426
00432 static PSoundChannel * CreateOpenedChannel(
00433 const PString & driverName,
00434 const PString & deviceName,
00435 Directions direction,
00436 unsigned numChannels = 1,
00437 unsigned sampleRate = 8000,
00438 unsigned bitsPerSample = 16,
00439 PPluginManager * pluginMgr = NULL
00440 );
00441
00454 static PString GetDefaultDevice(
00455 Directions dir
00456 );
00457
00466 static PStringList GetDeviceNames(
00467 Directions direction,
00468 PPluginManager * pluginMgr = NULL
00469 );
00470
00477 virtual BOOL Open(
00478 const PString & device,
00479 Directions dir,
00480 unsigned numChannels = 1,
00481 unsigned sampleRate = 8000,
00482 unsigned bitsPerSample = 16
00483 );
00484
00490 virtual BOOL IsOpen() const
00491 { return (baseChannel == NULL) ? FALSE : baseChannel->PChannel::IsOpen(); }
00492
00497 virtual BOOL Close()
00498 { return (baseChannel == NULL) ? -1 : baseChannel->Close(); }
00499
00505 virtual int GetHandle() const
00506 { return (baseChannel == NULL) ? -1 : baseChannel->PChannel::GetHandle(); }
00507
00513 virtual BOOL Abort()
00514 { return (baseChannel == NULL) ? FALSE : baseChannel->Abort(); }
00516
00528 virtual BOOL SetFormat(
00529 unsigned numChannels = 1,
00530 unsigned sampleRate = 8000,
00531 unsigned bitsPerSample = 16
00532 )
00533 { return (baseChannel == NULL) ? FALSE : baseChannel->SetFormat(numChannels, sampleRate, bitsPerSample); }
00534
00536 virtual unsigned GetChannels() const
00537 { return (baseChannel == NULL) ? 0 : baseChannel->GetChannels(); }
00538
00540 virtual unsigned GetSampleRate() const
00541 { return (baseChannel == NULL) ? 0 : baseChannel->GetSampleRate(); }
00542
00544 virtual unsigned GetSampleSize() const
00545 { return (baseChannel == NULL) ? 0 : baseChannel->GetSampleSize(); }
00546
00555 virtual BOOL SetBuffers(
00556 PINDEX size,
00557 PINDEX count = 2
00558 )
00559 { return (baseChannel == NULL) ? FALSE : baseChannel->SetBuffers(size, count); }
00560
00566 virtual BOOL GetBuffers(
00567 PINDEX & size,
00568 PINDEX & count
00569 )
00570 { return (baseChannel == NULL) ? FALSE : baseChannel->GetBuffers(size, count); }
00571
00572 enum {
00573 MaxVolume = 100
00574 };
00575
00582 virtual BOOL SetVolume(
00583 unsigned volume
00584 )
00585 { return (baseChannel == NULL) ? FALSE : baseChannel->SetVolume(volume); }
00586
00593 virtual BOOL GetVolume(
00594 unsigned & volume
00595 )
00596 { return (baseChannel == NULL) ? FALSE : baseChannel->GetVolume(volume); }
00598
00601
00613 virtual BOOL Write(const void * buf, PINDEX len)
00614 { return (baseChannel == NULL) ? FALSE : baseChannel->Write(buf, len); }
00615
00616 PINDEX GetLastWriteCount() const
00617 { return (baseChannel == NULL) ? lastWriteCount : baseChannel->GetLastWriteCount(); }
00618
00635 virtual BOOL PlaySound(
00636 const PSound & sound,
00637 BOOL wait = TRUE
00638 )
00639 { return (baseChannel == NULL) ? FALSE : baseChannel->PlaySound(sound, wait); }
00655 virtual BOOL PlayFile(
00656 const PFilePath & file,
00657 BOOL wait = TRUE
00658 )
00659 { return (baseChannel == NULL) ? FALSE : baseChannel->PlayFile(file, wait); }
00660
00667 virtual BOOL HasPlayCompleted()
00668 { return (baseChannel == NULL) ? FALSE : baseChannel->HasPlayCompleted(); }
00669
00676 virtual BOOL WaitForPlayCompletion()
00677 { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForPlayCompletion(); }
00678
00680
00695 virtual BOOL Read(
00696 void * buf,
00697 PINDEX len
00698 )
00699 { return (baseChannel == NULL) ? FALSE : baseChannel->Read(buf, len); }
00700
00701 PINDEX GetLastReadCount() const
00702 { return (baseChannel == NULL) ? lastReadCount : baseChannel->GetLastReadCount(); }
00703
00721 virtual BOOL RecordSound(
00722 PSound & sound
00723 )
00724 { return (baseChannel == NULL) ? FALSE : baseChannel->RecordSound(sound); }
00725
00738 virtual BOOL RecordFile(
00739 const PFilePath & file
00740 )
00741 { return (baseChannel == NULL) ? FALSE : baseChannel->RecordFile(file); }
00742
00749 virtual BOOL StartRecording()
00750 { return (baseChannel == NULL) ? FALSE : baseChannel->StartRecording(); }
00751
00759 virtual BOOL IsRecordBufferFull()
00760 { return (baseChannel == NULL) ? FALSE : baseChannel->IsRecordBufferFull(); }
00761
00770 virtual BOOL AreAllRecordBuffersFull()
00771 { return (baseChannel == NULL) ? FALSE : baseChannel->AreAllRecordBuffersFull(); }
00772
00780 virtual BOOL WaitForRecordBufferFull()
00781 { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForRecordBufferFull() ; }
00782
00791 virtual BOOL WaitForAllRecordBuffersFull()
00792 { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForAllRecordBuffersFull() ; }
00794
00795 protected:
00796 PSoundChannel * baseChannel;
00797 };
00798
00799
00801
00802
00803
00804 template <class className> class PSoundChannelPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00805 {
00806 public:
00807 virtual PObject * CreateInstance(int ) const { return new className; }
00808 virtual PStringList GetDeviceNames(int userData) const { return className::GetDeviceNames((PSoundChannel::Directions)userData); }
00809 };
00810
00811 #define PCREATE_SOUND_PLUGIN(name, className) \
00812 static PSoundChannelPluginServiceDescriptor<className> className##_descriptor; \
00813 PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)
00814
00815 #endif
00816
00817