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