YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
yarp::sig::Sound Class Reference

Class for storing sounds See Audio in YARP for additional documentation on YARP audio. More...

#include <yarp/sig/Sound.h>

+ Inheritance diagram for yarp::sig::Sound:

Public Types

typedef short int audio_sample
 

Public Member Functions

 Sound (size_t bytesPerSample=2)
 
 Sound (const Sound &alt)
 Copy constructor.
 
virtual ~Sound ()
 
const Sound & operator= (const Sound &alt)
 Assignment operator.
 
bool operator== (const Sound &alt) const
 Comparison operator.
 
Sound & operator+= (const Sound &alt)
 Addition assignment operator.
 
void overwrite (const Sound &alt, size_t offset=0, size_t len=0)
 It overwrites the sound with another sound, starting from a specified offset.
 
Sound subSound (size_t first_sample, size_t len)
 Returns a subpart of the sound.
 
void clear ()
 
void resize (size_t samples, size_t channels=1)
 Set the sound size.
 
audio_sample get (size_t sample, size_t channel=0) const
 
void set (audio_sample value, size_t sample, size_t channel=0)
 
audio_sample getSafe (size_t sample, size_t channel=0) const
 
void setSafe (audio_sample value, size_t sample, size_t channel=0)
 
bool isSample (size_t sample, size_t channel=0) const
 Check whether a sample lies within the sound.
 
void zero ()
 set all the samples to zero (silence)
 
void normalizeChannel (size_t channel)
 Normalize a specific channel of the sound.
 
void normalize ()
 Normalize a sound (the peak is searched among all channels)
 
void amplifyChannel (size_t channel, double gain)
 Amplify a specific channel of the sound.
 
void amplify (double gain)
 amplify a sound
 
void findPeakInChannel (size_t channelId, size_t &sampleId, audio_sample &sampleValue) const
 find the peak in a specific channel of the sound
 
void findPeak (size_t &channelId, size_t &sampleId, audio_sample &sampleValue) const
 find the peak in a sound
 
double sample2timestamp (size_t sampleid) const
 Utility function: return the timestamp (in seconds) given the sample id.
 
size_t timestamp2sample (double time) const
 Utility function: return the sample id given the timestamp (in seconds)
 
bool zeroChannel (size_t channel)
 set to zero all the samples of the specified channel @ param channel the channel number
 
