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 }
LogStream.h
yarp::dev::CircularAudioBuffer::write
void write(SAMPLE elem)
Definition: CircularAudioBuffer.h:48
yarp::dev::CircularAudioBuffer::clear
void clear()
Definition: CircularAudioBuffer.h:87
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::sig
Signal processing.
Definition: Image.h:25
yarp::sig::Sound::getSamples
size_t getSamples() const
Get the number of samples contained in the sound.
Definition: Sound.cpp:404
yarp::dev::AudioBufferSize
Definition: AudioBufferSize.h:26
yarp::dev::AudioBufferSize::getChannels
size_t getChannels()
Definition: AudioBufferSize.h:32
fakeSpeaker::close
bool close() override
Close the DeviceDriver.
Definition: fakeSpeaker.cpp:70
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
HW_CHANNELS
#define HW_CHANNELS
Definition: fakeSpeaker.cpp:23
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
yarp::sig::Sound::getChannels
size_t getChannels() const
Get the number of channels of the sound.
Definition: Sound.cpp:409
DEFAULT_PERIOD
#define DEFAULT_PERIOD
Definition: fakeMicrophone.h:19
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
SAMPLING_RATE
#define SAMPLING_RATE
Definition: fakeSpeaker.cpp:24
yarp::dev::CircularAudioBuffer::size
AudioBufferSize size()
Definition: CircularAudioBuffer.h:59
yarp::dev::AudioBufferSize::getBytes
size_t getBytes()
Definition: AudioBufferSize.h:34
Stamp.h
yarp::os::PeriodicThread::start
bool start()
Call this to start the thread.
Definition: PeriodicThread.cpp:311
fakeSpeaker::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: fakeSpeaker.cpp:42
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
yarp::os::PeriodicThread::setPeriod
bool setPeriod(double period)
Set the (new) period of the thread.
Definition: PeriodicThread.cpp:281
Thread.h
yarp::os::Searchable::find
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
yarp::sig::Sound::get
audio_sample get(size_t sample, size_t channel=0) const
Definition: Sound.cpp:174
fakeSpeaker::resetPlaybackAudioBuffer
virtual bool resetPlaybackAudioBuffer() override
Definition: fakeSpeaker.cpp:138
fakeSpeaker::getPlaybackAudioBufferCurrentSize
virtual bool getPlaybackAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size) override
Definition: fakeSpeaker.cpp:131
fakeSpeaker::renderSound
virtual bool renderSound(const yarp::sig::Sound &sound) override
Render a sound using a device (i.e.
Definition: fakeSpeaker.cpp:144
Semaphore.h
fakeSpeaker::fakeSpeaker
fakeSpeaker()
Definition: fakeSpeaker.cpp:32
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
yarp::dev::AudioBufferSize::getSamples
size_t getSamples()
Definition: AudioBufferSize.h:31
LogComponent.h
fakeSpeaker::getPlaybackAudioBufferMaxSize
virtual bool getPlaybackAudioBufferMaxSize(yarp::dev::AudioBufferSize &size) override
Definition: fakeSpeaker.cpp:124
audio_sample_16t
unsigned short int audio_sample_16t
Definition: audioFromFileDevice.cpp:32
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
yarp::sig::Sound
Class for storing sounds.
Definition: Sound.h:28
yarp::dev::CircularAudioBuffer_16t
yarp::dev::CircularAudioBuffer< unsigned short int > CircularAudioBuffer_16t
Definition: CircularAudioBuffer.h:118
yarp::dev::CircularAudioBuffer::read
SAMPLE read()
Definition: CircularAudioBuffer.h:71
yarp::sig::Sound::getInterleavedAudioRawData
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
yarp::os::PeriodicThread::stop
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Definition: PeriodicThread.cpp:296
Time.h
fakeSpeaker.h
fakeSpeaker::stopPlayback
virtual bool stopPlayback() override
Stop the playback.
Definition: fakeSpeaker.cpp:92
audio_sample_16t
unsigned short int audio_sample_16t
Definition: fakeSpeaker.cpp:30
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
yarp::dev::CircularAudioBuffer::getMaxSize
yarp::dev::AudioBufferSize getMaxSize()
Definition: CircularAudioBuffer.h:82
fakeSpeaker::~fakeSpeaker
~fakeSpeaker() override
Definition: fakeSpeaker.cpp:37
yarp::os::Value::asFloat64
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:225
fakeSpeaker::startPlayback
virtual bool startPlayback() override
Start the playback.
Definition: fakeSpeaker.cpp:87