sound.h
Go to the documentation of this file.
/*
* sound.h
*
* Sound interface class.
*
* Portable Windows Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Revision: 28275 $
* $Author: rjongbloed $
* $Date: 2012-08-29 21:42:35 -0500 (Wed, 29 Aug 2012) $
*/
#ifndef PTLIB_SOUND_H
#define PTLIB_SOUND_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include <ptlib/plugin.h>
#include <ptlib/pluginmgr.h>
class PSound : public PBYTEArray
{
PCLASSINFO(PSound, PBYTEArray);
public:
PSound(
unsigned numChannels = 1,
unsigned sampleRate = 8000,
unsigned bitsPerSample = 16,
PINDEX bufferSize = 0,
const BYTE * data = NULL
);
PSound(
const PFilePath & filename
);
PSound & operator=(
const PBYTEArray & data
);
PBoolean Load(
const PFilePath & filename
);
PBoolean Save(
const PFilePath & filename
);
PBoolean Play();
PBoolean Play(const PString & device);
void SetFormat(
unsigned numChannels,
unsigned sampleRate,
unsigned bitsPerSample
);
unsigned GetEncoding() const { return encoding; }
unsigned GetChannels() const { return numChannels; }
unsigned GetSampleRate() const { return sampleRate; }
unsigned GetSampleSize() const { return sampleSize; }
DWORD GetErrorCode() const { return dwLastError; }
PINDEX GetFormatInfoSize() const { return formatInfo.GetSize(); }
const void * GetFormatInfoData() const { return (const BYTE *)formatInfo; }
static PBoolean PlayFile(
const PFilePath & file,
PBoolean wait = true
);
static void Beep();
protected:
unsigned encoding;
unsigned numChannels;
unsigned sampleRate;
unsigned sampleSize;
DWORD dwLastError;
PBYTEArray formatInfo;
};
class PSoundChannel : public PChannel
{
PCLASSINFO(PSoundChannel, PChannel);
public:
enum Directions {
Closed = -1,
Recorder,
Player
};
PSoundChannel();
PSoundChannel(
const PString & device,
Directions dir,
unsigned numChannels = 1,
unsigned sampleRate = 8000,
unsigned bitsPerSample = 16
);
//
virtual ~PSoundChannel();
// Destroy and close the sound driver
static PStringArray GetDriverNames(
PPluginManager * pluginMgr = NULL
);
static PStringArray GetDriversDeviceNames(
const PString & driverName,
Directions direction,
PPluginManager * pluginMgr = NULL
);
// For backward compatibility
static inline PStringArray GetDeviceNames(
const PString & driverName,
Directions direction,
PPluginManager * pluginMgr = NULL
) { return GetDriversDeviceNames(driverName, direction, pluginMgr); }
static PSoundChannel * CreateChannel (
const PString & driverName,
PPluginManager * pluginMgr = NULL
);
/* Create the matching sound channel that corresponds to the device name.
So, for "fake" return a device that will generate fake video.
For "Phillips 680 webcam" (eg) will return appropriate grabber.
Note that Phillips will return the appropriate grabber also.
This is typically used with the return values from GetDeviceNames().
*/
static PSoundChannel * CreateChannelByName(
const PString & deviceName,
Directions direction,
PPluginManager * pluginMgr = NULL
);
static PSoundChannel * CreateOpenedChannel(
const PString & driverName,
const PString & deviceName,
Directions direction,
unsigned numChannels = 1,
unsigned sampleRate = 8000,
unsigned bitsPerSample = 16,
PPluginManager * pluginMgr = NULL
);
static PString GetDefaultDevice(
Directions dir // Sound I/O direction
);
static PStringArray GetDeviceNames(
Directions direction,
PPluginManager * pluginMgr = NULL
);
virtual PBoolean Open(
const PString & device,
Directions dir,
unsigned numChannels = 1,
unsigned sampleRate = 8000,
unsigned bitsPerSample = 16
);
virtual PBoolean IsOpen() const;
virtual PBoolean Close();
virtual int GetHandle() const;
virtual PString GetName() const;
Directions GetDirection() const
{
return activeDirection;
}
static const char * GetDirectionText(Directions dir);
virtual const char * GetDirectionText() const
{
return GetDirectionText(activeDirection);
}
virtual PBoolean Abort();
virtual PBoolean SetFormat(
unsigned numChannels = 1,
unsigned sampleRate = 8000,
unsigned bitsPerSample = 16
);
virtual unsigned GetChannels() const;
virtual unsigned GetSampleRate() const;
virtual unsigned GetSampleSize() const;
virtual PBoolean SetBuffers(
PINDEX size,
PINDEX count = 2
);
virtual PBoolean GetBuffers(
PINDEX & size, // Size of each buffer
PINDEX & count // Number of buffers
);
enum {
MaxVolume = 100
};
virtual PBoolean SetVolume(
unsigned volume
);
virtual PBoolean GetVolume(
unsigned & volume
);
virtual bool SetMute(
bool mute
);
virtual bool GetMute(
bool & mute
);
virtual PBoolean Write(const void * buf, PINDEX len);
virtual PBoolean Write(
const void * buf,
PINDEX len,
const void * mark
);
virtual PINDEX GetLastWriteCount() const;
virtual PBoolean PlaySound(
const PSound & sound,
PBoolean wait = true
);
virtual PBoolean PlayFile(
const PFilePath & file,
PBoolean wait = true
);
virtual PBoolean HasPlayCompleted();
virtual PBoolean WaitForPlayCompletion();
virtual PBoolean Read(
void * buf,
PINDEX len
);
PINDEX GetLastReadCount() const;
virtual PBoolean RecordSound(
PSound & sound
);
virtual PBoolean RecordFile(
const PFilePath & file
);
virtual PBoolean StartRecording();
virtual PBoolean IsRecordBufferFull();
virtual PBoolean AreAllRecordBuffersFull();
virtual PBoolean WaitForRecordBufferFull();
virtual PBoolean WaitForAllRecordBuffersFull();
protected:
PSoundChannel * m_baseChannel;
PReadWriteMutex m_baseMutex;
Directions activeDirection;
};
// define the sound plugin service descriptor
template <class className> class PSoundChannelPluginServiceDescriptor : public PDevicePluginServiceDescriptor
{
public:
virtual PObject * CreateInstance(int /*userData*/) const { return new className; }
virtual PStringArray GetDeviceNames(int userData) const { return className::GetDeviceNames((PSoundChannel::Directions)userData); }
};
#define PCREATE_SOUND_PLUGIN(name, className) \
static PSoundChannelPluginServiceDescriptor<className> className##_descriptor; \
PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)
#ifdef _WIN32
PPLUGIN_STATIC_LOAD(WindowsMultimedia, PSoundChannel);
#elif defined(__BEOS__)
PPLUGIN_STATIC_LOAD(BeOS, PSoundChannel);
#endif
#if defined(P_DIRECTSOUND)
PPLUGIN_STATIC_LOAD(DirectSound, PSoundChannel);
#endif
#if defined(P_WAVFILE)
PPLUGIN_STATIC_LOAD(WAVFile, PSoundChannel)
#endif
#endif // PTLIB_SOUND_H
// End Of File ///////////////////////////////////////////////////////////////
PSound::GetErrorCode
DWORD GetErrorCode() const
Get the platform dependent error code from the last file load.
Definition: sound.h:144
PSoundChannel
Abstract class for a generalised sound channel, and an implementation of PSoundChannel for old code t...
Definition: sound.h:251
PSound::PSound
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.
PSoundChannel::Directions
Directions
Definition: sound.h:258
PSound::PlayFile
static PBoolean PlayFile(const PFilePath &file, PBoolean wait=true)
Play a sound file to the default device.
PSoundChannelPluginServiceDescriptor
Definition: sound.h:784
PSound::GetSampleRate
unsigned GetSampleRate() const
Get the sample rate in samples per second.
Definition: sound.h:138
pluginmgr.h
PCLASSINFO
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PSoundChannel::activeDirection
Directions activeDirection
This is the direction that this sound channel is opened for use in.
Definition: sound.h:776
PSound::Beep
static void Beep()
Play the "standard" warning beep for the platform.
PDevicePluginServiceDescriptor
Definition: plugin.h:109
PPluginManager
Definition: pluginmgr.h:57
PSound::operator=
PSound & operator=(const PBYTEArray &data)
Set new data bytes for the sound.
PSoundChannel::m_baseChannel
PSoundChannel * m_baseChannel
Definition: sound.h:769
plugin.h
PFilePath
This class describes a full description for a file on the particular platform.
Definition: filepath.h:65
PSound::GetSampleSize
unsigned GetSampleSize() const
Get the sample size in bits per sample.
Definition: sound.h:141
PSoundChannelPluginServiceDescriptor::CreateInstance
virtual PObject * CreateInstance(int) const
Definition: sound.h:787
PStringArray
This is an array collection class of PString objects.
Definition: pstring.h:2024
PSoundChannel::Recorder
Definition: sound.h:260
PBoolean
BOOL PBoolean
Definition: object.h:102
PSound::Load
PBoolean Load(const PFilePath &filename)
Load a platform dependent sound file (eg .WAV file for Win32) into the object.
PSound::GetFormatInfoSize
PINDEX GetFormatInfoSize() const
Get the size of the platform dependent format info.
Definition: sound.h:147
PSound
A class representing a sound.
Definition: sound.h:52
PSound::GetEncoding
unsigned GetEncoding() const
Get the current encoding.
Definition: sound.h:132
PBYTEArray
Array of unsigned characters.
Definition: array.h:670
PSound::Play
PBoolean Play()
Play the sound on the default sound device.
PSound::GetChannels
unsigned GetChannels() const
Get the number of channels (mono/stereo) in the sound.
Definition: sound.h:135
PPLUGIN_STATIC_LOAD
PPLUGIN_STATIC_LOAD(FakeVideo, PVideoInputDevice)
PChannel
Abstract class defining I/O channel semantics.
Definition: channel.h:107
PString
The character string class.
Definition: pstring.h:108
PSound::dwLastError
DWORD dwLastError
Last error code for Load()/Save() functions.
Definition: sound.h:182
PSound::SetFormat
void SetFormat(unsigned numChannels, unsigned sampleRate, unsigned bitsPerSample)
Set the internal sound format to linear PCM at the specification in the parameters.
PSound::Save
PBoolean Save(const PFilePath &filename)
Save a platform dependent sound file (eg .WAV file for Win32) from the object.
PSound::sampleSize
unsigned sampleSize
Number of bits per sample.
Definition: sound.h:180
PSoundChannel::m_baseMutex
PReadWriteMutex m_baseMutex
Definition: sound.h:770
PSound::formatInfo
PBYTEArray formatInfo
Full info on the format (platform dependent)
Definition: sound.h:184
PSoundChannel::GetDeviceNames
static PStringArray GetDeviceNames(const PString &driverName, Directions direction, PPluginManager *pluginMgr=NULL)
Definition: sound.h:302
PSound::sampleRate
unsigned sampleRate
Samples per second.
Definition: sound.h:178
PSoundChannel::GetDirectionText
virtual const char * GetDirectionText() const
Definition: sound.h:418
PContainer::GetSize
virtual PINDEX GetSize() const
Get the current size of the container.
PReadWriteMutex
This class defines a thread synchronisation object.
Definition: syncthrd.h:251
PSound::numChannels
unsigned numChannels
Number of channels eg mono/stereo.
Definition: sound.h:176
PSoundChannelPluginServiceDescriptor::GetDeviceNames
virtual PStringArray GetDeviceNames(int userData) const
Definition: sound.h:788
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PSound::GetFormatInfoData
const void * GetFormatInfoData() const
Get pointer to the platform dependent format info.
Definition: sound.h:150
PSound::encoding
unsigned encoding
Format code.
Definition: sound.h:174
PSoundChannel::GetDirection
Directions GetDirection() const
Get the direction of the channel.
Definition: sound.h:410