YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include <QApplication>
7#include <QWidget>
8#include <QPushButton>
9#include <QHBoxLayout>
10#include <QLabel>
11#include <QThread>
12#include <QVBoxLayout>
13
14#include <yarp/os/Os.h>
15#include <yarp/os/Log.h>
16#include <yarp/os/LogStream.h>
17#include <yarp/os/Network.h>
18#include <yarp/os/RFModule.h>
19#include <yarp/os/Property.h>
20#include <yarp/os/Thread.h>
21#include <yarp/dev/PolyDriver.h>
23
24#ifdef _WIN32
25#include <Windows.h>
26#endif
27
28#include <iostream>
29
30class CustomWidget : public QWidget, public yarp::os::Thread
31{
32 Q_OBJECT
33private:
34 QPushButton* startButton = nullptr;
35 QPushButton* stopButton = nullptr;
36 QVBoxLayout* vlayout = nullptr;
37 QHBoxLayout* hlayout = nullptr;
38 QLabel* label = nullptr;
39
40public:
41 virtual ~CustomWidget ()
42 {
43 this->stop();
44 hlayout->removeWidget(startButton);
45 hlayout->removeWidget(stopButton);
46 if (startButton) { delete startButton; startButton = nullptr;}
47 if (stopButton) { delete stopButton; stopButton = nullptr;}
48 vlayout->removeWidget(label);
49 if (label) { delete label; label = nullptr; }
50 vlayout->removeItem(hlayout);
51 if (vlayout) { delete vlayout; vlayout = nullptr;}
52 if (hlayout) {delete hlayout; hlayout = nullptr;}
53 }
54
55 CustomWidget(QWidget *parent = nullptr) : QWidget(parent)
56 {
57 startButton = new QPushButton("Start", this);
58 stopButton = new QPushButton("Stop", this);
59 vlayout = new QVBoxLayout;
60 hlayout = new QHBoxLayout;
61 label = new QLabel("Device status: - - -");
62 hlayout->addWidget(startButton);
63 hlayout->addWidget(stopButton);
64 vlayout->addLayout(hlayout);
65 vlayout->addWidget(label);
66 setLayout(vlayout);
67
68 connect(startButton, &QPushButton::clicked, this, &CustomWidget::startFunction);
69 connect(stopButton, &QPushButton::clicked, this, &CustomWidget::stopFunction);
70 }
71
72 void run() override
73 {
74 while(!isStopping())
75 {
76 if (m_iAudioRec)
77 {
78 bool enabled;
79 m_iAudioRec->isRecording(enabled);
80 if (enabled) label->setText("Device status : recording");
81 else label->setText("Device status : idle");
82 }
83 if (m_iAudioPlay)
84 {
85 bool enabled;
86 m_iAudioPlay->isPlaying(enabled);
87 if (enabled) label->setText("Device status : playing");
88 else label->setText("Device status : idle");
89 }
91 }
92 }
93
94public slots:
96 {
97 if (m_iAudioRec) { m_iAudioRec->startRecording(); }
98 if (m_iAudioPlay) { m_iAudioPlay->startPlayback(); }
99 }
100
102 {
103 if (m_iAudioRec) { m_iAudioRec->stopRecording(); }
104 if (m_iAudioPlay) { m_iAudioPlay->stopPlayback(); }
105 }
106
107public:
108 void setInterface(yarp::dev::IAudioGrabberSound* audio_rec_interface, yarp::dev::IAudioRender* audio_play_interface)
109 {
110 m_iAudioRec = audio_rec_interface;
111 m_iAudioPlay = audio_play_interface;
112 this->start();
113 }
114
115private:
116 yarp::dev::IAudioGrabberSound* m_iAudioRec = nullptr;
117 yarp::dev::IAudioRender* m_iAudioPlay = nullptr;
118};
119
120#define ERROR_RETURN_CODE 0
121int main(int argc, char *argv[])
122{
123 //console output on windows
124 #ifdef _WIN32
125 AllocConsole();
126 freopen("CONOUT$", "w", stdout);
127 freopen("CONOUT$", "w", stderr);
128 #endif
129
130 // Yarp initialization
132 if (!yarp.checkNetwork())
133 {
134 yError("Error initializing yarp network (is yarpserver running?)");
135 return ERROR_RETURN_CODE;
136 }
138 rf.configure(argc, argv);
139
140 //check parameters
141 if (rf.check("help"))
142 {
143 yInfo() << "yarpaudiocontrolgui accepts the following options:";
144 yInfo() << "--local <portname> an optional parameter which defines the name of the local port. Default is /yarpaudiocontrolgui. Multiple yarpaudiocontrolgui must use different local names.";
145 yInfo() << "--remote-rec <portname> an optional parameter which defines the name of the rpc port of a audioRecorder_nws_yarp device.";
146 yInfo() << "--remote-play <portname> an optional parameter which defines the name of the rpc port of a audioPlayer_nws_yarp device.";
147 yInfo() << "NOTE: remote-rec and remote-play options are mutually exclusive.";
148 return 0;
149 }
150
151 std::string local = rf.find("local").asString();
152 std::string remote_rec = rf.find("remote-rec").asString();
153 std::string remote_play = rf.find("remote-play").asString();
154 if (local.empty()) { local = "/yarpaudiocontrolgui"; }
155 if (remote_play.empty() && remote_rec.empty())
156 {
157 yError() << "Please choose at least a recorder or a player device. Use --help for list of options.";
158 return ERROR_RETURN_CODE;
159 }
160 if (!remote_play.empty() && !remote_rec.empty())
161 {
162 yError() << "Please choose either a recorder or a player device, not both. remote-rec and remote-play options are mutually exclusive.";
163 return ERROR_RETURN_CODE;
164 }
165
166 //yarp audio client instance
167 yarp::dev::IAudioGrabberSound* iAudioRec = nullptr;
168 yarp::dev::IAudioRender* iAudioPlay = nullptr;
171
172 if (!remote_play.empty())
173 {
174 yarp::os::Property p_cfgPlay;
175 p_cfgPlay.put("device", "audioPlayer_nwc_yarp");
176 p_cfgPlay.put("remote", remote_play);
177 p_cfgPlay.put("local", local);
178 ddPlay.open(p_cfgPlay);
179 ddPlay.view(iAudioPlay);
180 if (iAudioPlay == nullptr)
181 {
182 yError()<<"Failed to open audioPlayerInterface";
183 return ERROR_RETURN_CODE;
184 }
185 }
186 if (!remote_rec.empty())
187 {
188 yarp::os::Property p_cfgRec;
189 p_cfgRec.put("device", "audioRecorder_nwc_yarp");
190 p_cfgRec.put("remote", remote_rec);
191 p_cfgRec.put("local", local);
192 ddRec.open(p_cfgRec);
193 ddRec.view(iAudioRec);
194 if (iAudioRec == nullptr)
195 {
196 yError() << "Failed to open audioRecorderInterface";
197 return ERROR_RETURN_CODE;
198 }
199 }
200
201 //qt initialization
202 QApplication app(argc, argv);
203 CustomWidget* widget = new CustomWidget;
204 widget->setInterface(iAudioRec, iAudioPlay);
205 widget->setMinimumWidth(400);
206 widget->show();
207 int r = app.exec();
208
209 //clean up
210 if (widget) {delete widget; widget = nullptr;}
211 ddRec.close();
212 ddPlay.close();
213
214 yInfo() << "yarpaudiocontrolgui says goodbye!";
215 return r;
216}
217
218#include "main.moc"
#define yInfo(...)
Definition Log.h:319
#define yError(...)
Definition Log.h:361
void run() override
Main body of the new thread.
Definition main.cpp:72
CustomWidget(QWidget *parent=nullptr)
Definition main.cpp:55
void stopFunction()
Definition main.cpp:101
void startFunction()
Definition main.cpp:95
virtual ~CustomWidget()
Definition main.cpp:41
void setInterface(yarp::dev::IAudioGrabberSound *audio_rec_interface, yarp::dev::IAudioRender *audio_play_interface)
Definition main.cpp:108
bool view(T *&x)
Get an interface to the device driver.
Read a YARP-format sound block from a device.
virtual yarp::dev::ReturnValue stopRecording()=0
Stop the recording.
virtual yarp::dev::ReturnValue isRecording(bool &recording_enabled)=0
Check if the recording has been enabled (e.g.
virtual yarp::dev::ReturnValue startRecording()=0
Start the recording.
Interface for rendering a YARP-format sound and controlling its reproduction ona device.
virtual yarp::dev::ReturnValue startPlayback()=0
Start the playback.
virtual yarp::dev::ReturnValue stopPlayback()=0
Stop the playback.
virtual yarp::dev::ReturnValue isPlaying(bool &playback_enabled)=0
Check if the playback has been enabled (e.g.
A container for a device driver.
Definition PolyDriver.h:23
bool close() override
Close the DeviceDriver.
bool open(const std::string &txt)
Construct and configure a device by its common name.
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:706
A class for storing options and configuration information.
Definition Property.h:33
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
Helper class for finding config files and other external resources.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
An abstraction for a thread of execution.
Definition Thread.h:21
bool stop()
Stop the thread.
Definition Thread.cpp:81
bool isStopping()
Returns true if the thread is stopping (Thread::stop has been called).
Definition Thread.cpp:99
bool start()
Start the new thread running.
Definition Thread.cpp:93
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
int main(int argc, char *argv[])
Definition main.cpp:385
#define ERROR_RETURN_CODE
Definition main.cpp:120
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
@ YARP_CLOCK_SYSTEM
Definition Time.h:28
The main, catch-all namespace for YARP.
Definition dirs.h:16