Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

PSoundChannel Class Reference

#include <sound.h>

Inheritance diagram for PSoundChannel:

PChannel PObject List of all members.

Construction

enum  Directions { Recorder, Player }
 PSoundChannel ()
 Create a sound channel.
 PSoundChannel (const PString &device, Directions dir, unsigned numChannels=1, unsigned sampleRate=8000, unsigned bitsPerSample=16)
virtual ~PSoundChannel ()

Channel set up functions

enum  { MaxVolume = 100 }
virtual BOOL SetFormat (unsigned numChannels=1, unsigned sampleRate=8000, unsigned bitsPerSample=16)
virtual unsigned GetChannels () const
 Get the number of channels (mono/stereo) in the sound.
virtual unsigned GetSampleRate () const
 Get the sample rate in samples per second.
virtual unsigned GetSampleSize () const
 Get the sample size in bits per sample.
virtual BOOL SetBuffers (PINDEX size, PINDEX count=2)
virtual BOOL GetBuffers (PINDEX &size, PINDEX &count)
virtual BOOL SetVolume (unsigned volume)
virtual BOOL GetVolume (unsigned &volume)

Open functions

virtual BOOL Open (const PString &device, Directions dir, unsigned numChannels=1, unsigned sampleRate=8000, unsigned bitsPerSample=16)
virtual BOOL IsOpen () const
virtual int GetHandle () const
virtual BOOL Abort ()
static PStringList GetDriverNames (PPluginManager *pluginMgr=NULL)
static PStringList GetDeviceNames (const PString &driverName, const PSoundChannel::Directions, PPluginManager *pluginMgr=NULL)
static PSoundChannelCreateChannel (const PString &driverName, PPluginManager *pluginMgr=NULL)
static PSoundChannelCreateOpenedChannel (const PString &driverName, const PString &deviceName, const PSoundChannel::Directions, unsigned numChannels=1, unsigned sampleRate=8000, unsigned bitsPerSample=16)
static PString GetDefaultDevice (Directions dir)
static PStringList GetDeviceNames (Directions dir)

Public Member Functions

Play functions
virtual BOOL Write (const void *buf, PINDEX len)
PINDEX GetLastWriteCount () const
virtual BOOL PlaySound (const PSound &sound, BOOL wait=TRUE)
virtual BOOL PlayFile (const PFilePath &file, BOOL wait=TRUE)
virtual BOOL HasPlayCompleted ()
virtual BOOL WaitForPlayCompletion ()
Record functions
virtual BOOL Read (void *buf, PINDEX len)
PINDEX GetLastReadCount () const
virtual BOOL RecordSound (PSound &sound)
virtual BOOL RecordFile (const PFilePath &file)
virtual BOOL StartRecording ()
virtual BOOL IsRecordBufferFull ()
virtual BOOL AreAllRecordBuffersFull ()
virtual BOOL WaitForRecordBufferFull ()
virtual BOOL WaitForAllRecordBuffersFull ()

Protected Attributes

PSoundChannelbaseChannel

Detailed Description

This class is both an abstract class for a generalised sound channel, and an implementation of PSoundChannel for old code that is not plugin-aware. When instantiated, it selects the first plugin of the base class "PSoundChannel"

As an abstract class, this represents a sound schannel. Drivers for real, platform dependent sound hardware will be ancestors of this class and can be found in the plugins section of PWLib.

A sound driver is either playing or recording. If simultaneous playing and recording is desired, two instances of PSoundChannel must be created.

The sound is buffered and the size and number of buffers should be set before playing/recording. Each call to Write() will use one buffer, so care needs to be taken not to use a large number of small writes but tailor the buffers to the size of each write you make.

Similarly for reading, an entire buffer must be read before any of it is available to a Read() call. Note that once a buffer is filled you can read it a byte at a time if desired, but as soon as all the data in the buffer is used returned, the next read will wait until the entire next buffer is read from the hardware. So again, tailor the number and size of buffers to the application. To avoid being blocked until the buffer fills, you can use the StartRecording() function to initiate the buffer filling, and the IsRecordingBufferFull() function to determine when the Read() function will no longer block.

