YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
sound_compression_mp3Test.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7#include <yarp/os/Network.h>
8#include <yarp/os/LogStream.h>
9#include <yarp/os/Time.h>
11#include <yarp/os/Carriers.h>
12#include <yarp/sig/Sound.h>
13#include <yarp/sig/SoundUtils.h>
14
16
17#include <array>
18#include <thread>
19#include <cmath>
20
21#include <catch2/catch_amalgamated.hpp>
22#include <harness.h>
23
24TEST_CASE("pm::sound_compression_mp3Test", "[yarp::pm]")
25{
26 YARP_REQUIRE_PLUGIN("sound_compression_mp3", "portmonitor")
27
29
31
32 REQUIRE(yarp::os::Carriers::getCarrierTemplate("portmonitor") != nullptr);
33
35
36 struct TestCase {
37 std::string carrier;
38 bool use_compression;
39 };
40
41 auto tc = GENERATE(
42 TestCase {"fast_tcp", false},
43 //NOT YET IMPEMENTED
44 TestCase {"fast_tcp+send.portmonitor+file.sound_compression_mp3+type.dll+recv.portmonitor+file.sound_compression_mp3+type.dll", true}
45 );
46
47 SECTION("Test sound transmission with and without MP3 compression")
48 {
51
52 sender.open("/send");
53 receiver.open("/recv");
54
55 bool b = yarp::os::Network::connect("/send", "/recv", tc.carrier);
56 REQUIRE(b);
57
58 // Create a test sound with known properties
59 const int frequency = 16000;
60 const size_t samples = 1600;
61 const size_t channels = 2;
62
63 yarp::sig::Sound& snd = sender.prepare();
64 snd.setFrequency(frequency);
65 snd.resize(samples, channels);
66
67 // Fill with a simple sine wave pattern
68 yarp::sig::utils::makeTone(snd,0.1, channels, frequency, 440);
69
70 sender.write();
71
73
74 yarp::sig::Sound* received = receiver.read();
75 REQUIRE(received != nullptr);
76
77 // Verify basic properties are preserved
78 CHECK(received->getFrequency() == frequency);
79 CHECK(received->getSamples() == samples);
80 CHECK(received->getChannels() == channels);
81
82 // For MP3 compression, allow some tolerance due to lossy compression
83 if (tc.use_compression) {
84 // Just verify the sound has reasonable values (not all zeros)
85 size_t has_non_zero = 0;
86 for (size_t i = 0; i < samples && !has_non_zero; i++) {
87 for (size_t c = 0; c < channels; c++) {
88 if (received->get(i, c) != 0) {
89 has_non_zero++;
90 break;
91 }
92 }
93 }
94 CHECK(has_non_zero<50);
95 } else {
96 // Without compression, data should match exactly
97 for (size_t i = 0; i < samples; i++) {
98 for (size_t c = 0; c < channels; c++) {
99 CHECK(received->get(i, c) == snd.get(i, c));
100 }
101 }
102 }
103
104 receiver.close();
105 sender.close();
106 }
107
109}
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
T * read(bool shouldWait=true) override
Read an available object from the port.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
static Carrier * getCarrierTemplate(const std::string &name)
Get template for carrier.
Definition Carriers.cpp:238
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition Network.cpp:784
static bool setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition Network.cpp:1137
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:718
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:39
void setFrequency(int freq)
Set the frequency of the sound (i.e.
Definition Sound.cpp:369
size_t getChannels() const
Get the number of channels of the sound.
Definition Sound.cpp:669
void resize(size_t samples, size_t channels=1)
Set the sound size.
Definition Sound.cpp:267
int getFrequency() const
Get the frequency of the sound (i.e.
Definition Sound.cpp:364
audio_sample get(size_t sample, size_t channel=0) const
Definition Sound.cpp:291
size_t getSamples() const
Get the number of samples contained in the sound.
Definition Sound.cpp:664
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
@ YARP_CLOCK_SYSTEM
Definition Time.h:28
bool makeTone(Sound &outSound, double duration_s=1.0, size_t channels=1, size_t sampleRate=16000, double frequency=440, size_t amplitude=30000)
Generate a sound containing a sine wave tone.
The main, catch-all namespace for YARP.
Definition dirs.h:16
TEST_CASE("pm::sound_compression_mp3Test", "[yarp::pm]")