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
00169
00170
00171
00172
00173
00174 #ifndef _PSOUND
00175 #define _PSOUND
00176
00177 #ifdef P_USE_PRAGMA
00178 #pragma interface
00179 #endif
00180
00181 #include <ptlib/plugin.h>
00182 #include <ptlib/pluginmgr.h>
00183
00191 class PSound : public PBYTEArray
00192 {
00193 PCLASSINFO(PSound, PBYTEArray);
00194
00195 public:
00204 PSound(
00205 unsigned numChannels = 1,
00206 unsigned sampleRate = 8000,
00207 unsigned bitsPerSample = 16,
00208 PINDEX bufferSize = 0,
00209 const BYTE * data = NULL
00210 );
00211
00214 PSound(
00215 const PFilePath & filename
00216 );
00217
00220 PSound & operator=(
00221 const PBYTEArray & data
00222 );
00224
00236 BOOL Load(
00237 const PFilePath & filename
00238 );
00239
00246 BOOL Save(
00247 const PFilePath & filename
00248 );
00250
00253
00254 BOOL Play();
00255
00257 BOOL Play(const PString & device);
00258
00262 void SetFormat(
00263 unsigned numChannels,
00264 unsigned sampleRate,
00265 unsigned bitsPerSample
00266 );
00267
00271 unsigned GetEncoding() const { return encoding; }
00272
00274 unsigned GetChannels() const { return numChannels; }
00275
00277 unsigned GetSampleRate() const { return sampleRate; }
00278
00280 unsigned GetSampleSize() const { return sampleSize; }
00281
00283 DWORD GetErrorCode() const { return dwLastError; }
00284
00286 PINDEX GetFormatInfoSize() const { return formatInfo.GetSize(); }
00287
00289 const void * GetFormatInfoData() const { return (const BYTE *)formatInfo; }
00291
00302 static BOOL PlayFile(
00303 const PFilePath & file,
00304 BOOL wait = TRUE
00305 );
00306
00308 static void Beep();
00310
00311 protected:
00313 unsigned encoding;
00315 unsigned numChannels;
00317 unsigned sampleRate;
00319 unsigned sampleSize;
00321 DWORD dwLastError;
00323 PBYTEArray formatInfo;
00324 };
00325
00326
00357 class PSoundChannel : public PChannel
00358 {
00359 PCLASSINFO(PSoundChannel, PChannel);
00360
00361 public:
00364 enum Directions {
00365 Recorder,
00366 Player
00367 };
00368
00370 PSoundChannel();
00371
00375 PSoundChannel(
00376 const PString & device,
00377 Directions dir,
00378 unsigned numChannels = 1,
00379 unsigned sampleRate = 8000,
00380 unsigned bitsPerSample = 16
00381 );
00382
00383
00384 virtual ~PSoundChannel();
00385
00387
00392 static PStringList GetDriverNames(
00393 PPluginManager * pluginMgr = NULL
00394 );
00395
00400 static PStringList GetDriversDeviceNames(
00401 const PString & driverName,
00402 Directions direction,
00403 PPluginManager * pluginMgr = NULL
00404 );
00405
00406
00407 static inline PStringList GetDeviceNames(
00408 const PString & driverName,
00409 Directions direction,
00410 PPluginManager * pluginMgr = NULL
00411 ) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
00412
00415 static PSoundChannel * CreateChannel (
00416 const PString & driverName,
00417 PPluginManager * pluginMgr = NULL
00418 );
00419
00420
00421
00422
00423
00424
00425
00426
00427 static PSoundChannel * CreateChannelByName(
00428 const PString & deviceName,
00429 Directions direction,
00430 PPluginManager * pluginMgr = NULL
00431 );
00432
00438 static PSoundChannel * CreateOpenedChannel(
00439 const PString & driverName,
00440 const PString & deviceName,
00441 Directions direction,
00442 unsigned numChannels = 1,
00443 unsigned sampleRate = 8000,
00444 unsigned bitsPerSample = 16,
00445 PPluginManager * pluginMgr = NULL
00446 );
00447
00460 static PString GetDefaultDevice(
00461 Directions dir
00462 );
00463
00472 static PStringList GetDeviceNames(
00473 Directions direction,
00474 PPluginManager * pluginMgr = NULL
00475 );
00476
00483 virtual BOOL Open(
00484 const PString & device,
00485 Directions dir,
00486 unsigned numChannels = 1,
00487 unsigned sampleRate = 8000,
00488 unsigned bitsPerSample = 16
00489 );
00490
00496 virtual BOOL IsOpen() const;
00497
00502 virtual BOOL Close();
00503
00509 virtual int GetHandle() const;
00510
00512 virtual PString GetName() const;
00513
00519 virtual BOOL Abort();
00521
00533 virtual BOOL SetFormat(
00534 unsigned numChannels = 1,
00535 unsigned sampleRate = 8000,
00536 unsigned bitsPerSample = 16
00537 );
00538
00540 virtual unsigned GetChannels() const;
00541
00543 virtual unsigned GetSampleRate() const;
00544
00546 virtual unsigned GetSampleSize() const;
00547
00556 virtual BOOL SetBuffers(
00557 PINDEX size,
00558 PINDEX count = 2
00559 );
00560
00566 virtual BOOL GetBuffers(
00567 PINDEX & size,
00568 PINDEX & count
00569 );
00570
00571 enum {
00572 MaxVolume = 100
00573 };
00574
00581 virtual BOOL SetVolume(
00582 unsigned volume
00583 );
00584
00591 virtual BOOL GetVolume(
00592 unsigned & volume
00593 );
00595
00598
00610 virtual BOOL Write(const void * buf, PINDEX len);
00611
00612 virtual PINDEX GetLastWriteCount() const;
00613
00630 virtual BOOL PlaySound(
00631 const PSound & sound,
00632 BOOL wait = TRUE
00633 );
00649 virtual BOOL PlayFile(
00650 const PFilePath & file,
00651 BOOL wait = TRUE
00652 );
00653
00660 virtual BOOL HasPlayCompleted();
00661
00668 virtual BOOL WaitForPlayCompletion();
00669
00671
00686 virtual BOOL Read(
00687 void * buf,
00688 PINDEX len
00689 );
00690
00691 PINDEX GetLastReadCount() const;
00692
00710 virtual BOOL RecordSound(
00711 PSound & sound
00712 );
00713
00726 virtual BOOL RecordFile(
00727 const PFilePath & file
00728 );
00729
00736 virtual BOOL StartRecording();
00737
00745 virtual BOOL IsRecordBufferFull();
00746
00755 virtual BOOL AreAllRecordBuffersFull();
00756
00764 virtual BOOL WaitForRecordBufferFull();
00765
00774 virtual BOOL WaitForAllRecordBuffersFull();
00776
00777 protected:
00778 PSoundChannel * baseChannel;
00779 };
00780
00781
00783
00784
00785
00786 template <class className> class PSoundChannelPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00787 {
00788 public:
00789 virtual PObject * CreateInstance(int ) const { return new className; }
00790 virtual PStringList GetDeviceNames(int userData) const { return className::GetDeviceNames((PSoundChannel::Directions)userData); }
00791 };
00792
00793 #define PCREATE_SOUND_PLUGIN(name, className) \
00794 static PSoundChannelPluginServiceDescriptor<className> className##_descriptor; \
00795 PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)
00796
00797 #endif
00798
00799