Note that this sound channel is implicitly a linear PCM channel. No data conversion is performed on data to/from the channel.


Member Enumeration Documentation

anonymous enum
 

Enumeration values:
MaxVolume 

enum PSoundChannel::Directions
 

Enumeration values:
Recorder 
Player 


Constructor & Destructor Documentation

PSoundChannel::PSoundChannel  ) 
 

Create a sound channel.

PSoundChannel::PSoundChannel const PString device,
Directions  dir,
unsigned  numChannels = 1,
unsigned  sampleRate = 8000,
unsigned  bitsPerSample = 16
 

Create a sound channel. Create a reference to the sound drivers for the platform.

Parameters:
dir  Name of sound driver/device
numChannels  Sound I/O direction
sampleRate  Number of channels eg mono/stereo
bitsPerSample  Samples per second Number of bits per sample

virtual PSoundChannel::~PSoundChannel  )  [virtual]
 


Member Function Documentation

virtual BOOL PSoundChannel::Abort  )  [inline, virtual]
 

Abort the background playing/recording of the sound channel.

Returns:
TRUE if the sound has successfully been aborted.

virtual BOOL PSoundChannel::AreAllRecordBuffersFull  )  [inline, virtual]
 

Determine if all of the record buffer allocated has been filled. There is an implicit Abort() of the recording if this occurs and recording is stopped. The channel may need to be closed and opened again to start a new recording.

Returns:
TRUE if the sound driver has filled a buffer.

static PSoundChannel* PSoundChannel::CreateChannel const PString driverName,
PPluginManager pluginMgr = NULL
[static]
 

Return sound channel object that correspond to the specified name

static PSoundChannel* PSoundChannel::CreateOpenedChannel const PString driverName,
const PString deviceName,
const PSoundChannel::Directions  ,
unsigned  numChannels = 1,
unsigned  sampleRate = 8000,
unsigned  bitsPerSample = 16
[static]
 

Return opened sound channel object that correspond to the specified name

virtual BOOL PSoundChannel::GetBuffers PINDEX &  size,
PINDEX &  count
[inline, virtual]
 

Get the internal buffers for the sound channel I/O.

Returns:
TRUE if the buffer size were obtained.

virtual unsigned PSoundChannel::GetChannels  )  const [inline, virtual]
 

Get the number of channels (mono/stereo) in the sound.

static PString PSoundChannel::GetDefaultDevice Directions  dir  )  [static]
 

Get the name for the default sound devices/driver that is on this platform. Note that a named device may not necessarily do both playing and recording so the arrays returned with the #dir# parameter in each value is not necessarily the same.

Returns:
A platform dependent string for the sound player/recorder.

static PStringList PSoundChannel::GetDeviceNames Directions  dir  )  [static]
 

Get the list of all devices name for the default sound devices/driver that is on this platform. Note that a named device may not necessarily do both playing and recording so the arrays returned with the #dir# parameter in each value is not necessarily the same.

Returns:
A platform dependent string for the sound player/recorder.

static PStringList PSoundChannel::GetDeviceNames const PString driverName,
const PSoundChannel::Directions  ,
PPluginManager pluginMgr = NULL
[static]
 

Return sound devices that correspond to the specified name

static PStringList PSoundChannel::GetDriverNames PPluginManager pluginMgr = NULL  )  [static]
 

Return names of all plugins that correspond to sound devices

virtual int PSoundChannel::GetHandle  )  const [inline, virtual]
 

Get the OS specific handle for the PSoundChannel.

Returns:
integer value of the handle.

Reimplemented from PChannel.

PINDEX PSoundChannel::GetLastReadCount  )  const [inline, virtual]
 

Get the number of bytes read by the last Read() call. This will be from 0 to the maximum number of bytes as passed to the Read() call.

Note that the number of bytes read may often be less than that asked for. Aside from the most common case of being at end of file, which the applications semantics may regard as an exception, there are some cases where this is normal. For example, if a PTextFile channel on the MSDOS platform is read from, then the translation of CR/LF pairs to
characters will result in the number of bytes returned being less than the size of the buffer supplied.

Returns:
the number of bytes read.

Reimplemented from PChannel.