int getFrequency () const
 Get the frequency of the sound (i.e.
 
void setFrequency (int freq)
 Set the frequency of the sound (i.e.
 
size_t getBytesPerSample () const
 Get the number of bytes per sample.
 
size_t getSamples () const
 Get the number of samples contained in the sound.
 
double getDuration () const
 Get the duration of sound in seconds.
 
size_t getChannels () const
 Get the number of channels of the sound.
 
Sound extractChannelAsSound (size_t channel_id) const
 Extract a single channel from the sound.
 
std::vector< std::reference_wrapper< audio_sample > > getChannel (size_t channel_id)
 
bool replaceChannel (size_t id, Sound channel)
 Replace a single channel of our current sound with a given sound constituted by a single channel The two sounds must have the same number of samples.
 
std::vector< std::reference_wrapper< audio_sample > > getInterleavedAudioRawData () const
 Returns a serialized version of the sound, in interleaved format, e.g.
 
std::vector< std::reference_wrapper< audio_sample > > getNonInterleavedAudioRawData () const
 Returns a serialized version of the sound, in non-interleaved format, e.g.
 
std::string toString () const
 Print matrix to a string.
 
void add_marker (const std::string &marker_label, size_t sample_id, int channel=-1)
 Adds a marker to the sound, identified by a label, a sample id and an optional channel number (if not specified, the marker is valid for all channels).
 
bool get_marker (const std::string &marker_label, size_t &sample_id, int &channel) const
 Gets the sample id and channel number of a marker given its label.
 
size_t getMarkersCount () const
 Gets the number of markers contained in the sound.
 
yarp::sig::SoundMarker getMarker (size_t index) const
 Get the marker at the specified index.
 
std::vector< yarp::sig::SoundMarker > getMarkers () const
 Get all the markers contained in the sound.
 
void remove_marker (const std::string &marker_label)
 Removes a marker given its label.
 
void remove_all_markers ()
 Removes all the markers contained in the sound.
 
Sound subSound (std::string marker_start, std::string marker_end)
 Returns a subSound of the current sound, starting from the sample identified by marker_start and ending at the sample identified by marker_end (including the two markers).
 
bool read (yarp::os::ConnectionReader &connection) override
 Read this object from a network connection.
 
bool write (yarp::os::ConnectionWriter &connection) const override
 Write this object to a network connection.
 
- Public Member Functions inherited from yarp::os::Portable
virtual Type getType () const
 
- Public Member Functions inherited from yarp::os::PortReader
virtual ~PortReader ()
 Destructor.
 
virtual Type getReadType () const
 
- Public Member Functions inherited from yarp::os::PortWriter
virtual ~PortWriter ()
 Destructor.
 
virtual void onCompletion () const
 This is called when the port has finished all writing operations.
 
virtual void onCommencement () const
 This is called when the port is about to begin writing operations.
 
virtual yarp::os::Type getWriteType () const
 

Additional Inherited Members

- Static Public Member Functions inherited from yarp::os::Portable
static bool copyPortable (const PortWriter &writer, PortReader &reader)
 Copy one portable to another, via writing and reading.
 

Detailed Description

Class for storing sounds See Audio in YARP for additional documentation on YARP audio.

Definition at line 38 of file Sound.h.

Member Typedef Documentation

◆ audio_sample

typedef short int yarp::sig::Sound::audio_sample

Definition at line 41 of file Sound.h.

Constructor & Destructor Documentation

◆ Sound() [1/2]

Sound::Sound ( size_t  bytesPerSample = 2)

Definition at line 56 of file Sound.cpp.

◆ Sound() [2/2]

Sound::Sound ( const Sound &  alt)

Copy constructor.

Clones the content of another sound.

Parameters
altthe image to sound

Definition at line 62 of file Sound.cpp.

◆ ~Sound()

Sound::~Sound ( )
virtual

Definition at line 262 of file Sound.cpp.

Member Function Documentation

◆ add_marker()

void Sound::add_marker ( const std::string &  marker_label,
size_t  sample_id,
int  channel = -1 
)

Adds a marker to the sound, identified by a label, a sample id and an optional channel number (if not specified, the marker is valid for all channels).

If a marker with the same label already exists, it will be overwritten.

Parameters
marker_labelthe label of the marker
sample_idthe sample id of the marker
channelthe channel number of the marker (optional, default value = -1, meaning that the marker is valid for all channels)

MARKERS

Definition at line 757 of file Sound.cpp.

◆ amplify()

void Sound::amplify ( double  gain)

amplify a sound

Parameters
[in]gainthe gain (1.0 = no amplification. Minimum value: 0)

Definition at line 711 of file Sound.cpp.

◆ amplifyChannel()

void Sound::amplifyChannel ( size_t  channel,
double  gain 
)

Amplify a specific channel of the sound.

Parameters
[in]gainthe gain (1.0 = no amplification. Minimum value: 0)
[in]channelthe sample id

Definition at line 698 of file Sound.cpp.

◆ clear()

void Sound::clear ( )

Definition at line 312 of file Sound.cpp.

◆ extractChannelAsSound()

Sound Sound::extractChannelAsSound ( size_t  channel_id) const

Extract a single channel from the sound.

Returns
a new sound constituted by the selected channel

Definition at line 519 of file Sound.cpp.

◆ findPeak()

void Sound::findPeak ( size_t &  channelId,
size_t &  sampleId,
audio_sample &  sampleValue 
) const

find the peak in a sound

Parameters
[out]channelIdthe returned channelId in which the peak is detected
[out]sampleIdthe returned sampleId in which the peak is detected
[out]sampleValuethe returned value of the sample in which the peak is detected

Definition at line 737 of file Sound.cpp.

◆ findPeakInChannel()

void Sound::findPeakInChannel ( size_t  channelId,
size_t &  sampleId,
audio_sample &  sampleValue 
) const

find the peak in a specific channel of the sound

Parameters
[in]channelIdthe channel in which the peak is searched
[out]sampleIdthe returned sampleId in which the peak is detected
[out]sampleValuethe returned value of the sample in which the peak is detected

Definition at line 719 of file Sound.cpp.

◆ get()

Sound::audio_sample Sound::get ( size_t  sample,
size_t  channel = 0 
) const

Definition at line 291 of file Sound.cpp.

◆ get_marker()

bool Sound::get_marker ( const std::string &  marker_label,
size_t &  sample_id,
int &  channel 
) const

Gets the sample id and channel number of a marker given its label.

If the marker is not found, the method returns false and the output parameters are not modified.

Parameters
marker_labelthe label of the marker
sample_idthe sample id of the marker (output parameter)
channelthe channel number of the marker (output parameter)
Returns
true if the marker is found, false otherwise

Definition at line 767 of file Sound.cpp.

◆ getBytesPerSample()

size_t Sound::getBytesPerSample ( ) const

Get the number of bytes per sample.

Returns
the number of bytes per sample

Definition at line 659 of file Sound.cpp.

◆ getChannel()

std::vector< std::reference_wrapper< Sound::audio_sample > > Sound::getChannel ( size_t  channel_id)

Definition at line 590 of file Sound.cpp.

◆ getChannels()

size_t Sound::getChannels ( ) const

Get the number of channels of the sound.

Returns
the number of channels

Definition at line 669 of file Sound.cpp.

◆ getDuration()

double Sound::getDuration ( ) const

Get the duration of sound in seconds.

Returns
the duration of the sound

Definition at line 674 of file Sound.cpp.

◆ getFrequency()

int Sound::getFrequency ( ) const

Get the frequency of the sound (i.e.

the number of samples per second)

Returns
the frequency of the sound

Definition at line 364 of file Sound.cpp.

◆ getInterleavedAudioRawData()

std::vector< std::reference_wrapper< Sound::audio_sample > > Sound::getInterleavedAudioRawData ( ) const

Returns a serialized version of the sound, in interleaved format, e.g.

for a sound composed by 3 channels, x samples: 1 11 21, 2 12 22, 3 13 23, 4 14 24 etc

Parameters
vecthe vector representing the serialized sound

Definition at line 605 of file Sound.cpp.

◆ getMarker()

yarp::sig::SoundMarker yarp::sig::Sound::getMarker ( size_t  index) const

Get the marker at the specified index.

The order of the markers is not guaranteed and may change in the future. If the index is out of range, the method throws an std::out_of_range exception. Used by yarp bindings.

Parameters
indexthe index of the marker to retrieve
Returns
the marker at the specified index

Definition at line 799 of file Sound.cpp.

◆ getMarkers()

std::vector< yarp::sig::SoundMarker > yarp::sig::Sound::getMarkers ( ) const

Get all the markers contained in the sound.

The order of the markers is not guaranteed and may change in the future. Used by yarp bindings.

Returns
a vector containing all the markers of the sound

Definition at line 809 of file Sound.cpp.

◆ getMarkersCount()

size_t yarp::sig::Sound::getMarkersCount ( ) const

Gets the number of markers contained in the sound.

Returns
the number of markers

Definition at line 794 of file Sound.cpp.

◆ getNonInterleavedAudioRawData()

std::vector< std::reference_wrapper< Sound::audio_sample > > Sound::getNonInterleavedAudioRawData ( ) const

Returns a serialized version of the sound, in non-interleaved format, e.g.

for a sound composed by 3 channels, x samples: 1 2 3 4 5.....etc, 11 12 13 14 15.....etc, 21 22 23 24 25.....etc

Parameters
vecthe vector representing the serialized sound

Definition at line 622 of file Sound.cpp.

◆ getSafe()

audio_sample yarp::sig::Sound::getSafe ( size_t  sample,
size_t  channel = 0 
) const
inline

Definition at line 107 of file Sound.h.

◆ getSamples()

size_t Sound::getSamples ( ) const

Get the number of samples contained in the sound.

Returns
the number of samples

Definition at line 664 of file Sound.cpp.

◆ isSample()

bool Sound::isSample ( size_t  sample,
size_t  channel = 0 
) const

Check whether a sample lies within the sound.

Parameters
samplethe sample to choose
channelthe channel to choose
Returns
true iff there is a sample at the given coordinate

Definition at line 654 of file Sound.cpp.

◆ normalize()

void Sound::normalize ( )

Normalize a sound (the peak is searched among all channels)

Definition at line 688 of file Sound.cpp.

◆ normalizeChannel()

void Sound::normalizeChannel ( size_t  channel)

Normalize a specific channel of the sound.

Parameters
channelthe channel to normalize

Definition at line 679 of file Sound.cpp.

◆ operator+=()

Sound & Sound::operator+= ( const Sound &  alt)

Addition assignment operator.

Appends a sound to another sound (internal memory reallocation)

Parameters
altthe sound to append

Definition at line 118 of file Sound.cpp.

◆ operator=()

const Sound & Sound::operator= ( const Sound &  alt)

Assignment operator.

Clones the content of another sound.

Parameters
altthe image to sound

Definition at line 160 of file Sound.cpp.

◆ operator==()

bool Sound::operator== ( const Sound &  alt) const

Comparison operator.

Compares two sounds

Returns
true if the two sounds are identical

Definition at line 539 of file Sound.cpp.

◆ overwrite()

void Sound::overwrite ( const Sound &  alt,
size_t  offset = 0,
size_t  len = 0 
)

It overwrites the sound with another sound, starting from a specified offset.

Parameters
altthe sound that will overwrite a portion of the current sound.
offsetthe starting sample for the overwrite operation. Default value = 0.
lenthe size (in samples) for the overwrite operation. If not specified, the whole alt sound will be used.

Definition at line 83 of file Sound.cpp.

◆ read()

bool Sound::read ( yarp::os::ConnectionReader &  reader)
overridevirtual

Read this object from a network connection.

Override this for your particular class.

Parameters
readeran interface to the network connection for reading
Returns
true iff the object is successfully read

Implements yarp::os::Portable.

Definition at line 374 of file Sound.cpp.

◆ remove_all_markers()

void Sound::remove_all_markers ( )

Removes all the markers contained in the sound.

Definition at line 789 of file Sound.cpp.

◆ remove_marker()

void Sound::remove_marker ( const std::string &  marker_label)

Removes a marker given its label.

If the marker is not found, the method does nothing.

Parameters
marker_labelthe label of the marker to remove

Definition at line 784 of file Sound.cpp.

◆ replaceChannel()

bool Sound::replaceChannel ( size_t  id,
Sound  channel 
)

Replace a single channel of our current sound with a given sound constituted by a single channel The two sounds must have the same number of samples.

Parameters
idthe channel of our sound that we want to replace
channelthe single-channel sound that we want to put in our sound

Definition at line 571 of file Sound.cpp.

◆ resize()

void Sound::resize ( size_t  samples,
size_t  channels = 1 
)

Set the sound size.

Parameters
samplesthe number of samples
channelsthe number of channels

Definition at line 267 of file Sound.cpp.

◆ sample2timestamp()

double yarp::sig::Sound::sample2timestamp ( size_t  sampleid) const
inline

Utility function: return the timestamp (in seconds) given the sample id.

Parameters
sampleidthe sample id
Returns
the timestamp of the sample

Definition at line 174 of file Sound.h.

◆ set()

void Sound::set ( audio_sample  value,
size_t  sample,
size_t  channel = 0 
)

Definition at line 342 of file Sound.cpp.

◆ setFrequency()

void Sound::setFrequency ( int  freq)

Set the frequency of the sound (i.e.

the number of samples per second)

Definition at line 369 of file Sound.cpp.

◆ setSafe()

void Sound::setSafe ( audio_sample  value,
size_t  sample,
size_t  channel = 0 
)

Definition at line 507 of file Sound.cpp.

◆ subSound() [1/2]

Sound Sound::subSound ( size_t  first_sample,
size_t  len 
)

Returns a subpart of the sound.

Parameters
first_samplethe starting sample number
lenthe max size (in samples) of the subSound

Definition at line 186 of file Sound.cpp.

◆ subSound() [2/2]

Sound Sound::subSound ( std::string  marker_start,
std::string  marker_end 
)

Returns a subSound of the current sound, starting from the sample identified by marker_start and ending at the sample identified by marker_end (including the two markers).

If one of the two markers is not found, or if marker_start is after marker_end, the method throws an std::invalid_argument exception.

Parameters
marker_startthe label of the marker identifying the starting sample of the subSound
marker_endthe label of the marker identifying the ending sample of the subSound
Returns
the subSound identified by the two markers

Definition at line 819 of file Sound.cpp.

◆ timestamp2sample()

size_t yarp::sig::Sound::timestamp2sample ( double  time) const
inline

Utility function: return the sample id given the timestamp (in seconds)

Parameters
timethe timestamp
Returns
the sample id

Definition at line 181 of file Sound.h.

◆ toString()

std::string Sound::toString ( ) const

Print matrix to a string.

Useful for debugging. The output string is represented in non-interleaved format

Definition at line 639 of file Sound.cpp.

◆ write()

bool Sound::write ( yarp::os::ConnectionWriter &  writer) const
overridevirtual

Write this object to a network connection.

Override this for your particular class. Be aware that depending on the nature of the connections a port has, and what protocol they use, and how efficient the YARP implementation is, this method may be called once, twice, or many times, as the result of a single call to Port::write

Parameters
writeran interface to the network connection for writing
Returns
true iff the object is successfully written

Implements yarp::os::Portable.

Definition at line 435 of file Sound.cpp.

◆ zero()

void Sound::zero ( )

set all the samples to zero (silence)

Definition at line 319 of file Sound.cpp.

◆ zeroChannel()

bool Sound::zeroChannel ( size_t  channel)

set to zero all the samples of the specified channel @ param channel the channel number

Returns
true iff operation is successful;

Definition at line 326 of file Sound.cpp.


The documentation for this class was generated from the following files: