YARP
Yet Another Robot Platform
fakeSpeaker.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include "fakeSpeaker.h"
10 
11 #include <string>
12 #include <yarp/os/Thread.h>
13 #include <yarp/os/Time.h>
14 #include <yarp/os/Semaphore.h>
15 #include <yarp/os/Stamp.h>
16 #include <yarp/os/LogComponent.h>
17 #include <yarp/os/LogStream.h>
18 
19 using namespace yarp::os;
20 using namespace yarp::dev;
21 using namespace yarp::sig;
22 
23 #define HW_CHANNELS 2
24 #define SAMPLING_RATE 44100
25 
26 namespace {
27 YARP_LOG_COMPONENT(FAKESPEAKER, "yarp.device.fakeSpeaker")
28 }
29 
30 typedef unsigned short int audio_sample_16t;
31 
34 {
35 }
36 
38 {
39  close();
40 }
41 
43 {
44  //sets the thread period
45  if( config.check("period"))
46  {
47  double period = config.find("period").asFloat64();
48  setPeriod(period);
49  yCInfo(FAKESPEAKER) << "Using chosen period of " << period << " s";
50  }
51  else
52  {
53  yCInfo(FAKESPEAKER) << "Using default period of " << DEFAULT_PERIOD << " s";
54  }
55 
56  //configuration of the simulated audio card
57  m_cfg_numSamples = config.check("samples",Value(SAMPLING_RATE),"Number of samples per network packet.").asInt32();
58  m_cfg_numChannels = config.check("channels",Value(HW_CHANNELS),"Number of audio channels.").asInt32();
59  m_cfg_frequency = config.check("frequency",Value(SAMPLING_RATE),"Sampling frequency.").asInt32();
60  m_cfg_bytesPerSample = config.check("channels",Value(2),"Bytes per sample.").asInt8();
61 
62  AudioBufferSize buffer_size(m_cfg_numSamples, m_cfg_numChannels, m_cfg_bytesPerSample);
63  m_outputBuffer = new yarp::dev::CircularAudioBuffer_16t("fake_speaker_buffer", buffer_size);
64 
65  //start the capture thread
66  start();
67  return true;
68 }
69 
71 {
73  if (m_outputBuffer)
74  {
75  delete m_outputBuffer;
76  m_outputBuffer = 0;
77  }
78  return true;
79 }
80 
81 
82 bool fakeSpeaker::threadInit()
83 {
84  return true;
85 }
86 
88 {
89  return true;
90 }
91 
93 {
94  return true;
95 }
96 
97 void fakeSpeaker::run()
98 {
99  // when not playing, do nothing
100  if (!m_isPlaying)
101  {
102  return;
103  }
104 
105  //playing implies emptying all the buffer
106  size_t siz_sam = m_outputBuffer->size().getSamples();
107  size_t siz_chn = m_outputBuffer->size().getChannels();
108  size_t siz_byt = m_outputBuffer->size().getBytes();
109  size_t buffer_size = siz_sam * siz_chn;
110  for (size_t i = 0; i<buffer_size; i++)
111  {
112  audio_sample_16t s = m_outputBuffer->read();
113  YARP_UNUSED(s);
114  }
115  yCDebug(FAKESPEAKER) << "Sound Playback complete";
116  yCDebug(FAKESPEAKER) << "Played " << siz_sam << " samples, " << siz_chn << " channels, " << siz_byt << " bytes";
117 
118  m_isPlaying = false;
119 #ifdef ADVANCED_DEBUG
120  yCDebug(FAKESPEAKER) << "b_pnt" << m_bpnt << "/" << fsize_in_bytes << " bytes";
121 #endif
122 }
123 
125 {
126  size = this->m_outputBuffer->getMaxSize();
127  return true;
128 }
129 
130 
132 {
133  size = this->m_outputBuffer->size();
134  return true;
135 }
136 
137 
139 {
140  m_outputBuffer->clear();
141  return true;
142 }
143 
145 {
146  if (m_renderSoundImmediate) m_outputBuffer->clear();
147 
148 // size_t num_bytes = sound.getBytesPerSample();
149  size_t num_channels = sound.getChannels();
150  size_t num_samples = sound.getSamples();
151 
152  for (size_t i = 0; i<num_samples; i++)
153  for (size_t j = 0; j<num_channels; j++)
154  m_outputBuffer->write(sound.get(i, j));
155  auto debug_p = sound.getInterleavedAudioRawData();
156 
157  m_isPlaying = true;
158  return true;
159 }
virtual bool getPlaybackAudioBufferMaxSize(yarp::dev::AudioBufferSize &size) override
virtual bool stopPlayback() override
Stop the playback.
Definition: fakeSpeaker.cpp:92
virtual bool renderSound(const yarp::sig::Sound &sound) override
Render a sound using a device (i.e.
~fakeSpeaker() override
Definition: fakeSpeaker.cpp:37
bool close() override
Close the DeviceDriver.
Definition: fakeSpeaker.cpp:70
virtual bool resetPlaybackAudioBuffer() override
virtual bool startPlayback() override
Start the playback.
Definition: fakeSpeaker.cpp:87
virtual bool getPlaybackAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size) override
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: fakeSpeaker.cpp:42
yarp::dev::AudioBufferSize getMaxSize()
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:69
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.
A single value (typically within a Bottle).
Definition: Value.h:47
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:225
Class for storing sounds.
Definition: Sound.h:28
std::vector< std::reference_wrapper< audio_sample > > getInterleavedAudioRawData() const
Returns a serialized version of the sound, in interleaved format, e.g.
Definition: Sound.cpp:343
size_t getChannels() const
Get the number of channels of the sound.
Definition: Sound.cpp:409
audio_sample get(size_t sample, size_t channel=0) const
Definition: Sound.cpp:174
size_t getSamples() const
Get the number of samples contained in the sound.
Definition: Sound.cpp:404
unsigned short int audio_sample_16t
#define DEFAULT_PERIOD
#define SAMPLING_RATE
Definition: fakeSpeaker.cpp:24
#define HW_CHANNELS
Definition: fakeSpeaker.cpp:23
unsigned short int audio_sample_16t
Definition: fakeSpeaker.cpp:30
#define yCInfo(component,...)
Definition: LogComponent.h:135
#define yCDebug(component,...)
Definition: LogComponent.h:112
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
An interface for the device drivers.
yarp::dev::CircularAudioBuffer< unsigned short int > CircularAudioBuffer_16t
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:25
#define YARP_UNUSED(var)
Definition: api.h:159