PINDEX PSoundChannel::GetLastWriteCount  )  const [inline, virtual]
 

Get the number of bytes written by the last Write() call.

Note that the number of bytes written may often be less, or even more, than that asked for. A common case of it being less is where the disk is full. An example of where the bytes written is more is as follows. On a PTextFile# channel on the MSDOS platform, there is translation of
to CR/LF pairs. This will result in the number of bytes returned being more than that requested.

Returns:
the number of bytes written.

Reimplemented from PChannel.

virtual unsigned PSoundChannel::GetSampleRate  )  const [inline, virtual]
 

Get the sample rate in samples per second.

virtual unsigned PSoundChannel::GetSampleSize  )  const [inline, virtual]
 

Get the sample size in bits per sample.

virtual BOOL PSoundChannel::GetVolume unsigned &  volume  )  [inline, virtual]
 

Get the volume of the play/read process. The volume range is 0 == quiet. 100 == LOUD.

Returns:
TRUE if there were no errors.
Parameters:
volume  Variable to receive volume level.

virtual BOOL PSoundChannel::HasPlayCompleted  )  [inline, virtual]
 

Indicate if the sound play begun with PlayBuffer() or PlayFile() has completed.

Returns:
TRUE if the sound has completed playing.

virtual BOOL PSoundChannel::IsOpen  )  const [inline, virtual]
 

Test if this instance of PSoundChannel is open.

Returns:
TRUE if this instance is open.

Reimplemented from PChannel.

virtual BOOL PSoundChannel::IsRecordBufferFull  )  [inline, virtual]
 

Determine if a record buffer has been filled, so that the next Read() call will not block. Provided that the amount of data read is less than the buffer size.

Returns:
TRUE if the sound driver has filled a buffer.

virtual BOOL PSoundChannel::Open const PString device,
Directions  dir,
unsigned  numChannels = 1,
unsigned  sampleRate = 8000,
unsigned  bitsPerSample = 16
[virtual]
 

Open the specified device for playing or recording. The device name is platform specific and is as returned in the GetDevices() function.

Returns:
TRUE if the sound device is valid for playing/recording.
Parameters:
dir  Name of sound driver/device
numChannels  Sound I/O direction
sampleRate  Number of channels eg mono/stereo
bitsPerSample  Samples per second Number of bits per sample

virtual BOOL PSoundChannel::PlayFile const PFilePath file,
BOOL  wait = TRUE
[inline, virtual]
 

Play a sound file to the open device. If the #wait# parameter is TRUE then the function does not return until the file has been played. If FALSE then the sound play is begun asynchronously and the function returns immediately.

Note if the driver is closed of the object destroyed then the sound play is aborted.

Also note that not all possible sounds and sound files are playable by this library. No format conversions between sound object and driver are performed.

Returns:
TRUE if the sound is playing or has played.
Parameters:
wait  Sound file to play. Flag to play sound synchronously.

virtual BOOL PSoundChannel::PlaySound const PSound sound,
BOOL  wait = TRUE
[inline, virtual]
 

Play a sound to the open device. If the #wait# parameter is TRUE then the function does not return until the file has been played. If FALSE then the sound play is begun asynchronously and the function returns immediately.

Note if the driver is closed of the object destroyed then the sound play is aborted.

Also note that not all possible sounds and sound files are playable by this library. No format conversions between sound object and driver are performed.

Returns:
TRUE if the sound is playing or has played.
Parameters:
wait  Sound to play. Flag to play sound synchronously.

virtual BOOL PSoundChannel::Read void *  buf,
PINDEX  len
[inline, virtual]
 

Low level read from the channel. This function may block until the requested number of characters were read or the read timeout was reached. The GetLastReadCount() function returns the actual number of bytes read.

The GetErrorCode() function should be consulted after Read() returns FALSE to determine what caused the failure.

Returns:
TRUE indicates that at least one character was read from the channel. FALSE means no bytes were read due to timeout or some other I/O error.
Parameters:
len  Pointer to a block of memory to receive the read bytes. Maximum number of bytes to read into the buffer.

Reimplemented from PChannel.

