31#define NetUint8 unsigned char
35#define NetUint16 short unsigned int
68 init(
alt.m_bytesPerSample);
69 m_frequency =
alt.m_frequency;
70 m_channels =
alt.m_channels;
71 m_samples =
alt.m_samples;
72 if (m_bytesPerSample == 1)
74 *(std::vector<NetUint8>*)(implementation) = *(std::vector<NetUint8>*)(
alt.implementation);
76 if (m_bytesPerSample == 2)
78 *(std::vector<NetUint16>*)(implementation) = *(std::vector<NetUint16>*)(
alt.implementation);
82 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
89 if (
alt.m_channels != m_channels)
91 yCError(
SOUND,
"unable to concatenate sounds with different number of channels!");
94 if (
alt.m_frequency != m_frequency)
96 yCError(
SOUND,
"unable to concatenate sounds with different sample rate!");
108 for (
size_t ch = 0; ch < m_channels; ch++)
111 unsigned char* dst = &this->getRawData()[
pdst];
113 unsigned char* src = &
alt.getRawData()[
psrc];
114 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!");
125 if (
alt.m_frequency!= m_frequency)
127 yCError(
SOUND,
"unable to concatenate sounds with different sample rate!");
132 this->
resize(this->m_samples+
alt.m_samples,m_channels);
136 unsigned char*
pout = this->getRawData();
140 for (
size_t ch=0; ch<m_channels; ch++)
157 init(
alt.m_bytesPerSample);
158 m_frequency =
alt.m_frequency;
159 m_channels =
alt.m_channels;
160 m_samples =
alt.m_samples;
162 if (m_bytesPerSample == 1)
164 *(std::vector<NetUint8>*)(implementation) = *(std::vector<NetUint8>*)(
alt.implementation);
167 else if (m_bytesPerSample == 2)
169 *(std::vector<NetUint16>*)(implementation) = *(std::vector<NetUint16>*)(
alt.implementation);
173 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
210 for (
size_t c = 0;
c < this->m_channels;
c++) {
218void Sound::init(
size_t bytesPerSample)
220 delete_implementation();
221 if (bytesPerSample == 1)
223 implementation =
new std::vector<NetUint8>;
225 else if (bytesPerSample==2)
227 implementation =
new std::vector<NetUint16>;
231 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
238 this->m_bytesPerSample = bytesPerSample;
241void Sound::delete_implementation()
243 if (implementation !=
nullptr)
245 if (m_bytesPerSample == 1)
247 std::vector<NetUint8>*
p = (std::vector<NetUint8>*)(implementation);
249 implementation =
nullptr;
251 else if (m_bytesPerSample == 2)
253 std::vector<NetUint16>*
p = (std::vector<NetUint16>*)(implementation);
255 implementation =
nullptr;
259 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
267 delete_implementation();
272 delete_implementation();
273 if (m_bytesPerSample == 1)
275 implementation =
new std::vector<NetUint8>;
280 else if (m_bytesPerSample == 2)
282 implementation =
new std::vector<NetUint16>;
289 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
296 if (m_bytesPerSample == 1)
298 auto*
pp = ((std::vector<NetUint8>*)(implementation));
302 else if (m_bytesPerSample == 2)
304 auto*
pp = ((std::vector<NetUint16>*)(implementation));
310 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
317 size_t size = this->getRawDataSize();
318 unsigned char*
p = this->getRawData();
324 if (
chan > this->m_channels) {
327 for (
size_t i = 0;
i < this->m_samples;
i++)
336 if (m_bytesPerSample == 1)
338 auto*
pp = ((std::vector<NetUint8>*)(implementation));
343 else if (m_bytesPerSample == 2)
345 auto*
pp = ((std::vector<NetUint16>*)(implementation));
352 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
363 this->m_frequency =
freq;
370 bool ok =
connection.expectBlock((
char*)&header,
sizeof(header));
386 auto*
pp = ((std::vector<NetUint16>*)(implementation));
410 auto*
pp = ((std::vector<NetUint16>*)(implementation));
411 size_t siz =
pp->size();
415 connection.appendBlock((
char*)&header,
sizeof(header));
416 for (
size_t l = 0;
l <
pp->size();
l++) {
424unsigned char *Sound::getRawData()
const
426 if (m_bytesPerSample == 1)
428 auto*
pp = ((std::vector<NetUint8>*)(implementation));
432 else if (m_bytesPerSample == 2)
434 auto*
pp = ((std::vector<NetUint16>*)(implementation));
436 return (
unsigned char*)(
addr);
439 yCError(
SOUND,
"sound only implemented for 8-16 bit samples");
443size_t Sound::getRawDataSize()
const
445 return this->m_bytesPerSample*this->m_channels*this->m_samples;
463 news.setFrequency(this->m_frequency);
464 news.resize(this->m_samples, 1);
466 unsigned char*
p_src = this->getRawData();
467 unsigned char*
p_dst =
news.getRawData();
482 if (this->m_channels !=
alt.getChannels()) {
485 if (this->m_bytesPerSample !=
alt.getBytesPerSample()) {
488 if (this->m_frequency !=
alt.getFrequency()) {
491 if (this->m_samples !=
alt.getSamples()) {
495 for (
size_t ch = 0; ch < this->m_channels; ch++)
497 for (
size_t s = 0; s < this->m_samples; s++)
499 if (this->
getSafe(s, ch) !=
alt.getSafe(s, ch))
514 if (this->m_samples !=
schannel.getSamples()) {
517 for (
size_t s = 0; s < this->m_samples; s++)
526 auto*
pp = ((std::vector<NetUint16>*)(implementation))->data() +
channel_id*m_samples;
528 std::vector<std::reference_wrapper<audio_sample>>
vec;
529 vec.reserve(this->m_samples);
530 for (
size_t t = 0;
t < this->m_samples;
t++)
532 unsigned char *
addr = (
unsigned char* )(
pp);
541 auto*
pp = ((std::vector<NetUint16>*)(implementation))->data();
543 std::vector<std::reference_wrapper<audio_sample>>
vec;
544 vec.reserve(this->m_samples*this->m_channels);
545 for (
size_t t = 0;
t < this->m_samples;
t++)
547 for (
size_t c = 0;
c < this->m_channels;
c++)
550 vec.push_back(std::ref(*
addr));
558 auto*
pp = ((std::vector<NetUint16>*)(implementation))->data();
560 std::vector<std::reference_wrapper<audio_sample>>
vec;
561 vec.reserve(this->m_samples*this->m_channels);
562 for (
size_t c = 0;
c < this->m_channels;
c++)
564 for (
size_t t = 0;
t < this->m_samples;
t++)
567 vec.push_back(std::ref(*
addr));
576 for (
size_t c = 0;
c < this->m_channels;
c++)
578 for (
size_t t = 0;
t < this->m_samples;
t++)
581 s += std::to_string(this->
get(
t,
c));
595 return this->m_bytesPerSample;
600 return this->m_samples;
605 return this->m_channels;
610 return (
double)(this->m_samples)*(
double)(1 / this->m_frequency);
634 unsigned char*
pc = this->getRawData();
638 for (
size_t t = 0;
t < this->m_samples;
t++,
p++)
647 for (
size_t c = 0;
c < this->m_channels;
c++)
657 unsigned char*
pc = this->getRawData();
661 for (
size_t t = 0;
t < this->m_samples;
t++,
p++)
673 for (
size_t c = 0;
c < this->m_channels;
c++)
A mini-server for performing network communication in the background.
An interface for reading from a network connection.
An interface for writing to a 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.
bool isSample(size_t sample, size_t channel=0) const
Check whether a sample lies within the sound.
bool clearChannel(size_t channel)
set to zero all the samples of the specified channel @ param channel the channel number
void setFrequency(int freq)
Set the frequency of the sound (i.e.
Sound subSound(size_t first_sample, size_t last_sample)
Returns a subpart of the sound.
std::vector< std::reference_wrapper< audio_sample > > getNonInterleavedAudioRawData() const
Returns a serialized version of the sound, in non-interleaved format, e.g.
const Sound & operator=(const Sound &alt)
Assignment operator.
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.
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.
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
void clear()
set all the samples to zero (silence)
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.
std::string toString() const
Print matrix to a string.
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 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.