|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
30 #define HELPER(x) (*((FlexImage*)(x)))
44 m_frequency = alt.m_frequency;
50 if (alt.m_channels!= m_channels)
52 yCError(SOUND,
"unable to concatenate sounds with different number of channels!");
55 if (alt.m_frequency!= m_frequency)
57 yCError(SOUND,
"unable to concatenate sounds with different sample rate!");
62 this->
resize(this->m_samples+alt.m_samples,m_channels);
64 unsigned char* p1 = orig.getRawData();
65 unsigned char* p2 = alt.getRawData();
66 unsigned char* pout = this->getRawData();
68 for (
size_t ch=0; ch<m_channels; ch++)
79 memcpy((
void *) &pout[out1], (
void *) (p1+ori1), s1);
80 memcpy((
void *) &pout[out2], (
void *) (p2+alt1), s2);
93 m_frequency = alt.m_frequency;
98 void Sound::synchronize()
101 m_samples = img.
width();
102 m_channels = img.
height();
107 if (last_sample > this->m_samples)
108 last_sample = m_samples;
109 if (first_sample > this->m_samples)
110 first_sample = m_samples;
111 if (last_sample < first_sample)
112 last_sample = first_sample;
116 s.
resize(last_sample-first_sample, this->m_channels);
132 for (
size_t i=first_sample; i<last_sample; i++)
134 for (
size_t c=0; c< this->m_channels; c++)
135 s.
set(this->get(i,c),j,c);
144 void Sound::init(
size_t bytesPerSample)
147 yCAssert(SOUND, implementation!=
nullptr);
151 HELPER(implementation).setQuantum(2);
155 this->m_bytesPerSample = bytesPerSample;
160 if (implementation!=
nullptr)
162 delete &
HELPER(implementation);
163 implementation =
nullptr;
178 if (m_bytesPerSample ==2)
180 return *(
reinterpret_cast<NetUint16*
>(addr));
184 yCError(SOUND,
"sound only implemented for 16 bit samples");
191 size_t size = this->getRawDataSize();
192 unsigned char* p = this->getRawData();
198 if (chan > this->m_channels)
return false;
199 for (
size_t i = 0; i < this->m_samples; i++)
210 if (m_bytesPerSample ==2)
212 *(
reinterpret_cast<NetUint16*
>(addr)) = value;
217 yCError(SOUND,
"sound only implemented for 16 bit samples");
228 this->m_frequency = freq;
252 unsigned char *Sound::getRawData()
const
258 size_t Sound::getRawDataSize()
const
268 set(value, sample, channel);
272 yCError(SOUND) <<
"Sample out of bound:" << sample <<
"," << channel;
278 Sound news(this->m_bytesPerSample);
280 news.
resize(this->m_samples, 1);
282 unsigned char* p_src = this->getRawData();
283 unsigned char* p_dst = news.getRawData();
286 size_t first_sample = 0 + (this->m_samples * this->m_bytesPerSample)*channel_id;
288 size_t last_sample = first_sample + (this->m_samples * this->m_bytesPerSample);
289 for (
auto i = first_sample; i < last_sample; i++)
291 p_dst[j++] = p_src[i];
298 if (this->m_channels != alt.
getChannels())
return false;
300 if (this->m_frequency != alt.
getFrequency())
return false;
301 if (this->m_samples != alt.
getSamples())
return false;
303 for (
size_t ch = 0; ch < this->m_channels; ch++)
305 for (
size_t s = 0; s < this->m_samples; s++)
320 if (this->m_samples != schannel.
getSamples())
return false;
321 for (
size_t s = 0; s < this->m_samples; s++)
332 std::vector<std::reference_wrapper<audio_sample>> vec;
333 vec.reserve(this->m_samples);
334 for (
size_t t = 0;
t < this->m_samples;
t++)
338 vec.push_back(std::ref(*addr2));
347 std::vector<std::reference_wrapper<audio_sample>> vec;
348 vec.reserve(this->m_samples*this->m_channels);
349 for (
size_t t = 0;
t < this->m_samples;
t++)
351 for (
size_t c = 0; c < this->m_channels; c++)
355 vec.push_back(std::ref(*addr2));
365 std::vector<std::reference_wrapper<audio_sample>> vec;
366 vec.reserve(this->m_samples*this->m_channels);
367 for (
size_t c = 0; c < this->m_channels; c++)
369 for (
size_t t = 0;
t < this->m_samples;
t++)
373 vec.push_back(std::ref(*addr2));
382 for (
size_t c = 0; c < this->m_channels; c++)
384 for (
size_t t = 0;
t < this->m_samples;
t++)
387 s += std::to_string(this->
get(
t, c));
396 return (sample<this->m_samples && channel<this->m_channels);
401 return this->m_bytesPerSample;
406 return this->m_samples;
411 return this->m_channels;
416 return (
double)(this->m_samples)*(
double)(1 / this->m_frequency);
void setFrequency(int freq)
Set the frequency of the sound (i.e.
A simple collection of objects that can be described and transmitted in a portable way.
bool isSample(size_t sample, size_t channel=0) const
Check whether a sample lies within 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 ...
This is a base class for objects that can be both read from and be written to the YARP network.
int getFrequency() const
Get the frequency of the sound (i.e.
Group a pair of objects to be sent and received together.
Sound extractChannelAsSound(size_t channel_id) const
Extract a single channel from the sound.
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
size_t getSamples() const
Get the number of samples contained in the sound.
#define YARP_LOG_COMPONENT(name,...)
bool copy(const Image &alt)
Copy operator.
size_t getChannels() const
Get the number of channels of the sound.
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
size_t width() const
Gets width of image in pixels.
Sound & operator+=(const Sound &alt)
Addition assignment operator.
Sound(size_t bytesPerSample=2)
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
bool operator==(const Sound &alt) const
Comparison operator.
std::vector< std::reference_wrapper< audio_sample > > getNonInterleavedAudioRawData() const
Returns a serialized version of the sound, in non-interleaved format, e.g.
An interface for writing to a network connection.
std::vector< std::reference_wrapper< audio_sample > > getChannel(size_t channel_id)
Image class with user control of representation details.
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
audio_sample getSafe(size_t sample, size_t channel=0) const
void set(audio_sample value, size_t sample, size_t channel=0)
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
size_t height() const
Gets height of image in pixels.
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
audio_sample get(size_t sample, size_t channel=0) const
void setSafe(audio_sample value, size_t sample, size_t channel=0)
bool clearChannel(size_t channel)
set to zero all the samples of the specified channel @ param channel the channel number
#define yCAssert(component, x)
An interface for reading from a network connection.
#define yCError(component,...)
virtual std::int32_t asInt32() const
Get 32-bit integer value.
double getDuration() const
Get the duration of sound in seconds.
An interface to the operating system, including Port based communication.
const Sound & operator=(const Sound &alt)
Assignment operator.
Class for storing sounds.
std::uint16_t NetUint16
Definition of the NetUint16 type.
The main, catch-all namespace for YARP.
std::string toString() const
Print matrix to a string.
Sound subSound(size_t first_sample, size_t last_sample)
Returns a subpart of the sound.
void clear()
set all the samples to zero (silence)
void resize(size_t samples, size_t channels=1)
Set the sound size.
std::vector< std::reference_wrapper< audio_sample > > getInterleavedAudioRawData() const
Returns a serialized version of the sound, in interleaved format, e.g.
size_t getBytesPerSample() const
Get the number of bytes per sample.
unsigned char * getRawImage() const
Access to the internal image buffer.