virtual BOOL PSoundChannel::RecordFile const PFilePath file  )  [inline, virtual]
 

Record into the platform dependent sound file all of the buffer's of sound data. Use the SetBuffers() function to determine how long the recording will be made.

Note that this function will block until all of the data is buffered. If you wish to do this asynchronously, use StartRecording() and AreAllrecordBuffersFull() to determine when you can call RecordSound() without blocking.

Returns:
TRUE if the sound has been recorded.
Parameters:
file  Sound file recorded

virtual BOOL PSoundChannel::RecordSound PSound sound  )  [inline, virtual]
 

Record into the sound object all of the buffer's of sound data. Use the SetBuffers() function to determine how long the recording will be made.

For the Win32 platform, the most efficient way to record a PSound is to use the SetBuffers() function to set a single buffer of the desired size and then do the recording. For Linux OSS this can cause problems as the buffers are rounded up to a power of two, so to gain more accuracy you need a number of smaller buffers.

Note that this function will block until all of the data is buffered. If you wish to do this asynchronously, use StartRecording() and AreAllrecordBuffersFull() to determine when you can call RecordSound() without blocking.

Returns:
TRUE if the sound has been recorded.
Parameters:
sound  Sound recorded

virtual BOOL PSoundChannel::SetBuffers PINDEX  size,
PINDEX  count = 2
[inline, virtual]
 

Set the internal buffers for the sound channel I/O.

Note that with Linux OSS, the size is always rounded up to the nearest power of two, so 20000 => 32768.

Returns:
TRUE if the sound device is valid for playing/recording.
Parameters:
count  Size of each buffer Number of buffers

virtual BOOL PSoundChannel::SetFormat unsigned  numChannels = 1,
unsigned  sampleRate = 8000,
unsigned  bitsPerSample = 16
[inline, virtual]
 

Set the format for play/record. Note that linear PCM data is the only one supported at this time.

Note that if the PlayFile() function is used, this may be overridden by information in the file being played.

Returns:
TRUE if the format is valid.
Parameters:
sampleRate  Number of channels eg mono/stereo
bitsPerSample  Samples per second Number of bits per sample

virtual BOOL PSoundChannel::SetVolume unsigned  volume  )  [inline, virtual]
 

Set the volume of the play/read process. The volume range is 0 == quiet. 100 == LOUD.

Returns:
TRUE if there were no errors.
Parameters:
volume  New volume level

virtual BOOL PSoundChannel::StartRecording  )  [inline, virtual]
 

Start filling record buffers. The first call to Read() will also initiate the recording.

Returns:
TRUE if the sound driver has successfully started recording.

virtual BOOL PSoundChannel::WaitForAllRecordBuffersFull  )  [inline, virtual]
 

Block the thread until all of the record buffer allocated has been filled. There is an implicit Abort() of the recording if this occurs and recording is stopped. The channel may need to be closed and opened again to start a new recording.

Returns:
TRUE if the sound driver has filled a buffer.

virtual BOOL PSoundChannel::WaitForPlayCompletion  )  [inline, virtual]
 

Block the thread until the sound play begun with PlayBuffer() or PlayFile() has completed.

Returns:
TRUE if the sound has successfully completed playing.

virtual BOOL PSoundChannel::WaitForRecordBufferFull  )  [inline, virtual]
 

Block the thread until a record buffer has been filled, so that the next Read() call will not block. Provided that the amount of data read is less than the buffer size.

Returns:
TRUE if the sound driver has filled a buffer.

virtual BOOL PSoundChannel::Write const void *  buf,
PINDEX  len
[inline, virtual]
 

Low level write (or play) to the channel. This function will block until the requested number of characters are written or the write timeout is reached. The GetLastWriteCount() function returns the actual number of bytes written.

The GetErrorCode() function should be consulted after Write() returns FALSE to determine what caused the failure.

Returns:
TRUE if at least len bytes were written to the channel.

Reimplemented from PChannel.


Member Data Documentation

PSoundChannel* PSoundChannel::baseChannel [protected]
 


The documentation for this class was generated from the following file:
Generated on Mon Feb 21 20:43:16 2005 for PWLib by  doxygen 1.4.1