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