YARP
Yet Another Robot Platform
fakeSpeaker.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 
6 #include "fakeSpeaker.h"
7 
8 #include <string>
9 #include <yarp/os/Thread.h>
10 #include <yarp/os/Time.h>
11 #include <yarp/os/Semaphore.h>
12 #include <yarp/os/Stamp.h>
13 #include <yarp/os/LogComponent.h>
14 #include <yarp/os/LogStream.h>
15 
16 using namespace yarp::os;
17 using namespace yarp::dev;
18 using namespace yarp::sig;
19 
20 constexpr double c_DEFAULT_PERIOD = 0.01; //s
21 
22 namespace {
23 YARP_LOG_COMPONENT(FAKESPEAKER, "yarp.device.fakeSpeaker")
24 }
25 
26 typedef unsigned short int audio_sample_16t;
27 
30 {
31 }
32 
34 {
35  close();
36 }
37 
39 {
40  if (config.check("help"))
41  {
42  yCInfo(FAKESPEAKER, "Some examples:");
43  yCInfo(FAKESPEAKER, "yarpdev --device fakeSpeaker --help");
44  yCInfo(FAKESPEAKER, "yarpdev --device AudioPlayerWrapper --subdevice fakeSpeaker --start");
45  return false;
46  }
47 
48  bool b = configurePlayerAudioDevice(config.findGroup("AUDIO_BASE"), "fakeSpeaker");
49  if (!b) { return false; }
50 
51  //sets the thread period
52  if( config.check("period"))
53  {
54  double period = config.find("period").asFloat64();
55  setPeriod(period);
56  yCInfo(FAKESPEAKER) << "Using chosen period of " << period << " s";
57  }
58  else
59  {
60  yCInfo(FAKESPEAKER) << "Using default period of " << c_DEFAULT_PERIOD << " s";
61  }
62 
63  //start the capture thread
64  start();
65  return true;
66 }
67 
69 {
71 
72  //wait until the thread is stopped...
73 
74  return true;
75 }
76 
77 
78 bool fakeSpeaker::threadInit()
79 {
80  return true;
81 }
82 
83 void fakeSpeaker::run()
84 {
85  // when not playing, do nothing
86  if (!m_playback_enabled)
87  {
88  return;
89  }
90  // if the buffer is empty, do nothing
91  if (m_outputBuffer->size().getSamples() == 0)
92  {
93  return;
94  }
95 
96  //playing implies emptying all the buffer
97  size_t siz_sam = m_outputBuffer->size().getSamples();
98  size_t siz_chn = m_outputBuffer->size().getChannels();
99  size_t siz_byt = m_outputBuffer->size().getBytes();
100  size_t buffer_size = siz_sam * siz_chn;
101  for (size_t i = 0; i<buffer_size; i++)
102  {
104  s= audio_sample_16t(double(s)*m_hw_gain);
105  // if a stop is received during the playback, then exits...
106  if (!m_playback_enabled) {return;}
107  }
108 
109  //the playback is complete...
110  if (m_outputBuffer->size().getSamples()==0)
111  {
112  yCDebug(FAKESPEAKER) << "Sound Playback complete";
113  yCDebug(FAKESPEAKER) << "Played " << siz_sam << " samples, " << siz_chn << " channels, " << siz_byt << " bytes";
114  }
115 }
116 
117 bool fakeSpeaker::setHWGain(double gain)
118 {
119  std::lock_guard<std::recursive_mutex> lock(m_mutex);
120  if (gain > 0)
121  {
122  m_hw_gain = gain;
123  return true;
124  }
125  return false;
126 }
127 
129 {
130  yCError(FAKESPEAKER, "configureDeviceAndStart() Not yet implemented");
131  return true;
132 }
133 
135 {
136  yCError(FAKESPEAKER, "interruptDeviceAndClose() Not yet implemented");
137  return true;
138 }
unsigned short int audio_sample_16t
~fakeSpeaker() override
Definition: fakeSpeaker.cpp:33
virtual bool configureDeviceAndStart() override
bool close() override
Close the DeviceDriver.
Definition: fakeSpeaker.cpp:68
virtual bool interruptDeviceAndClose() override
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: fakeSpeaker.cpp:38
virtual bool setHWGain(double gain) override
Sets the hardware gain of the playback device (if supported by the hardware)
yarp::dev::CircularAudioBuffer_16t * m_outputBuffer
bool configurePlayerAudioDevice(yarp::os::Searchable &config, std::string device_name)
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:222
constexpr double c_DEFAULT_PERIOD
Definition: fakeSpeaker.cpp:20
unsigned short int audio_sample_16t
Definition: fakeSpeaker.cpp:26
#define yCInfo(component,...)
Definition: LogComponent.h:132
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCDebug(component,...)
Definition: LogComponent.h:109
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22