YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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>
14#include <yarp/os/LogStream.h>
15
16using namespace yarp::os;
17using namespace yarp::dev;
18using namespace yarp::sig;
19
20constexpr double c_DEFAULT_PERIOD = 0.01; //s
21
22namespace {
23YARP_LOG_COMPONENT(FAKESPEAKER, "yarp.device.fakeSpeaker")
24}
25
26typedef unsigned short int audio_sample_16t;
27
32
37
39{
40 if (!this->parseParams(config)) {return false;}
41
42 bool b = configurePlayerAudioDevice(config.findGroup("AUDIO_BASE"), "fakeSpeaker");
43 if (!b) { return false; }
44
45 //sets the thread period
47
48 //start the capture thread
49 start();
50 return true;
51}
52
54{
56
57 //wait until the thread is stopped...
58
59 return true;
60}
61
62
63bool FakeSpeaker::threadInit()
64{
65 return true;
66}
67
68void FakeSpeaker::run()
69{
70 // when not playing, do nothing
72 {
73 return;
74 }
75 // if the buffer is empty, do nothing
76 if (m_outputBuffer->size().getSamples() == 0)
77 {
78 return;
79 }
80
81 //playing implies emptying all the buffer
84 size_t siz_byt = m_outputBuffer->size().getBytes();
85 size_t buffer_size = siz_sam * siz_chn;
86 for (size_t i = 0; i<buffer_size; i++)
87 {
89 s= audio_sample_16t(double(s)*m_hw_gain);
90 // if a stop is received during the playback, then exits...
91 if (!m_playback_enabled) {return;}
92 }
93
94 //the playback is complete...
95 if (m_outputBuffer->size().getSamples()==0)
96 {
97 yCDebug(FAKESPEAKER) << "Sound Playback complete";
98 yCDebug(FAKESPEAKER) << "Played " << siz_sam << " samples, " << siz_chn << " channels, " << siz_byt << " bytes";
99 }
100}
101
103{
104 std::lock_guard<std::recursive_mutex> lock(m_mutex);
105 if (gain > 0)
106 {
107 m_hw_gain = gain;
108 return ReturnValue_ok;
109 }
110 return ReturnValue::return_code::return_value_error_method_failed;
111}
112
114{
115 yCError(FAKESPEAKER, "configureDeviceAndStart() Not yet implemented");
116 return true;
117}
118
120{
121 yCError(FAKESPEAKER, "interruptDeviceAndClose() Not yet implemented");
122 return true;
123}
unsigned short int audio_sample_16t
constexpr double c_DEFAULT_PERIOD
unsigned short int audio_sample_16t
#define ReturnValue_ok
Definition ReturnValue.h:77
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
~FakeSpeaker() override
virtual bool interruptDeviceAndClose() override
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
virtual yarp::dev::ReturnValue setHWGain(double gain) override
Sets the hardware gain of the playback device (if supported by the hardware)
virtual bool configureDeviceAndStart() override
bool close() override
Close the DeviceDriver.
yarp::dev::CircularAudioBuffer_16t * m_outputBuffer
bool configurePlayerAudioDevice(yarp::os::Searchable &config, std::string device_name)
yarp::sig::AudioBufferSize size()
A mini-server for performing network communication in the background.
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:31
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.