YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Sound.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <yarp/sig/Sound.h>
8#include <yarp/os/Bottle.h>
10#include <yarp/os/LogStream.h>
11#include <yarp/os/Time.h>
12#include <yarp/os/Value.h>
13#include <functional>
14
17
18#include <cstring>
19#include <cstdio>
20#include <vector>
21
22using namespace yarp::sig;
23using namespace yarp::os;
24
25namespace {
26YARP_LOG_COMPONENT(SOUND, "yarp.sig.Sound")
27}
28
29#ifndef NetUint8
30#define NetUint8 unsigned char
31#endif
32
33#ifndef NetUint16
34#define NetUint16 short unsigned int
35#endif
36
55
56Sound::Sound(size_t bytesPerSample)
57{
58 init(bytesPerSample);
59 m_frequency = 0;
60}
61
63{
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)
69 {
70 *(std::vector<NetUint8>*)(implementation) = *(std::vector<NetUint8>*)(alt.implementation);
71 }
72 if (m_bytesPerSample == 2)
73 {
74 *(std::vector<NetUint16>*)(implementation) = *(std::vector<NetUint16>*)(alt.implementation);
75 }
76 else
77 {
78 yCError(SOUND, "sound only implemented for 8-16 bit samples");
79 yCAssert(SOUND,false); // that's all that's implemented right now
80 }
81}
82
83void Sound::overwrite(const Sound& alt, size_t offset, size_t len)
84{
85 if (alt.m_channels != m_channels)
86 {
87 yCError(SOUND, "unable to overwrite sounds with different number of channels!");
88 return;
89 }
90 if (alt.m_frequency != m_frequency)
91 {
92 yCError(SOUND, "unable to overwrite sounds with different sample rate!");
93 return;
94 }
95
96 size_t current_size = (*this).getSamples();
97 size_t alt_size = alt.getSamples();
98 if (len == 0) len = alt_size;
99 if ( offset + len > current_size)
100 {
101 len = current_size - offset;
102 }
103
104 for (size_t ch = 0; ch < m_channels; ch++)
105 {
106 size_t pdst = ch * this->getBytesPerSample() * this->m_samples + offset* this->getBytesPerSample();
107 unsigned char* dst = &this->getRawData()[pdst];
108 size_t psrc = ch * this->getBytesPerSample() * alt.m_samples;
109 unsigned char* src = &alt.getRawData()[psrc];
110 memcpy((void*) dst, (void*) src, len * this->m_bytesPerSample);
111 }
112
113 //invalidate all the markers
114 m_markers.clear();
115
116}
117
119{
120 if (alt.m_channels!= m_channels)
121 {
122 yCError(SOUND) << "unable to concatenate sounds with different number of channels!" <<
123 alt.m_channels << "!=" << m_channels;
124 return *this;
125 }
126 if (alt.m_frequency!= m_frequency)
127 {
128 yCError(SOUND) << "unable to concatenate sounds with different sample rate!" <<
129 alt.m_frequency << "!=" << m_frequency;
130 return *this;
131 }
132
133 Sound orig= *this;
134 this->resize(this->m_samples+alt.m_samples,m_channels);
135
136 unsigned char* orig_start = orig.getRawData();
137 unsigned char* alt_start = alt.getRawData();
138 unsigned char* pout = this->getRawData();
139 size_t orig_singlechannel_size = orig.getBytesPerSample() * orig.m_samples;
140 size_t alt_singlechannel_size = alt.getBytesPerSample() * alt.m_samples;
141
142 for (size_t ch=0; ch<m_channels; ch++)
143 {
144 size_t out1 = ch* this->getBytesPerSample() * this->m_samples;
145 size_t out2 = ch* this->getBytesPerSample() * this->m_samples + this->getBytesPerSample() * orig.m_samples;
146
147 size_t orig_pointer = ch * orig.getBytesPerSample() * orig.m_samples;
148 size_t alt_pointer = ch * orig.getBytesPerSample() * alt.m_samples;
149
151 memcpy((void *) &pout[out2], (void *) (alt_start + alt_pointer), alt_singlechannel_size);
152 }
153
154 //invalidate all the markers
155 m_markers.clear();
156
157 return *this;
158}
159
161{
162 if (this == &alt) { return *this; }
163
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;
169
170 if (m_bytesPerSample == 1)
171 {
172 *(std::vector<NetUint8>*)(implementation) = *(std::vector<NetUint8>*)(alt.implementation);
173 return *this;
174 }
175 else if (m_bytesPerSample == 2)
176 {
177 *(std::vector<NetUint16>*)(implementation) = *(std::vector<NetUint16>*)(alt.implementation);
178 return *this;
179 }
180
181 yCError(SOUND, "sound only implemented for 8-16 bit samples");
182 yCAssert(SOUND, false); // that's all that's implemented right now
183 return *this;
184}
185
187{
188 Sound s;
189
190 // Nothing to copy
191 if (len == 0 || first_sample >= m_samples) {
192 s.setFrequency(m_frequency);
193 s.resize(0, m_channels);
194 return s;
195 }
196
197 size_t last_sample = std::min(first_sample + len, m_samples);
199
200 s.resize(out_len, this->m_channels);
201 s.setFrequency(this->m_frequency);
202
203 //safe implementation
204 size_t j=0;
205 for (size_t i=first_sample; i<last_sample; i++)
206 {
207 for (size_t c = 0; c < this->m_channels; c++) {
208 s.set(this->get(i, c), j, c);
209 }
210 j++;
211 }
212 return s;
213}
214
215void Sound::init(size_t bytesPerSample)
216{
217 delete_implementation();
218 if (bytesPerSample == 1)
219 {
220 implementation = new std::vector<NetUint8>;
221 }
222 else if (bytesPerSample==2)
223 {
224 implementation = new std::vector<NetUint16>;
225 }
226 else
227 {
228 yCError(SOUND, "sound only implemented for 8-16 bit samples");
229 yCAssert(SOUND, false); // that's all that's implemented right now
230 }
231
232 yCAssert(SOUND, implementation!=nullptr);
233 m_samples = 0;
234 m_channels = 1;
235 this->m_bytesPerSample = bytesPerSample;
236}
237
238void Sound::delete_implementation()
239{
240 if (implementation != nullptr)
241 {
242 if (m_bytesPerSample == 1)
243 {
244 std::vector<NetUint8>*p = (std::vector<NetUint8>*)(implementation);
245 delete p;
246 implementation = nullptr;
247 }
248 else if (m_bytesPerSample == 2)
249 {
250 std::vector<NetUint16>* p = (std::vector<NetUint16>*)(implementation);
251 delete p;
252 implementation = nullptr;
253 }
254 else
255 {
256 yCError(SOUND, "sound only implemented for 8-16 bit samples");
257 yCAssert(SOUND, false); // that's all that's implemented right now
258 }
259 }
260}
261
263{
264 delete_implementation();
265}
266
267void Sound::resize(size_t samples, size_t channels)
268{
269 delete_implementation();
270 if (m_bytesPerSample == 1)
271 {
272 implementation = new std::vector<NetUint8>;
273 ((std::vector<NetUint8>*)(implementation))->resize(samples * channels);
274 m_channels = channels;
275 m_samples = samples;
276 }
277 else if (m_bytesPerSample == 2)
278 {
279 implementation = new std::vector<NetUint16>;
280 ((std::vector<NetUint16>*)(implementation))->resize(samples*channels);
281 m_channels=channels;
282 m_samples=samples;
283 }
284 else
285 {
286 yCError(SOUND, "sound only implemented for 8-16 bit samples");
287 yCAssert(SOUND, false); // that's all that's implemented right now
288 }
289}
290
291Sound::audio_sample Sound::get(size_t location, size_t channel) const
292{
293 if (m_bytesPerSample == 1)
294 {
295 auto* pp = ((std::vector<NetUint8>*)(implementation));
296 NetUint8* addr = pp->data() + location + channel * this->m_samples;
297 return *(reinterpret_cast<NetUint8*>(addr));
298 }
299 else if (m_bytesPerSample == 2)
300 {
301 auto* pp = ((std::vector<NetUint16>*)(implementation));
302 NetUint16* addr = pp->data() + location + channel * this->m_samples;
303 return *(reinterpret_cast<NetUint16*>(addr));
304 }
305 else
306 {
307 yCError(SOUND, "sound only implemented for 8-16 bit samples");
308 }
309 return 0;
310}
311
313{
314 resize(0,0);
315 m_markers.clear();
316 m_frequency =0;
317}
318
320{
321 size_t size = this->getRawDataSize();
322 unsigned char* p = this->getRawData();
323 memset(p,0,size);
324}
325
327{
328 if (chan > this->m_channels) {
329 return false;
330 }
331 for (size_t i = 0; i < this->m_samples; i++)
332 {
333 set(0, i, chan);
334 }
335
336 //invalidate all the markers
337 m_markers.clear();
338
339 return true;
340}
341
342void Sound::set(audio_sample value, size_t location, size_t channel)
343{
344 if (m_bytesPerSample == 1)
345 {
346 auto* pp = ((std::vector<NetUint8>*)(implementation));
347 NetUint8* addr = pp->data() + location +channel * this->m_samples;
348 *(reinterpret_cast<NetUint8*>(addr)) = value;
349 return;
350 }
351 else if (m_bytesPerSample == 2)
352 {
353 auto* pp = ((std::vector<NetUint16>*)(implementation));
354 NetUint16* addr = pp->data() + location + channel * this->m_samples;
355 *(reinterpret_cast<NetUint16*>(addr)) = value;
356 return;
357 }
358 else
359 {
360 yCError(SOUND, "sound only implemented for 8-16 bit samples");
361 }
362}
363
365{
366 return m_frequency;
367}
368
370{
371 this->m_frequency = freq;
372}
373
375{
376 connection.convertTextMode();
377 SoundHeader header;
378 bool ok = connection.expectBlock((char*)&header, sizeof(header));
379 if (!ok) {
380 return false;
381 }
382
383 init(header.bytesPerSample);
384 m_frequency = header.frequency;
385 m_channels = header.channelsSize;
386 m_samples = header.samplesSize;
387
388 connection.expectInt32(); //dummy list
389 size_t data_size = connection.expectInt32(); //data size
390 if (data_size != m_channels * m_samples)
391 {
392 return false;
393 }
394
395 //sound data
396 auto* pp = ((std::vector<NetUint16>*)(implementation));
397 pp->resize(data_size);
398 for (size_t l = 0; l < data_size; l++)
399 {
400 pp->at(l) = connection.expectInt16();
401 }
402
403 connection.expectInt32(); //dummy list
404 size_t markersListLen = connection.expectInt32(); //markers size
405
406 //markers data
407 m_markers.clear();
408 for (size_t i = 0; i < markersListLen; i++)
409 {
410 int innerTag = connection.expectInt32();
411 if (innerTag != BOTTLE_TAG_LIST) {
412 return false;
413 }
414
415 int innerSize = connection.expectInt32();
416 if (innerSize != 4) {
417 return false;
418 }
419
420 std::string key = connection.expectString();
421
423 marker.label = connection.expectString();
424 marker.channel = connection.expectInt32();
425 marker.sample_id = static_cast<size_t>(connection.expectInt64());
426
427 m_markers[key] = marker;
428 }
429
430
431 return true;
432}
433
434
436{
437 SoundHeader header;
439 header.outerListLen = 6;
440 header.samplesSizeTag = BOTTLE_TAG_INT32; //1
441 header.samplesSize = m_samples;
443 header.bytesPerSample = m_bytesPerSample;
445 header.channelsSize = m_channels;
446 header.frequencyTag = BOTTLE_TAG_INT32; //4
447 header.frequency = m_frequency;
448
449 connection.appendBlock((char*)&header, sizeof(header));
450
451 auto* pp = ((std::vector<NetUint16>*)(implementation));
452 size_t siz = pp->size();
454 connection.appendInt32(siz);
455
456 //add the sound data
457 for (size_t l = 0; l < pp->size(); l++) {
458 connection.appendInt16(pp->at(l));
459 }
460
461 connection.appendInt32(BOTTLE_TAG_LIST);
462 connection.appendInt32(static_cast<int32_t>(m_markers.size()));
463
464 //add the markers data
465 for (const auto& it : m_markers) {
466 const std::string& key = it.first;
467 const yarp::sig::SoundMarker& marker = it.second;
468
469 connection.appendInt32(BOTTLE_TAG_LIST);
470 connection.appendInt32(4); // key + 3 fields of the struct
471
472 connection.appendString(key);
473 connection.appendString(marker.label);
474 connection.appendInt32(marker.channel);
475 connection.appendInt64(static_cast<int64_t>(marker.sample_id));
476 }
477
478 connection.convertTextMode();
479 return true;
480}
481
482unsigned char *Sound::getRawData() const
483{
484 if (m_bytesPerSample == 1)
485 {
486 auto* pp = ((std::vector<NetUint8>*)(implementation));
487 NetUint8* addr = pp->data();
488 return addr;
489 }
490 else if (m_bytesPerSample == 2)
491 {
492 auto* pp = ((std::vector<NetUint16>*)(implementation));
493 NetUint16* addr = pp->data();
494 return (unsigned char*)(addr);
495 }
496
497 yCError(SOUND, "sound only implemented for 8-16 bit samples");
498 yCAssert(SOUND, false); // that's all that's implemented right now
499 return nullptr; // unreachable, but avoids compiler warning
500}
501
502size_t Sound::getRawDataSize() const
503{
504 return this->m_bytesPerSample*this->m_channels*this->m_samples;
505}
506
507void Sound::setSafe(audio_sample value, size_t sample, size_t channel)
508{
509 if (isSample(sample, channel))
510 {
511 set(value, sample, channel);
512 }
513 else
514 {
515 yCError(SOUND) << "Sample out of bound:" << sample << "," << channel;
516 }
517}
518
520{
521 Sound news(this->m_bytesPerSample);
522 news.setFrequency(this->m_frequency);
523 news.resize(this->m_samples, 1);
524
525 unsigned char* p_src = this->getRawData();
526 unsigned char* p_dst = news.getRawData();
527 size_t j = 0;
528 //pointer to the first element of the row of the matrix we need to copy
529 size_t first_sample = 0 + (this->m_samples * this->m_bytesPerSample)*channel_id;
530 //pointer to the last element of the row of the matrix we need to copy
531 size_t last_sample = first_sample + (this->m_samples * this->m_bytesPerSample);
532 for (auto i = first_sample; i < last_sample; i++)
533 {
534 p_dst[j++] = p_src[i];
535 }
536 return news;
537}
538
539bool Sound::operator==(const Sound& alt) const
540{
541 if (this->m_channels != alt.getChannels()) {
542 return false;
543 }
544 if (this->m_bytesPerSample != alt.getBytesPerSample()) {
545 return false;
546 }
547 if (this->m_frequency != alt.getFrequency()) {
548 return false;
549 }
550 if (this->m_samples != alt.getSamples()) {
551 return false;
552 }
553 if (this->m_markers != alt.m_markers) {
554 return false;
555 }
556
557 for (size_t ch = 0; ch < this->m_channels; ch++)
558 {
559 for (size_t s = 0; s < this->m_samples; s++)
560 {
561 if (this->getSafe(s, ch) != alt.getSafe(s, ch))
562 {
563 return false;
564 }
565 }
566 }
567
568 return true;
569}
570
572{
573 if (schannel.getChannels() != 1) {
574 return false;
575 }
576 if (this->m_samples != schannel.getSamples()) {
577 return false;
578 }
579 for (size_t s = 0; s < this->m_samples; s++)
580 {
581 this->setSafe(schannel.getSafe(s, 0), s, id);
582 }
583
584 //invalidate all the markers
585 m_markers.clear();
586
587 return true;
588}
589
590std::vector<std::reference_wrapper<Sound::audio_sample>> Sound::getChannel(size_t channel_id)
591{
592 auto* pp = ((std::vector<NetUint16>*)(implementation))->data() + channel_id*m_samples;
593
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++)
597 {
598 unsigned char *addr = (unsigned char* )(pp);
599 audio_sample* addr2 = reinterpret_cast<audio_sample*>(addr);
600 vec.push_back(std::ref(*addr2));
601 }
602 return vec;
603}
604
605std::vector<std::reference_wrapper<Sound::audio_sample>> Sound::getInterleavedAudioRawData() const
606{
607 auto* pp = ((std::vector<NetUint16>*)(implementation))->data();
608
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++)
612 {
613 for (size_t c = 0; c < this->m_channels; c++)
614 {
615 audio_sample* addr = (audio_sample*)(pp) + t + c*m_samples;
616 vec.push_back(std::ref(*addr));
617 }
618 }
619 return vec;
620}
621
622std::vector<std::reference_wrapper<Sound::audio_sample>> Sound::getNonInterleavedAudioRawData() const
623{
624 auto* pp = ((std::vector<NetUint16>*)(implementation))->data();
625
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++)
629 {
630 for (size_t t = 0; t < this->m_samples; t++)
631 {
632 audio_sample* addr = (audio_sample*)(pp)+ t + c * m_samples;
633 vec.push_back(std::ref(*addr));
634 }
635 }
636 return vec;
637}
638
639std::string Sound::toString() const
640{
641 std::string s;
642 for (size_t c = 0; c < this->m_channels; c++)
643 {
644 for (size_t t = 0; t < this->m_samples; t++)
645 {
646 s += " ";
647 s += std::to_string(this->get(t, c));
648 }
649 s += '\n';
650 }
651 return s;
652}
653
654bool Sound::isSample(size_t sample, size_t channel) const
655{
656 return (sample<this->m_samples && channel<this->m_channels);
657}
658
660{
661 return this->m_bytesPerSample;
662}
663
664size_t Sound::getSamples() const
665{
666 return this->m_samples;
667}
668
669size_t Sound::getChannels() const
670{
671 return this->m_channels;
672}
673
674double Sound::getDuration() const
675{
676 return (double)(this->m_samples)*(double)(1.0 / this->m_frequency);
677}
678
679void Sound::normalizeChannel(size_t channel)
680{
681 size_t maxsampleid = 0;
684 double gain = 1 / (maxsamplevalue / 32767.0);
685 amplifyChannel(channel,gain);
686}
687
689{
690 size_t maxsampleid = 0;
691 size_t maxchannelid = 0;
694 double gain = 1 / (maxsamplevalue/32767.0);
695 amplify(gain);
696}
697
698void Sound::amplifyChannel(size_t channel, double gain)
699{
700 unsigned char* pc = this->getRawData();
701 audio_sample* p = reinterpret_cast<audio_sample*>(pc);
702 p+= this->m_samples * channel;
703
704 for (size_t t = 0; t < this->m_samples; t++, p++)
705 {
706 double amplified_value = (*p) * gain;
707 *p = (int)(amplified_value); //should i limit this range
708 }
709}
710
711void Sound::amplify(double gain)
712{
713 for (size_t c = 0; c < this->m_channels; c++)
714 {
715 amplifyChannel(c,gain);
716 }
717}
718
720{
721 sampleId = 0;
722 sampleValue = 0;
723 unsigned char* pc = this->getRawData();
724 audio_sample* p = reinterpret_cast<audio_sample*>(pc);
725 p += this->m_samples * channelId;
726
727 for (size_t t = 0; t < this->m_samples; t++, p++)
728 {
729 if (*p > sampleValue)
730 {
731 sampleValue = (*p);
732 sampleId= t;
733 }
734 }
735}
736
752
756
757void Sound::add_marker(const std::string& marker_label, size_t sample_id, int channel)
758{
760 sm.sample_id = sample_id;
761 sm.channel=channel;
762 sm.label=marker_label;
763
764 m_markers[marker_label] = sm;
765}
766
767bool Sound::get_marker(const std::string& marker_label, size_t& sample_id, int& channel) const
768{
769 auto it = m_markers.find(marker_label);
770 if (it == m_markers.end())
771 {
772 yCWarning(SOUND) << "Sound marker not found:" << marker_label;
773 sample_id=0;
774 channel=-1;
775 return false;
776 }
777
778 const SoundMarker& sm = it->second;
779 sample_id = sm.sample_id;
780 channel = sm.channel;
781 return true;
782}
783
784void Sound::remove_marker(const std::string& marker_label)
785{
786 m_markers.erase(marker_label);
787}
788
790{
791 m_markers.clear();
792}
793
795{
796 return m_markers.size();
797}
798
800{
801 if (index >= m_markers.size()) {
802 return yarp::sig::SoundMarker();
803 }
804 auto it = m_markers.begin();
805 std::advance(it, index);
806 return it->second;
807}
808
809std::vector<yarp::sig::SoundMarker> yarp::sig::Sound::getMarkers() const
810{
811 std::vector<yarp::sig::SoundMarker> v;
812 v.reserve(m_markers.size());
813 for (auto const& [key, val] : m_markers) {
814 v.push_back(val);
815 }
816 return v;
817}
818
820{
821 Sound s;
822
824 mstart.sample_id =0;
825
827 mend.sample_id = this->m_samples - 1;
828
829 auto its = m_markers.find(marker_start);
830 if (its == m_markers.end())
831 {
832 yCWarning(SOUND) << "Sound marker not found:" << marker_start;
833 }
834 else
835 {
836 mstart = its->second;
837 }
838
839 auto ite = m_markers.find(marker_end);
840 if (ite == m_markers.end())
841 {
842 yCWarning(SOUND) << "Sound marker not found:" << marker_end;
843 }
844 else
845 {
846 mend = ite->second;
847 }
848
849 s = subSound(mstart.sample_id, mend.sample_id-mstart.sample_id+1);
850 return s;
851}
#define BOTTLE_TAG_INT16
Definition Bottle.h:20
#define BOTTLE_TAG_INT32
Definition Bottle.h:21
#define BOTTLE_TAG_LIST
Definition Bottle.h:29
yarp::os::NetInt32 frequency
Definition Sound.cpp:50
yarp::os::NetInt32 samplesSizeTag
Definition Sound.cpp:43
yarp::os::NetInt32 frequencyTag
Definition Sound.cpp:49
yarp::os::NetInt32 bytesPerSample
Definition Sound.cpp:48
SoundHeader()=default
yarp::os::NetInt32 outerListTag
Definition Sound.cpp:41
yarp::os::NetInt32 samplesSize
Definition Sound.cpp:44
yarp::os::NetInt32 channelsSizeTag
Definition Sound.cpp:45
yarp::os::NetInt32 channelsSize
Definition Sound.cpp:46
yarp::os::NetInt32 outerListLen
Definition Sound.cpp:42
yarp::os::NetInt32 bytesPerSampleTag
Definition Sound.cpp:47
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.
Definition Portable.h:25
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:42
void setSafe(audio_sample value, size_t sample, size_t channel=0)
Definition Sound.cpp:507
Sound extractChannelAsSound(size_t channel_id) const
Extract a single channel from the sound.
Definition Sound.cpp:519
virtual ~Sound()
Definition Sound.cpp:262
size_t getMarkersCount() const
Gets the number of markers contained in the sound.
Definition Sound.cpp:794
bool isSample(size_t sample, size_t channel=0) const
Check whether a sample lies within the sound.
Definition Sound.cpp:654
void setFrequency(int freq)
Set the frequency of the sound (i.e.
Definition Sound.cpp:369
void zero()
set all the samples to zero (silence)
Definition Sound.cpp:319
Sound subSound(size_t first_sample, size_t len)
Returns a subpart of the sound.
Definition Sound.cpp:186
yarp::sig::SoundMarker getMarker(size_t index) const
Get the marker at the specified index.
Definition Sound.cpp:799
std::vector< std::reference_wrapper< audio_sample > > getNonInterleavedAudioRawData() const
Returns a serialized version of the sound, in non-interleaved format, e.g.
Definition Sound.cpp:622
void remove_all_markers()
Removes all the markers contained in the sound.
Definition Sound.cpp:789
const Sound & operator=(const Sound &alt)
Assignment operator.
Definition Sound.cpp:160
std::vector< yarp::sig::SoundMarker > getMarkers() const
Get all the markers contained in the sound.
Definition Sound.cpp:809
void amplifyChannel(size_t channel, double gain)
Amplify a specific channel of the sound.
Definition Sound.cpp:698
size_t getBytesPerSample() const
Get the number of bytes per sample.
Definition Sound.cpp:659
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...
Definition Sound.cpp:757
std::vector< std::reference_wrapper< audio_sample > > getInterleavedAudioRawData() const
Returns a serialized version of the sound, in interleaved format, e.g.
Definition Sound.cpp:605
double getDuration() const
Get the duration of sound in seconds.
Definition Sound.cpp:674
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition Sound.cpp:374
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.
Definition Sound.cpp:767
size_t getChannels() const
Get the number of channels of the sound.
Definition Sound.cpp:669
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 ...
Definition Sound.cpp:571
void normalize()
Normalize a sound (the peak is searched among all channels)
Definition Sound.cpp:688
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
Definition Sound.cpp:435
void amplify(double gain)
amplify a sound
Definition Sound.cpp:711
Sound(size_t bytesPerSample=2)
Definition Sound.cpp:56
void resize(size_t samples, size_t channels=1)
Set the sound size.
Definition Sound.cpp:267
void normalizeChannel(size_t channel)
Normalize a specific channel of the sound.
Definition Sound.cpp:679
void findPeak(size_t &channelId, size_t &sampleId, audio_sample &sampleValue) const
find the peak in a sound
Definition Sound.cpp:737
int getFrequency() const
Get the frequency of the sound (i.e.
Definition Sound.cpp:364
bool zeroChannel(size_t channel)
set to zero all the samples of the specified channel @ param channel the channel number
Definition Sound.cpp:326
std::string toString() const
Print matrix to a string.
Definition Sound.cpp:639
void remove_marker(const std::string &marker_label)
Removes a marker given its label.
Definition Sound.cpp:784
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.
Definition Sound.cpp:83
audio_sample get(size_t sample, size_t channel=0) const
Definition Sound.cpp:291
std::vector< std::reference_wrapper< audio_sample > > getChannel(size_t channel_id)
Definition Sound.cpp:590
Sound & operator+=(const Sound &alt)
Addition assignment operator.
Definition Sound.cpp:118
bool operator==(const Sound &alt) const
Comparison operator.
Definition Sound.cpp:539
void set(audio_sample value, size_t sample, size_t channel=0)
Definition Sound.cpp:342
size_t getSamples() const
Get the number of samples contained in the sound.
Definition Sound.cpp:664
audio_sample getSafe(size_t sample, size_t channel=0) const
Definition Sound.h:110
void findPeakInChannel(size_t channelId, size_t &sampleId, audio_sample &sampleValue) const
find the peak in a specific channel of the sound
Definition Sound.cpp:719
#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.
Definition OSTestMsg.cpp:13
std::uint16_t NetUint16
Definition of the NetUint16 type.
Definition NetUint16.h:29
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition NetInt32.h:29
std::uint8_t NetUint8
Definition of the NetUint8 type.
Definition NetUint8.h:14
The main, catch-all namespace for YARP.
Definition dirs.h:16
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition system.h:193
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition system.h:192