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
#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
00184 );
00186
00198 BOOL
Load(
00199
const PFilePath & filename
00200 );
00201
00208 BOOL
Save(
00209
const PFilePath & filename
00210 );
00212
00215
00216 BOOL
Play();
00217
00221
void SetFormat(
00222
unsigned numChannels,
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
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
00393 );
00394
00403
static PStringList GetDeviceNames(
00404 Directions dir
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,
00497 PINDEX & count
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
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