30#define NetUint8 unsigned char
34#define NetUint16 short unsigned int
64 init(
alt.m_bytesPerSample);
65 m_frequency =
alt.m_frequency;
66 m_channels =
alt.m_channels;
67 m_samples =
alt.m_samples;
68 if (m_bytesPerSample == 1)
70 *(std::vector<NetUint8>*)(implementation) = *(std::vector<NetUint8>*)(
alt.implementation);
72 if (m_bytesPerSample == 2)
74 *(std::vector<NetUint16>*)(implementation) = *(std::vector<NetUint16>*)(
alt.implementation);
78 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
85 if (
alt.m_channels != m_channels)
87 yCError(
SOUND,
"unable to overwrite sounds with different number of channels!");
90 if (
alt.m_frequency != m_frequency)
92 yCError(
SOUND,
"unable to overwrite sounds with different sample rate!");
104 for (
size_t ch = 0; ch < m_channels; ch++)
107 unsigned char* dst = &this->getRawData()[
pdst];
109 unsigned char* src = &
alt.getRawData()[
psrc];
110 memcpy((
void*) dst, (
void*) src, len * this->m_bytesPerSample);
120 if (
alt.m_channels!= m_channels)
122 yCError(
SOUND) <<
"unable to concatenate sounds with different number of channels!" <<
123 alt.m_channels <<
"!=" << m_channels;
126 if (
alt.m_frequency!= m_frequency)
128 yCError(
SOUND) <<
"unable to concatenate sounds with different sample rate!" <<
129 alt.m_frequency <<
"!=" << m_frequency;
134 this->
resize(this->m_samples+
alt.m_samples,m_channels);
138 unsigned char*
pout = this->getRawData();
142 for (
size_t ch=0; ch<m_channels; ch++)
162 if (
this == &
alt) {
return *
this; }
164 init(
alt.m_bytesPerSample);
165 m_frequency =
alt.m_frequency;
166 m_channels =
alt.m_channels;
167 m_samples =
alt.m_samples;
168 m_markers =
alt.m_markers;
170 if (m_bytesPerSample == 1)
172 *(std::vector<NetUint8>*)(implementation) = *(std::vector<NetUint8>*)(
alt.implementation);
175 else if (m_bytesPerSample == 2)
177 *(std::vector<NetUint16>*)(implementation) = *(std::vector<NetUint16>*)(
alt.implementation);
181 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
207 for (
size_t c = 0;
c < this->m_channels;
c++) {
215void Sound::init(
size_t bytesPerSample)
217 delete_implementation();
218 if (bytesPerSample == 1)
220 implementation =
new std::vector<NetUint8>;
222 else if (bytesPerSample==2)
224 implementation =
new std::vector<NetUint16>;
228 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
235 this->m_bytesPerSample = bytesPerSample;
238void Sound::delete_implementation()
240 if (implementation !=
nullptr)
242 if (m_bytesPerSample == 1)
244 std::vector<NetUint8>*p = (std::vector<NetUint8>*)(implementation);
246 implementation =
nullptr;
248 else if (m_bytesPerSample == 2)
250 std::vector<NetUint16>* p = (std::vector<NetUint16>*)(implementation);
252 implementation =
nullptr;
256 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
264 delete_implementation();
269 delete_implementation();
270 if (m_bytesPerSample == 1)
272 implementation =
new std::vector<NetUint8>;
273 ((std::vector<NetUint8>*)(implementation))->
resize(samples * channels);
274 m_channels = channels;
277 else if (m_bytesPerSample == 2)
279 implementation =
new std::vector<NetUint16>;
280 ((std::vector<NetUint16>*)(implementation))->
resize(samples*channels);
286 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
293 if (m_bytesPerSample == 1)
295 auto*
pp = ((std::vector<NetUint8>*)(implementation));
299 else if (m_bytesPerSample == 2)
301 auto*
pp = ((std::vector<NetUint16>*)(implementation));
307 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
321 size_t size = this->getRawDataSize();
322 unsigned char* p = this->getRawData();
328 if (
chan > this->m_channels) {
331 for (
size_t i = 0; i < this->m_samples; i++)
344 if (m_bytesPerSample == 1)
346 auto*
pp = ((std::vector<NetUint8>*)(implementation));
351 else if (m_bytesPerSample == 2)
353 auto*
pp = ((std::vector<NetUint16>*)(implementation));
360 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
371 this->m_frequency =
freq;
378 bool ok = connection.
expectBlock((
char*)&header,
sizeof(header));
396 auto*
pp = ((std::vector<NetUint16>*)(implementation));
449 connection.
appendBlock((
char*)&header,
sizeof(header));
451 auto*
pp = ((std::vector<NetUint16>*)(implementation));
452 size_t siz =
pp->size();
457 for (
size_t l = 0;
l <
pp->size();
l++) {
465 for (
const auto&
it : m_markers) {
466 const std::string& key =
it.first;
482unsigned char *Sound::getRawData()
const
484 if (m_bytesPerSample == 1)
486 auto*
pp = ((std::vector<NetUint8>*)(implementation));
490 else if (m_bytesPerSample == 2)
492 auto*
pp = ((std::vector<NetUint16>*)(implementation));
494 return (
unsigned char*)(
addr);
497 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
502size_t Sound::getRawDataSize()
const
504 return this->m_bytesPerSample*this->m_channels*this->m_samples;
511 set(value, sample, channel);
515 yCError(
SOUND) <<
"Sample out of bound:" << sample <<
"," << channel;
522 news.setFrequency(this->m_frequency);
523 news.resize(this->m_samples, 1);
525 unsigned char*
p_src = this->getRawData();
526 unsigned char*
p_dst =
news.getRawData();
541 if (this->m_channels !=
alt.getChannels()) {
544 if (this->m_bytesPerSample !=
alt.getBytesPerSample()) {
547 if (this->m_frequency !=
alt.getFrequency()) {
550 if (this->m_samples !=
alt.getSamples()) {
553 if (this->m_markers !=
alt.m_markers) {
557 for (
size_t ch = 0; ch < this->m_channels; ch++)
559 for (
size_t s = 0; s < this->m_samples; s++)
561 if (this->
getSafe(s, ch) !=
alt.getSafe(s, ch))
576 if (this->m_samples !=
schannel.getSamples()) {
579 for (
size_t s = 0; s < this->m_samples; s++)
592 auto*
pp = ((std::vector<NetUint16>*)(implementation))->data() +
channel_id*m_samples;
594 std::vector<std::reference_wrapper<audio_sample>>
vec;
595 vec.reserve(this->m_samples);
596 for (
size_t t = 0; t < this->m_samples; t++)
598 unsigned char *
addr = (
unsigned char* )(
pp);
607 auto*
pp = ((std::vector<NetUint16>*)(implementation))->data();
609 std::vector<std::reference_wrapper<audio_sample>>
vec;
610 vec.reserve(this->m_samples*this->m_channels);
611 for (
size_t t = 0; t < this->m_samples; t++)
613 for (
size_t c = 0;
c < this->m_channels;
c++)
616 vec.push_back(std::ref(*
addr));
624 auto*
pp = ((std::vector<NetUint16>*)(implementation))->data();
626 std::vector<std::reference_wrapper<audio_sample>>
vec;
627 vec.reserve(this->m_samples*this->m_channels);
628 for (
size_t c = 0;
c < this->m_channels;
c++)
630 for (
size_t t = 0; t < this->m_samples; t++)
633 vec.push_back(std::ref(*
addr));
642 for (
size_t c = 0;
c < this->m_channels;
c++)
644 for (
size_t t = 0; t < this->m_samples; t++)
647 s += std::to_string(this->
get(t,
c));
661 return this->m_bytesPerSample;
666 return this->m_samples;
671 return this->m_channels;
676 return (
double)(this->m_samples)*(
double)(1.0 / this->m_frequency);
700 unsigned char*
pc = this->getRawData();
702 p+= this->m_samples * channel;
704 for (
size_t t = 0; t < this->m_samples; t++, p++)
713 for (
size_t c = 0;
c < this->m_channels;
c++)
723 unsigned char*
pc = this->getRawData();
727 for (
size_t t = 0; t < this->m_samples; t++, p++)
739 for (
size_t c = 0;
c < this->m_channels;
c++)
760 sm.sample_id = sample_id;
770 if (
it == m_markers.end())
779 sample_id =
sm.sample_id;
780 channel =
sm.channel;
796 return m_markers.size();
801 if (index >= m_markers.size()) {
804 auto it = m_markers.begin();
805 std::advance(
it, index);
811 std::vector<yarp::sig::SoundMarker> v;
812 v.reserve(m_markers.size());
813 for (
auto const& [key, val] : m_markers) {
827 mend.sample_id = this->m_samples - 1;
830 if (
its == m_markers.end())
840 if (
ite == m_markers.end())
A mini-server for performing network communication in the background.
An interface for reading from a network connection.
virtual std::string expectString()
Read a string from the network connection.
virtual bool expectBlock(char *data, size_t len)=0
Read a block of data from the network connection.
virtual std::int32_t expectInt32()=0
Read a 32-bit integer from the network connection.
virtual bool convertTextMode()=0
Reads in a standard description in text mode, and converts it to a standard description in binary.
virtual std::int64_t expectInt64()=0
Read a 64-bit integer from the network connection.
virtual std::int16_t expectInt16()=0
Read a 16-bit integer from the network connection.
An interface for writing to a network connection.
virtual void appendInt64(std::int64_t data)=0
Send a representation of a 64-bit integer to the network connection.
virtual void appendInt16(std::int16_t data)=0
Send a representation of a 16-bit integer to the network connection.
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
virtual void appendInt32(std::int32_t data)=0
Send a representation of a 32-bit integer to the network connection.
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
void appendString(const std::string &str)
Send a string to the network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
void setSafe(audio_sample value, size_t sample, size_t channel=0)
Sound extractChannelAsSound(size_t channel_id) const
Extract a single channel from the sound.
size_t getMarkersCount() const
Gets the number of markers contained in the sound.
bool isSample(size_t sample, size_t channel=0) const
Check whether a sample lies within the sound.
void setFrequency(int freq)
Set the frequency of the sound (i.e.
void zero()
set all the samples to zero (silence)
Sound subSound(size_t first_sample, size_t len)
Returns a subpart of the sound.
yarp::sig::SoundMarker getMarker(size_t index) const
Get the marker at the specified index.
std::vector< std::reference_wrapper< audio_sample > > getNonInterleavedAudioRawData() const
Returns a serialized version of the sound, in non-interleaved format, e.g.
void remove_all_markers()
Removes all the markers contained in the sound.
const Sound & operator=(const Sound &alt)
Assignment operator.
std::vector< yarp::sig::SoundMarker > getMarkers() const
Get all the markers contained in the sound.
void amplifyChannel(size_t channel, double gain)
Amplify a specific channel of the sound.
size_t getBytesPerSample() const
Get the number of bytes per sample.
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...
std::vector< std::reference_wrapper< audio_sample > > getInterleavedAudioRawData() const
Returns a serialized version of the sound, in interleaved format, e.g.
double getDuration() const
Get the duration of sound in seconds.
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
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 getChannels() const
Get the number of channels of the sound.
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 ...
void normalize()
Normalize a sound (the peak is searched among all channels)
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
void amplify(double gain)
amplify a sound
Sound(size_t bytesPerSample=2)
void resize(size_t samples, size_t channels=1)
Set the sound size.
void normalizeChannel(size_t channel)
Normalize a specific channel of the sound.
void findPeak(size_t &channelId, size_t &sampleId, audio_sample &sampleValue) const
find the peak in a sound
int getFrequency() const
Get the frequency of the sound (i.e.
bool zeroChannel(size_t channel)
set to zero all the samples of the specified channel @ param channel the channel number
std::string toString() const
Print matrix to a string.
void remove_marker(const std::string &marker_label)
Removes a marker given its label.
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.
audio_sample get(size_t sample, size_t channel=0) const
std::vector< std::reference_wrapper< audio_sample > > getChannel(size_t channel_id)
Sound & operator+=(const Sound &alt)
Addition assignment operator.
bool operator==(const Sound &alt) const
Comparison operator.
void set(audio_sample value, size_t sample, size_t channel=0)
size_t getSamples() const
Get the number of samples contained in the sound.
audio_sample getSafe(size_t sample, size_t channel=0) const
void findPeakInChannel(size_t channelId, size_t &sampleId, audio_sample &sampleValue) const
find the peak in a specific channel of the sound
#define yCError(component,...)
#define yCAssert(component, x)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface to the operating system, including Port based communication.
std::uint16_t NetUint16
Definition of the NetUint16 type.
std::int32_t NetInt32
Definition of the NetInt32 type.
std::uint8_t NetUint8
Definition of the NetUint8 type.
The main, catch-all namespace for YARP.
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.