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 #ifndef _PSOUND
00163 #define _PSOUND
00164
00165 #ifdef P_USE_PRAGMA
00166 #pragma interface
00167 #endif
00168
00169 #include <ptlib/pluginmgr.h>
00170
00178 class PSound : public PBYTEArray
00179 {
00180 PCLASSINFO(PSound, PBYTEArray);
00181
00182 public:
00191 PSound(
00192 unsigned numChannels = 1,
00193 unsigned sampleRate = 8000,
00194 unsigned bitsPerSample = 16,
00195 PINDEX bufferSize = 0,
00196 const BYTE * data = NULL
00197 );
00198
00201 PSound(
00202 const PFilePath & filename
00203 );
00204
00207 PSound & operator=(
00208 const PBYTEArray & data
00209 );
00211
00223 BOOL Load(
00224 const PFilePath & filename
00225 );
00226
00233 BOOL Save(
00234 const PFilePath & filename
00235 );
00237
00240
00241 BOOL Play();
00242
00244 BOOL Play(const PString & device);
00245
00249 void SetFormat(
00250 unsigned numChannels,
00251 unsigned sampleRate,
00252 unsigned bitsPerSample
00253 );
00254
00258 unsigned GetEncoding() const { return encoding; }
00259
00261 unsigned GetChannels() const { return numChannels; }
00262
00264 unsigned GetSampleRate() const { return sampleRate; }
00265
00267 unsigned GetSampleSize() const { return sampleSize; }
00268
00270 DWORD GetErrorCode() const { return dwLastError; }
00271
00273 PINDEX GetFormatInfoSize() const { return formatInfo.GetSize(); }
00274
00276 const void * GetFormatInfoData() const { return (const BYTE *)formatInfo; }
00278
00289 static BOOL PlayFile(
00290 const PFilePath & file,
00291 BOOL wait = TRUE
00292 );
00293
00295 static void Beep();
00297
00298 protected:
00300 unsigned encoding;
00302 unsigned numChannels;
00304 unsigned sampleRate;
00306 unsigned sampleSize;
00308 DWORD dwLastError;
00310 PBYTEArray formatInfo;
00311 };
00312
00313
00344 class PSoundChannel : public PChannel
00345 {
00346 PCLASSINFO(PSoundChannel, PChannel);
00347
00348 public:
00351 enum Directions {
00352 Recorder,
00353 Player
00354 };
00355
00357 PSoundChannel();
00358
00362 PSoundChannel(
00363 const PString & device,
00364 Directions dir,
00365 unsigned numChannels = 1,
00366 unsigned sampleRate = 8000,
00367 unsigned bitsPerSample = 16
00368 );
00369
00370
00371 virtual ~PSoundChannel();
00372
00374
00379 static PStringList GetDriverNames(
00380 PPluginManager * pluginMgr = NULL
00381 );
00382
00387 static PStringList GetDriversDeviceNames(
00388 const PString & driverName,
00389 Directions direction,
00390 PPluginManager * pluginMgr = NULL
00391 );
00392
00393
00394 static inline PStringList GetDeviceNames(
00395 const PString & driverName,
00396 Directions direction,
00397 PPluginManager * pluginMgr = NULL
00398 ) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
00399
00402 static PSoundChannel * CreateChannel (
00403 const PString & driverName,
00404 PPluginManager * pluginMgr = NULL
00405 );
00406
00407
00408
00409
00410
00411
00412
00413
00414 static PSoundChannel * CreateChannelByName(
00415 const PString & deviceName,
00416 Directions direction,
00417 PPluginManager * pluginMgr = NULL
00418 );
00419
00425 static PSoundChannel * CreateOpenedChannel(
00426 const PString & driverName,
00427 const PString & deviceName,
00428 Directions direction,
00429 unsigned numChannels = 1,
00430 unsigned sampleRate = 8000,
00431 unsigned bitsPerSample = 16,
00432 PPluginManager * pluginMgr = NULL
00433 );
00434
00447 static PString GetDefaultDevice(
00448 Directions dir
00449 );
00450
00459 static PStringList GetDeviceNames(
00460 Directions direction,
00461 PPluginManager * pluginMgr = NULL
00462 );
00463
00470 virtual BOOL Open(
00471 const PString & device,
00472 Directions dir,
00473 unsigned numChannels = 1,
00474 unsigned sampleRate = 8000,
00475 unsigned bitsPerSample = 16
00476 );
00477
00483 virtual BOOL IsOpen() const
00484 { return (baseChannel == NULL) ? FALSE : baseChannel->PChannel::IsOpen(); }
00485
00491 virtual int GetHandle() const
00492 { return (baseChannel == NULL) ? -1 : baseChannel->PChannel::GetHandle(); }
00493
00499 virtual BOOL Abort()
00500 { return (baseChannel == NULL) ? FALSE : baseChannel->Abort(); }
00502
00514 virtual BOOL SetFormat(
00515 unsigned numChannels = 1,
00516 unsigned sampleRate = 8000,
00517 unsigned bitsPerSample = 16
00518 )
00519 { return (baseChannel == NULL) ? FALSE : baseChannel->SetFormat(numChannels, sampleRate, bitsPerSample); }
00520
00522 virtual unsigned GetChannels() const
00523 { return (baseChannel == NULL) ? 0 : baseChannel->GetChannels(); }
00524
00526 virtual unsigned GetSampleRate() const
00527 { return (baseChannel == NULL) ? 0 : baseChannel->GetSampleRate(); }
00528
00530 virtual unsigned GetSampleSize() const
00531 { return (baseChannel == NULL) ? 0 : baseChannel->GetSampleSize(); }
00532
00541 virtual BOOL SetBuffers(
00542 PINDEX size,
00543 PINDEX count = 2
00544 )
00545 { return (baseChannel == NULL) ? FALSE : baseChannel->SetBuffers(size, count); }
00546
00552 virtual BOOL GetBuffers(
00553 PINDEX & size,
00554 PINDEX & count
00555 )
00556 { return (baseChannel == NULL) ? FALSE : baseChannel->GetBuffers(size, count); }
00557
00558 enum {
00559 MaxVolume = 100
00560 };
00561
00568 virtual BOOL SetVolume(
00569 unsigned volume
00570 )
00571 { return (baseChannel == NULL) ? FALSE : baseChannel->SetVolume(volume); }
00572
00579 virtual BOOL GetVolume(
00580 unsigned & volume
00581 )
00582 { return (baseChannel == NULL) ? FALSE : baseChannel->GetVolume(volume); }
00584
00587
00599 virtual BOOL Write(const void * buf, PINDEX len)
00600 { return (baseChannel == NULL) ? FALSE : baseChannel->Write(buf, len); }
00601
00602 PINDEX GetLastWriteCount() const
00603 { return (baseChannel == NULL) ? lastWriteCount : baseChannel->GetLastWriteCount(); }
00604
00621 virtual BOOL PlaySound(
00622 const PSound & sound,
00623 BOOL wait = TRUE
00624 )
00625 { return (baseChannel == NULL) ? FALSE : baseChannel->PlaySound(sound, wait); }
00641 virtual BOOL PlayFile(
00642 const PFilePath & file,
00643 BOOL wait = TRUE
00644 )
00645 { return (baseChannel == NULL) ? FALSE : baseChannel->PlayFile(file, wait); }
00646
00653 virtual BOOL HasPlayCompleted()
00654 { return (baseChannel == NULL) ? FALSE : baseChannel->HasPlayCompleted(); }
00655
00662 virtual BOOL WaitForPlayCompletion()
00663 { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForPlayCompletion(); }
00664
00666
00681 virtual BOOL Read(
00682 void * buf,
00683 PINDEX len
00684 )
00685 { return (baseChannel == NULL) ? FALSE : baseChannel->Read(buf, len); }
00686
00687 PINDEX GetLastReadCount() const
00688 { return (baseChannel == NULL) ? lastReadCount : baseChannel->GetLastReadCount(); }
00689
00707 virtual BOOL RecordSound(
00708 PSound & sound
00709 )
00710 { return (baseChannel == NULL) ? FALSE : baseChannel->RecordSound(sound); }
00711
00724 virtual BOOL RecordFile(
00725 const PFilePath & file
00726 )
00727 { return (baseChannel == NULL) ? FALSE : baseChannel->RecordFile(file); }
00728
00735 virtual BOOL StartRecording()
00736 { return (baseChannel == NULL) ? FALSE : baseChannel->StartRecording(); }
00737
00745 virtual BOOL IsRecordBufferFull()
00746 { return (baseChannel == NULL) ? FALSE : baseChannel->IsRecordBufferFull(); }
00747
00756 virtual BOOL AreAllRecordBuffersFull()
00757 { return (baseChannel == NULL) ? FALSE : baseChannel->AreAllRecordBuffersFull(); }
00758
00766 virtual BOOL WaitForRecordBufferFull()
00767 { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForRecordBufferFull() ; }
00768
00777 virtual BOOL WaitForAllRecordBuffersFull()
00778 { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForAllRecordBuffersFull() ; }
00780
00781 protected:
00782 PSoundChannel * baseChannel;
00783 };
00784
00785
00787
00788
00789
00790 template <class className> class PSoundChannelPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00791 {
00792 public:
00793 virtual PObject * CreateInstance(int ) const { return new className; }
00794 virtual PStringList GetDeviceNames(int userData) const { return className::GetDeviceNames((PSoundChannel::Directions)userData); }
00795 };
00796
00797 #define PCREATE_SOUND_PLUGIN(name, className) \
00798 static PSoundChannelPluginServiceDescriptor<className> className##_descriptor; \
00799 PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)
00800
00801 #endif
00802
00803