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/sig/Sound.h>
12#include <yarp/sig/SoundUtils.h>
13
15
16#include <array>
17#include <thread>
18#include <cmath>
19
20#include <catch2/catch_amalgamated.hpp>
21#include <harness.h>
22
23TEST_CASE("pm::sound_compression_mp3Test", "[yarp::pm]")
24{
25 YARP_REQUIRE_PLUGIN("sound_compression_mp3", "portmonitor")
26
28
31
32 struct TestCase {
33 std::string carrier;
34 bool use_compression;
35 };
36
37 auto tc = GENERATE(
38 TestCase {"fast_tcp", false},
39 //NOT YET IMPEMENTED
40 TestCase {"fast_tcp+send.portmonitor+file.sound_compression_mp3+type.dll+recv.portmonitor+file.sound_compression_mp3+type.dll", true}
41 );
42
43 SECTION("Test sound transmission with and without MP3 compression")
44 {
47
48 sender.open("/send");
49 receiver.open("/recv");
50
51 bool b = yarp::os::Network::connect("/send", "/recv", tc.carrier);
52 REQUIRE(b);
53
54 // Create a test sound with known properties
55 const int frequency = 16000;
56 const size_t samples = 1600;
57 const size_t channels = 2;
58
59 yarp::sig::Sound& snd = sender.prepare();
60 snd.setFrequency(frequency);
61 snd.resize(samples, channels);
62
63 // Fill with a simple sine wave pattern
64 yarp::sig::utils::makeTone(snd,0.1, channels, frequency, 440);
65
66 sender.write();
67
69
70 yarp::sig::Sound* received = receiver.read();
71 REQUIRE(received != nullptr);
72
73 // Verify basic properties are preserved
74 CHECK(received->getFrequency() == frequency);
75 CHECK(received->getSamples() == samples);
76 CHECK(received->getChannels() == channels);
77
78 // For MP3 compression, allow some tolerance due to lossy compression
79 if (tc.use_compression) {
80 // Just verify the sound has reasonable values (not all zeros)
81 size_t has_non_zero = 0;
82 for (size_t i = 0; i < samples && !has_non_zero; i++) {
83 for (size_t c = 0; c < channels; c++) {
84 if (received->get(i, c) != 0) {
85 has_non_zero++;
86 break;
87 }
88 }
89 }
90 CHECK(has_non_zero<50);
91 } else {
92 // Without compression, data should match exactly
93 for (size_t i = 0; i < samples; i++) {
94 for (size_t c = 0; c < channels; c++) {
95 CHECK(received->get(i, c) == snd.get(i, c));
96 }
97 }
98 }
99
100 receiver.close();
101 sender.close();
102 }
103
105}
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 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:751
static bool setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition Network.cpp:1118
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:42
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]")