PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sound.h
Go to the documentation of this file.
1 /*
2  * sound.h
3  *
4  * Sound interface class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  */
29 
30 
31 #ifndef PTLIB_SOUND_H
32 #define PTLIB_SOUND_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <ptlib/plugin.h>
39 #include <ptlib/pluginmgr.h>
40 #include <ptclib/delaychan.h>
41 
42 
43 #define PSOUND_PCM16 "PCM-16"
44 
45 
53 class PSound : public PBYTEArray
54 {
55  PCLASSINFO(PSound, PBYTEArray);
56 
57  public:
66  PSound(
67  unsigned numChannels = 1,
68  unsigned sampleRate = 8000,
69  unsigned bitsPerSample = 16,
70  PINDEX bufferSize = 0,
71  const BYTE * data = NULL
72  );
73 
76  PSound(
77  const PFilePath & filename
78  );
79 
82  PSound & operator=(
83  const PBYTEArray & data
84  );
86 
98  PBoolean Load(
99  const PFilePath & filename
100  );
101 
108  PBoolean Save(
109  const PFilePath & filename
110  );
112 
115  PBoolean Play();
117 
119  PBoolean Play(const PString & device);
120 
124  void SetFormat(
125  unsigned numChannels,
126  unsigned sampleRate,
127  unsigned bitsPerSample
128  );
129 
133  unsigned GetEncoding() const { return encoding; }
134 
136  unsigned GetChannels() const { return numChannels; }
137 
139  unsigned GetSampleRate() const { return sampleRate; }
140 
142  unsigned GetSampleSize() const { return sampleSize; }
143 
145  DWORD GetErrorCode() const { return dwLastError; }
146 
148  PINDEX GetFormatInfoSize() const { return formatInfo.GetSize(); }
149 
151  const void * GetFormatInfoData() const { return (const BYTE *)formatInfo; }
153 
164  static bool PlayFile(
165  const PFilePath & file,
166  bool wait = true,
167  unsigned volume = 100
168  );
169 
171  static void Beep();
172 
176  static bool ConvertPCM(
177  const short * srcPtr,
178  PINDEX & srcSize,
179  unsigned srcRate,
180  unsigned srcChannels,
181  short * dstPtr,
182  PINDEX & dstSize,
183  unsigned dstRate,
184  unsigned dstChannels
185  );
187 
188  protected:
190  unsigned encoding;
192  unsigned numChannels;
194  unsigned sampleRate;
196  unsigned sampleSize;
198  DWORD dwLastError;
201 };
202 
203 
268 {
269  PCLASSINFO(PSoundChannel, PIndirectChannel);
270 
271  public:
274  P_DECLARE_STREAMABLE_ENUM(Directions,
275  Recorder,
276  Player,
277  Closed
278  );
279 
281  PSoundChannel();
282 
283  enum {
284  DefaultBufferSize = 320, // 20ms at 8kHz
286  };
287 
289  struct Params {
290  Params(
291  Directions dir = Player,
292  const PString & device = PString::Empty(),
293  const PString & driver = PString::Empty(),
294  unsigned channels = 1,
295  unsigned sampleRate = 8000,
296  unsigned bitsPerSample = 16,
297  unsigned bufferSize = DefaultBufferSize,
298  unsigned bufferCount = DefaultBufferCount,
299  PPluginManager * pluginMgr = NULL
300  );
301 
302  Directions m_direction;
305  unsigned m_channels;
306  unsigned m_sampleRate;
307  unsigned m_bitsPerSample;
308  unsigned m_bufferSize;
311  unsigned m_bufferCount;
312  PPluginManager * m_pluginMgr;
313 
314  void SetBufferCountFromMS(unsigned milliseconds);
315  friend ostream & operator<<(ostream &, const Params & params);
316  };
317 
322  const Params & params
323  );
324 
325  // Backward compatibility
327  const PString & device,
328  Directions dir,
329  unsigned numChannels = 1,
330  unsigned sampleRate = 8000,
331  unsigned bitsPerSample = 16
332  );
334 
340  PPluginManager * pluginMgr = NULL
341  );
342 
348  const PString & driverName,
349  Directions direction,
350  PPluginManager * pluginMgr = NULL
351  );
352 
353  // For backward compatibility
355  const PString & driverName,
356  Directions direction,
357  PPluginManager * pluginMgr = NULL
358  ) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
359 
362  static PSoundChannel * CreateChannel (
363  const PString & driverName,
364  PPluginManager * pluginMgr = NULL
365  );
366 
367  /* Create the matching sound channel that corresponds to the device name.
368  So, for "fake" return a device that will generate fake video.
369  For "Phillips 680 webcam" (eg) will return appropriate grabber.
370  Note that Phillips will return the appropriate grabber also.
371 
372  This is typically used with the return values from GetDeviceNames().
373  */
375  const PString & deviceName,
376  Directions direction,
377  PPluginManager * pluginMgr = NULL
378  );
379 
386  const Params & params
387  );
388 
389  // For backward compatibility
391  const PString & driverName,
392  const PString & deviceName,
393  Directions direction,
394  unsigned numChannels = 1,
395  unsigned sampleRate = 8000,
396  unsigned bitsPerSample = 16,
397  PPluginManager * pluginMgr = NULL
398  );
399 
408  static PString GetDefaultDevice(
409  Directions dir // Sound I/O direction
410  );
411 
425  Directions direction,
426  PPluginManager * pluginMgr = NULL
427  );
428 
435  virtual bool Open(
436  const Params & params
437  );
438 
439  // For backward compatibility
440  bool Open(
441  const PString & device,
442  Directions dir,
443  unsigned numChannels = 1,
444  unsigned sampleRate = 8000,
445  unsigned bitsPerSample = 16,
446  PPluginManager * pluginMgr = NULL
447  );
448 
450  Directions GetDirection() const
451  {
452  return m_activeDirection;
453  }
454 
456  static const char * GetDirectionText(Directions dir);
457 
459  virtual const char * GetDirectionText() const
460  {
462  }
463 
471  virtual PBoolean Abort();
473 
485  virtual PBoolean SetFormat(
486  unsigned numChannels = 1,
487  unsigned sampleRate = 8000,
488  unsigned bitsPerSample = 16
489  );
490 
492  virtual unsigned GetChannels() const;
493 
495  virtual unsigned GetSampleRate() const;
496 
498  virtual unsigned GetSampleSize() const;
499 
508  virtual PBoolean SetBuffers(
509  PINDEX size,
510  PINDEX count = 2
511  );
512 
518  virtual PBoolean GetBuffers(
519  PINDEX & size, // Size of each buffer in bytes
520  PINDEX & count // Number of buffers
521  );
522 
523  enum {
524  MaxVolume = 100
525  };
526 
535  virtual PBoolean SetVolume(
536  unsigned volume
537  );
538 
547  virtual PBoolean GetVolume(
548  unsigned & volume
549  );
550 
556  virtual bool SetMute(
557  bool mute
558  );
559 
565  virtual bool GetMute(
566  bool & mute
567  );
568 
570 
589  virtual PBoolean PlaySound(
590  const PSound & sound,
591  PBoolean wait = true
592  );
593 
609  virtual bool PlayFile(
610  const PFilePath & file,
611  bool wait = true,
612  unsigned volume = 100
613  );
614 
621  virtual PBoolean HasPlayCompleted();
622 
630 
640  static PString TestPlayer(
641  const Params & params,
642  const PNotifier & progress = PNotifier(),
643  const char * toneSpec = NULL
644  );
646 
666  virtual PBoolean RecordSound(
667  PSound & sound
668  );
669 
682  virtual PBoolean RecordFile(
683  const PFilePath & file
684  );
685 
692  virtual PBoolean StartRecording();
693 
701  virtual PBoolean IsRecordBufferFull();
702 
712 
721 
731 
739  static PString TestRecorder(
740  const Params & recorderParams,
741  const Params & playerParams,
742  const PNotifier & progress = PNotifier(),
743  unsigned seconds = 5
744  );
746 
747  protected:
748  PSoundChannel * GetSoundChannel() const { return dynamic_cast<PSoundChannel *>(readChannel); }
749 
754  Directions m_activeDirection;
755 
756  P_REMOVE_VIRTUAL(PBoolean, Open(const PString &,Directions,unsigned,unsigned,unsigned),false);
757 };
758 
759 
761 
763 {
765  public:
767 
768  virtual PBoolean Close();
769  virtual PBoolean Write(const void * buf, PINDEX len);
770  virtual PBoolean Read(void * buf, PINDEX len);
771 
772  virtual PBoolean SetFormat(unsigned numChannels, unsigned sampleRate, unsigned bitsPerSample);
773  virtual unsigned GetChannels() const;
774  virtual unsigned GetSampleRate() const;
775  virtual unsigned GetSampleSize() const;
776  virtual PBoolean SetBuffers(PINDEX size, PINDEX);
777  virtual PBoolean GetBuffers(PINDEX & size, PINDEX & count);
778 
779  virtual PBoolean HasPlayCompleted();
781  virtual PBoolean StartRecording();
782  virtual PBoolean IsRecordBufferFull();
786 
787  virtual PBoolean SetVolume(unsigned volume);
788  virtual PBoolean GetVolume(unsigned & volume);
789  virtual bool SetMute(bool mute);
790  virtual bool GetMute(bool & mute);
791 
792  protected:
793  virtual bool RawWrite(const void * data, PINDEX size);
794  virtual bool RawRead(void * data, PINDEX size);
795  virtual bool Rewind();
796  bool InternalFlush();
797 
798  unsigned m_sampleRate;
799  unsigned m_channels;
801  unsigned m_bufferSize;
802  unsigned m_bufferCount;
803  PINDEX m_bufferPos;
807  bool m_muted;
808  PDECLARE_MUTEX(m_writeMutex);
809 };
810 
811 
813 
814 // define the sound plugin service descriptor
815 
817 
818 #define PCREATE_SOUND_PLUGIN_EX(name, InstanceClass, extra) \
819  PCREATE_PLUGIN(name, PSoundChannel, InstanceClass, PPlugin_PSoundChannel, \
820  virtual PStringArray GetDeviceNames(P_INT_PTR userData) const { return InstanceClass::GetDeviceNames((PSoundChannel::Directions)userData); } \
821  extra)
822 
823 #define PCREATE_SOUND_PLUGIN(name, InstanceClass) PCREATE_SOUND_PLUGIN_EX(name, InstanceClass, )
824 
825 
826 #define P_NULL_AUDIO_DEVICE "NullAudio"
828 
829 #ifdef _WIN32
830  #define P_WINDOWS_MULTIMEDIA_DRIVER "WindowsMultimedia"
831  PPLUGIN_STATIC_LOAD(WindowsMultimedia, PSoundChannel);
832 #elif defined(P_ANDROID)
834 #elif defined(P_MACOSX) || defined(P_IOS)
836 #elif defined(__BEOS__)
838 #endif
839 
840 #if defined(P_DIRECTSOUND)
841  #define P_DIRECT_SOUND_DRIVER "DirectSound"
842  PPLUGIN_STATIC_LOAD(DirectSound, PSoundChannel);
843 #endif
844 
845 #if defined(P_WAVFILE)
846  #define P_WAV_FILE_DRIVER "WAVFile"
848 #endif
849 
850 #if P_DTMF
852 #endif
853 
854 
855 #endif // PTLIB_SOUND_H
856 
857 
858 // End Of File ///////////////////////////////////////////////////////////////
Directions GetDirection() const
Get the direction of the channel.
Definition: sound.h:450
virtual bool PlayFile(const PFilePath &file, bool wait=true, unsigned volume=100)
Play a sound file to the open device.
unsigned m_sampleRate
Definition: sound.h:798
Abstract class for a generalised sound channel, and an implementation of PSoundChannel for old code t...
Definition: sound.h:267
PSound(unsigned numChannels=1, unsigned sampleRate=8000, unsigned bitsPerSample=16, PINDEX bufferSize=0, const BYTE *data=NULL)
Create a new sound, using the parameters provided.
virtual bool RawWrite(const void *data, PINDEX size)
virtual PBoolean Read(void *buf, PINDEX len)
Low level read from the channel.
virtual PBoolean WaitForPlayCompletion()
Block calling thread until the sound play begun with PlaySound() or PlayFile() has completed...
bool m_autoRepeat
Definition: sound.h:805
friend ostream & operator<<(ostream &, const Params &params)
unsigned GetChannels() const
Get the number of channels (mono/stereo) in the sound.
Definition: sound.h:136
Definition: sound.h:524
Class specialisation for PNotifierTemplate&lt;P_INT_PTR&gt;
PString m_device
Name of sound device.
Definition: sound.h:303
unsigned GetSampleSize() const
Get the sample size in bits per sample.
Definition: sound.h:142
static void Beep()
Play the &quot;standard&quot; warning beep for the platform.
virtual PBoolean GetVolume(unsigned &volume)
Get the volume of the play/read process.
const void * GetFormatInfoData() const
Get pointer to the platform dependent format info.
Definition: sound.h:151
unsigned m_bufferSize
Size of the internal buffer in bytes, typically the smallest amount of data channel can read or writt...
Definition: sound.h:308
static PStringArray GetDriversDeviceNames(const PString &driverName, Directions direction, PPluginManager *pluginMgr=NULL)
Get sound devices that correspond to the specified driver name.
static PString TestRecorder(const Params &recorderParams, const Params &playerParams, const PNotifier &progress=PNotifier(), unsigned seconds=5)
Test the specified device for playing.
virtual PBoolean SetVolume(unsigned volume)
Set the volume of the play/read process.
virtual bool Open(const Params &params)
Open the specified device for playing or recording.
PSound & operator=(const PBYTEArray &data)
Set new data bytes for the sound.
virtual unsigned GetSampleSize() const
Get the sample size in bits per sample.
virtual PBoolean AreAllRecordBuffersFull()
Determine if all of the record buffer allocated has been filled.
virtual PBoolean HasPlayCompleted()
Indicate if the sound play begun with PlayBuffer() or PlayFile() has completed.
virtual PBoolean StartRecording()
Start filling record buffers.
This class describes a full description for a file on the particular platform.
Definition: filepath.h:61
virtual PBoolean RecordFile(const PFilePath &file)
Record into the platform dependent sound file all of the buffer&#39;s of sound data.
Directions m_direction
Sound I/O direction.
Definition: sound.h:302
#define PPLUGIN_STATIC_LOAD(serviceName, serviceType)
Definition: plugin.h:123
virtual bool RawRead(void *data, PINDEX size)
void SetBufferCountFromMS(unsigned milliseconds)
virtual PBoolean WaitForPlayCompletion()
Block calling thread until the sound play begun with PlaySound() or PlayFile() has completed...
This is a channel that operates indirectly through another channel(s).
Definition: indchan.h:45
virtual PBoolean SetFormat(unsigned numChannels=1, unsigned sampleRate=8000, unsigned bitsPerSample=16)
Set the format for play/record.
static PSoundChannel * CreateChannelByName(const PString &deviceName, Directions direction, PPluginManager *pluginMgr=NULL)
virtual PBoolean Write(const void *buf, PINDEX len)
Low level write to the channel.
Definition: sound.h:762
PAdaptiveDelay m_Pacing
Definition: sound.h:806
virtual unsigned GetSampleSize() const
Get the sample size in bits per sample.
virtual PBoolean WaitForAllRecordBuffersFull()
Block the thread until all of the record buffer allocated has been filled.
virtual PBoolean GetVolume(unsigned &volume)
Get the volume of the play/read process.
This is an array collection class of PString objects.
Definition: pstring.h:2365
PChannel * readChannel
Channel for read operations.
Definition: indchan.h:362
virtual PBoolean StartRecording()
Start filling record buffers.
virtual PBoolean AreAllRecordBuffersFull()
Determine if all of the record buffer allocated has been filled.
static PStringArray GetDriverNames(PPluginManager *pluginMgr=NULL)
Get the list of available sound drivers (plug-ins)
PSoundChannel * GetSoundChannel() const
Definition: sound.h:748
virtual PBoolean Close()
Close the channel.
virtual PBoolean Abort()
Abort the background playing/recording of the sound channel.
DWORD GetErrorCode() const
Get the platform dependent error code from the last file load.
Definition: sound.h:145
unsigned m_bytesPerSample
Definition: sound.h:800
unsigned m_channels
Number of channels eg mono/stereo.
Definition: sound.h:305
#define PCREATE_PLUGIN_DEVICE(serviceType)
Definition: plugin.h:92
PBoolean Load(const PFilePath &filename)
Load a platform dependent sound file (eg .WAV file for Win32) into the object.
virtual PBoolean IsRecordBufferFull()
Determine if a record buffer has been filled, so that the next Read() call will not block...
virtual PBoolean WaitForRecordBufferFull()
Block the thread until a record buffer has been filled, so that the next Read() call will not block...
PINDEX GetFormatInfoSize() const
Get the size of the platform dependent format info.
Definition: sound.h:148
A class representing a sound.
Definition: sound.h:53
unsigned m_bufferCount
Definition: sound.h:802
Array of unsigned characters.
Definition: array.h:605
static PString TestPlayer(const Params &params, const PNotifier &progress=PNotifier(), const char *toneSpec=NULL)
Test the specified device for playing.
PBoolean Play()
Play the sound on the default sound device.
virtual PBoolean WaitForAllRecordBuffersFull()
Block the thread until all of the record buffer allocated has been filled.
virtual PBoolean SetBuffers(PINDEX size, PINDEX)
Set the internal buffers for the sound channel I/O.
virtual unsigned GetSampleRate() const
Get the sample rate in samples per second.
static PString GetDefaultDevice(Directions dir)
Get the name for the default sound devices/driver that is on this platform.
virtual PBoolean SetFormat(unsigned numChannels, unsigned sampleRate, unsigned bitsPerSample)
Set the format for play/record.
static PSoundChannel * CreateChannel(const PString &driverName, PPluginManager *pluginMgr=NULL)
Create the sound channel that corresponds to the specified driver name.
static bool PlayFile(const PFilePath &file, bool wait=true, unsigned volume=100)
Play a sound file to the default device.
P_REMOVE_VIRTUAL(PBoolean, Open(const PString &, Directions, unsigned, unsigned, unsigned), false)
virtual PINDEX GetSize() const
Get the current size of the container.
bool PBoolean
Definition: object.h:174
bool m_muted
Definition: sound.h:807
PDECLARE_MUTEX(m_writeMutex)
PNotifierTemplate< P_INT_PTR > PNotifier
Definition: notifier.h:145
The character string class.
Definition: pstring.h:108
DWORD dwLastError
Last error code for Load()/Save() functions.
Definition: sound.h:198
virtual PBoolean GetBuffers(PINDEX &size, PINDEX &count)
Get the internal buffers for the sound channel I/O.
Class for implementing an &quot;adaptive&quot; delay.
Definition: delaychan.h:48
unsigned m_channels
Definition: sound.h:799
Directions m_activeDirection
This is the direction that this sound channel is opened for use in.
Definition: sound.h:754
void SetFormat(unsigned numChannels, unsigned sampleRate, unsigned bitsPerSample)
Set the internal sound format to linear PCM at the specification in the parameters.
virtual unsigned GetChannels() const
Get the number of channels (mono/stereo) in the sound.
PBoolean Save(const PFilePath &filename)
Save a platform dependent sound file (eg .WAV file for Win32) from the object.
virtual bool GetMute(bool &mute)
Get the mute state of the play/read process.
virtual PBoolean HasPlayCompleted()
Indicate if the sound play begun with PlayBuffer() or PlayFile() has completed.
PPluginManager * m_pluginMgr
Definition: sound.h:312
virtual PBoolean GetBuffers(PINDEX &size, PINDEX &count)
Get the internal buffers for the sound channel I/O.
unsigned m_sampleRate
Samples per second.
Definition: sound.h:306
unsigned GetEncoding() const
Get the current encoding.
Definition: sound.h:133
static const PString & Empty()
Return an empty string.
unsigned sampleSize
Number of bits per sample.
Definition: sound.h:196
virtual PBoolean WaitForRecordBufferFull()
Block the thread until a record buffer has been filled, so that the next Read() call will not block...
virtual PBoolean IsRecordBufferFull()
Determine if a record buffer has been filled, so that the next Read() call will not block...
static bool ConvertPCM(const short *srcPtr, PINDEX &srcSize, unsigned srcRate, unsigned srcChannels, short *dstPtr, PINDEX &dstSize, unsigned dstRate, unsigned dstChannels)
Convert PCM data sample rates and channel depth.
PBYTEArray formatInfo
Full info on the format (platform dependent)
Definition: sound.h:200
virtual bool SetMute(bool mute)
Set the mute state of the play/read process.
virtual unsigned GetChannels() const
Get the number of channels (mono/stereo) in the sound.
Params(Directions dir=Player, const PString &device=PString::Empty(), const PString &driver=PString::Empty(), unsigned channels=1, unsigned sampleRate=8000, unsigned bitsPerSample=16, unsigned bufferSize=DefaultBufferSize, unsigned bufferCount=DefaultBufferCount, PPluginManager *pluginMgr=NULL)
static PStringArray GetDeviceNames(const PString &driverName, Directions direction, PPluginManager *pluginMgr=NULL)
Definition: sound.h:354
virtual bool Rewind()
unsigned sampleRate
Samples per second.
Definition: sound.h:194
virtual const char * GetDirectionText() const
Get text representing the direction of the channel.
Definition: sound.h:459
unsigned m_bufferCount
Number of buffers to queue.
Definition: sound.h:311
virtual PBoolean PlaySound(const PSound &sound, PBoolean wait=true)
Play a sound to the open device.
virtual bool GetMute(bool &mute)
Get the mute state of the play/read process.
Definition: sound.h:284
virtual unsigned GetSampleRate() const
Get the sample rate in samples per second.
PINDEX m_bufferPos
Definition: sound.h:803
PSoundChannel()
Create a sound channel.
virtual bool SetMute(bool mute)
Set the mute state of the play/read process.
Definition: sound.h:285
Parameters for opening a sound channel.
Definition: sound.h:289
unsigned numChannels
Number of channels eg mono/stereo.
Definition: sound.h:192
unsigned m_bitsPerSample
Number of bits per sample.
Definition: sound.h:307
virtual PBoolean RecordSound(PSound &sound)
Record into the sound object all of the buffer&#39;s of sound data.
virtual PBoolean SetBuffers(PINDEX size, PINDEX count=2)
Set the internal buffers for the sound channel I/O.
P_DECLARE_STREAMABLE_ENUM(Directions, Recorder, Player, Closed)
PString m_driver
Name of sound driver.
Definition: sound.h:304
unsigned encoding
Format code.
Definition: sound.h:190
unsigned GetSampleRate() const
Get the sample rate in samples per second.
Definition: sound.h:139
static PSoundChannel * CreateOpenedChannel(const Params &params)
Create an opened sound channel that corresponds to the specified names.
virtual PBoolean SetVolume(unsigned volume)
Set the volume of the play/read process.
PBYTEArray m_buffer
Definition: sound.h:804
unsigned m_bufferSize
Definition: sound.h:801