YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
soundfilter_resampleTest.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
20#include <catch2/catch_amalgamated.hpp>
21#include <harness.h>
22
23TEST_CASE("pm::soundfilter_resampleTest", "[yarp::pm]")
24{
25 YARP_REQUIRE_PLUGIN("soundfilter_resample", "portmonitor")
26
28
31
32 REQUIRE(yarp::os::Carriers::getCarrierTemplate("portmonitor") != nullptr);
33
34 struct TestCase {
35 std::string carrier;
36 int expected_frequency;
37 int channels;
38 };
39
40 auto tc = GENERATE(
41 TestCase {"fast_tcp", 16000 ,2},
42 TestCase {"fast_tcp+recv.portmonitor+file.soundfilter_resample+channel.0+frequency.8000+gain_percent.200+type.dll", 8000, 1}
43 );
44
45 SECTION("Test sound resampling")
46 {
49
50 sender.open("/send");
51 receiver.open("/recv");
52
53 bool b = yarp::os::Network::connect("/send", "/recv", tc.carrier);
54 REQUIRE(b);
55
56 // Create a test sound with 16000 Hz
57 const int original_frequency = 16000;
58 const size_t samples = 1600;
59 const size_t channels = 2;
60
61 yarp::sig::Sound& snd = sender.prepare();
62 snd.setFrequency(original_frequency);
63 snd.resize(samples, channels);
64
65 // Fill with a simple sine wave pattern
66 yarp::sig::utils::makeTone(snd,0.1, channels, original_frequency, 440);
67
68 sender.write();
69
71
72 yarp::sig::Sound* received = receiver.read();
73 REQUIRE(received != nullptr);
74
75 // Verify the frequency is as expected (original or resampled)
76 if (received->getSamples() !=0)
77 {
78 CHECK(received->getFrequency() == tc.expected_frequency);
79 CHECK(received->getChannels() == tc.channels);
80 }
81 else
82 {
83 // Probably the resampling filter dropped all samples.
84 // This may happen currently if libsoxr is not installed
85 }
86 receiver.close();
87 sender.close();
88 }
89
91}
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
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::soundfilter_resampleTest", "[yarp::pm]")