📄 sound.h
字号:
@return A platform dependent string for the sound player/recorder. */ static PString GetDefaultDevice( Directions dir // Sound I/O direction ); /**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. @return A platform dependent string for the sound player/recorder. */ static PStringList GetDeviceNames( Directions dir // Sound I/O direction ); /**Open the specified device for playing or recording. The device name is platform specific and is as returned in the GetDevices() function. @return TRUE if the sound device is valid for playing/recording. */ virtual BOOL Open( const PString & device, /// Name of sound driver/device Directions dir, /// Sound I/O direction unsigned numChannels = 1, /// Number of channels eg mono/stereo unsigned sampleRate = 8000, /// Samples per second unsigned bitsPerSample = 16 /// Number of bits per sample ); /**Test if this instance of PSoundChannel is open. @return TRUE if this instance is open. */ virtual BOOL IsOpen() const { return (baseChannel == NULL) ? FALSE : baseChannel->PChannel::IsOpen(); } /**Get the OS specific handle for the PSoundChannel. @return integer value of the handle. */ virtual int GetHandle() const { return (baseChannel == NULL) ? -1 : baseChannel->PChannel::GetHandle(); } /**Abort the background playing/recording of the sound channel. @return TRUE if the sound has successfully been aborted. */ virtual BOOL Abort() { return (baseChannel == NULL) ? FALSE : baseChannel->Abort(); } //@} /**@name Channel set up functions */ //@{ /**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. @return TRUE if the format is valid. */ virtual BOOL SetFormat( unsigned numChannels = 1, /// Number of channels eg mono/stereo unsigned sampleRate = 8000, /// Samples per second unsigned bitsPerSample = 16 /// Number of bits per sample ) { return (baseChannel == NULL) ? FALSE : baseChannel->SetFormat(numChannels, sampleRate, bitsPerSample); } /// Get the number of channels (mono/stereo) in the sound. virtual unsigned GetChannels() const { return (baseChannel == NULL) ? 0 : baseChannel->GetChannels(); } /// Get the sample rate in samples per second. virtual unsigned GetSampleRate() const { return (baseChannel == NULL) ? 0 : baseChannel->GetSampleRate(); } /// Get the sample size in bits per sample. virtual unsigned GetSampleSize() const { return (baseChannel == NULL) ? 0 : baseChannel->GetSampleSize(); } /**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. @return TRUE if the sound device is valid for playing/recording. */ virtual BOOL SetBuffers( PINDEX size, /// Size of each buffer PINDEX count = 2 /// Number of buffers ) { return (baseChannel == NULL) ? FALSE : baseChannel->SetBuffers(size, count); } /**Get the internal buffers for the sound channel I/O. @return TRUE if the buffer size were obtained. */ virtual BOOL GetBuffers( PINDEX & size, // Size of each buffer PINDEX & count // Number of buffers ) { return (baseChannel == NULL) ? FALSE : baseChannel->GetBuffers(size, count); } enum { MaxVolume = 100 }; /**Set the volume of the play/read process. The volume range is 0 == quiet. 100 == LOUD. @return TRUE if there were no errors. */ virtual BOOL SetVolume( unsigned volume /// New volume level ) { return (baseChannel == NULL) ? FALSE : baseChannel->SetVolume(volume); } /**Get the volume of the play/read process. The volume range is 0 == quiet. 100 == LOUD. @return TRUE if there were no errors. */ virtual BOOL GetVolume( unsigned & volume /// Variable to receive volume level. ) { return (baseChannel == NULL) ? FALSE : baseChannel->GetVolume(volume); } //@} /**@name Play functions */ //@{ /** 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. @return TRUE if at least len bytes were written to the channel. */ virtual BOOL Write(const void * buf, PINDEX len) { return (baseChannel == NULL) ? FALSE : baseChannel->Write(buf, len); } PINDEX GetLastWriteCount() const { return (baseChannel == NULL) ? lastWriteCount : baseChannel->GetLastWriteCount(); } /**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. @return TRUE if the sound is playing or has played. */ virtual BOOL PlaySound( const PSound & sound, /// Sound to play. BOOL wait = TRUE /// Flag to play sound synchronously. ) { return (baseChannel == NULL) ? FALSE : baseChannel->PlaySound(sound, wait); } /**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. @return TRUE if the sound is playing or has played. */ virtual BOOL PlayFile( const PFilePath & file, /// Sound file to play. BOOL wait = TRUE /// Flag to play sound synchronously. ) { return (baseChannel == NULL) ? FALSE : baseChannel->PlayFile(file, wait); } /**Indicate if the sound play begun with PlayBuffer() or PlayFile() has completed. @return TRUE if the sound has completed playing. */ virtual BOOL HasPlayCompleted() { return (baseChannel == NULL) ? FALSE : baseChannel->HasPlayCompleted(); } /**Block the thread until the sound play begun with PlayBuffer() or PlayFile() has completed. @return TRUE if the sound has successfully completed playing. */ virtual BOOL WaitForPlayCompletion() { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForPlayCompletion(); } //@} /**@name Record functions */ //@{ /** 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. @return 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. */ virtual BOOL Read( void * buf, /// Pointer to a block of memory to receive the read bytes. PINDEX len /// Maximum number of bytes to read into the buffer. ) { return (baseChannel == NULL) ? FALSE : baseChannel->Read(buf, len); } PINDEX GetLastReadCount() const { return (baseChannel == NULL) ? lastReadCount : baseChannel->GetLastReadCount(); } /**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. @return TRUE if the sound has been recorded. */ virtual BOOL RecordSound( PSound & sound /// Sound recorded ) { return (baseChannel == NULL) ? FALSE : baseChannel->RecordSound(sound); } /**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. @return TRUE if the sound has been recorded. */ virtual BOOL RecordFile( const PFilePath & file /// Sound file recorded ) { return (baseChannel == NULL) ? FALSE : baseChannel->RecordFile(file); } /**Start filling record buffers. The first call to Read() will also initiate the recording. @return TRUE if the sound driver has successfully started recording. */ virtual BOOL StartRecording() { return (baseChannel == NULL) ? FALSE : baseChannel->StartRecording(); } /**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. @return TRUE if the sound driver has filled a buffer. */ virtual BOOL IsRecordBufferFull() { return (baseChannel == NULL) ? FALSE : baseChannel->IsRecordBufferFull(); } /**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. @return TRUE if the sound driver has filled a buffer. */ virtual BOOL AreAllRecordBuffersFull() { return (baseChannel == NULL) ? FALSE : baseChannel->AreAllRecordBuffersFull(); } /**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. @return TRUE if the sound driver has filled a buffer. */ virtual BOOL WaitForRecordBufferFull() { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForRecordBufferFull() ; } /**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. @return TRUE if the sound driver has filled a buffer. */ virtual BOOL WaitForAllRecordBuffersFull() { return (baseChannel == NULL) ? FALSE : baseChannel->WaitForAllRecordBuffersFull() ; } //@} protected: PSoundChannel * baseChannel;};/////////////////////////////////////////////////////////////////////////// define the sound plugin service descriptorclass PSoundChannelPluginServiceDescriptor : public PPluginServiceDescriptor { public: PSoundChannelPluginServiceDescriptor( unsigned (*_GetVersion)(), PSoundChannel *(*_CreateInstance)(), PStringArray (*_GetDeviceNames)(PSoundChannel::Directions) ) : PPluginServiceDescriptor(_GetVersion), CreateInstance(_CreateInstance), GetDeviceNames(_GetDeviceNames) { } PSoundChannel *(*CreateInstance)(); PStringArray (*GetDeviceNames) (PSoundChannel::Directions);};#define PCREATE_SOUND_SERVICE_DESCRIPTOR(className, versionFn) \PSoundChannel * className##_CreateInstance () \{ \ return new className; \} \\PStringArray className##_GetDeviceNames (PSoundChannel::Directions dir) \{ \ return className::GetDeviceNames(dir); \} \\PSoundChannelPluginServiceDescriptor className##_descriptor(\ versionFn, \ className##_CreateInstance, \ className##_GetDeviceNames \); \#define PCREATE_SOUND_PLUGIN(name, className) \PCREATE_PLUGIN_VERSION_FN(name, PSoundChannel) \PCREATE_SOUND_SERVICE_DESCRIPTOR(className, PPLUGIN_VERSION_FN(name, PSoundChannel)) \PCREATE_PLUGIN(name, PSoundChannel, &className##_descriptor)#endif// End Of File ///////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -