YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SoundFilter_resample.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
9
10using namespace yarp::os;
11
12namespace {
13YARP_LOG_COMPONENT(SOUNDFILTER_RESAMPLE, "soundfilter_resample")
14
15} //anonymous namespace
16
18{
19 yCDebug(SOUNDFILTER_RESAMPLE, "created!\n");
20 yCDebug(SOUNDFILTER_RESAMPLE, "I am attached to the %s\n",
21 (options.find("sender_side").asBool()) ? "sender side" : "receiver side");
22
23 //set the user parameters
24 m_channel = (options.check("channel") ? options.find("channel").asInt8() : -1);
25 m_output_freq = (options.check("frequency") ? options.find("frequency").asInt32() : -1);
26 m_gain = (options.check("gain_percent") ? options.find("gain_percent").asFloat32() : -1)/100;
27
28 if (m_channel < 0)
29 {
30 yCInfo(SOUNDFILTER_RESAMPLE, "no channels selected for extraction, using all sound channels");
31 }
32 else
33 {
34 yCInfo(SOUNDFILTER_RESAMPLE, "channel selected: %d", m_channel);
35 }
36
37 if (m_gain < 0)
38 {
39 yCInfo(SOUNDFILTER_RESAMPLE, "No amplification requested");
40 }
41 else
42 {
43 yCInfo(SOUNDFILTER_RESAMPLE, "Amplification gain: %f (%d%%)", m_gain, int(m_gain*100));
44 }
45
46 if (m_output_freq < 0)
47 {
48 yCInfo(SOUNDFILTER_RESAMPLE, "No resampling requested");
49 }
50 else if (m_output_freq == 0)
51 {
52 yCError(SOUNDFILTER_RESAMPLE, "Invalid frequency value selected");
53 return false;
54 }
55 else
56 {
57 yCInfo(SOUNDFILTER_RESAMPLE, "output frequency : %d", m_output_freq);
58 }
59
60 return true;
61}
62
66
68{
69 m_channel = params.find("channel").asInt8();
70 m_output_freq = params.find("frequency").asInt32();
71 return false;
72}
73
75{
76 params.put("channel", m_channel);
77 params.put("frequency", m_output_freq);
78 return false;
79}
80
82{
84 if (bt == NULL)
85 {
86 yCWarning(SOUNDFILTER_RESAMPLE, "expected type Sound but got wrong data type!\n");
87 return false;
88 }
89
90 return true;
91}
92
94{
95 //get data to process
97 if (s == NULL)
98 {
99 yCWarning(SOUNDFILTER_RESAMPLE, "expected type Sound but got wrong data type!\n");
100 return thing;
101 }
102
103 //extract one channel
104 m_s2.clear();
105 if (m_channel >=0)
106 {
107 m_s2 = s->extractChannelAsSound(m_channel);
108 }
109 else
110 {
111 m_s2 = *s;
112 }
113
114 if (m_gain >= 0)
115 {
116 m_s2.amplify(m_gain);
117 }
118
119 if (m_output_freq>0)
120 {
121 bool b= yarp::sig::soundfilters::resample (m_s2,m_output_freq);
122 if (!b) yCError(SOUNDFILTER_RESAMPLE, "Resample failed!\n");
123 }
124
125 //send data
126 m_th.setPortWriter(&m_s2);
127 return m_th;
128}
129
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
yarp::os::Things & update(yarp::os::Things &thing) override
After data get accpeted in the accept() callback, an instance of that is given to the update function...
void destroy() override
This will be called when the portmonitor object destroyes.
bool accept(yarp::os::Things &thing) override
This will be called when the data reach the portmonitor object.
void trig() override
This will be called when one of the peer connections to the same import port receives data.
bool getparam(yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are requested via YARP admin port.
bool setparam(const yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are set via YARP admin port.
A mini-server for performing network communication in the background.
A class for storing options and configuration information.
Definition Property.h:32
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:980
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Base class for generic things.
Definition Things.h:18
T * cast_as()
Definition Things.h:53
void setPortWriter(yarp::os::PortWriter *writer)
Set the reference to a PortWriter object.
Definition Things.cpp:26
virtual std::int8_t asInt8() const
Get 8-bit integer value.
Definition Value.cpp:192
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:186
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:204
virtual yarp::conf::float32_t asFloat32() const
Get 32-bit floating point value.
Definition Value.cpp:216
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:39
Sound extractChannelAsSound(size_t channel_id) const
Extract a single channel from the sound.
Definition Sound.cpp:519
void amplify(double gain)
amplify a sound
Definition Sound.cpp:711
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface to the operating system, including Port based communication.
bool resample(yarp::sig::Sound &snd, size_t frequency)
Resample a sound.