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
15void split(const std::string& s, char delim, std::vector<std::string>& elements)
16{
17 std::istringstream iss(s);
18 std::string item;
19 while (std::getline(iss, item, delim)) {
20 elements.push_back(item);
21 }
22}
23} //anonymous namespace
24
26{
27 // Split command line string using '+' delimiter
28 std::vector<std::string> parameters;
29 split(carrierString, '+', parameters);
30
31 // Iterate over result strings
32 for (std::string param : parameters)
33 {
34 // If there is no '.', then the param is bad formatted, skip it.
35 auto pointPosition = param.find('.');
36 if (pointPosition == std::string::npos)
37 {
38 continue;
39 }
40
41 // Otherwise, separate key and value
42 std::string paramKey = param.substr(0, pointPosition);
44 std::string s = param.substr(pointPosition + 1, param.length());
45 paramValue.fromString(s.c_str());
46
47 //and append to the returned property
48 prop.put(paramKey, paramValue);
49 }
50 return;
51}
52
54{
55 yCDebug(SOUNDFILTER_RESAMPLE, "created!\n");
56 yCDebug(SOUNDFILTER_RESAMPLE, "I am attached to the %s\n",
57 (options.find("sender_side").asBool()) ? "sender side" : "receiver side");
58
59 //parse the user parameters
61 yCDebug(SOUNDFILTER_RESAMPLE) << "user params:" << options.toString();
62 std::string str = options.find("carrier").asString();
64 yCDebug(SOUNDFILTER_RESAMPLE) << "parsed params:" << m_user_params.toString();
65
66 //set the user parameters
67 m_channel = (m_user_params.check("channel") ? m_user_params.find("channel").asInt8() : -1);
68 m_output_freq = (m_user_params.check("frequency") ? m_user_params.find("frequency").asInt32() : -1);
69 m_gain = (m_user_params.check("gain_percent") ? m_user_params.find("gain_percent").asFloat32() : -1)/100;
70
71 if (m_channel < 0)
72 {
73 yCInfo(SOUNDFILTER_RESAMPLE, "no channels selected for extraction, using all sound channels");
74 }
75 else
76 {
77 yCInfo(SOUNDFILTER_RESAMPLE, "channel selected: %d", m_channel);
78 }
79
80 if (m_gain < 0)
81 {
82 yCInfo(SOUNDFILTER_RESAMPLE, "No amplification requested");
83 }
84 else
85 {
86 yCInfo(SOUNDFILTER_RESAMPLE, "Amplification gain: %f (%d%%)", m_gain, int(m_gain*100));
87 }
88
89 if (m_output_freq < 0)
90 {
91 yCInfo(SOUNDFILTER_RESAMPLE, "No resampling requested");
92 }
93 else if (m_output_freq == 0)
94 {
95 yCError(SOUNDFILTER_RESAMPLE, "Invalid frequency value selected");
96 return false;
97 }
98 else
99 {
100 yCInfo(SOUNDFILTER_RESAMPLE, "output frequency : %d", m_output_freq);
101 }
102
103 return true;
104}
105
109
111{
112 m_channel = params.find("channel").asInt8();
113 m_output_freq = params.find("frequency").asInt32();
114 return false;
115}
116
118{
119 params.put("channel", m_channel);
120 params.put("frequency", m_output_freq);
121 return false;
122}
123
125{
127 if (bt == NULL)
128 {
129 yCWarning(SOUNDFILTER_RESAMPLE, "expected type Sound but got wrong data type!\n");
130 return false;
131 }
132
133 return true;
134}
135
137{
138 //get data to process
140 if (s == NULL)
141 {
142 yCWarning(SOUNDFILTER_RESAMPLE, "expected type Sound but got wrong data type!\n");
143 return thing;
144 }
145
146 //extract one channel
147 m_s2.clear();
148 if (m_channel >=0)
149 {
150 m_s2 = s->extractChannelAsSound(m_channel);
151 }
152 else
153 {
154 m_s2 = *s;
155 }
156
157 if (m_gain >= 0)
158 {
159 m_s2.amplify(m_gain);
160 }
161
162 if (m_output_freq>0)
163 {
164 yarp::sig::soundfilters::resample (m_s2,m_output_freq);
165 }
166
167 //send data
168 m_th.setPortWriter(&m_s2);
169 return m_th;
170}
171
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 getParamsFromCommandLine(std::string carrierString, yarp::os::Property &prop)
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:33
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
std::string toString() const override
Return a standard text representation of the content of the object.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
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
A single value (typically within a Bottle).
Definition Value.h:43
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 std::string asString() const
Get string value.
Definition Value.cpp:234
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:25
Sound extractChannelAsSound(size_t channel_id) const
Extract a single channel from the sound.
Definition Sound.cpp:460
void amplify(double gain)
amplify a sound
Definition Sound.cpp:645
void clear()
set all the samples to zero (silence)
Definition Sound.cpp:315
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.
An interface to the operating system, including Port based communication.
bool resample(yarp::sig::Sound &snd, size_t frequency)
Resample a sound.