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 #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 = PTrue
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
00218 class PSoundChannel : public PChannel
00219 {
00220 PCLASSINFO(PSoundChannel, PChannel);
00221
00222 public:
00225 enum Directions {
00226 Recorder,
00227 Player
00228 };
00229
00231 PSoundChannel();
00232
00236 PSoundChannel(
00237 const PString & device,
00238 Directions dir,
00239 unsigned numChannels = 1,
00240 unsigned sampleRate = 8000,
00241 unsigned bitsPerSample = 16
00242 );
00243
00244
00245 virtual ~PSoundChannel();
00246
00248
00253 static PStringArray GetDriverNames(
00254 PPluginManager * pluginMgr = NULL
00255 );
00256
00261 static PStringArray GetDriversDeviceNames(
00262 const PString & driverName,
00263 Directions direction,
00264 PPluginManager * pluginMgr = NULL
00265 );
00266
00267
00268 static inline PStringArray GetDeviceNames(
00269 const PString & driverName,
00270 Directions direction,
00271 PPluginManager * pluginMgr = NULL
00272 ) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
00273
00276 static PSoundChannel * CreateChannel (
00277 const PString & driverName,
00278 PPluginManager * pluginMgr = NULL
00279 );
00280
00281
00282
00283
00284
00285
00286
00287
00288 static PSoundChannel * CreateChannelByName(
00289 const PString & deviceName,
00290 Directions direction,
00291 PPluginManager * pluginMgr = NULL
00292 );
00293
00299 static PSoundChannel * CreateOpenedChannel(
00300 const PString & driverName,
00301 const PString & deviceName,
00302 Directions direction,
00303 unsigned numChannels = 1,
00304 unsigned sampleRate = 8000,
00305 unsigned bitsPerSample = 16,
00306 PPluginManager * pluginMgr = NULL
00307 );
00308
00321 static PString GetDefaultDevice(
00322 Directions dir
00323 );
00324
00333 static PStringArray GetDeviceNames(
00334 Directions direction,
00335 PPluginManager * pluginMgr = NULL
00336 );
00337
00344 virtual PBoolean Open(
00345 const PString & device,
00346 Directions dir,
00347 unsigned numChannels = 1,
00348 unsigned sampleRate = 8000,
00349 unsigned bitsPerSample = 16
00350 );
00351
00357 virtual PBoolean IsOpen() const;
00358
00363 virtual PBoolean Close();
00364
00370 virtual int GetHandle() const;
00371
00373 virtual PString GetName() const;
00374
00380 virtual PBoolean Abort();
00382
00394 virtual PBoolean SetFormat(
00395 unsigned numChannels = 1,
00396 unsigned sampleRate = 8000,
00397 unsigned bitsPerSample = 16
00398 );
00399
00401 virtual unsigned GetChannels() const;
00402
00404 virtual unsigned GetSampleRate() const;
00405
00407 virtual unsigned GetSampleSize() const;
00408
00417 virtual PBoolean SetBuffers(
00418 PINDEX size,
00419 PINDEX count = 2
00420 );
00421
00427 virtual PBoolean GetBuffers(
00428 PINDEX & size,
00429 PINDEX & count
00430 );
00431
00432 enum {
00433 MaxVolume = 100
00434 };
00435
00444 virtual PBoolean SetVolume(
00445 unsigned volume
00446 );
00447
00456 virtual PBoolean GetVolume(
00457 unsigned & volume
00458 );
00460
00463
00475 virtual PBoolean Write(const void * buf, PINDEX len);
00476
00477 virtual PINDEX GetLastWriteCount() const;
00478
00495 virtual PBoolean PlaySound(
00496 const PSound & sound,
00497 PBoolean wait = PTrue
00498 );
00514 virtual PBoolean PlayFile(
00515 const PFilePath & file,
00516 PBoolean wait = PTrue
00517 );
00518
00525 virtual PBoolean HasPlayCompleted();
00526
00533 virtual PBoolean WaitForPlayCompletion();
00534
00536
00551 virtual PBoolean Read(
00552 void * buf,
00553 PINDEX len
00554 );
00555
00556 PINDEX GetLastReadCount() const;
00557
00575 virtual PBoolean RecordSound(
00576 PSound & sound
00577 );
00578
00591 virtual PBoolean RecordFile(
00592 const PFilePath & file
00593 );
00594
00601 virtual PBoolean StartRecording();
00602
00610 virtual PBoolean IsRecordBufferFull();
00611
00620 virtual PBoolean AreAllRecordBuffersFull();
00621
00629 virtual PBoolean WaitForRecordBufferFull();
00630
00639 virtual PBoolean WaitForAllRecordBuffersFull();
00641
00642 protected:
00643 PSoundChannel * baseChannel;
00644 };
00645
00646
00648
00649
00650
00651 template <class className> class PSoundChannelPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00652 {
00653 public:
00654 virtual PObject * CreateInstance(int ) const { return new className; }
00655 virtual PStringArray GetDeviceNames(int userData) const { return className::GetDeviceNames((PSoundChannel::Directions)userData); }
00656 };
00657
00658 #define PCREATE_SOUND_PLUGIN(name, className) \
00659 static PSoundChannelPluginServiceDescriptor<className> className##_descriptor; \
00660 PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)
00661
00662
00663 #endif // PTLIB_SOUND_H
00664
00665
00666